Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
11592 amit.gupta 1
package in.shop2020.mobileapi.serving.controllers;
9570 anupam.sin 2
 
11516 amit.gupta 3
import in.shop2020.mobileapi.serving.pojos.CartPojo;
10724 amit.gupta 4
import in.shop2020.mobileapi.serving.pojos.RedirectPojo;
9577 anupam.sin 5
import in.shop2020.mobileapi.serving.services.ContentServingService;
9667 anupam.sin 6
import in.shop2020.mobileapi.serving.utils.PojoPopulator;
9577 anupam.sin 7
import in.shop2020.mobileapi.serving.utils.SnippetType;
9570 anupam.sin 8
import in.shop2020.model.v1.catalog.CatalogService.Client;
9
import in.shop2020.model.v1.catalog.Item;
10
import in.shop2020.model.v1.user.Cart;
11592 amit.gupta 11
import in.shop2020.model.v1.user.CartPlus;
9713 anupam.sin 12
import in.shop2020.model.v1.user.PromotionException;
13
import in.shop2020.model.v1.user.PromotionService;
9570 anupam.sin 14
import in.shop2020.model.v1.user.ShoppingCartException;
15
import in.shop2020.model.v1.user.UserContextService;
16
import in.shop2020.thrift.clients.CatalogClient;
9713 anupam.sin 17
import in.shop2020.thrift.clients.PromotionClient;
9570 anupam.sin 18
import in.shop2020.thrift.clients.UserClient;
19
 
20
import java.util.List;
21
import java.util.Map;
22
 
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
 
11516 amit.gupta 53
    private long id =-1; //CartId
9667 anupam.sin 54
    private String cartPojoJson;
9676 anupam.sin 55
    private long quantity;
9697 anupam.sin 56
    private int insuranceType;
11386 amit.gupta 57
    UserContextService.Client userClient;
9570 anupam.sin 58
 
11386 amit.gupta 59
    public CartController() throws Exception{
9570 anupam.sin 60
        super();
61
    }
62
 
11535 amit.gupta 63
    public UserContextService.Client getClient() throws Exception{
11602 amit.gupta 64
    	userClient = new UserClient().getClient();
11535 amit.gupta 65
    	return userClient;
11593 amit.gupta 66
    }	
9808 anupam.sin 67
    public String index() {
9709 anupam.sin 68
        if(id != -1){
9570 anupam.sin 69
            try {
11511 amit.gupta 70
            	log.info("Before cart response--------");
11592 amit.gupta 71
                CartPlus cartResponse  = getClient().validateCartPlus(id, -1);
11511 amit.gupta 72
                log.info("After cart response--------");
11823 amit.gupta 73
                if (errorMsg == null || errorMsg.equals("")) {
11917 amit.gupta 74
                	if(cartResponse.getValidateCartMessages().get(1)!=null && !cartResponse.getValidateCartMessages().get(1).equals("")){
75
                		errorMsg = cartResponse.getValidateCartMessages().get(1);
76
                	}else {
77
                		errorMsg = cartResponse.getValidateCartMessages().get(0);
78
                	}
11823 amit.gupta 79
                }
80
                cartPojoJson = new Gson().toJson(PojoPopulator.getCartPojo(cartResponse.getCart(), errorMsg, 
11592 amit.gupta 81
                		cartResponse.getPinCode(), cartResponse.isNeedInsuranceInfo()));
10551 amit.gupta 82
                log.info(cartPojoJson);
9570 anupam.sin 83
            } catch (Exception e) {
11593 amit.gupta 84
                /*TTransportException te = (TTransportException)e;
85
                log.warn(e.getLocalizedMessage() + " " + te.getType());*/
9570 anupam.sin 86
                log.warn("Unable to validate the cart: ", e);
87
            }
11516 amit.gupta 88
        } else {
89
        	cartPojoJson = new Gson().toJson(new CartPojo());
9570 anupam.sin 90
        }
91
        return "index";
92
    }
93
 
