Subversion Repositories SmartDukaan

Rev

Rev 9686 | Rev 9709 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
9576 anupam.sin 1
package in.shop2020.mobileapi.serving.controllers;
9570 anupam.sin 2
 
3
import in.shop2020.datalogger.EventType;
9577 anupam.sin 4
import in.shop2020.mobileapi.serving.services.ContentServingService;
9667 anupam.sin 5
import in.shop2020.mobileapi.serving.utils.PojoPopulator;
9577 anupam.sin 6
import in.shop2020.mobileapi.serving.utils.SnippetType;
9570 anupam.sin 7
import in.shop2020.model.v1.catalog.CatalogService.Client;
8
import in.shop2020.model.v1.catalog.Item;
9
import in.shop2020.model.v1.user.Cart;
10
import in.shop2020.model.v1.user.ShoppingCartException;
11
import in.shop2020.model.v1.user.UserContextService;
12
import in.shop2020.thrift.clients.CatalogClient;
13
import in.shop2020.thrift.clients.UserClient;
14
import in.shop2020.utils.DataLogger;
15
 
16
import java.util.List;
17
import java.util.Map;
18
import java.util.StringTokenizer;
19
 
20
import org.apache.commons.lang.StringUtils;
21
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;
28
import org.apache.thrift.TException;
29
 
9667 anupam.sin 30
import com.google.gson.Gson;
9570 anupam.sin 31
 
9667 anupam.sin 32
 
