Subversion Repositories SmartDukaan

Rev

Rev 10482 | Rev 10528 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
9576 anupam.sin 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()) {
10527 amit.gupta 93
                	log.info("Insurer is set: " + line.isSetInsurer());
10482 amit.gupta 94
                	if(line.isSetInsurer()){
95
                		long defaultAddressId = usc.getClient().getDefaultAddressId(userinfo.getUserId());
96
                		needInuranceInfo = !userClient.isInsuranceDetailPresent(defaultAddressId);
10527 amit.gupta 97
                		log.info("Need insurance");
10482 amit.gupta 98
                		break;
99
                	}
100
                }
9667 anupam.sin 101
 
10482 amit.gupta 102
                cartPojoJson = new Gson().toJson(PojoPopulator.getCartPojo(cart, errorMsg, pincode, needInuranceInfo));
9808 anupam.sin 103
 
9570 anupam.sin 104
            } catch (Exception e) {
105
                // This exception can be ignored for showing the cart. Not so
106
                // innocent when this occurs at the time of checkout or when the
107
                // user is proceeding to pay.
108
                log.warn("Unable to validate the cart: ", e);
109
            }
110
        }
111
        return "index";
112
    }
113
 
114
    public String create() {
9676 anupam.sin 115
        try {
10261 amit.gupta 116
        	NotificationPojo np = new NotificationPojo();
9676 anupam.sin 117
            UserClient userServiceClient = new UserClient();
118
            UserContextService.Client userClient = userServiceClient.getClient();
9709 anupam.sin 119
            cartMsg = userClient.addItemToCart(id, itemId, 1, -1);
9680 amit.gupta 120
            if (!("".equals(cartMsg))) {
10261 amit.gupta 121
            	String emailId = "";
122
        		try {
123
        			emailId = this.request.getParameter("emailId");
124
        			if(emailId!=null){
125
        				np.setMessage("Could not add this item to cart");
126
        				np.setStatus("oos");
127
        				cartPojoJson = new Gson().toJson(np);
128
        				return "index";
129
 
130
        			}
131
        			itemId = Long.parseLong(this.request.getParameter("itemId"));
132
 
133
        	        CatalogClient catalogClientService;
134
        			catalogClientService = new CatalogClient();
135
        			in.shop2020.model.v1.catalog.CatalogService.Client client = catalogClientService.getClient();
136
        			client.addProductNotification(itemId, emailId);
137
        		} catch (Exception e) {
138
        			log.error("Exception while adding product notification for item: " + itemId + " and email: " + emailId, e);
139
        		}
140
        		log.debug("Got product notification for product: " + itemId + "  from user: " + emailId );
141
        		np.setMessage("You will be notified when product is avaliable");
142
        		np.setStatus("success");
143
        		cartPojoJson = new Gson().toJson(np);
144
                return "index";
9676 anupam.sin 145
            }
146
        } catch (TException e) {
147
            log.error("Unable to create or add to cart because of: ", e);
9713 anupam.sin 148
            errorMsg = "Unable to add to cart. Please try again.";
9676 anupam.sin 149
        } catch (Exception e) {
150
            log.error("Unable to create or add to cart because of: ", e);
9713 anupam.sin 151
            errorMsg = "Unable to add to cart. Please try again.";
9570 anupam.sin 152
        }
153
 
9676 anupam.sin 154
    return index();
155
    }       
9570 anupam.sin 156
 
9676 anupam.sin 157
 
158
    // DELETE /entity
159
    public String destroy() {
9709 anupam.sin 160
        if(id > 0)  {
9570 anupam.sin 161
            try {
9676 anupam.sin 162
                UserClient userContextServiceClient = new UserClient();
163
                in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
164
 
9709 anupam.sin 165
                userClient.deleteItemFromCart(id, itemId);
9676 anupam.sin 166
                return index();
167
            } catch (ShoppingCartException e) {
168
                log.error("Unable to delete item from cart: ", e);
9713 anupam.sin 169
                errorMsg = "Unable to delete item. Please try again.";
9570 anupam.sin 170
            } catch (TException e) {
9676 anupam.sin 171
                log.error("Unable to delete item from cart: ", e);
9713 anupam.sin 172
                errorMsg = "Unable to delete item. Please try again.";
9570 anupam.sin 173
            } catch (Exception e) {
9676 anupam.sin 174
                log.error("Unable to delete item from cart: ", e);
9713 anupam.sin 175
                errorMsg = "Unable to delete item. Please try again.";
9570 anupam.sin 176
            }
177
        }
9676 anupam.sin 178
        return index();
9570 anupam.sin 179
    }
9676 anupam.sin 180
 
181
    //PUT
