Subversion Repositories SmartDukaan

Rev

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

Rev 9697 Rev 9709
Line 1... Line 1...
1
package in.shop2020.mobileapi.serving.controllers;
1
package in.shop2020.mobileapi.serving.controllers;
2
 
2
 
3
import in.shop2020.datalogger.EventType;
-
 
4
import in.shop2020.mobileapi.serving.services.ContentServingService;
3
import in.shop2020.mobileapi.serving.services.ContentServingService;
5
import in.shop2020.mobileapi.serving.utils.PojoPopulator;
4
import in.shop2020.mobileapi.serving.utils.PojoPopulator;
6
import in.shop2020.mobileapi.serving.utils.SnippetType;
5
import in.shop2020.mobileapi.serving.utils.SnippetType;
7
import in.shop2020.model.v1.catalog.CatalogService.Client;
6
import in.shop2020.model.v1.catalog.CatalogService.Client;
8
import in.shop2020.model.v1.catalog.Item;
7
import in.shop2020.model.v1.catalog.Item;
9
import in.shop2020.model.v1.user.Cart;
8
import in.shop2020.model.v1.user.Cart;
10
import in.shop2020.model.v1.user.ShoppingCartException;
9
import in.shop2020.model.v1.user.ShoppingCartException;
11
import in.shop2020.model.v1.user.UserContextService;
10
import in.shop2020.model.v1.user.UserContextService;
12
import in.shop2020.thrift.clients.CatalogClient;
11
import in.shop2020.thrift.clients.CatalogClient;
13
import in.shop2020.thrift.clients.UserClient;
12
import in.shop2020.thrift.clients.UserClient;
14
import in.shop2020.utils.DataLogger;
-
 
15
 
13
 
16
import java.util.List;
14
import java.util.List;
17
import java.util.Map;
15
import java.util.Map;
18
import java.util.StringTokenizer;
-
 
19
 
16
 
20
import org.apache.commons.lang.StringUtils;
17
import org.apache.commons.lang.StringUtils;
21
import org.apache.log4j.Logger;
18
import org.apache.log4j.Logger;
22
import org.apache.struts2.convention.annotation.Action;
-
 
23
import org.apache.struts2.convention.annotation.Actions;
-
 
24
import org.apache.struts2.convention.annotation.InterceptorRef;
-
 
25
import org.apache.struts2.convention.annotation.Result;
-
 
26
import org.apache.struts2.convention.annotation.Results;
-
 
27
import org.apache.struts2.interceptor.ParameterAware;
19
import org.apache.struts2.interceptor.ParameterAware;
28
import org.apache.thrift.TException;
20
import org.apache.thrift.TException;
29
 
21
 
30
import com.google.gson.Gson;
22
import com.google.gson.Gson;
31
 
23
 
32
 
-
 
33
@Results({
-
 
34
    @Result(name = "index", location = "cart-index.vm"),
-
 
35
    @Result(name="redirect", type="redirectAction", params = {"actionName" , "cart"}),
-
 
36
    @Result(name="failure", location="cart-failure.vm"),
-
 
37
    @Result(name="success", location="cart-success.vm")
-
 
38
})
-
 
39
 
-
 
