Subversion Repositories SmartDukaan

Rev

Rev 9815 | Rev 10482 | 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;
9713 anupam.sin 10
import in.shop2020.model.v1.user.PromotionException;
11
import in.shop2020.model.v1.user.PromotionService;
9570 anupam.sin 12
import in.shop2020.model.v1.user.ShoppingCartException;
13
import in.shop2020.model.v1.user.UserContextService;
14
import in.shop2020.thrift.clients.CatalogClient;
9713 anupam.sin 15
import in.shop2020.thrift.clients.PromotionClient;
9570 anupam.sin 16
import in.shop2020.thrift.clients.UserClient;
17
 
18
import java.util.List;
19
import java.util.Map;
20
 
21
import org.apache.commons.lang.StringUtils;
22
import org.apache.log4j.Logger;
23
import org.apache.struts2.interceptor.ParameterAware;
24
import org.apache.thrift.TException;
25
 
9667 anupam.sin 26
import com.google.gson.Gson;
9570 anupam.sin 27
 
28
public class CartController extends BaseController implements ParameterAware{
29
 
30
    private static final long serialVersionUID = 1L;
31
    private static Logger log = Logger.getLogger(Class.class);
32
    Map<String, String[]> reqparams = null;
33
 
34
    private int variationId = 0;
35
    private String totalamount;
36
 
37
    private String errorMsg = "";
38
    private String cartMsg = "";
39
 
40
    private String pincode = "110001";
41
 
42
    private String couponCode = null;
43
 
44
    private String discountedAmount;
45
 
46
    private long itemId;
47
    private String insuranceResult;
48
 
49
    private boolean toInsure;
50
 
9676 anupam.sin 51
 
9709 anupam.sin 52
    private long id; //CartId
9667 anupam.sin 53
    private String cartPojoJson;
9676 anupam.sin 54
    private long quantity;
9697 anupam.sin 55
    private int insuranceType;
9570 anupam.sin 56
 
57
    public CartController(){
58
        super();
59
    }
60
 
9808 anupam.sin 61
    public String index() {
9709 anupam.sin 62
        if(id != -1){
9570 anupam.sin 63
            try {
64
                UserContextService.Client userClient = (new UserClient()).getClient();
9709 anupam.sin 65
                List<String> cartResponse  = userClient.validateCart(id, -1);
9713 anupam.sin 66
                if (!"".equals(cartResponse.get(0))) {
67
                    errorMsg = cartResponse.get(0);
68
                }
9570 anupam.sin 69
                if(StringUtils.isNotEmpty(cartResponse.get(1))) {
70
                    addActionMessage(cartResponse.get(1));
71
                }
72
                log.info("Cart Change/EMI Message rcvd from the service is:" + errorMsg);
9667 anupam.sin 73
 
9686 amit.gupta 74
                Cart cart = userClient.getCurrentCart(userinfo.getUserId());
9808 anupam.sin 75
                String pincode = "110001";
76
                UserClient usc = new UserClient();
77
                if(cart.getAddressId() < 1) {
78
                    try {
79
                        pincode = usc.getClient().getDefaultPincode(userinfo.getUserId());
80
                    } catch(Exception e) {
81
                        log.error("Unable to get default pincode for userId : " + userinfo.getUserId(), e);
82
                    }
83
                } else {
84
                    try {
85
                        pincode = usc.getClient().getAddressById(cart.getAddressId()).getPin();
86
                    } catch(Exception e) {
87
                        log.error("Unable to get address for addressId : " + cart.getAddressId(), e);
88
                    }   
89
                }
9667 anupam.sin 90
 
9808 anupam.sin 91
                cartPojoJson = new Gson().toJson(PojoPopulator.getCartPojo(cart, errorMsg, pincode));
92
 
9570 anupam.sin 93
            } catch (Exception e) {
94
                // This exception can be ignored for showing the cart. Not so
95
                // innocent when this occurs at the time of checkout or when the
96
                // user is proceeding to pay.
97
                log.warn("Unable to validate the cart: ", e);
98
            }
99
        }
100
        return "index";
101
    }
102
 
103
    public String create() {
9676 anupam.sin 104
        try {
10261 amit.gupta 105
        	NotificationPojo np = new NotificationPojo();
9676 anupam.sin 106
            UserClient userServiceClient = new UserClient();
107
            UserContextService.Client userClient = userServiceClient.getClient();
9709 anupam.sin 108
            cartMsg = userClient.addItemToCart(id, itemId, 1, -1);
9680 amit.gupta 109
            if (!("".equals(cartMsg))) {
10261 amit.gupta 110
            	String emailId = "";
111
        		try {
112
        			emailId = this.request.getParameter("emailId");
113
        			if(emailId!=null){
114
        				np.setMessage("Could not add this item to cart");
115
        				np.setStatus("oos");
116
        				cartPojoJson = new Gson().toJson(np);
117
        				return "index";
118
 
119
        			}
120
        			itemId = Long.parseLong(this.request.getParameter("itemId"));
121
 
122
        	        CatalogClient catalogClientService;
123
        			catalogClientService = new CatalogClient();
124
        			in.shop2020.model.v1.catalog.CatalogService.Client client = catalogClientService.getClient();
125
        			client.addProductNotification(itemId, emailId);
126
        		} catch (Exception e) {
127
        			log.error("Exception while adding product notification for item: " + itemId + " and email: " + emailId, e);
128
        		}
129
        		log.debug("Got product notification for product: " + itemId + "  from user: " + emailId );
130
        		np.setMessage("You will be notified when product is avaliable");
131
        		np.setStatus("success");
132
        		cartPojoJson = new Gson().toJson(np);
133
                return "index";
9676 anupam.sin 134
            }
135
        } catch (TException e) {
136
            log.error("Unable to create or add to cart because of: ", e);
9713 anupam.sin 137
            errorMsg = "Unable to add to cart. Please try again.";
9676 anupam.sin 138
        } catch (Exception e) {
139
            log.error("Unable to create or add to cart because of: ", e);
9713 anupam.sin 140
            errorMsg = "Unable to add to cart. Please try again.";
9570 anupam.sin 141
        }
142
 
9676 anupam.sin 143
    return index();
144
    }       
9570 anupam.sin 145
 
9676 anupam.sin 146
 
147
    // DELETE /entity
148
    public String destroy() {
9709 anupam.sin 149
        if(id > 0)  {
9570 anupam.sin 150
            try {
9676 anupam.sin 151
                UserClient userContextServiceClient = new UserClient();
152
                in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
153
 
9709 anupam.sin 154
                userClient.deleteItemFromCart(id, itemId);
9676 anupam.sin 155
                return index();
156
            } catch (ShoppingCartException e) {
157
                log.error("Unable to delete item from cart: ", e);
9713 anupam.sin 158
                errorMsg = "Unable to delete item. Please try again.";
9570 anupam.sin 159
            } catch (TException e) {
9676 anupam.sin 160
                log.error("Unable to delete item from cart: ", e);
9713 anupam.sin 161
                errorMsg = "Unable to delete item. Please try again.";
9570 anupam.sin 162
            } catch (Exception e) {
9676 anupam.sin 163
                log.error("Unable to delete item from cart: ", e);
9713 anupam.sin 164
                errorMsg = "Unable to delete item. Please try again.";
9570 anupam.sin 165
            }
166
        }
9676 anupam.sin 167
        return index();
9570 anupam.sin 168
    }
9676 anupam.sin 169
 
170
    //PUT
9570 anupam.sin 171
    public String update() {
9676 anupam.sin 172
        if(quantity <= 0)   {
173
            log.info("Not valid item quantity. Unable to change item quantity.");
9713 anupam.sin 174
            errorMsg = "Invalid item quantity";
9676 anupam.sin 175
        } else  {
9709 anupam.sin 176
            if(updateItemQuantityInCart(id, itemId, quantity))    {
9676 anupam.sin 177
                return index();
178
            }
179
        }
180
        return index();
9570 anupam.sin 181
    }
182
 
183
    private boolean updateItemQuantityInCart(long cartId, long itemId, long quantity){
184
        try {
185
            UserClient userContextServiceClient = new UserClient();
186
            in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
187
 
9602 amit.gupta 188
            userClient.addItemToCart(cartId, itemId, quantity, -1);
9570 anupam.sin 189
            return true;
190
        } catch (ShoppingCartException e) {
191
            log.error("Unable to update the item quantity in the cart: ", e);
9713 anupam.sin 192
            errorMsg = "Unable to change quantity. Please try again.";
9570 anupam.sin 193
        } catch (TException e) {
194
            log.error("Unable to update the item quantity in the cart: ", e);
9713 anupam.sin 195
            errorMsg = "Unable to change quantity. Please try again.";
9570 anupam.sin 196
        } catch (Exception e) {
197
            log.error("Unable to update the item quantity in the cart: ", e);
9713 anupam.sin 198
            errorMsg = "Unable to change quantity. Please try again.";
9570 anupam.sin 199
        }
200
        return false;
201
    }
9676 anupam.sin 202
 
9570 anupam.sin 203
    public String insureItem() {
9697 anupam.sin 204
        insuranceResult = "";
205
        try {
206
            UserContextService.Client usc = new UserClient().getClient();
9709 anupam.sin 207
            if(usc.insureItem(itemId, id, toInsure, insuranceType)) {
9697 anupam.sin 208
                setInsuranceResult("SUCCESS");
209
            } else {
210
                setInsuranceResult("FAILURE");
211
            }
212
        } catch (Exception e) {
9709 anupam.sin 213
            log.error("Unable to insure item : " + itemId + " for cart : " + id, e);
9697 anupam.sin 214
            setInsuranceResult("FAILURE");
215
        }
9570 anupam.sin 216
        return "insurance-result";
217
    }
9713 anupam.sin 218
 
219
    public String edit()  {
220
        String action = request.getParameter("action");
221
        UserClient userServiceClient = null;
222
        PromotionClient promotionServiceClient = null;
223
        try {
224
            if(action == null || action.isEmpty())  {
225
                errorMsg = "Invalid Request Action";
226
                return index();
227
            }
228
            String couponCode = request.getParameter("coupon_code");
229
 
230
            if (action.equalsIgnoreCase("applycoupon"))   {
231
                if (couponCode == null || couponCode.isEmpty()) {
232
                    errorMsg = "Coupon Code field cannot be left empty";
233
                    return index();
234
                }
235
 
236
                promotionServiceClient = new PromotionClient();
237
                PromotionService.Client promotionClient = promotionServiceClient.getClient();
238
 
239
                promotionClient.applyCoupon(couponCode, id);
240
 
241
            }
242
            else if (action.equals("removecoupon"))    {
243
                userServiceClient = new UserClient();
244
                UserContextService.Client userClient = userServiceClient.getClient();
245
                userClient.removeCoupon(id);
246
            }
247
        } catch (PromotionException e) {
248
            log.info("Invalid coupon: " + e.getMessage());
249
            errorMsg = "Invalid coupon : " + e.getMessage();
250
        } catch (Exception e) {
251
            log.error("Unable to apply or remove coupon", e);
252
            errorMsg = "Unable to apply or remove coupon";
253
        }
254
        return index();
255
    }
9808 anupam.sin 256
 
257
    public String changeAddress() {
258
        long addressId;
259
        if(request.getParameter("addressId") == null || request.getParameter("addressId").isEmpty()) {
260
            errorMsg = "Null or empty addressId";
261
            return index();
262
        } else {
9815 anupam.sin 263
            addressId = Long.parseLong(request.getParameter("addressId"));
9808 anupam.sin 264
        }
265
 
266
        try {
267
            UserClient usc = new UserClient();
9815 anupam.sin 268
            if(request.getParameter("addressType").equalsIgnoreCase("store")) {
9808 anupam.sin 269
                usc.getClient().addStoreToCart(id, addressId);
9815 anupam.sin 270
            } else if (request.getParameter("addressType").equalsIgnoreCase("home")){
9808 anupam.sin 271
                usc.getClient().addAddressToCart(id, addressId);
272
            } else {
273
                errorMsg = "Invalid addressType. It can only be set to home or store.";
274
            }
275
        } catch(Exception e) {
276
            log.error("Unable to change address", e);
277
        }
278
        return index();
279
    }
9570 anupam.sin 280
 
281
    public long getItemId(){
282
        return this.itemId;
283
    }
284
 
9684 amit.gupta 285
    public void setItemId(long itemId){
286
    	this.itemId = itemId;
287
    }
288
 
9570 anupam.sin 289
    public String getTotalAmount() {
290
        return totalamount;
291
    }
292
 
293
    public String getPinCode() {
294
        return pincode;
295
    }
296
 
297
    public String getCouponCode()  {
298
        return couponCode;
299
    }
300
 
301
    public String getDiscountedAmount()   {
302
        return discountedAmount;
303
    }
304
 
305
    public String getErrorMsg()    {
306
        return errorMsg;
307
    }
308
 
9576 anupam.sin 309
//    public long getNumberOfItems(){
310
//        return userinfo.getTotalItems();
311
//    }
9570 anupam.sin 312
 
313
    public String getCartMsg(){
314
        if(cartMsg.equals("")){
315
            return null;
316
        }
317
        return cartMsg;
318
    }
319
 
320
    public String getSnippets(){
321
        String snippets = "";
322
        CatalogClient csc;
323
        try {
324
            csc = new CatalogClient();
325
            List<Long> similarItems = csc.getClient().getSimilarItemsCatalogIds(0, 4, itemId);
326
            for(Long catalogId: similarItems){
9602 amit.gupta 327
                snippets = snippets + ContentServingService.getSnippet(SnippetType.WIDGET_SNIPPET, catalogId+"", -1);
9570 anupam.sin 328
            }
329
        } catch (Exception e) {
330
            log.error("Unable to initialise Catalogservice Client");
331
        }       
332
        return snippets;
333
    }
334
 
335
    @Override
336
    public void setParameters(Map<String, String[]> parameters) {
337
        this.reqparams = parameters;    
338
    }
339
 
9576 anupam.sin 340
//    @Override
341
//    public String getHeaderSnippet() {
342
//        String url = request.getQueryString();
343
//        if (url == null) {
344
//            url = "";
345
//        } else {
346
//            url = "?" + url;
347
//        }
348
//        url = request.getRequestURI() + url;
349
//        return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), userinfo.getTotalItems(), url , 0, false);
350
//    }
9570 anupam.sin 351
 
9576 anupam.sin 352
//    public boolean isUserLoggedIn() {
353
//        return userinfo.isLoggedIn();
354
//    }
9570 anupam.sin 355
 
356
    public void setVariationId(String uri)  {
357
        if (uri.equals("/cart1"))   {
358
            this.variationId = 1;
359
        }
360
    }
361
 
362
    public int getVariationId() {
363
        return this.variationId;
364
    }
365
 
366
    public String getActionMessage(){
367
        if(cartMsg.contains("out of stock")){
368
            return "Notify me when this product is in stock.";
369
        }else {
370
            return "Notify me when this product is available.";
371
        }
372
    }
373
 
374
    public String getOfferNote(){
375
        String note = null;
376
        if(cartMsg.contains("out of stock")){
377
            return note;
378
        }
379
        else {
380
            try {
381
                CatalogClient catalogServiceClient = new CatalogClient();
382
                Client catalogClient = catalogServiceClient.getClient();
383
                Item it = catalogClient.getItem(itemId);
384
                note = it.getBestDealText();
385
            } catch (Exception e)  {
386
                log.error("Unable to get the offertext because of: ", e);
387
            }
388
        }
389
        return note;
390
    }
391
 
392
    public String getInsuranceResult() {
393
        return insuranceResult;
394
    }
395
 
396
    public void setInsuranceResult(String insuranceResult) {
397
        this.insuranceResult = insuranceResult;
398
    }
399
 
400
    public void setToInsure(boolean toInsure) {
401
        this.toInsure = toInsure;
402
    }
403
 
404
    public boolean getToInsure() {
405
        return toInsure;
406
    }
407
 
9709 anupam.sin 408
    public long getId() {
409
        return id;
9570 anupam.sin 410
    }
411
 
9709 anupam.sin 412
    public void setId(long id) {
413
        this.id = id;
9570 anupam.sin 414
    }
9667 anupam.sin 415
 
416
 
417
    public void setCartPojoJson(String cartPojoJson) {
418
        this.cartPojoJson = cartPojoJson;
419
    }
420
 
421
 
422
    public String getCartPojoJson() {
423
        return cartPojoJson;
424
    }
9676 anupam.sin 425
 
426
    public void setQuantity(long quantity) {
427
        this.quantity = quantity;
428
    }
429
 
430
    public long getQuantity() {
431
        return quantity;
432
    }
9697 anupam.sin 433
 
434
    public int getInsuranceType() {
435
        return insuranceType;
436
    }
437
 
438
    public void setInsuranceType(int insuranceType) {
439
        this.insuranceType = insuranceType;
440
    }
9570 anupam.sin 441
}