Subversion Repositories SmartDukaan

Rev

Rev 13066 | Rev 17784 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 13066 Rev 17782
Line 1... Line 1...
1
package in.shop2020.mobileapi.serving.controllers;
1
package in.shop2020.mobileapi.serving.controllers;
2
 
2
 
-
 
3
import in.shop2020.logistics.DeliveryType;
-
 
4
import in.shop2020.metamodel.util.ProductPojo;
3
import in.shop2020.mobileapi.serving.pojos.CartPojo;
5
import in.shop2020.mobileapi.serving.pojos.CartPojo;
4
import in.shop2020.mobileapi.serving.pojos.RedirectPojo;
6
import in.shop2020.mobileapi.serving.pojos.RedirectPojo;
5
import in.shop2020.mobileapi.serving.services.ContentServingService;
7
import in.shop2020.mobileapi.serving.services.ContentServingService;
-
 
8
import in.shop2020.mobileapi.serving.services.LogisticsService;
6
import in.shop2020.mobileapi.serving.utils.PojoPopulator;
9
import in.shop2020.mobileapi.serving.utils.PojoPopulator;
7
import in.shop2020.mobileapi.serving.utils.SnippetType;
10
import in.shop2020.mobileapi.serving.utils.SnippetType;
8
import in.shop2020.model.v1.catalog.CatalogService.Client;
11
import in.shop2020.model.v1.catalog.CatalogService.Client;
9
import in.shop2020.model.v1.catalog.Item;
12
import in.shop2020.model.v1.catalog.Item;
10
import in.shop2020.model.v1.user.Cart;
13
import in.shop2020.model.v1.user.Cart;
11
import in.shop2020.model.v1.user.CartPlus;
14
import in.shop2020.model.v1.user.CartPlus;
-
 
15
import in.shop2020.model.v1.user.ItemQuantity;
12
import in.shop2020.model.v1.user.PromotionException;
16
import in.shop2020.model.v1.user.PromotionException;
13
import in.shop2020.model.v1.user.PromotionService;
17
import in.shop2020.model.v1.user.PromotionService;
14
import in.shop2020.model.v1.user.ShoppingCartException;
18
import in.shop2020.model.v1.user.ShoppingCartException;
15
import in.shop2020.model.v1.user.UserContextService;
19
import in.shop2020.model.v1.user.UserContextService;
16
import in.shop2020.thrift.clients.CatalogClient;
20
import in.shop2020.thrift.clients.CatalogClient;
17
import in.shop2020.thrift.clients.PromotionClient;
21
import in.shop2020.thrift.clients.PromotionClient;
18
import in.shop2020.thrift.clients.UserClient;
22
import in.shop2020.thrift.clients.UserClient;
19
 
23
 
-
 
24
import java.util.ArrayList;
20
import java.util.List;
25
import java.util.List;
21
import java.util.Map;
26
import java.util.Map;
22
 
27
 
-
 
28
import org.apache.commons.lang.StringUtils;
23
import org.apache.log4j.Logger;
29
import org.apache.log4j.Logger;
24
import org.apache.struts2.interceptor.ParameterAware;
30
import org.apache.struts2.interceptor.ParameterAware;
25
import org.apache.thrift.TException;
31
import org.apache.thrift.TException;
-
 
32
import org.json.JSONArray;
-
 
33
import org.json.JSONObject;
26
 
34
 
27
import com.google.gson.Gson;
35
import com.google.gson.Gson;
28
 
36
 
