Subversion Repositories SmartDukaan

Rev

Rev 20984 | 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
 
20600 amit.gupta 3
import in.shop2020.config.ConfigException;
17782 amit.gupta 4
import in.shop2020.logistics.DeliveryType;
5
import in.shop2020.metamodel.util.ProductPojo;
11516 amit.gupta 6
import in.shop2020.mobileapi.serving.pojos.CartPojo;
10724 amit.gupta 7
import in.shop2020.mobileapi.serving.pojos.RedirectPojo;
9577 anupam.sin 8
import in.shop2020.mobileapi.serving.services.ContentServingService;
17782 amit.gupta 9
import in.shop2020.mobileapi.serving.services.LogisticsService;
9667 anupam.sin 10
import in.shop2020.mobileapi.serving.utils.PojoPopulator;
9577 anupam.sin 11
import in.shop2020.mobileapi.serving.utils.SnippetType;
9570 anupam.sin 12
import in.shop2020.model.v1.catalog.CatalogService.Client;
13
import in.shop2020.model.v1.catalog.Item;
14
import in.shop2020.model.v1.user.Cart;
11592 amit.gupta 15
import in.shop2020.model.v1.user.CartPlus;
17782 amit.gupta 16
import in.shop2020.model.v1.user.ItemQuantity;
9713 anupam.sin 17
import in.shop2020.model.v1.user.PromotionException;
18
import in.shop2020.model.v1.user.PromotionService;
9570 anupam.sin 19
import in.shop2020.model.v1.user.ShoppingCartException;
20
import in.shop2020.model.v1.user.UserContextService;
21
import in.shop2020.thrift.clients.CatalogClient;
9713 anupam.sin 22
import in.shop2020.thrift.clients.PromotionClient;
9570 anupam.sin 23
import in.shop2020.thrift.clients.UserClient;
20600 amit.gupta 24
import in.shop2020.thrift.clients.config.ConfigClient;
9570 anupam.sin 25
 
17782 amit.gupta 26
import java.util.ArrayList;
17797 amit.gupta 27
import java.util.Arrays;
9570 anupam.sin 28
import java.util.List;
29
import java.util.Map;
30
 
17782 amit.gupta 31
import org.apache.commons.lang.StringUtils;
9570 anupam.sin 32
import org.apache.log4j.Logger;
33
import org.apache.struts2.interceptor.ParameterAware;
34
import org.apache.thrift.TException;
17782 amit.gupta 35
import org.json.JSONArray;
36
import org.json.JSONObject;
9570 anupam.sin 37
 
9667 anupam.sin 38
import com.google.gson.Gson;
9570 anupam.sin 39
 
