Subversion Repositories SmartDukaan

Rev

Rev 11386 | Rev 11514 | 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;
11511 amit.gupta 26
import org.apache.thrift.transport.TTransportException;
9570 anupam.sin 27
 
9667 anupam.sin 28
import com.google.gson.Gson;
9570 anupam.sin 29
 
30
public class CartController extends BaseController implements ParameterAware{
31
 
32
    private static final long serialVersionUID = 1L;
33
    private static Logger log = Logger.getLogger(Class.class);
34
    Map<String, String[]> reqparams = null;
35
 
36
    private int variationId = 0;
37
    private String totalamount;
38
 
39
    private String errorMsg = "";
40
    private String cartMsg = "";
41
 
42
    private String pincode = "110001";
43
 
44
    private String couponCode = null;
45
 
46
    private String discountedAmount;
47
 
48
    private long itemId;
49
    private String insuranceResult;
50
 
51
    private boolean toInsure;
52
 
9676 anupam.sin 53
 
9709 anupam.sin 54
    private long id; //CartId
9667 anupam.sin 55
    private String cartPojoJson;
9676 anupam.sin 56
    private long quantity;
9697 anupam.sin 57
    private int insuranceType;
11386 amit.gupta 58
    UserContextService.Client userClient;
9570 anupam.sin 59
 
11386 amit.gupta 60
    public CartController() throws Exception{
9570 anupam.sin 61
        super();
11386 amit.gupta 62
        userClient = new UserClient().getClient();
9570 anupam.sin 63
    }
64
 
9808 anupam.sin 65
    public String index() {
9709 anupam.sin 66
        if(id != -1){
9570 anupam.sin 67
            try {
11511 amit.gupta 68
            	log.info("Before cart response--------");
9709 anupam.sin 69
                List<String> cartResponse  = userClient.validateCart(id, -1);
11511 amit.gupta 70
                log.info("After cart response--------");
9713 anupam.sin 71
                if (!"".equals(cartResponse.get(0))) {
72
                    errorMsg = cartResponse.get(0);
73
                }
9570 anupam.sin 74
                if(StringUtils.isNotEmpty(cartResponse.get(1))) {
75
                    addActionMessage(cartResponse.get(1));
76
                }
77
                log.info("Cart Change/EMI Message rcvd from the service is:" + errorMsg);
9667 anupam.sin 78
 
9686 amit.gupta 79
                Cart cart = userClient.getCurrentCart(userinfo.getUserId());
9808 anupam.sin 80
                String pincode = "110001";
10482 amit.gupta 81
                if(!cart.isSetAddressId()) {
9808 anupam.sin 82
                    try {
11386 amit.gupta 83
                        pincode = userClient.getDefaultPincode(userinfo.getUserId());
9808 anupam.sin 84
                    } catch(Exception e) {
85
                        log.error("Unable to get default pincode for userId : " + userinfo.getUserId(), e);
86
                    }
87
                } else {
88
                    try {
11386 amit.gupta 89
                        pincode = userClient.getAddressById(cart.getAddressId()).getPin();
9808 anupam.sin 90
                    } catch(Exception e) {
91
                        log.error("Unable to get address for addressId : " + cart.getAddressId(), e);
92
                    }   
93
                }
10482 amit.gupta 94
                Boolean needInuranceInfo = false;
95
                for(Line line : cart.getLines()) {
10554 amit.gupta 96
                	log.info("Insurer is set: " + String.valueOf(line.getInsurer()>0));
10553 amit.gupta 97
                	if(line.getInsurer()>0){
11386 amit.gupta 98
                		long defaultAddressId = userClient.getDefaultAddressId(userinfo.getUserId());
10550 amit.gupta 99
                		log.info(defaultAddressId);
10552 amit.gupta 100
                		needInuranceInfo = !userClient.isInsuranceDetailPresent(defaultAddressId);
10528 amit.gupta 101
                		log.info("Need insurance: " + needInuranceInfo);
10482 amit.gupta 102
                		break;
103
                	}
104
                }
9667 anupam.sin 105
 
10482 amit.gupta 106
                cartPojoJson = new Gson().toJson(PojoPopulator.getCartPojo(cart, errorMsg, pincode, needInuranceInfo));
10551 amit.gupta 107
                log.info(cartPojoJson);
9570 anupam.sin 108
            } catch (Exception e) {
11511 amit.gupta 109
                TTransportException te = (TTransportException)e;
110
                log.warn(e.getLocalizedMessage() + " " + te.getType());
9570 anupam.sin 111
                log.warn("Unable to validate the cart: ", e);
112
            }
113
        }
114
        return "index";
115
    }
116
 
117
    public String create() {
10724 amit.gupta 118
    	RedirectPojo rp = new RedirectPojo();
119
    	rp.setRedirectUrl("");
9676 anupam.sin 120
        try {
10724 amit.gupta 121
        	//NotificationPojo np = new NotificationPojo();
11511 amit.gupta 122
            //UserClient userServiceClient = new UserClient();
123
            //UserContextService.Client userClient = userServiceClient.getClient();
9709 anupam.sin 124
            cartMsg = userClient.addItemToCart(id, itemId, 1, -1);
9680 amit.gupta 125
            if (!("".equals(cartMsg))) {
10767 amit.gupta 126
            	String emailId = this.request.getParameter("email");
10766 amit.gupta 127
            	log.info("QuesyString" + this.request.getParameterMap().toString());
10724 amit.gupta 128
    			if(emailId==null || emailId.equals("")){
129
    				rp.setMessage("Item is currently out of stock");
130
    			} else {	
131
        			try {
132
	        	        CatalogClient catalogClientService;
133
	        			catalogClientService = new CatalogClient();
134
	        			in.shop2020.model.v1.catalog.CatalogService.Client client = catalogClientService.getClient();
135
	        			client.addProductNotification(itemId, emailId);
136
	        			log.debug("Got product notification for product: " + itemId + "  from user: " + emailId );
137
	        			rp.setMessage("You will be notified when product is avaliable");
138
        			} catch (Exception e) {
139
        				rp.setMessage("Error occurred while adding to notificiation");
140
        				log.error("Exception while adding product notification for item: " + itemId + " and email: " + emailId, e);
10261 amit.gupta 141
        			}
10724 amit.gupta 142
 
10261 amit.gupta 143
        		}
10724 amit.gupta 144
            } else {
145
            	rp.setRedirectUrl("cart");
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
        }
10724 amit.gupta 154
        cartPojoJson = new Gson().toJson(rp);
155
        return "index";
9676 anupam.sin 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 {
9815 anupam.sin 279
            if(request.getParameter("addressType").equalsIgnoreCase("store")) {
11386 amit.gupta 280
                userClient.addStoreToCart(id, addressId);
9815 anupam.sin 281
            } else if (request.getParameter("addressType").equalsIgnoreCase("home")){
11386 amit.gupta 282
                userClient.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
}