Subversion Repositories SmartDukaan

Rev

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