Subversion Repositories SmartDukaan

Rev

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