40
public class CartController extends BaseController implements ParameterAware{
41
 
42
    private static final long serialVersionUID = 1L;
43
    private static Logger log = Logger.getLogger(Class.class);
44
    Map<String, String[]> reqparams = null;
45
 
46
    private int variationId = 0;
47
    private String totalamount;
48
 
49
    private String errorMsg = "";
50
    private String cartMsg = "";
51
 
52
    private String pincode = "110001";
53
 
54
    private String couponCode = null;
55
 
56
    private String discountedAmount;
57
 
58
    private long itemId;
59
    private String insuranceResult;
60
 
61
    private boolean toInsure;
12021 amit.gupta 62
    private boolean privateDealUser = false;
12022 amit.gupta 63
    private boolean autoApplicationOff = true;
9570 anupam.sin 64
 
11516 amit.gupta 65
    private long id =-1; //CartId
9667 anupam.sin 66
    private String cartPojoJson;
17784 amit.gupta 67
    private String cartMap = null;
9676 anupam.sin 68
    private long quantity;
9697 anupam.sin 69
    private int insuranceType;
11386 amit.gupta 70
    UserContextService.Client userClient;
9570 anupam.sin 71
 
11386 amit.gupta 72
    public CartController() throws Exception{
9570 anupam.sin 73
        super();
74
    }
75
 
11535 amit.gupta 76
    public UserContextService.Client getClient() throws Exception{
11602 amit.gupta 77
    	userClient = new UserClient().getClient();
11535 amit.gupta 78
    	return userClient;
11593 amit.gupta 79
    }	
9808 anupam.sin 80
    public String index() {
20600 amit.gupta 81
		try {
82
			if(ConfigClient.getClient().get("SALES_FLAG").equals("0")){
83
				cartPojoJson = new Gson().toJson(new CartPojo());
84
				return "index";
85
			}
86
		} catch (ConfigException e1) {
87
			cartPojoJson = new Gson().toJson(new CartPojo());
88
			return "index";
89
		}
9709 anupam.sin 90
        if(id != -1){
9570 anupam.sin 91
            try {
11983 amit.gupta 92
            	String cc = null;
12022 amit.gupta 93
            	log.info(privateDealUser + "  "  + autoApplicationOff);
94
            	if(privateDealUser && !autoApplicationOff) {
11983 amit.gupta 95
            		cc = "saholicdeals";
96
            	}
13066 amit.gupta 97
            	log.info("Before cart response--------");
11983 amit.gupta 98
            	CartPlus cartResponse  = getClient().validateCartPlus(id, -1, cc);
11511 amit.gupta 99
                log.info("After cart response--------");
11823 amit.gupta 100
                if (errorMsg == null || errorMsg.equals("")) {
11917 amit.gupta 101
                	if(cartResponse.getValidateCartMessages().get(1)!=null && !cartResponse.getValidateCartMessages().get(1).equals("")){
102
                		errorMsg = cartResponse.getValidateCartMessages().get(1);
103
                	}else {
104
                		errorMsg = cartResponse.getValidateCartMessages().get(0);
105
                	}
11823 amit.gupta 106
                }
107
                cartPojoJson = new Gson().toJson(PojoPopulator.getCartPojo(cartResponse.getCart(), errorMsg, 
11592 amit.gupta 108
                		cartResponse.getPinCode(), cartResponse.isNeedInsuranceInfo()));
10551 amit.gupta 109
                log.info(cartPojoJson);
9570 anupam.sin 110
            } catch (Exception e) {
11593 amit.gupta 111
                /*TTransportException te = (TTransportException)e;
112
                log.warn(e.getLocalizedMessage() + " " + te.getType());*/
9570 anupam.sin 113
                log.warn("Unable to validate the cart: ", e);
114
            }
11516 amit.gupta 115
        } else {
116
        	cartPojoJson = new Gson().toJson(new CartPojo());
9570 anupam.sin 117
        }
118
        return "index";
119
    }
120
 
121
    public String create() {
10724 amit.gupta 122
    	RedirectPojo rp = new RedirectPojo();
123
    	rp.setRedirectUrl("");
9676 anupam.sin 124
        try {
20600 amit.gupta 125
 
126
			if(ConfigClient.getClient().get("SALES_FLAG").equals("1")){
127
				cartMsg = getClient().addItemToCart(id, itemId, 1, -1);
128
			} else {
129
				cartMsg = "Sale is off";
130
			}
11526 amit.gupta 131
        	//Try adding to cart
11535 amit.gupta 132
            log.info("Cart message while adding item to cart - " + cartMsg);
11526 amit.gupta 133
            //If could not add to cart try to add notification reminder if email exists.
9680 amit.gupta 134
            if (!("".equals(cartMsg))) {
10767 amit.gupta 135
            	String emailId = this.request.getParameter("email");
10766 amit.gupta 136
            	log.info("QuesyString" + this.request.getParameterMap().toString());
10724 amit.gupta 137
    			if(emailId==null || emailId.equals("")){
11526 amit.gupta 138
    				//User tries to add to cart but product is actually out of stock
10724 amit.gupta 139
    				rp.setMessage("Item is currently out of stock");
140
    			} else {	
141
        			try {
142
	        	        CatalogClient catalogClientService;
143
	        			catalogClientService = new CatalogClient();
144
	        			in.shop2020.model.v1.catalog.CatalogService.Client client = catalogClientService.getClient();
145
	        			client.addProductNotification(itemId, emailId);
146
	        			log.debug("Got product notification for product: " + itemId + "  from user: " + emailId );
147
	        			rp.setMessage("You will be notified when product is avaliable");
148
        			} catch (Exception e) {
149
        				rp.setMessage("Error occurred while adding to notificiation");
150
        				log.error("Exception while adding product notification for item: " + itemId + " and email: " + emailId, e);
10261 amit.gupta 151
        			}
10724 amit.gupta 152
 
10261 amit.gupta 153
        		}
10724 amit.gupta 154
            } else {
155
            	rp.setRedirectUrl("cart");
9676 anupam.sin 156
            }
157
        } catch (TException e) {
158
            log.error("Unable to create or add to cart because of: ", e);
9713 anupam.sin 159
            errorMsg = "Unable to add to cart. Please try again.";
9676 anupam.sin 160
        } catch (Exception e) {
161
            log.error("Unable to create or add to cart because of: ", e);
9713 anupam.sin 162
            errorMsg = "Unable to add to cart. Please try again.";
9570 anupam.sin 163
        }
10724 amit.gupta 164
        cartPojoJson = new Gson().toJson(rp);
165
        return "index";
9676 anupam.sin 166
    }       
9570 anupam.sin 167
 
9676 anupam.sin 168
 
169
    // DELETE /entity
170
    public String destroy() {
9709 anupam.sin 171
        if(id > 0)  {
9570 anupam.sin 172
            try {
11535 amit.gupta 173
                getClient().deleteItemFromCart(id, itemId);
9676 anupam.sin 174
                return index();
175
            } catch (ShoppingCartException e) {
176
                log.error("Unable to delete item from cart: ", e);
9713 anupam.sin 177
                errorMsg = "Unable to delete item. Please try again.";
9570 anupam.sin 178
            } catch (TException e) {
9676 anupam.sin 179
                log.error("Unable to delete item from cart: ", e);
9713 anupam.sin 180
                errorMsg = "Unable to delete item. Please try again.";
9570 anupam.sin 181
            } catch (Exception e) {
9676 anupam.sin 182
                log.error("Unable to delete item from cart: ", e);
9713 anupam.sin 183
                errorMsg = "Unable to delete item. Please try again.";
9570 anupam.sin 184
            }
185
        }
9676 anupam.sin 186
        return index();
9570 anupam.sin 187
    }
9676 anupam.sin 188
 
189
    //PUT
9570 anupam.sin 190
    public String update() {
9676 anupam.sin 191
        if(quantity <= 0)   {
192
            log.info("Not valid item quantity. Unable to change item quantity.");
9713 anupam.sin 193
            errorMsg = "Invalid item quantity";
9676 anupam.sin 194
        } else  {
9709 anupam.sin 195
            if(updateItemQuantityInCart(id, itemId, quantity))    {
9676 anupam.sin 196
                return index();
197
            }
198
        }
199
        return index();
9570 anupam.sin 200
    }
201
 
202
    private boolean updateItemQuantityInCart(long cartId, long itemId, long quantity){
203
        try {
11535 amit.gupta 204
            getClient().addItemToCart(cartId, itemId, quantity, -1);
9570 anupam.sin 205
            return true;
206
        } catch (ShoppingCartException e) {
207
            log.error("Unable to update the item quantity in the cart: ", e);
9713 anupam.sin 208
            errorMsg = "Unable to change quantity. Please try again.";
9570 anupam.sin 209
        } catch (TException e) {
210
            log.error("Unable to update the item quantity in the cart: ", e);
9713 anupam.sin 211
            errorMsg = "Unable to change quantity. Please try again.";
9570 anupam.sin 212
        } catch (Exception e) {
213
            log.error("Unable to update the item quantity in the cart: ", e);
9713 anupam.sin 214
            errorMsg = "Unable to change quantity. Please try again.";
9570 anupam.sin 215
        }
216
        return false;
217
    }
9676 anupam.sin 218
 
9570 anupam.sin 219
    public String insureItem() {
9697 anupam.sin 220
        insuranceResult = "";
221
        try {
11535 amit.gupta 222
            if(getClient().insureItem(itemId, id, toInsure, insuranceType)) {
9697 anupam.sin 223
                setInsuranceResult("SUCCESS");
224
            } else {
225
                setInsuranceResult("FAILURE");
226
            }
227
        } catch (Exception e) {
9709 anupam.sin 228
            log.error("Unable to insure item : " + itemId + " for cart : " + id, e);
9697 anupam.sin 229
            setInsuranceResult("FAILURE");
230
        }
9570 anupam.sin 231
        return "insurance-result";
232
    }
9713 anupam.sin 233
 
234
    public String edit()  {
235
        String action = request.getParameter("action");
236
        PromotionClient promotionServiceClient = null;
237
        try {
238
            if(action == null || action.isEmpty())  {
239
                errorMsg = "Invalid Request Action";
240
                return index();
241
            }
242
            String couponCode = request.getParameter("coupon_code");
243
 
244
            if (action.equalsIgnoreCase("applycoupon"))   {
245
                if (couponCode == null || couponCode.isEmpty()) {
246
                    errorMsg = "Coupon Code field cannot be left empty";
247
                    return index();
248
                }
249
 
250
                promotionServiceClient = new PromotionClient();
251
                PromotionService.Client promotionClient = promotionServiceClient.getClient();
252
 
11768 amit.gupta 253
                Cart cart = promotionClient.applyCoupon(couponCode, id);
11823 amit.gupta 254
            	errorMsg = "Coupon successfully applied";
11861 amit.gupta 255
            	if(cart.getMessage() != null && !cart.getMessage().equals("")){
11823 amit.gupta 256
                	errorMsg = errorMsg +  ", " + cart.getMessage();
11768 amit.gupta 257
                }
9713 anupam.sin 258
            }
259
            else if (action.equals("removecoupon"))    {
11535 amit.gupta 260
                getClient().removeCoupon(id);
9713 anupam.sin 261
            }
262
        } catch (PromotionException e) {
263
            log.info("Invalid coupon: " + e.getMessage());
11823 amit.gupta 264
            errorMsg = e.getMessage();
9713 anupam.sin 265
        } catch (Exception e) {
266
            log.error("Unable to apply or remove coupon", e);
267
            errorMsg = "Unable to apply or remove coupon";
268
        }
269
        return index();
270
    }
9808 anupam.sin 271
 
272
    public String changeAddress() {
273
        long addressId;
274
        if(request.getParameter("addressId") == null || request.getParameter("addressId").isEmpty()) {
275
            errorMsg = "Null or empty addressId";
276
            return index();
277
        } else {
9815 anupam.sin 278
            addressId = Long.parseLong(request.getParameter("addressId"));
9808 anupam.sin 279
        }
280
 
281
        try {
9815 anupam.sin 282
            if(request.getParameter("addressType").equalsIgnoreCase("store")) {
11535 amit.gupta 283
                getClient().addStoreToCart(id, addressId);
9815 anupam.sin 284
            } else if (request.getParameter("addressType").equalsIgnoreCase("home")){
11535 amit.gupta 285
                getClient().addAddressToCart(id, addressId);
9808 anupam.sin 286
            } else {
287
                errorMsg = "Invalid addressType. It can only be set to home or store.";
288
            }
289
        } catch(Exception e) {
290
            log.error("Unable to change address", e);
291
        }
292
        return index();
293
    }
9570 anupam.sin 294
 
295
    public long getItemId(){
296
        return this.itemId;
297
    }
298
 
9684 amit.gupta 299
    public void setItemId(long itemId){
300
    	this.itemId = itemId;
301
    }
302
 
9570 anupam.sin 303
    public String getTotalAmount() {
304
        return totalamount;
305
    }
306
 
307
    public String getPinCode() {
308
        return pincode;
309
    }
310
 
311
    public String getCouponCode()  {
312
        return couponCode;
313
    }
314
 
315
    public String getDiscountedAmount()   {
316
        return discountedAmount;
317
    }
318
 
319
    public String getErrorMsg()    {
320
        return errorMsg;
321
    }
322
 
9576 anupam.sin 323
//    public long getNumberOfItems(){
324
//        return userinfo.getTotalItems();
325
//    }
9570 anupam.sin 326
 
327
    public String getCartMsg(){
328
        if(cartMsg.equals("")){
329
            return null;
330
        }
331
        return cartMsg;
332
    }
333
 
334
    public String getSnippets(){
335
        String snippets = "";
336
        CatalogClient csc;
337
        try {
338
            csc = new CatalogClient();
339
            List<Long> similarItems = csc.getClient().getSimilarItemsCatalogIds(0, 4, itemId);
340
            for(Long catalogId: similarItems){
9602 amit.gupta 341
                snippets = snippets + ContentServingService.getSnippet(SnippetType.WIDGET_SNIPPET, catalogId+"", -1);
9570 anupam.sin 342
            }
343
        } catch (Exception e) {
344
            log.error("Unable to initialise Catalogservice Client");
345
        }       
346
        return snippets;
347
    }
348
 
349
    @Override
350
    public void setParameters(Map<String, String[]> parameters) {
351
        this.reqparams = parameters;    
352
    }
353
 
9576 anupam.sin 354
//    @Override
355
//    public String getHeaderSnippet() {
356
//        String url = request.getQueryString();
357
//        if (url == null) {
358
//            url = "";
359
//        } else {
360
//            url = "?" + url;
361
//        }
362
//        url = request.getRequestURI() + url;
363
//        return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), userinfo.getTotalItems(), url , 0, false);
364
//    }
9570 anupam.sin 365
 
9576 anupam.sin 366
//    public boolean isUserLoggedIn() {
367
//        return userinfo.isLoggedIn();
368
//    }
9570 anupam.sin 369
 
370
    public void setVariationId(String uri)  {
371
        if (uri.equals("/cart1"))   {
372
            this.variationId = 1;
373
        }
374
    }
375
 
376
    public int getVariationId() {
377
        return this.variationId;
378
    }
379
 
380
    public String getActionMessage(){
381
        if(cartMsg.contains("out of stock")){
382
            return "Notify me when this product is in stock.";
383
        }else {
384
            return "Notify me when this product is available.";
385
        }
386
    }
387
 
388
    public String getOfferNote(){
389
        String note = null;
390
        if(cartMsg.contains("out of stock")){
391
            return note;
392
        }
393
        else {
394
            try {
395
                CatalogClient catalogServiceClient = new CatalogClient();
396
                Client catalogClient = catalogServiceClient.getClient();
397
                Item it = catalogClient.getItem(itemId);
398
                note = it.getBestDealText();
399
            } catch (Exception e)  {
400
                log.error("Unable to get the offertext because of: ", e);
401
            }
402
        }
403
        return note;
404
    }
405
 
406
    public String getInsuranceResult() {
407
        return insuranceResult;
408
    }
409
 
410
    public void setInsuranceResult(String insuranceResult) {
411
        this.insuranceResult = insuranceResult;
412
    }
413
 
414
    public void setToInsure(boolean toInsure) {
415
        this.toInsure = toInsure;
416
    }
417
 
418
    public boolean getToInsure() {
419
        return toInsure;
420
    }
421
 
9709 anupam.sin 422
    public long getId() {
423
        return id;
9570 anupam.sin 424
    }
425
 
9709 anupam.sin 426
    public void setId(long id) {
427
        this.id = id;
9570 anupam.sin 428
    }
9667 anupam.sin 429
 
430
 
431
    public void setCartPojoJson(String cartPojoJson) {
432
        this.cartPojoJson = cartPojoJson;
433
    }
434
 
435
 
436
    public String getCartPojoJson() {
437
        return cartPojoJson;
438
    }
17782 amit.gupta 439
 
17784 amit.gupta 440
    public void setCartMap(String cartMap) {
17785 amit.gupta 441
    	log.info("I am in cartMap" + " " + cartMap);
442
    	log.info(this.request.getParameterMap());
17784 amit.gupta 443
    	this.cartMap = cartMap;
444
    }
17782 amit.gupta 445
 
20979 amit.gupta 446
    public String pinServiceable(){
447
    	return "";
448
    }
17784 amit.gupta 449
 
17782 amit.gupta 450
    public String validateCart() {
17785 amit.gupta 451
    	log.info(this.cartMap);
17858 amit.gupta 452
    	JSONObject jsonObj  = null;
17782 amit.gupta 453
    	if(!userinfo.isPrivateDealUser()){
454
    		cartPojoJson = "{\"response\":\"error\", \"message\":\"Invalid request.\"}";
455
    	} else {
17858 amit.gupta 456
    		if(StringUtils.isEmpty(cartMap)){
17860 amit.gupta 457
    			jsonObj = new JSONObject("{\"cartItems\":[]}");
17858 amit.gupta 458
    		}else {
459
    			jsonObj = new JSONObject(cartMap);
460
    		}
17869 amit.gupta 461
			try{
462
				List<ItemQuantity> itemQuantities = new ArrayList<ItemQuantity>();
463
				JSONArray cartItems = jsonObj.getJSONArray("cartItems");
464
				List<Integer> itemsList = new ArrayList<Integer>();
465
				for (int i=0; i< cartItems.length(); i++) {
466
					JSONObject obj = cartItems.getJSONObject(i);
17943 amit.gupta 467
					//Just to avoid garbage values
17869 amit.gupta 468
					int itemId = 0;
469
					try{
470
						itemId = obj.getInt("itemId");
471
					} catch(Exception e){
472
						continue;
17856 amit.gupta 473
					}
17869 amit.gupta 474
					Integer quantity = obj.getInt("quantity");
475
					ItemQuantity iq = new ItemQuantity(itemId, quantity);
476
					itemQuantities.add(iq);
477
					itemsList.add(itemId);
478
				}
479
				userClient = getClient();
480
				if (itemsList.size()==0 || userClient.addItemsToCart(id, itemQuantities, jsonObj.has("couponCode")? jsonObj.getString("couponCode"):null)){
481
					log.info("Items added to cart Successfully");
482
					//Now validate cart and provide appropriate response.
483
					String cartString = userClient.validateCartNew(id, userinfo.getPincode(), -1);
17940 amit.gupta 484
					//System.out.println("cartString  "  + cartString);
17869 amit.gupta 485
					JSONObject cartObj = new JSONObject(cartString);
486
					JSONArray arr = cartObj.getJSONArray("cartItems");
17940 amit.gupta 487
					int maxEstimate=-2;
17869 amit.gupta 488
					boolean allSame=true;
489
					int removedCount = 0;
490
					cartObj.put("cartMessagesMerged", 0);
491
					for (int j=0; j<arr.length(); j++){
492
						JSONObject itemObj = arr.getJSONObject(j-removedCount);
493
						if(!itemsList.contains(itemObj.getInt("itemId"))){
494
							 if(itemObj.getInt("quantity")==0){
495
								arr.remove(j-removedCount);
496
								removedCount++;
17902 amit.gupta 497
								if (itemObj.has("estimate") && itemObj.getInt("estimate")==-1){
17869 amit.gupta 498
									cartObj.put("cartMessageUndeliverable", cartObj.getInt("cartMessageUndeliverable") - 1);
17856 amit.gupta 499
								} else {
17869 amit.gupta 500
									cartObj.put("cartMessageOOS", cartObj.getInt("cartMessageOOS") - 1);
17856 amit.gupta 501
								}
17869 amit.gupta 502
								continue;
503
							 } else {
504
								JSONArray messagesArray = new JSONArray();
505
								itemObj.put("cartItemMessages", messagesArray);
17945 amit.gupta 506
								if(itemsList.size()>0){
17901 amit.gupta 507
									JSONObject message=new JSONObject();
508
									message.put("type","info");
509
									message.put("messageText","Added from earlier cart");
510
									messagesArray.put(message);
511
									cartObj.put("cartMessagesMerged", cartObj.getInt("cartMessagesMerged") + 1);
512
								}
17869 amit.gupta 513
							 }
17856 amit.gupta 514
						}
17869 amit.gupta 515
						ProductPojo pp = PojoPopulator.getShortContent(itemObj.getLong("catalogItemId"));
20986 amit.gupta 516
 
517
						String productProperties = ContentServingService.getSnippet(SnippetType.PRODUCT_PROPERTIES_SNIPPET, pp.getId() +"", -1);
518
 
20984 amit.gupta 519
						JSONObject productPropertiesInJson = new JSONObject(productProperties);
520
						productPropertiesInJson.getString("categoryName");
17940 amit.gupta 521
						if(itemObj.has("estimate")){
522
							if(allSame){
523
								allSame = maxEstimate==-2 || maxEstimate==itemObj.getInt("estimate");
524
							}
525
							if(itemObj.getInt("estimate") > maxEstimate){
526
								maxEstimate = itemObj.getInt("estimate");
527
							}
17869 amit.gupta 528
						}
529
						itemObj.put("imageUrl", pp.getImageUrl());
20983 amit.gupta 530
						itemObj.put("categoryName", pp.getCategoryName());
17869 amit.gupta 531
						itemObj.put("title", pp.getTitle());
532
					}
533
 
17870 amit.gupta 534
					ArrayList<JSONObject> listdata = new ArrayList<JSONObject>();     
17869 amit.gupta 535
					if (arr != null) { 
536
					   for (int i=0;i<arr.length();i++){
537
						   JSONArray itemMessages = arr.getJSONObject(i).optJSONArray(("cartItemMessages"));
538
						   if(itemMessages!=null && itemMessages.length()>0){
17943 amit.gupta 539
							   listdata.add(0,arr.getJSONObject(i));
540
						   } else {
17870 amit.gupta 541
							   listdata.add(arr.getJSONObject(i));
17869 amit.gupta 542
						   }
543
					   } 
544
					}
545
					arr = new JSONArray();
546
					for(int i=0;i<listdata.size();i++){
547
						arr.put(listdata.get(i));
548
					}
549
					cartObj.put("cartItems", arr);
550
					JSONArray cartMessagesArray = new JSONArray();
551
					for (String message :Arrays.asList("cartMessageOOS", "cartMessageUndeliverable", "cartMessageChanged","cartMessagesMerged")){
552
						int count = cartObj.getInt(message);
553
						if (count>0) {
554
							String type="danger";
555
							JSONObject cartMessage=new JSONObject();
556
							if (message.equals("cartMessagesMerged")) {
557
								type = "info";
558
								if (count==1){
559
									cartMessage.put("messageText","1 item is added from earlier cart");
560
								}else {
561
									cartMessage.put("messageText","Few items are added from earlier cart");
562
								}
563
							} else if (message.equals("cartMessageOOS")) {
564
								if (count==1){
565
									cartMessage.put("messageText","One item is currently Out of Stock");
566
								}else {
567
									cartMessage.put("messageText","Few items are currently Out of Stock");
568
								}
569
							} else if (message.equals("cartMessageUndeliverable")) {
570
								if (count==1){
571
									cartMessage.put("messageText","One item is undeliverable");
572
								}else {
573
									cartMessage.put("messageText","Few items are underiverable");
574
								}
575
							} else {
576
								if (count==1){
577
									cartMessage.put("messageText","One item qty has changed");
578
								}else {
579
									cartMessage.put("messageText","Few items qty have changed");
580
								}
17857 amit.gupta 581
							}
17869 amit.gupta 582
							cartMessage.put("type",type);
583
							cartMessagesArray.put(cartMessage);
17856 amit.gupta 584
						}
17869 amit.gupta 585
					}
586
					cartObj.put("cartMessages", cartMessagesArray);
587
 
588
					if (maxEstimate==-1){
589
						cartObj.put("estimateString", "Can't ship here");
17941 amit.gupta 590
					}
591
					else if(maxEstimate==-2){
592
						cartObj.put("estimateString", "Out of Stock");
17856 amit.gupta 593
					} else {
17942 amit.gupta 594
						cartObj.put("cod", getClient().showCODOption(id, 0, userinfo.getPincode()));
17869 amit.gupta 595
						cartObj.put("estimateString", LogisticsService.getDeliveryDateString(maxEstimate,DeliveryType.COD));
17856 amit.gupta 596
					}
17869 amit.gupta 597
					cartObj.put("maxEstimate", maxEstimate);
598
					cartPojoJson = cartObj.toString();
599
				} else {
600
					cartPojoJson = "{\"response\":\"error\", \"message\":\"Problem occurred while updating cart\"}";
601
				}
602
			}catch(Exception e) {
603
				cartPojoJson = "{\"response\":\"error\", \"message\":\"Something went wrong while validation\"}";
604
				e.printStackTrace();
605
			}
17782 amit.gupta 606
    	}
607
    	setResultJson(cartPojoJson);
608
    	return "index";
609
    }
9676 anupam.sin 610
 
611
    public void setQuantity(long quantity) {
612
        this.quantity = quantity;
613
    }
614
 
615
    public long getQuantity() {
616
        return quantity;
617
    }
9697 anupam.sin 618
 
619
    public int getInsuranceType() {
620
        return insuranceType;
621
    }
622
 
623
    public void setInsuranceType(int insuranceType) {
624
        this.insuranceType = insuranceType;
625
    }
11983 amit.gupta 626
 
12021 amit.gupta 627
	public void setPrivateDealUser(boolean privateDealUser) {
628
		this.privateDealUser = privateDealUser;
11983 amit.gupta 629
	}
630
 
631
	public boolean isPrivateDealUser() {
12021 amit.gupta 632
		return privateDealUser;
11983 amit.gupta 633
	}
634
 
12022 amit.gupta 635
	public void setAutoApplicationOff(boolean autoApplicationOff) {
636
		this.autoApplicationOff = autoApplicationOff;
11983 amit.gupta 637
	}
638
 
12022 amit.gupta 639
	public boolean isAutoApplicationOff() {
640
		return autoApplicationOff;
11983 amit.gupta 641
	}
9570 anupam.sin 642
}