Subversion Repositories SmartDukaan

Rev

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