Subversion Repositories SmartDukaan

Rev

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