Subversion Repositories SmartDukaan

Rev

Rev 11511 | Rev 11516 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
10550 amit.gupta 1
	package in.shop2020.mobileapi.serving.controllers;
9570 anupam.sin 2
 
10724 amit.gupta 3
import in.shop2020.mobileapi.serving.pojos.RedirectPojo;
9577 anupam.sin 4
import in.shop2020.mobileapi.serving.services.ContentServingService;
9667 anupam.sin 5
import in.shop2020.mobileapi.serving.utils.PojoPopulator;
9577 anupam.sin 6
import in.shop2020.mobileapi.serving.utils.SnippetType;
9570 anupam.sin 7
import in.shop2020.model.v1.catalog.CatalogService.Client;
8
import in.shop2020.model.v1.catalog.Item;
9
import in.shop2020.model.v1.user.Cart;
10482 amit.gupta 10
import in.shop2020.model.v1.user.Line;
9713 anupam.sin 11
import in.shop2020.model.v1.user.PromotionException;
12
import in.shop2020.model.v1.user.PromotionService;
9570 anupam.sin 13
import in.shop2020.model.v1.user.ShoppingCartException;
14
import in.shop2020.model.v1.user.UserContextService;
15
import in.shop2020.thrift.clients.CatalogClient;
9713 anupam.sin 16
import in.shop2020.thrift.clients.PromotionClient;
9570 anupam.sin 17
import in.shop2020.thrift.clients.UserClient;
18
 
19
import java.util.List;
20
import java.util.Map;
21
 
22
import org.apache.commons.lang.StringUtils;
23
import org.apache.log4j.Logger;
24
import org.apache.struts2.interceptor.ParameterAware;
25
import org.apache.thrift.TException;
11511 amit.gupta 26
import org.apache.thrift.transport.TTransportException;
9570 anupam.sin 27
 
9667 anupam.sin 28
import com.google.gson.Gson;
9570 anupam.sin 29
 
