Subversion Repositories SmartDukaan

Rev

Rev 11768 | Rev 11861 | 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("")) {
74
                	errorMsg = cartResponse.getValidateCartMessages().get(0);
75
                }
76
                cartPojoJson = new Gson().toJson(PojoPopulator.getCartPojo(cartResponse.getCart(), errorMsg, 
11592 amit.gupta 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);
11823 amit.gupta 218
            	errorMsg = "Coupon successfully applied";
219
            	if(cart.getMessage() != null && cart.getMessage().equals("")){
220
                	errorMsg = errorMsg +  ", " + cart.getMessage();
11768 amit.gupta 221
                }
9713 anupam.sin 222
            }
223
            else if (action.equals("removecoupon"))    {
11535 amit.gupta 224
                getClient().removeCoupon(id);
9713 anupam.sin 225
            }
226
        } catch (PromotionException e) {
227
            log.info("Invalid coupon: " + e.getMessage());
11823 amit.gupta 228
            errorMsg = e.getMessage();
9713 anupam.sin 229
        } catch (Exception e) {
230
            log.error("Unable to apply or remove coupon", e);
231
            errorMsg = "Unable to apply or remove coupon";
232
        }
233
        return index();
234
    }
9808 anupam.sin 235
 
236
    public String changeAddress() {
237
        long addressId;
238
        if(request.getParameter("addressId") == null || request.getParameter("addressId").isEmpty()) {
239
            errorMsg = "Null or empty addressId";
240
            return index();
241
        } else {
9815 anupam.sin 242
            addressId = Long.parseLong(request.getParameter("addressId"));
9808 anupam.sin 243
        }
244
 
245
        try {
9815 anupam.sin 246
            if(request.getParameter("addressType").equalsIgnoreCase("store")) {
11535 amit.gupta 247
                getClient().addStoreToCart(id, addressId);
9815 anupam.sin 248
            } else if (request.getParameter("addressType").equalsIgnoreCase("home")){
11535 amit.gupta 249
                getClient().addAddressToCart(id, addressId);
9808 anupam.sin 250
            } else {
251
                errorMsg = "Invalid addressType. It can only be set to home or store.";
252
            }
253
        } catch(Exception e) {
254
            log.error("Unable to change address", e);
255
        }
256
        return index();
257
    }
9570 anupam.sin 258
 
259
    public long getItemId(){
260
        return this.itemId;
261
    }
262
 
9684 amit.gupta 263
    public void setItemId(long itemId){
264
    	this.itemId = itemId;
265
    }
266
 
9570 anupam.sin 267
    public String getTotalAmount() {
268
        return totalamount;
269
    }
270
 
271
    public String getPinCode() {
272
        return pincode;
273
    }
274
 
275
    public String getCouponCode()  {
276
        return couponCode;
277
    }
278
 
279
    public String getDiscountedAmount()   {
280
        return discountedAmount;
281
    }
282
 
283
    public String getErrorMsg()    {
284
        return errorMsg;
285
    }
286
 
9576 anupam.sin 287
//    public long getNumberOfItems(){
288
//        return userinfo.getTotalItems();
289
//    }
9570 anupam.sin 290
 
291
    public String getCartMsg(){
292
        if(cartMsg.equals("")){
293
            return null;
294
        }
295
        return cartMsg;
296
    }
297
 
298
    public String getSnippets(){
299
        String snippets = "";
300
        CatalogClient csc;
301
        try {
302
            csc = new CatalogClient();
303
            List<Long> similarItems = csc.getClient().getSimilarItemsCatalogIds(0, 4, itemId);
304
            for(Long catalogId: similarItems){
9602 amit.gupta 305
                snippets = snippets + ContentServingService.getSnippet(SnippetType.WIDGET_SNIPPET, catalogId+"", -1);
9570 anupam.sin 306
            }
307
        } catch (Exception e) {
308
            log.error("Unable to initialise Catalogservice Client");
309
        }       
310
        return snippets;
311
    }
312
 
313
    @Override
314
    public void setParameters(Map<String, String[]> parameters) {
315
        this.reqparams = parameters;    
316
    }
317
 
9576 anupam.sin 318
//    @Override
319
//    public String getHeaderSnippet() {
320
//        String url = request.getQueryString();
321
//        if (url == null) {
322
//            url = "";
323
//        } else {
324
//            url = "?" + url;
325
//        }
326
//        url = request.getRequestURI() + url;
327
//        return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), userinfo.getTotalItems(), url , 0, false);
328
//    }
9570 anupam.sin 329
 
9576 anupam.sin 330
//    public boolean isUserLoggedIn() {
331
//        return userinfo.isLoggedIn();
332
//    }
9570 anupam.sin 333
 
334
    public void setVariationId(String uri)  {
335
        if (uri.equals("/cart1"))   {
336
            this.variationId = 1;
337
        }
338
    }
339
 
340
    public int getVariationId() {
341
        return this.variationId;
342
    }
343
 
344
    public String getActionMessage(){
345
        if(cartMsg.contains("out of stock")){
346
            return "Notify me when this product is in stock.";
347
        }else {
348
            return "Notify me when this product is available.";
349
        }
350
    }
351
 
352
    public String getOfferNote(){
353
        String note = null;
354
        if(cartMsg.contains("out of stock")){
355
            return note;
356
        }
357
        else {
358
            try {
359
                CatalogClient catalogServiceClient = new CatalogClient();
360
                Client catalogClient = catalogServiceClient.getClient();
361
                Item it = catalogClient.getItem(itemId);
362
                note = it.getBestDealText();
363
            } catch (Exception e)  {
364
                log.error("Unable to get the offertext because of: ", e);
365
            }
366
        }
367
        return note;
368
    }
369
 
370
    public String getInsuranceResult() {
371
        return insuranceResult;
372
    }
373
 
374
    public void setInsuranceResult(String insuranceResult) {
375
        this.insuranceResult = insuranceResult;
376
    }
377
 
378
    public void setToInsure(boolean toInsure) {
379
        this.toInsure = toInsure;
380
    }
381
 
382
    public boolean getToInsure() {
383
        return toInsure;
384
    }
385
 
9709 anupam.sin 386
    public long getId() {
387
        return id;
9570 anupam.sin 388
    }
389
 
9709 anupam.sin 390
    public void setId(long id) {
391
        this.id = id;
9570 anupam.sin 392
    }
9667 anupam.sin 393
 
394
 
395
    public void setCartPojoJson(String cartPojoJson) {
396
        this.cartPojoJson = cartPojoJson;
397
    }
398
 
399
 
400
    public String getCartPojoJson() {
401
        return cartPojoJson;
402
    }
9676 anupam.sin 403
 
404
    public void setQuantity(long quantity) {
405
        this.quantity = quantity;
406
    }
407
 
408
    public long getQuantity() {
409
        return quantity;
410
    }
9697 anupam.sin 411
 
412
    public int getInsuranceType() {
413
        return insuranceType;
414
    }
415
 
416
    public void setInsuranceType(int insuranceType) {
417
        this.insuranceType = insuranceType;
418
    }
9570 anupam.sin 419
}