40
public class CartController extends BaseController implements ParameterAware{
24
public class CartController extends BaseController implements ParameterAware{
41
    
25
    
42
    private static final long serialVersionUID = 1L;
26
    private static final long serialVersionUID = 1L;
43
    private static Logger log = Logger.getLogger(Class.class);
27
    private static Logger log = Logger.getLogger(Class.class);
44
    Map<String, String[]> reqparams = null;
28
    Map<String, String[]> reqparams = null;
Line 59... Line 43...
59
    private String insuranceResult;
43
    private String insuranceResult;
60
    
44
    
61
    private boolean toInsure;
45
    private boolean toInsure;
62
    
46
    
63
    
47
    
64
    private long cartId;
48
    private long id; //CartId
65
    private String cartPojoJson;
49
    private String cartPojoJson;
66
    private long quantity;
50
    private long quantity;
67
    private int insuranceType;
51
    private int insuranceType;
68
    
52
    
69
    public CartController(){
53
    public CartController(){
70
        super();
54
        super();
71
    }
55
    }
72
    
56
    
73
    public String index()  {
57
    public String index()  {
74
        if(cartId != -1){
58
        if(id != -1){
75
            try {
59
            try {
76
                UserContextService.Client userClient = (new UserClient()).getClient();
60
                UserContextService.Client userClient = (new UserClient()).getClient();
77
                List<String> cartResponse  = userClient.validateCart(cartId, -1);
61
                List<String> cartResponse  = userClient.validateCart(id, -1);
78
                errorMsg = cartResponse.get(0);
62
                errorMsg = cartResponse.get(0);
79
                if(StringUtils.isNotEmpty(cartResponse.get(1))) {
63
                if(StringUtils.isNotEmpty(cartResponse.get(1))) {
80
                    addActionMessage(cartResponse.get(1));
64
                    addActionMessage(cartResponse.get(1));
81
                }
65
                }
82
                log.info("Cart Change/EMI Message rcvd from the service is:" + errorMsg);
66
                log.info("Cart Change/EMI Message rcvd from the service is:" + errorMsg);
Line 96... Line 80...
96
 
80
 
97
    public String create() {
81
    public String create() {
98
        try {
82
        try {
99
            UserClient userServiceClient = new UserClient();
83
            UserClient userServiceClient = new UserClient();
100
            UserContextService.Client userClient = userServiceClient.getClient();
84
            UserContextService.Client userClient = userServiceClient.getClient();
101
            cartMsg = userClient.addItemToCart(cartId, itemId, 1, -1);
85
            cartMsg = userClient.addItemToCart(id, itemId, 1, -1);
102
            if (!("".equals(cartMsg))) {
86
            if (!("".equals(cartMsg))) {
103
                return "outofstock";
87
                return "outofstock";
104
            }
88
            }
105
        } catch (TException e) {
89
        } catch (TException e) {
106
            log.error("Unable to create or add to cart because of: ", e);
90
            log.error("Unable to create or add to cart because of: ", e);
Line 112... Line 96...
112
    }       
96
    }       
113
 
97
 
114
 
98
 
115
    // DELETE /entity
99
    // DELETE /entity
116
    public String destroy() {
100
    public String destroy() {
117
        if(cartId > 0)  {
101
        if(id > 0)  {
118
            try {
102
            try {
119
                UserClient userContextServiceClient = new UserClient();
103
                UserClient userContextServiceClient = new UserClient();
120
                in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
104
                in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
121
 
105
 
122
                userClient.deleteItemFromCart(cartId, itemId);
106
                userClient.deleteItemFromCart(id, itemId);
123
                return index();
107
                return index();
124
            } catch (ShoppingCartException e) {
108
            } catch (ShoppingCartException e) {
125
                log.error("Unable to delete item from cart: ", e);
109
                log.error("Unable to delete item from cart: ", e);
126
            } catch (TException e) {
110
            } catch (TException e) {
127
                log.error("Unable to delete item from cart: ", e);
111
                log.error("Unable to delete item from cart: ", e);
Line 135... Line 119...
135
    //PUT
119
    //PUT
136
    public String update() {
120
    public String update() {
137
        if(quantity <= 0)   {
121
        if(quantity <= 0)   {
138
            log.info("Not valid item quantity. Unable to change item quantity.");
122
            log.info("Not valid item quantity. Unable to change item quantity.");
139
        } else  {
123
        } else  {
140
            if(updateItemQuantityInCart(cartId, itemId, quantity))    {
124
            if(updateItemQuantityInCart(id, itemId, quantity))    {
141
                return index();
125
                return index();
142
            }
126
            }
143
        }
127
        }
144
        return index();
128
        return index();
145
    }
129
    }
Line 163... Line 147...
163
           
147
           
164
    public String insureItem() {
148
    public String insureItem() {
165
        insuranceResult = "";
149
        insuranceResult = "";
166
        try {
150
        try {
167
            UserContextService.Client usc = new UserClient().getClient();
151
            UserContextService.Client usc = new UserClient().getClient();
168
            if(usc.insureItem(itemId, cartId, toInsure, insuranceType)) {
152
            if(usc.insureItem(itemId, id, toInsure, insuranceType)) {
169
                setInsuranceResult("SUCCESS");
153
                setInsuranceResult("SUCCESS");
170
            } else {
154
            } else {
171
                setInsuranceResult("FAILURE");
155
                setInsuranceResult("FAILURE");
172
            }
156
            }
173
        } catch (Exception e) {
157
        } catch (Exception e) {
174
            log.error("Unable to insure item : " + itemId + " for cart : " + cartId, e);
158
            log.error("Unable to insure item : " + itemId + " for cart : " + id, e);
175
            setInsuranceResult("FAILURE");
159
            setInsuranceResult("FAILURE");
176
        }
160
        }
177
        return "insurance-result";
161
        return "insurance-result";
178
    }
162
    }
179
 
163
 
Line 302... Line 286...
302
 
286
 
303
    public boolean getToInsure() {
287
    public boolean getToInsure() {
304
        return toInsure;
288
        return toInsure;
305
    }
289
    }
306
 
290
 
307
    public long getCartId() {
291
    public long getId() {
308
        return cartId;
292
        return id;
309
    }
293
    }
310
 
294
 
311
    public void setCartId(long cartId) {
295
    public void setId(long id) {
312
        this.cartId = cartId;
296
        this.id = id;
313
    }
297
    }
314
 
298
 
315
 
299
 
316
    public void setCartPojoJson(String cartPojoJson) {
300
    public void setCartPojoJson(String cartPojoJson) {
317
        this.cartPojoJson = cartPojoJson;
301
        this.cartPojoJson = cartPojoJson;