30
public class CartController extends BaseController implements ParameterAware{
31
 
32
    private static final long serialVersionUID = 1L;
33
    private static Logger log = Logger.getLogger(Class.class);
34
    Map<String, String[]> reqparams = null;
35
 
36
    private int variationId = 0;
37
    private String totalamount;
38
 
39
    private String errorMsg = "";
40
    private String cartMsg = "";
41
 
42
    private String pincode = "110001";
43
 
44
    private String couponCode = null;
45
 
46
    private String discountedAmount;
47
 
48
    private long itemId;
49
    private String insuranceResult;
50
 
51
    private boolean toInsure;
52
 
9676 anupam.sin 53
 
9709 anupam.sin 54
    private long id; //CartId
9667 anupam.sin 55
    private String cartPojoJson;
9676 anupam.sin 56
    private long quantity;
9697 anupam.sin 57
    private int insuranceType;
11386 amit.gupta 58
    UserContextService.Client userClient;
9570 anupam.sin 59
 
11386 amit.gupta 60
    public CartController() throws Exception{
9570 anupam.sin 61
        super();
11386 amit.gupta 62
        userClient = new UserClient().getClient();
9570 anupam.sin 63
    }
64
 
9808 anupam.sin 65
    public String index() {
9709 anupam.sin 66
        if(id != -1){
9570 anupam.sin 67
            try {
11511 amit.gupta 68
            	log.info("Before cart response--------");
9709 anupam.sin 69
                List<String> cartResponse  = userClient.validateCart(id, -1);
11511 amit.gupta 70
                log.info("After cart response--------");
9713 anupam.sin 71
                if (!"".equals(cartResponse.get(0))) {
72
                    errorMsg = cartResponse.get(0);
73
                }
9570 anupam.sin 74
                if(StringUtils.isNotEmpty(cartResponse.get(1))) {
75
                    addActionMessage(cartResponse.get(1));
76
                }
77
                log.info("Cart Change/EMI Message rcvd from the service is:" + errorMsg);
9667 anupam.sin 78
 
9686 amit.gupta 79
                Cart cart = userClient.getCurrentCart(userinfo.getUserId());
9808 anupam.sin 80
                String pincode = "110001";
10482 amit.gupta 81
                if(!cart.isSetAddressId()) {
9808 anupam.sin 82
                    try {
11386 amit.gupta 83
                        pincode = userClient.getDefaultPincode(userinfo.getUserId());
9808 anupam.sin 84
                    } catch(Exception e) {
85
                        log.error("Unable to get default pincode for userId : " + userinfo.getUserId(), e);
86
                    }
87
                } else {
88
                    try {
11386 amit.gupta 89
                        pincode = userClient.getAddressById(cart.getAddressId()).getPin();
9808 anupam.sin 90
                    } catch(Exception e) {
91
                        log.error("Unable to get address for addressId : " + cart.getAddressId(), e);
92
                    }   
93
                }
10482 amit.gupta 94
                Boolean needInuranceInfo = false;
95
                for(Line line : cart.getLines()) {
10554 amit.gupta 96
                	log.info("Insurer is set: " + String.valueOf(line.getInsurer()>0));
10553 amit.gupta 97
                	if(line.getInsurer()>0){
11386 amit.gupta 98
                		long defaultAddressId = userClient.getDefaultAddressId(userinfo.getUserId());
10550 amit.gupta 99
                		log.info(defaultAddressId);
10552 amit.gupta 100
                		needInuranceInfo = !userClient.isInsuranceDetailPresent(defaultAddressId);
10528 amit.gupta 101
                		log.info("Need insurance: " + needInuranceInfo);
10482 amit.gupta 102
                		break;
103
                	}
104
                }
9667 anupam.sin 105
 
10482 amit.gupta 106
                cartPojoJson = new Gson().toJson(PojoPopulator.getCartPojo(cart, errorMsg, pincode, needInuranceInfo));
10551 amit.gupta 107
                log.info(cartPojoJson);
9570 anupam.sin 108
            } catch (Exception e) {
11511 amit.gupta 109
                TTransportException te = (TTransportException)e;
110
                log.warn(e.getLocalizedMessage() + " " + te.getType());
9570 anupam.sin 111
                log.warn("Unable to validate the cart: ", e);
112
            }
113
        }
114
        return "index";
115
    }
116
 
117
    public String create() {
10724 amit.gupta 118
    	RedirectPojo rp = new RedirectPojo();
119
    	rp.setRedirectUrl("");
9676 anupam.sin 120
        try {
10724 amit.gupta 121
        	//NotificationPojo np = new NotificationPojo();
11511 amit.gupta 122
            //UserClient userServiceClient = new UserClient();
123
            //UserContextService.Client userClient = userServiceClient.getClient();
9709 anupam.sin 124
            cartMsg = userClient.addItemToCart(id, itemId, 1, -1);
9680 amit.gupta 125
            if (!("".equals(cartMsg))) {
10767 amit.gupta 126
            	String emailId = this.request.getParameter("email");
10766 amit.gupta 127
            	log.info("QuesyString" + this.request.getParameterMap().toString());
10724 amit.gupta 128
    			if(emailId==null || emailId.equals("")){
129
    				rp.setMessage("Item is currently out of stock");
130
    			} else {	
131
        			try {
132
	        	        CatalogClient catalogClientService;
133
	        			catalogClientService = new CatalogClient();
134
	        			in.shop2020.model.v1.catalog.CatalogService.Client client = catalogClientService.getClient();
135
	        			client.addProductNotification(itemId, emailId);
136
	        			log.debug("Got product notification for product: " + itemId + "  from user: " + emailId );
137
	        			rp.setMessage("You will be notified when product is avaliable");
138
        			} catch (Exception e) {
139
        				rp.setMessage("Error occurred while adding to notificiation");
140
        				log.error("Exception while adding product notification for item: " + itemId + " and email: " + emailId, e);
10261 amit.gupta 141
        			}
10724 amit.gupta 142
 
10261 amit.gupta 143
        		}
10724 amit.gupta 144
            } else {
145
            	rp.setRedirectUrl("cart");
9676 anupam.sin 146
            }
147
        } catch (TException e) {
148
            log.error("Unable to create or add to cart because of: ", e);
9713 anupam.sin 149
            errorMsg = "Unable to add to cart. Please try again.";
9676 anupam.sin 150
        } catch (Exception e) {
151
            log.error("Unable to create or add to cart because of: ", e);
9713 anupam.sin 152
            errorMsg = "Unable to add to cart. Please try again.";
9570 anupam.sin 153
        }
10724 amit.gupta 154
        cartPojoJson = new Gson().toJson(rp);
155
        return "index";
9676 anupam.sin 156
    }       
9570 anupam.sin 157
 
9676 anupam.sin 158
 
159
    // DELETE /entity
160
    public String destroy() {
9709 anupam.sin 161
        if(id > 0)  {
9570 anupam.sin 162
            try {
9709 anupam.sin 163
                userClient.deleteItemFromCart(id, itemId);
9676 anupam.sin 164
                return index();
165
            } catch (ShoppingCartException e) {
166
                log.error("Unable to delete item from cart: ", e);
9713 anupam.sin 167
                errorMsg = "Unable to delete item. Please try again.";
9570 anupam.sin 168
            } catch (TException e) {
9676 anupam.sin 169
                log.error("Unable to delete item from cart: ", e);
9713 anupam.sin 170
                errorMsg = "Unable to delete item. Please try again.";
9570 anupam.sin 171
            } catch (Exception e) {
9676 anupam.sin 172
                log.error("Unable to delete item from cart: ", e);
9713 anupam.sin 173
                errorMsg = "Unable to delete item. Please try again.";
9570 anupam.sin 174
            }
175
        }
9676 anupam.sin 176
        return index();
9570 anupam.sin 177
    }
9676 anupam.sin 178
 
179
    //PUT
9570 anupam.sin 180
    public String update() {
9676 anupam.sin 181
        if(quantity <= 0)   {
182
            log.info("Not valid item quantity. Unable to change item quantity.");
9713 anupam.sin 183
            errorMsg = "Invalid item quantity";
9676 anupam.sin 184
        } else  {
9709 anupam.sin 185
            if(updateItemQuantityInCart(id, itemId, quantity))    {
9676 anupam.sin 186
                return index();
187
            }
188
        }
189
        return index();
9570 anupam.sin 190
    }
191
 
192
    private boolean updateItemQuantityInCart(long cartId, long itemId, long quantity){
193
        try {
9602 amit.gupta 194
            userClient.addItemToCart(cartId, itemId, quantity, -1);
9570 anupam.sin 195
            return true;
196
        } catch (ShoppingCartException e) {
197
            log.error("Unable to update the item quantity in the cart: ", e);
9713 anupam.sin 198
            errorMsg = "Unable to change quantity. Please try again.";
9570 anupam.sin 199
        } catch (TException e) {
200
            log.error("Unable to update the item quantity in the cart: ", e);
9713 anupam.sin 201
            errorMsg = "Unable to change quantity. Please try again.";
9570 anupam.sin 202
        } catch (Exception e) {
203
            log.error("Unable to update the item quantity in the cart: ", e);
9713 anupam.sin 204
            errorMsg = "Unable to change quantity. Please try again.";
9570 anupam.sin 205
        }
206
        return false;
207
    }
9676 anupam.sin 208
 
9570 anupam.sin 209
    public String insureItem() {
9697 anupam.sin 210
        insuranceResult = "";
211
        try {
11514 amit.gupta 212
            if(userClient.insureItem(itemId, id, toInsure, insuranceType)) {
9697 anupam.sin 213
                setInsuranceResult("SUCCESS");
214
            } else {
215
                setInsuranceResult("FAILURE");
216
            }
217
        } catch (Exception e) {
9709 anupam.sin 218
            log.error("Unable to insure item : " + itemId + " for cart : " + id, e);
9697 anupam.sin 219
            setInsuranceResult("FAILURE");
220
        }
9570 anupam.sin 221
        return "insurance-result";
222
    }
9713 anupam.sin 223
 
224
    public String edit()  {
225
        String action = request.getParameter("action");
226
        PromotionClient promotionServiceClient = null;
227
        try {
228
            if(action == null || action.isEmpty())  {
229
                errorMsg = "Invalid Request Action";
230
                return index();
231
            }
232
            String couponCode = request.getParameter("coupon_code");
233
 
234
            if (action.equalsIgnoreCase("applycoupon"))   {
235
                if (couponCode == null || couponCode.isEmpty()) {
236
                    errorMsg = "Coupon Code field cannot be left empty";
237
                    return index();
238
                }
239
 
240
                promotionServiceClient = new PromotionClient();
241
                PromotionService.Client promotionClient = promotionServiceClient.getClient();
242
 
243
                promotionClient.applyCoupon(couponCode, id);
244
 
245
            }
246
            else if (action.equals("removecoupon"))    {
247
                userClient.removeCoupon(id);
248
            }
249
        } catch (PromotionException e) {
250
            log.info("Invalid coupon: " + e.getMessage());
251
            errorMsg = "Invalid coupon : " + e.getMessage();
252
        } catch (Exception e) {
253
            log.error("Unable to apply or remove coupon", e);
254
            errorMsg = "Unable to apply or remove coupon";
255
        }
256
        return index();
257
    }
9808 anupam.sin 258
 
259
    public String changeAddress() {
260
        long addressId;
261
        if(request.getParameter("addressId") == null || request.getParameter("addressId").isEmpty()) {
262
            errorMsg = "Null or empty addressId";
263
            return index();
264
        } else {
9815 anupam.sin 265
            addressId = Long.parseLong(request.getParameter("addressId"));
9808 anupam.sin 266
        }
267
 
268
        try {
9815 anupam.sin 269
            if(request.getParameter("addressType").equalsIgnoreCase("store")) {
11386 amit.gupta 270
                userClient.addStoreToCart(id, addressId);
9815 anupam.sin 271
            } else if (request.getParameter("addressType").equalsIgnoreCase("home")){
11386 amit.gupta 272
                userClient.addAddressToCart(id, addressId);
9808 anupam.sin 273
            } else {
274
                errorMsg = "Invalid addressType. It can only be set to home or store.";
275
            }
276
        } catch(Exception e) {
277
            log.error("Unable to change address", e);
278
        }
279
        return index();
280
    }
9570 anupam.sin 281
 
282
    public long getItemId(){
283
        return this.itemId;
284
    }
285
 
9684 amit.gupta 286
    public void setItemId(long itemId){
287
    	this.itemId = itemId;
288
    }
289
 
9570 anupam.sin 290
    public String getTotalAmount() {
291
        return totalamount;
292
    }
293
 
294
    public String getPinCode() {
295
        return pincode;
296
    }
297
 
298
    public String getCouponCode()  {
299
        return couponCode;
300
    }
301
 
302
    public String getDiscountedAmount()   {
303
        return discountedAmount;
304
    }
305
 
306
    public String getErrorMsg()    {
307
        return errorMsg;
308
    }
309
 
9576 anupam.sin 310
//    public long getNumberOfItems(){
311
//        return userinfo.getTotalItems();
312
//    }
9570 anupam.sin 313
 
314
    public String getCartMsg(){
315
        if(cartMsg.equals("")){
316
            return null;
317
        }
318
        return cartMsg;
319
    }
320
 
321
    public String getSnippets(){
322
        String snippets = "";
323
        CatalogClient csc;
324
        try {
325
            csc = new CatalogClient();
326
            List<Long> similarItems = csc.getClient().getSimilarItemsCatalogIds(0, 4, itemId);
327
            for(Long catalogId: similarItems){
9602 amit.gupta 328
                snippets = snippets + ContentServingService.getSnippet(SnippetType.WIDGET_SNIPPET, catalogId+"", -1);
9570 anupam.sin 329
            }
330
        } catch (Exception e) {
331
            log.error("Unable to initialise Catalogservice Client");
332
        }       
333
        return snippets;
334
    }
335
 
336
    @Override
337
    public void setParameters(Map<String, String[]> parameters) {
338
        this.reqparams = parameters;    
339
    }
340
 
9576 anupam.sin 341
//    @Override
342
//    public String getHeaderSnippet() {
343
//        String url = request.getQueryString();
344
//        if (url == null) {
345
//            url = "";
346
//        } else {
347
//            url = "?" + url;
348
//        }
349
//        url = request.getRequestURI() + url;
350
//        return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), userinfo.getTotalItems(), url , 0, false);
351
//    }
9570 anupam.sin 352
 
9576 anupam.sin 353
//    public boolean isUserLoggedIn() {
354
//        return userinfo.isLoggedIn();
355
//    }
9570 anupam.sin 356
 
357
    public void setVariationId(String uri)  {
358
        if (uri.equals("/cart1"))   {
359
            this.variationId = 1;
360
        }
361
    }
362
 
363
    public int getVariationId() {
364
        return this.variationId;
365
    }
366
 
367
    public String getActionMessage(){
368
        if(cartMsg.contains("out of stock")){
369
            return "Notify me when this product is in stock.";
370
        }else {
371
            return "Notify me when this product is available.";
372
        }
373
    }
374
 
375
    public String getOfferNote(){
376
        String note = null;
377
        if(cartMsg.contains("out of stock")){
378
            return note;
379
        }
380
        else {
381
            try {
382
                CatalogClient catalogServiceClient = new CatalogClient();
383
                Client catalogClient = catalogServiceClient.getClient();
384
                Item it = catalogClient.getItem(itemId);
385
                note = it.getBestDealText();
386
            } catch (Exception e)  {
387
                log.error("Unable to get the offertext because of: ", e);
388
            }
389
        }
390
        return note;
391
    }
392
 
393
    public String getInsuranceResult() {
394
        return insuranceResult;
395
    }
396
 
397
    public void setInsuranceResult(String insuranceResult) {
398
        this.insuranceResult = insuranceResult;
399
    }
400
 
401
    public void setToInsure(boolean toInsure) {
402
        this.toInsure = toInsure;
403
    }
404
 
405
    public boolean getToInsure() {
406
        return toInsure;
407
    }
408
 
9709 anupam.sin 409
    public long getId() {
410
        return id;
9570 anupam.sin 411
    }
412
 
9709 anupam.sin 413
    public void setId(long id) {
414
        this.id = id;
9570 anupam.sin 415
    }
9667 anupam.sin 416
 
417
 
418
    public void setCartPojoJson(String cartPojoJson) {
419
        this.cartPojoJson = cartPojoJson;
420
    }
421
 
422
 
423
    public String getCartPojoJson() {
424
        return cartPojoJson;
425
    }
9676 anupam.sin 426
 
427
    public void setQuantity(long quantity) {
428
        this.quantity = quantity;
429
    }
430
 
431
    public long getQuantity() {
432
        return quantity;
433
    }
9697 anupam.sin 434
 
435
    public int getInsuranceType() {
436
        return insuranceType;
437
    }
438
 
439
    public void setInsuranceType(int insuranceType) {
440
        this.insuranceType = insuranceType;
441
    }
9570 anupam.sin 442
}