Subversion Repositories SmartDukaan

Rev

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