Subversion Repositories SmartDukaan

Rev

Rev 11593 | Rev 11768 | 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
 
217
                promotionClient.applyCoupon(couponCode, id);
218
 
219
            }
220
            else if (action.equals("removecoupon"))    {
11535 amit.gupta 221
                getClient().removeCoupon(id);
9713 anupam.sin 222
            }
223
        } catch (PromotionException e) {
224
            log.info("Invalid coupon: " + e.getMessage());
225
            errorMsg = "Invalid coupon : " + e.getMessage();
226
        } catch (Exception e) {
227
            log.error("Unable to apply or remove coupon", e);
228
            errorMsg = "Unable to apply or remove coupon";
229
        }
230
        return index();
231
    }
9808 anupam.sin 232
 
233
    public String changeAddress() {
234
        long addressId;
235
        if(request.getParameter("addressId") == null || request.getParameter("addressId").isEmpty()) {
236
            errorMsg = "Null or empty addressId";
237
            return index();
238
        } else {
9815 anupam.sin 239
            addressId = Long.parseLong(request.getParameter("addressId"));
9808 anupam.sin 240
        }
241
 
242
        try {
9815 anupam.sin 243
            if(request.getParameter("addressType").equalsIgnoreCase("store")) {
11535 amit.gupta 244
                getClient().addStoreToCart(id, addressId);
9815 anupam.sin 245
            } else if (request.getParameter("addressType").equalsIgnoreCase("home")){
11535 amit.gupta 246
                getClient().addAddressToCart(id, addressId);
9808 anupam.sin 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
}