Subversion Repositories SmartDukaan

Rev

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