Subversion Repositories SmartDukaan

Rev

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