Subversion Repositories SmartDukaan

Rev

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