Subversion Repositories SmartDukaan

Rev

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