9570 anupam.sin 182
    public String update() {
9676 anupam.sin 183
        if(quantity <= 0)   {
184
            log.info("Not valid item quantity. Unable to change item quantity.");
9713 anupam.sin 185
            errorMsg = "Invalid item quantity";
9676 anupam.sin 186
        } else  {
9709 anupam.sin 187
            if(updateItemQuantityInCart(id, itemId, quantity))    {
9676 anupam.sin 188
                return index();
189
            }
190
        }
191
        return index();
9570 anupam.sin 192
    }
193
 
194
    private boolean updateItemQuantityInCart(long cartId, long itemId, long quantity){
195
        try {
196
            UserClient userContextServiceClient = new UserClient();
197
            in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
198
 
9602 amit.gupta 199
            userClient.addItemToCart(cartId, itemId, quantity, -1);
9570 anupam.sin 200
            return true;
201
        } catch (ShoppingCartException e) {
202
            log.error("Unable to update the item quantity in the cart: ", e);
9713 anupam.sin 203
            errorMsg = "Unable to change quantity. Please try again.";
9570 anupam.sin 204
        } catch (TException e) {
205
            log.error("Unable to update the item quantity in the cart: ", e);
9713 anupam.sin 206
            errorMsg = "Unable to change quantity. Please try again.";
9570 anupam.sin 207
        } catch (Exception e) {
208
            log.error("Unable to update the item quantity in the cart: ", e);
9713 anupam.sin 209
            errorMsg = "Unable to change quantity. Please try again.";
9570 anupam.sin 210
        }
211
        return false;
212
    }
9676 anupam.sin 213
 
9570 anupam.sin 214
    public String insureItem() {
9697 anupam.sin 215
        insuranceResult = "";
216
        try {
217
            UserContextService.Client usc = new UserClient().getClient();
9709 anupam.sin 218
            if(usc.insureItem(itemId, id, toInsure, insuranceType)) {
9697 anupam.sin 219
                setInsuranceResult("SUCCESS");
220
            } else {
221
                setInsuranceResult("FAILURE");
222
            }
223
        } catch (Exception e) {
9709 anupam.sin 224
            log.error("Unable to insure item : " + itemId + " for cart : " + id, e);
9697 anupam.sin 225
            setInsuranceResult("FAILURE");
226
        }
9570 anupam.sin 227
        return "insurance-result";
228
    }
9713 anupam.sin 229
 
230
    public String edit()  {
231
        String action = request.getParameter("action");
232
        UserClient userServiceClient = null;
233
        PromotionClient promotionServiceClient = null;
234
        try {
235
            if(action == null || action.isEmpty())  {
236
                errorMsg = "Invalid Request Action";
237
                return index();
238
            }
239
            String couponCode = request.getParameter("coupon_code");
240
 
241
            if (action.equalsIgnoreCase("applycoupon"))   {
242
                if (couponCode == null || couponCode.isEmpty()) {
243
                    errorMsg = "Coupon Code field cannot be left empty";
244
                    return index();
245
                }
246
 
247
                promotionServiceClient = new PromotionClient();
248
                PromotionService.Client promotionClient = promotionServiceClient.getClient();
249
 
250
                promotionClient.applyCoupon(couponCode, id);
251
 
252
            }
253
            else if (action.equals("removecoupon"))    {
254
                userServiceClient = new UserClient();
255
                UserContextService.Client userClient = userServiceClient.getClient();
256
                userClient.removeCoupon(id);
257
            }
258
        } catch (PromotionException e) {
259
            log.info("Invalid coupon: " + e.getMessage());
260
            errorMsg = "Invalid coupon : " + e.getMessage();
261
        } catch (Exception e) {
262
            log.error("Unable to apply or remove coupon", e);
263
            errorMsg = "Unable to apply or remove coupon";
264
        }
265
        return index();
266
    }
9808 anupam.sin 267
 
268
    public String changeAddress() {
269
        long addressId;
270
        if(request.getParameter("addressId") == null || request.getParameter("addressId").isEmpty()) {
271
            errorMsg = "Null or empty addressId";
272
            return index();
273
        } else {
9815 anupam.sin 274
            addressId = Long.parseLong(request.getParameter("addressId"));
9808 anupam.sin 275
        }
276
 
277
        try {
278
            UserClient usc = new UserClient();
9815 anupam.sin 279
            if(request.getParameter("addressType").equalsIgnoreCase("store")) {
9808 anupam.sin 280
                usc.getClient().addStoreToCart(id, addressId);
9815 anupam.sin 281
            } else if (request.getParameter("addressType").equalsIgnoreCase("home")){
9808 anupam.sin 282
                usc.getClient().addAddressToCart(id, addressId);
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
}