94
    public String create() {
10724 amit.gupta 95
    	RedirectPojo rp = new RedirectPojo();
96
    	rp.setRedirectUrl("");
9676 anupam.sin 97
        try {
11526 amit.gupta 98
        	//Try adding to cart
11535 amit.gupta 99
            cartMsg = getClient().addItemToCart(id, itemId, 1, -1);
100
            log.info("Cart message while adding item to cart - " + cartMsg);
11526 amit.gupta 101
            //If could not add to cart try to add notification reminder if email exists.
9680 amit.gupta 102
            if (!("".equals(cartMsg))) {
10767 amit.gupta 103
            	String emailId = this.request.getParameter("email");
10766 amit.gupta 104
            	log.info("QuesyString" + this.request.getParameterMap().toString());
10724 amit.gupta 105
    			if(emailId==null || emailId.equals("")){
11526 amit.gupta 106
    				//User tries to add to cart but product is actually out of stock
10724 amit.gupta 107
    				rp.setMessage("Item is currently out of stock");
108
    			} else {	
109
        			try {
110
	        	        CatalogClient catalogClientService;
111
	        			catalogClientService = new CatalogClient();
112
	        			in.shop2020.model.v1.catalog.CatalogService.Client client = catalogClientService.getClient();
113
	        			client.addProductNotification(itemId, emailId);
114
	        			log.debug("Got product notification for product: " + itemId + "  from user: " + emailId );
115
	        			rp.setMessage("You will be notified when product is avaliable");
116
        			} catch (Exception e) {
117
        				rp.setMessage("Error occurred while adding to notificiation");
118
        				log.error("Exception while adding product notification for item: " + itemId + " and email: " + emailId, e);
10261 amit.gupta 119
        			}
10724 amit.gupta 120
 
10261 amit.gupta 121
        		}
10724 amit.gupta 122
            } else {
123
            	rp.setRedirectUrl("cart");
9676 anupam.sin 124
            }
125
        } catch (TException e) {
126
            log.error("Unable to create or add to cart because of: ", e);
9713 anupam.sin 127
            errorMsg = "Unable to add to cart. Please try again.";
9676 anupam.sin 128
        } catch (Exception e) {
129
            log.error("Unable to create or add to cart because of: ", e);
9713 anupam.sin 130
            errorMsg = "Unable to add to cart. Please try again.";
9570 anupam.sin 131
        }
10724 amit.gupta 132
        cartPojoJson = new Gson().toJson(rp);
133
        return "index";
9676 anupam.sin 134
    }       
9570 anupam.sin 135
 
9676 anupam.sin 136
 
137
    // DELETE /entity
138
    public String destroy() {
9709 anupam.sin 139
        if(id > 0)  {
9570 anupam.sin 140
            try {
11535 amit.gupta 141
                getClient().deleteItemFromCart(id, itemId);
9676 anupam.sin 142
                return index();
143
            } catch (ShoppingCartException e) {
144
                log.error("Unable to delete item from cart: ", e);
9713 anupam.sin 145
                errorMsg = "Unable to delete item. Please try again.";
9570 anupam.sin 146
            } catch (TException e) {
9676 anupam.sin 147
                log.error("Unable to delete item from cart: ", e);
9713 anupam.sin 148
                errorMsg = "Unable to delete item. Please try again.";
9570 anupam.sin 149
            } catch (Exception e) {
9676 anupam.sin 150
                log.error("Unable to delete item from cart: ", e);
9713 anupam.sin 151
                errorMsg = "Unable to delete item. Please try again.";
9570 anupam.sin 152
            }
153
        }
9676 anupam.sin 154
        return index();
9570 anupam.sin 155
    }
9676 anupam.sin 156
 
157
    //PUT
9570 anupam.sin 158
    public String update() {
9676 anupam.sin 159
        if(quantity <= 0)   {
160
            log.info("Not valid item quantity. Unable to change item quantity.");
9713 anupam.sin 161
            errorMsg = "Invalid item quantity";
9676 anupam.sin 162
        } else  {
9709 anupam.sin 163
            if(updateItemQuantityInCart(id, itemId, quantity))    {
9676 anupam.sin 164
                return index();
165
            }
166
        }
167
        return index();
9570 anupam.sin 168
    }
169
 
170
    private boolean updateItemQuantityInCart(long cartId, long itemId, long quantity){
171
        try {
11535 amit.gupta 172
            getClient().addItemToCart(cartId, itemId, quantity, -1);
9570 anupam.sin 173
            return true;
174
        } catch (ShoppingCartException e) {
175
            log.error("Unable to update the item quantity in the cart: ", e);
9713 anupam.sin 176
            errorMsg = "Unable to change quantity. Please try again.";
9570 anupam.sin 177
        } catch (TException e) {
178
            log.error("Unable to update the item quantity in the cart: ", e);
9713 anupam.sin 179
            errorMsg = "Unable to change quantity. Please try again.";
9570 anupam.sin 180
        } catch (Exception e) {
181
            log.error("Unable to update the item quantity in the cart: ", e);
9713 anupam.sin 182
            errorMsg = "Unable to change quantity. Please try again.";
9570 anupam.sin 183
        }
184
        return false;
185
    }
