Subversion Repositories SmartDukaan

Rev

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