9570 anupam.sin 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{
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;
62
 
9676 anupam.sin 63
 
9570 anupam.sin 64
    private long cartId;
9667 anupam.sin 65
    private String cartPojoJson;
9676 anupam.sin 66
    private long quantity;
9697 anupam.sin 67
    private int insuranceType;
9570 anupam.sin 68
 
69
    public CartController(){
70
        super();
71
    }
72
 
73
    public String index()  {
74
        if(cartId != -1){
75
            try {
76
                UserContextService.Client userClient = (new UserClient()).getClient();
9602 amit.gupta 77
                List<String> cartResponse  = userClient.validateCart(cartId, -1);
9570 anupam.sin 78
                errorMsg = cartResponse.get(0);
79
                if(StringUtils.isNotEmpty(cartResponse.get(1))) {
80
                    addActionMessage(cartResponse.get(1));
81
                }
82
                log.info("Cart Change/EMI Message rcvd from the service is:" + errorMsg);
9667 anupam.sin 83
 
9686 amit.gupta 84
                Cart cart = userClient.getCurrentCart(userinfo.getUserId());
9667 anupam.sin 85
                cartPojoJson = new Gson().toJson(PojoPopulator.getCartPojo(cart, errorMsg));
86
 
9570 anupam.sin 87
            } catch (Exception e) {
88
                // This exception can be ignored for showing the cart. Not so
89
                // innocent when this occurs at the time of checkout or when the
90
                // user is proceeding to pay.
91
                log.warn("Unable to validate the cart: ", e);
92
            }
93
        }
94
        return "index";
95
    }
96
 
97
    public String create() {
9676 anupam.sin 98
        try {
99
            UserClient userServiceClient = new UserClient();
100
            UserContextService.Client userClient = userServiceClient.getClient();
101
            cartMsg = userClient.addItemToCart(cartId, itemId, 1, -1);
9680 amit.gupta 102
            if (!("".equals(cartMsg))) {
9676 anupam.sin 103
                return "outofstock";
104
            }
105
        } catch (TException e) {
106
            log.error("Unable to create or add to cart because of: ", e);
107
        } catch (Exception e) {
108
            log.error("Unable to create or add to cart because of: ", e);
9570 anupam.sin 109
        }
110
 
9676 anupam.sin 111
    return index();
112
    }       
9570 anupam.sin 113
 
9676 anupam.sin 114
 
115
    // DELETE /entity
116
    public String destroy() {
117
        if(cartId > 0)  {
9570 anupam.sin 118
            try {
9676 anupam.sin 119
                UserClient userContextServiceClient = new UserClient();
120
                in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
121
 
122
                userClient.deleteItemFromCart(cartId, itemId);
123
                return index();
124
            } catch (ShoppingCartException e) {
125
                log.error("Unable to delete item from cart: ", e);
9570 anupam.sin 126
            } catch (TException e) {
9676 anupam.sin 127
                log.error("Unable to delete item from cart: ", e);
9570 anupam.sin 128
            } catch (Exception e) {
9676 anupam.sin 129
                log.error("Unable to delete item from cart: ", e);
9570 anupam.sin 130
            }
131
        }
9676 anupam.sin 132
        return index();
9570 anupam.sin 133
    }
9676 anupam.sin 134
 
135
    //PUT
9570 anupam.sin 136
    public String update() {
9676 anupam.sin 137
        if(quantity <= 0)   {
138
            log.info("Not valid item quantity. Unable to change item quantity.");
139
        } else  {
140
            if(updateItemQuantityInCart(cartId, itemId, quantity))    {
141
                return index();
142
            }
143
        }
144
        return index();
9570 anupam.sin 145
    }
146
 
147
    private boolean updateItemQuantityInCart(long cartId, long itemId, long quantity){
148
        try {
149
            UserClient userContextServiceClient = new UserClient();
150
            in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
151
 
9602 amit.gupta 152
            userClient.addItemToCart(cartId, itemId, quantity, -1);
9570 anupam.sin 153
            return true;
154
        } catch (ShoppingCartException e) {
155
            log.error("Unable to update the item quantity in the cart: ", e);
156
        } catch (TException e) {
157
            log.error("Unable to update the item quantity in the cart: ", e);
158
        } catch (Exception e) {
159
            log.error("Unable to update the item quantity in the cart: ", e);
160
        }
161
        return false;
162
    }
9676 anupam.sin 163
 
9570 anupam.sin 164
    public String insureItem() {
9697 anupam.sin 165
        insuranceResult = "";
166
        try {
167
            UserContextService.Client usc = new UserClient().getClient();
168
            if(usc.insureItem(itemId, cartId, toInsure, insuranceType)) {
169
                setInsuranceResult("SUCCESS");
170
            } else {
171
                setInsuranceResult("FAILURE");
172
            }
173
        } catch (Exception e) {
174
            log.error("Unable to insure item : " + itemId + " for cart : " + cartId, e);
175
            setInsuranceResult("FAILURE");
176
        }
9570 anupam.sin 177
        return "insurance-result";
178
    }
179
 
180
    public long getItemId(){
181
        return this.itemId;
182
    }
183
 
9684 amit.gupta 184
    public void setItemId(long itemId){
185
    	this.itemId = itemId;
186
    }
187
 
9570 anupam.sin 188
    public String getTotalAmount() {
189
        return totalamount;
190
    }
191
 
192
    public String getPinCode() {
193
        return pincode;
194
    }
195
 
196
    public String getCouponCode()  {
197
        return couponCode;
198
    }
199
 
200
    public String getDiscountedAmount()   {
201
        return discountedAmount;
202
    }
203
 
204
    public String getErrorMsg()    {
205
        return errorMsg;
206
    }
207
 
9576 anupam.sin 208
//    public long getNumberOfItems(){
209
//        return userinfo.getTotalItems();
210
//    }
9570 anupam.sin 211
 
212
    public String getCartMsg(){
213
        if(cartMsg.equals("")){
214
            return null;
215
        }
216
        return cartMsg;
217
    }
218
 
219
    public String getSnippets(){
220
        String snippets = "";
221
        CatalogClient csc;
222
        try {
223
            csc = new CatalogClient();
224
            List<Long> similarItems = csc.getClient().getSimilarItemsCatalogIds(0, 4, itemId);
225
            for(Long catalogId: similarItems){
9602 amit.gupta 226
                snippets = snippets + ContentServingService.getSnippet(SnippetType.WIDGET_SNIPPET, catalogId+"", -1);
9570 anupam.sin 227
            }
228
        } catch (Exception e) {
229
            log.error("Unable to initialise Catalogservice Client");
230
        }       
231
        return snippets;
232
    }
233
 
234
    @Override
235
    public void setParameters(Map<String, String[]> parameters) {
236
        this.reqparams = parameters;    
237
    }
238
 
9576 anupam.sin 239
//    @Override
240
//    public String getHeaderSnippet() {
241
//        String url = request.getQueryString();
242
//        if (url == null) {
243
//            url = "";
244
//        } else {
245
//            url = "?" + url;
246
//        }
247
//        url = request.getRequestURI() + url;
248
//        return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), userinfo.getTotalItems(), url , 0, false);
249
//    }
9570 anupam.sin 250
 
9576 anupam.sin 251
//    public boolean isUserLoggedIn() {
252
//        return userinfo.isLoggedIn();
253
//    }
9570 anupam.sin 254
 
255
    public void setVariationId(String uri)  {
256
        if (uri.equals("/cart1"))   {
257
            this.variationId = 1;
258
        }
259
    }
260
 
261
    public int getVariationId() {
262
        return this.variationId;
263
    }
264
 
265
    public String getActionMessage(){
266
        if(cartMsg.contains("out of stock")){
267
            return "Notify me when this product is in stock.";
268
        }else {
269
            return "Notify me when this product is available.";
270
        }
271
    }
272
 
273
    public String getOfferNote(){
274
        String note = null;
275
        if(cartMsg.contains("out of stock")){
276
            return note;
277
        }
278
        else {
279
            try {
280
                CatalogClient catalogServiceClient = new CatalogClient();
281
                Client catalogClient = catalogServiceClient.getClient();
282
                Item it = catalogClient.getItem(itemId);
283
                note = it.getBestDealText();
284
            } catch (Exception e)  {
285
                log.error("Unable to get the offertext because of: ", e);
286
            }
287
        }
288
        return note;
289
    }
290
 
291
    public String getInsuranceResult() {
292
        return insuranceResult;
293
    }
294
 
295
    public void setInsuranceResult(String insuranceResult) {
296
        this.insuranceResult = insuranceResult;
297
    }
298
 
299
    public void setToInsure(boolean toInsure) {
300
        this.toInsure = toInsure;
301
    }
302
 
303
    public boolean getToInsure() {
304
        return toInsure;
305
    }
306
 
307
    public long getCartId() {
308
        return cartId;
309
    }
310
 
311
    public void setCartId(long cartId) {
312
        this.cartId = cartId;
313
    }
9667 anupam.sin 314
 
315
 
316
    public void setCartPojoJson(String cartPojoJson) {
317
        this.cartPojoJson = cartPojoJson;
318
    }
319
 
320
 
321
    public String getCartPojoJson() {
322
        return cartPojoJson;
323
    }
9676 anupam.sin 324
 
325
    public void setQuantity(long quantity) {
326
        this.quantity = quantity;
327
    }
328
 
329
    public long getQuantity() {
330
        return quantity;
331
    }
9697 anupam.sin 332
 
333
    public int getInsuranceType() {
334
        return insuranceType;
335
    }
336
 
337
    public void setInsuranceType(int insuranceType) {
338
        this.insuranceType = insuranceType;
339
    }
9570 anupam.sin 340
}