9676 anupam.sin 186
 
9570 anupam.sin 187
    public String insureItem() {
9697 anupam.sin 188
        insuranceResult = "";
189
        try {
11535 amit.gupta 190
            if(getClient().insureItem(itemId, id, toInsure, insuranceType)) {
9697 anupam.sin 191
                setInsuranceResult("SUCCESS");
192
            } else {
193
                setInsuranceResult("FAILURE");
194
            }
195
        } catch (Exception e) {
9709 anupam.sin 196
            log.error("Unable to insure item : " + itemId + " for cart : " + id, e);
9697 anupam.sin 197
            setInsuranceResult("FAILURE");
198
        }
9570 anupam.sin 199
        return "insurance-result";
200
    }
9713 anupam.sin 201
 
202
    public String edit()  {
203
        String action = request.getParameter("action");
204
        PromotionClient promotionServiceClient = null;
205
        try {
206
            if(action == null || action.isEmpty())  {
207
                errorMsg = "Invalid Request Action";
208
                return index();
209
            }
210
            String couponCode = request.getParameter("coupon_code");
211
 
212
            if (action.equalsIgnoreCase("applycoupon"))   {
213
                if (couponCode == null || couponCode.isEmpty()) {
214
                    errorMsg = "Coupon Code field cannot be left empty";
215
                    return index();
216
                }
217
 
218
                promotionServiceClient = new PromotionClient();
219
                PromotionService.Client promotionClient = promotionServiceClient.getClient();
220
 
11768 amit.gupta 221
                Cart cart = promotionClient.applyCoupon(couponCode, id);
11823 amit.gupta 222
            	errorMsg = "Coupon successfully applied";
11861 amit.gupta 223
            	if(cart.getMessage() != null && !cart.getMessage().equals("")){
11823 amit.gupta 224
                	errorMsg = errorMsg +  ", " + cart.getMessage();
11768 amit.gupta 225
                }
9713 anupam.sin 226
            }
227
            else if (action.equals("removecoupon"))    {
11535 amit.gupta 228
                getClient().removeCoupon(id);
9713 anupam.sin 229
            }
230
        } catch (PromotionException e) {
231
            log.info("Invalid coupon: " + e.getMessage());
11823 amit.gupta 232
            errorMsg = e.getMessage();
9713 anupam.sin 233
        } catch (Exception e) {
234
            log.error("Unable to apply or remove coupon", e);
235
            errorMsg = "Unable to apply or remove coupon";
236
        }
237
        return index();
238
    }
9808 anupam.sin 239
 
240
    public String changeAddress() {
241
        long addressId;
242
        if(request.getParameter("addressId") == null || request.getParameter("addressId").isEmpty()) {
243
            errorMsg = "Null or empty addressId";
244
            return index();
245
        } else {
9815 anupam.sin 246
            addressId = Long.parseLong(request.getParameter("addressId"));
9808 anupam.sin 247
        }
248
 
249
        try {
9815 anupam.sin 250
            if(request.getParameter("addressType").equalsIgnoreCase("store")) {
11535 amit.gupta 251
                getClient().addStoreToCart(id, addressId);
9815 anupam.sin 252
            } else if (request.getParameter("addressType").equalsIgnoreCase("home")){
11535 amit.gupta 253
                getClient().addAddressToCart(id, addressId);
9808 anupam.sin 254
            } else {
255
                errorMsg = "Invalid addressType. It can only be set to home or store.";
256
            }
257
        } catch(Exception e) {
258
            log.error("Unable to change address", e);
259
        }
260
        return index();
261
    }
9570 anupam.sin 262
 
263
    public long getItemId(){
264
        return this.itemId;
265
    }
266
 
9684 amit.gupta 267
    public void setItemId(long itemId){
268
    	this.itemId = itemId;
269
    }
270
 
9570 anupam.sin 271
    public String getTotalAmount() {
272
        return totalamount;
273
    }
274
 
275
    public String getPinCode() {
276
        return pincode;
277
    }
278
 
279
    public String getCouponCode()  {
280
        return couponCode;
281
    }
