Subversion Repositories SmartDukaan

Rev

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