Subversion Repositories SmartDukaan

Rev

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