29
public class CartController extends BaseController implements ParameterAware{
37
public class CartController extends BaseController implements ParameterAware{
30
    
38
    
Line 408... Line 416...
408
 
416
 
409
 
417
 
410
    public String getCartPojoJson() {
418
    public String getCartPojoJson() {
411
        return cartPojoJson;
419
        return cartPojoJson;
412
    }
420
    }
-
 
421
    
-
 
422
    
-
 
423
    public String validateCart() {
-
 
424
    	if(!userinfo.isPrivateDealUser()){
-
 
425
    		cartPojoJson = "{\"response\":\"error\", \"message\":\"Invalid request.\"}";
-
 
426
    	} else {
-
 
427
    		String cartMap = request.getParameter("cartMap");
-
 
428
    		if(StringUtils.isEmpty(cartMap))
-
 
429
    			cartPojoJson = "{\"response\":\"error\", \"message\":\"Empty Cart.\"}";
-
 
430
    		else {
-
 
431
    			try{
-
 
432
    				List<ItemQuantity> itemQuantities = new ArrayList<ItemQuantity>();
-
 
433
    				JSONObject jsonObj = new JSONObject(cartMap);
-
 
434
    				JSONArray cartItems = jsonObj.getJSONArray("cartItems");
-
 
435
    				if (cartItems==null || cartItems.length()==0){
-
 
436
    					cartPojoJson = "{\"response\":\"error\", \"message\":\"Empty Cart.\"}";
-
 
437
    				} else {
-
 
438
    					for (int i=0; i< cartItems.length(); i++) {
-
 
439
    						JSONObject obj = cartItems.getJSONObject(i);
-
 
440
    						int itemId = obj.getInt("itemId");
-
 
441
    						Integer quantity = obj.getInt("quantity");
-
 
442
    						ItemQuantity iq = new ItemQuantity(itemId, quantity);
-
 
443
    						itemQuantities.add(iq);
-
 
444
    					}
-
 
445
    					userClient = getClient();
-
 
446
    					if (userClient.addItemsToCart(jsonObj.getLong("cartId"), itemQuantities, jsonObj.getString("couponCode"))){
-
 
447
    						log.info("Items added to cart Successfully");
-
 
448
    						//Now validate cart and provide appropriate response.
-
 
449
    						String cartString = userClient.validateCartNew(id, userinfo.getPincode(), -1);
-
 
450
    						JSONObject cartObj = new JSONObject(cartString);
-
 
451
    						JSONArray arr = cartObj.getJSONArray("cartItems");
-
 
452
    						int maxEstimate=0;
-
 
453
    						boolean allSame=true;
-
 
454
    						for (int j=0; j<arr.length(); j++){
-
 
455
    							JSONObject itemObj = arr.getJSONObject(j);
-
 
456
    							ProductPojo pp = PojoPopulator.getShortContent(itemObj.getLong("catalogItemId"));
-
 
457
    							if(allSame){
-
 
458
    								allSame = maxEstimate==0 || maxEstimate==itemObj.getInt("estimate");
-
 
459
    							}
-
 
460
    							if(itemObj.getInt("estimate")>maxEstimate){
-
 
461
    								maxEstimate = itemObj.getInt("estimate");
-
 
462
    							}
-
 
463
    							cartObj.put("imageUrl", pp.getImageUrl());
-
 
464
    							cartObj.put("title", pp.getTitle());
-
 
465
    						}
-
 
466
    						cartObj.put("estimateString", LogisticsService.getDeliveryDateString(maxEstimate,DeliveryType.COD));
-
 
467
    						cartObj.put("sameEstimate", allSame);
-
 
468
    						cartPojoJson = new Gson().toJson(cartObj);
-
 
469
    					} else {
-
 
470
    						cartPojoJson = "{\"response\":\"error\", \"message\":\"Problem occurred while updating cart\"}";
-
 
471
    					}
-
 
472
    				}
-
 
473
    			}catch(Exception e) {
-
 
474
    				cartPojoJson = "{\"response\":\"error\", \"message\":\"Something went wrong while validation\"}";
-
 
475
    				e.printStackTrace();
-
 
476
    			}
-
 
477
    		}
-
 
478
    		
-
 
479
    	}
-
 
480
    	setResultJson(cartPojoJson);
-
 
481
    	return "index";
-
 
482
    }
413
 
483
 
414
    public void setQuantity(long quantity) {
484
    public void setQuantity(long quantity) {
415
        this.quantity = quantity;
485
        this.quantity = quantity;
416
    }
486
    }
417
 
487