Subversion Repositories SmartDukaan

Rev

Rev 10724 | Rev 10765 | 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;
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() {
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))) {
10724 amit.gupta 124
            	String emailId = this.request.getParameter("emailId");
10764 amit.gupta 125
            	log.info("this.request.getParameter(\"emailId\")" + this.request.getParameter("emailId"));
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 {
277
            UserClient usc = new UserClient();
9815 anupam.sin 278
            if(request.getParameter("addressType").equalsIgnoreCase("store")) {
9808 anupam.sin 279
                usc.getClient().addStoreToCart(id, addressId);
9815 anupam.sin 280
            } else if (request.getParameter("addressType").equalsIgnoreCase("home")){
9808 anupam.sin 281
                usc.getClient().addAddressToCart(id, addressId);
282
            } else {
283
                errorMsg = "Invalid addressType. It can only be set to home or store.";
284
            }
285
        } catch(Exception e) {
286
            log.error("Unable to change address", e);
287
        }
288
        return index();
289
    }
9570 anupam.sin 290
 
291
    public long getItemId(){
292
        return this.itemId;
293
    }
294
 
9684 amit.gupta 295
    public void setItemId(long itemId){
296
    	this.itemId = itemId;
297
    }
298
 
9570 anupam.sin 299
    public String getTotalAmount() {
300
        return totalamount;
301
    }
302
 
303
    public String getPinCode() {
304
        return pincode;
305
    }
306
 
307
    public String getCouponCode()  {
308
        return couponCode;
309
    }
310
 
311
    public String getDiscountedAmount()   {
312
        return discountedAmount;
313
    }
314
 
315
    public String getErrorMsg()    {
316
        return errorMsg;
317
    }
318
 
9576 anupam.sin 319
//    public long getNumberOfItems(){
320
//        return userinfo.getTotalItems();
321
//    }
9570 anupam.sin 322
 
323
    public String getCartMsg(){
324
        if(cartMsg.equals("")){
325
            return null;
326
        }
327
        return cartMsg;
328
    }
329
 
330
    public String getSnippets(){
331
        String snippets = "";
332
        CatalogClient csc;
333
        try {
334
            csc = new CatalogClient();
335
            List<Long> similarItems = csc.getClient().getSimilarItemsCatalogIds(0, 4, itemId);
336
            for(Long catalogId: similarItems){
9602 amit.gupta 337
                snippets = snippets + ContentServingService.getSnippet(SnippetType.WIDGET_SNIPPET, catalogId+"", -1);
9570 anupam.sin 338
            }
339
        } catch (Exception e) {
340
            log.error("Unable to initialise Catalogservice Client");
341
        }       
342
        return snippets;
343
    }
344
 
345
    @Override
346
    public void setParameters(Map<String, String[]> parameters) {
347
        this.reqparams = parameters;    
348
    }
349
 
9576 anupam.sin 350
//    @Override
351
//    public String getHeaderSnippet() {
352
//        String url = request.getQueryString();
353
//        if (url == null) {
354
//            url = "";
355
//        } else {
356
//            url = "?" + url;
357
//        }
358
//        url = request.getRequestURI() + url;
359
//        return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), userinfo.getTotalItems(), url , 0, false);
360
//    }
9570 anupam.sin 361
 
9576 anupam.sin 362
//    public boolean isUserLoggedIn() {
363
//        return userinfo.isLoggedIn();
364
//    }
9570 anupam.sin 365
 
366
    public void setVariationId(String uri)  {
367
        if (uri.equals("/cart1"))   {
368
            this.variationId = 1;
369
        }
370
    }
371
 
372
    public int getVariationId() {
373
        return this.variationId;
374
    }
375
 
376
    public String getActionMessage(){
377
        if(cartMsg.contains("out of stock")){
378
            return "Notify me when this product is in stock.";
379
        }else {
380
            return "Notify me when this product is available.";
381
        }
382
    }
383
 
384
    public String getOfferNote(){
385
        String note = null;
386
        if(cartMsg.contains("out of stock")){
387
            return note;
388
        }
389
        else {
390
            try {
391
                CatalogClient catalogServiceClient = new CatalogClient();
392
                Client catalogClient = catalogServiceClient.getClient();
393
                Item it = catalogClient.getItem(itemId);
394
                note = it.getBestDealText();
395
            } catch (Exception e)  {
396
                log.error("Unable to get the offertext because of: ", e);
397
            }
398
        }
399
        return note;
400
    }
401
 
402
    public String getInsuranceResult() {
403
        return insuranceResult;
404
    }
405
 
406
    public void setInsuranceResult(String insuranceResult) {
407
        this.insuranceResult = insuranceResult;
408
    }
409
 
410
    public void setToInsure(boolean toInsure) {
411
        this.toInsure = toInsure;
412
    }
413
 
414
    public boolean getToInsure() {
415
        return toInsure;
416
    }
417
 
9709 anupam.sin 418
    public long getId() {
419
        return id;
9570 anupam.sin 420
    }
421
 
9709 anupam.sin 422
    public void setId(long id) {
423
        this.id = id;
9570 anupam.sin 424
    }
9667 anupam.sin 425
 
426
 
427
    public void setCartPojoJson(String cartPojoJson) {
428
        this.cartPojoJson = cartPojoJson;
429
    }
430
 
431
 
432
    public String getCartPojoJson() {
433
        return cartPojoJson;
434
    }
9676 anupam.sin 435
 
436
    public void setQuantity(long quantity) {
437
        this.quantity = quantity;
438
    }
439
 
440
    public long getQuantity() {
441
        return quantity;
442
    }
9697 anupam.sin 443
 
444
    public int getInsuranceType() {
445
        return insuranceType;
446
    }
447
 
448
    public void setInsuranceType(int insuranceType) {
449
        this.insuranceType = insuranceType;
450
    }
9570 anupam.sin 451
}