282
 
283
    public String getDiscountedAmount()   {
284
        return discountedAmount;
285
    }
286
 
287
    public String getErrorMsg()    {
288
        return errorMsg;
289
    }
290
 
9576 anupam.sin 291
//    public long getNumberOfItems(){
292
//        return userinfo.getTotalItems();
293
//    }
9570 anupam.sin 294
 
295
    public String getCartMsg(){
296
        if(cartMsg.equals("")){
297
            return null;
298
        }
299
        return cartMsg;
300
    }
301
 
302
    public String getSnippets(){
303
        String snippets = "";
304
        CatalogClient csc;
305
        try {
306
            csc = new CatalogClient();
307
            List<Long> similarItems = csc.getClient().getSimilarItemsCatalogIds(0, 4, itemId);
308
            for(Long catalogId: similarItems){
9602 amit.gupta 309
                snippets = snippets + ContentServingService.getSnippet(SnippetType.WIDGET_SNIPPET, catalogId+"", -1);
9570 anupam.sin 310
            }
311
        } catch (Exception e) {
312
            log.error("Unable to initialise Catalogservice Client");
313
        }       
314
        return snippets;
315
    }
316
 
317
    @Override
318
    public void setParameters(Map<String, String[]> parameters) {
319
        this.reqparams = parameters;    
320
    }
321
 
9576 anupam.sin 322
//    @Override
323
//    public String getHeaderSnippet() {
324
//        String url = request.getQueryString();
325
//        if (url == null) {
326
//            url = "";
327
//        } else {
328
//            url = "?" + url;
329
//        }
330
//        url = request.getRequestURI() + url;
331
//        return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), userinfo.getTotalItems(), url , 0, false);
332
//    }
9570 anupam.sin 333
 
9576 anupam.sin 334
//    public boolean isUserLoggedIn() {
335
//        return userinfo.isLoggedIn();
336
//    }
9570 anupam.sin 337
 
338
    public void setVariationId(String uri)  {
339
        if (uri.equals("/cart1"))   {
340
            this.variationId = 1;
341
        }
342
    }
343
 
344
    public int getVariationId() {
345
        return this.variationId;
346
    }
347
 
348
    public String getActionMessage(){
349
        if(cartMsg.contains("out of stock")){
350
            return "Notify me when this product is in stock.";
351
        }else {
352
            return "Notify me when this product is available.";
353
        }
354
    }
355
 
356
    public String getOfferNote(){
357
        String note = null;
358
        if(cartMsg.contains("out of stock")){
359
            return note;
360
        }
361
        else {
362
            try {
363
                CatalogClient catalogServiceClient = new CatalogClient();
364
                Client catalogClient = catalogServiceClient.getClient();
365
                Item it = catalogClient.getItem(itemId);
366
                note = it.getBestDealText();
367
            } catch (Exception e)  {
368
                log.error("Unable to get the offertext because of: ", e);
369
            }
370
        }
371
        return note;
372
    }
373
 
374
    public String getInsuranceResult() {
375
        return insuranceResult;
376
    }
377
 
378
    public void setInsuranceResult(String insuranceResult) {
379
        this.insuranceResult = insuranceResult;
380
    }
381
 
382
    public void setToInsure(boolean toInsure) {
383
        this.toInsure = toInsure;
384
    }
385
 
386
    public boolean getToInsure() {
387
        return toInsure;
388
    }
389
 
9709 anupam.sin 390
    public long getId() {
391
        return id;
9570 anupam.sin 392
    }
393
 
9709 anupam.sin 394
    public void setId(long id) {
395
        this.id = id;
9570 anupam.sin 396
    }
9667 anupam.sin 397
 
398
 
399
    public void setCartPojoJson(String cartPojoJson) {
400
        this.cartPojoJson = cartPojoJson;
401
    }
402
 
403
 
404
    public String getCartPojoJson() {
405
        return cartPojoJson;
406
    }
9676 anupam.sin 407
 
408
    public void setQuantity(long quantity) {
409
        this.quantity = quantity;
410
    }
411
 
412
    public long getQuantity() {
413
        return quantity;
414
    }
9697 anupam.sin 415
 
416
    public int getInsuranceType() {
417
        return insuranceType;
418
    }
419
 
420
    public void setInsuranceType(int insuranceType) {
421
        this.insuranceType = insuranceType;
422
    }
9570 anupam.sin 423
}