Subversion Repositories SmartDukaan

Rev

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

Rev 9667 Rev 9676
Line 57... Line 57...
57
    
57
    
58
    private long itemId;
58
    private long itemId;
59
    private String insuranceResult;
59
    private String insuranceResult;
60
    
60
    
61
    private boolean toInsure;
61
    private boolean toInsure;
62
    private long productId;
62
    
63
    
63
    
64
    private long cartId;
64
    private long cartId;
65
    private String cartPojoJson;
65
    private String cartPojoJson;
-
 
66
    private long quantity;
66
    
67
    
67
    public CartController(){
68
    public CartController(){
68
        super();
69
        super();
69
    }
70
    }
70
    
71
    
71
 
-
 
72
    public String index()  {
72
    public String index()  {
73
        if(cartId != -1){
73
        if(cartId != -1){
74
            try {
74
            try {
75
                UserContextService.Client userClient = (new UserClient()).getClient();
75
                UserContextService.Client userClient = (new UserClient()).getClient();
76
                Cart cart = userClient.getCurrentCart(userinfo.getUserId());
76
                Cart cart = userClient.getCurrentCart(userinfo.getUserId());
Line 92... Line 92...
92
            }
92
            }
93
        }
93
        }
94
        return "index";
94
        return "index";
95
    }
95
    }
96
 
96
 
97
    // POST /entity
-
 
98
 
-
 
99
 
-
 
100
    public String create() {
97
    public String create() {
101
        log.info("CartController.create");
-
 
102
 
-
 
103
        printParams();
-
 
104
 
-
 
105
        long userId = userinfo.getUserId();
98
        long userId = userinfo.getUserId();
106
 
-
 
107
        log.info("user id is " + userId);
-
 
108
        log.info("cart id is " + cartId);
-
 
109
        
99
        
110
        log.info("item id is " + this.reqparams.get("productid"));
-
 
111
        
-
 
112
        String itemIds = "";
-
 
113
        if (this.reqparams.get("productid") != null) {
-
 
114
            itemIds = this.reqparams.get("productid")[0];
-
 
115
        }else{
-
 
116
            return "failure";
-
 
117
        }
-
 
118
        
-
 
119
        StringTokenizer tokenizer = new StringTokenizer(itemIds, "_");
-
 
120
        while (tokenizer.hasMoreTokens()) {
-
 
121
            itemId = Long.parseLong(tokenizer.nextToken());
-
 
122
 
-
 
123
            try {
100
        try {
124
                UserClient userServiceClient = new UserClient();
101
            UserClient userServiceClient = new UserClient();
125
                UserContextService.Client userClient = userServiceClient.getClient();
102
            UserContextService.Client userClient = userServiceClient.getClient();
126
                if (cartId == 0){
103
            if (cartId == 0){
127
                    cartId = userClient.getUserById(userId).getActiveCartId();
104
                cartId = userClient.getUserById(userId).getActiveCartId();
128
                }
-
 
129
                // If we add multiple items to cart and get some message from service, 
-
 
130
                // first message to be preserved and presented to the user.
-
 
131
                if(cartMsg.equals("")){
-
 
132
                    cartMsg = userClient.addItemToCart(cartId, itemId, 1, -1);
-
 
133
                }else{
-
 
134
                    userClient.addItemToCart(cartId, itemId, 1, -1);
-
 
135
                }
-
 
136
                int totalItems = userClient.getCart(cartId).getLinesSize();
-
 
137
            } catch (TException e) {
-
 
138
                log.error("Unable to create or add to cart because of: ", e);
-
 
139
            } catch (Exception e) {
-
 
140
                log.error("Unable to create or add to cart because of: ", e);
-
 
141
            }
105
            }
-
 
106
            cartMsg = userClient.addItemToCart(cartId, itemId, 1, -1);
-
 
107
            if ("".equals(cartMsg)) {
-
 
108
                return "outofstock";
142
 
109
            }
-
 
110
        } catch (TException e) {
-
 
111
            log.error("Unable to create or add to cart because of: ", e);
-
 
112
        } catch (Exception e) {
-
 
113
            log.error("Unable to create or add to cart because of: ", e);
143
        }
114
        }
-
 
115
        
144
        return "success";
116
    return index();
145
    }       
117
    }       
146
 
118
 
147
 
119
 
148
    // DELETE /entity
120
    // DELETE /entity
149
    public String destroy() {
121
    public String destroy() {
150
//        log.info("CartController.destroy");
-
 
151
//        printParams();
-
 
152
//        log.info("item id is " + this.request.getParameter("productid"));
-
 
153
//        
-
 
154
//        String itemIdString = this.request.getParameter("productid");
-
 
155
//        itemId = Long.parseLong(itemIdString);
-
 
156
//
-
 
157
//        if(userinfo.getCartId() == -1)  {
122
        if(cartId > 0)  {
158
//            log.info("Cart does not exist. Nothing to delete.");
-
 
159
//        } else  {
123
            try {
160
//            if(deleteItemFromCart(userinfo.getCartId(), itemId, userinfo.getUserId(), userinfo.isSessionId()))  {
-
 
161
//                updateUserSessionInfo(userinfo.getCartId());
124
                UserClient userContextServiceClient = new UserClient();
162
//                DataLogger.logData(EventType.DELETE_FROM_CART, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
125
                in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
163
//                        Long.toString(userinfo.getCartId()), itemIdString);
-
 
164
//                return "redirect";
-
 
165
//            }
-
 
166
//        }
-
 
167
        return "redirect";
-
 
168
    }
-
 
169
 
126
 
170
    // DELETE /entity
-
 
171
    public String update() {
-
 
172
//        log.info("CartController.update");
127
                userClient.deleteItemFromCart(cartId, itemId);
173
//        printParams();
128
                return index();
174
//        log.info("item id is " + this.request.getParameter("productid"));
-
 
175
//        log.info("item quantity is " + this.request.getParameter("quantity"));
-
 
176
//        String itemIdString = this.request.getParameter("productid");
-
 
177
//        String quantityString = this.request.getParameter("quantity");
-
 
178
//        long itemId = Long.parseLong(itemIdString);
129
            } catch (ShoppingCartException e) {
179
//        long quantity = Long.parseLong(quantityString);
130
                log.error("Unable to delete item from cart: ", e);
180
//
-
 
181
//        if(quantity <= 0)   {
131
            } catch (TException e) {
182
//            log.info("Not valid item quantity. Unable to change item quantity.");
132
                log.error("Unable to delete item from cart: ", e);
183
//        } else  {
133
            } catch (Exception e) {
184
//            if(updateItemQuantityInCart(userinfo.getCartId(), itemId, quantity))    {
-
 
185
//                DataLogger.logData(EventType.UPDATE_CART_QUANTITY, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
-
 
186
//                        Long.toString(userinfo.getCartId()), Long.toString(itemId), Long.toString(quantity));
-
 
187
//                return "redirect";
134
                log.error("Unable to delete item from cart: ", e);
188
//            }
135
            }
189
//        }
136
        }
190
//        DataLogger.logData(EventType.UPDATE_CART_QUANTITY_FAILED, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
-
 
191
//                    Long.toString(userinfo.getCartId()), Long.toString(itemId), Long.toString(quantity));
-
 
192
//        addActionError("Unable to update the quantity");
-
 
193
        return "redirect";
137
        return index();
194
    }
138
    }
195
    
139
    
-
 
140
    //PUT
196
    public void printParams(){
141
    public String update() {
197
        for(String param : reqparams.keySet()) {
142
        if(quantity <= 0)   {
198
            log.info("param name is " + param);
143
            log.info("Not valid item quantity. Unable to change item quantity.");
-
 
144
        } else  {
199
            log.info("param first is " + reqparams.get(param)[0]);
145
            if(updateItemQuantityInCart(cartId, itemId, quantity))    {
-
 
146
                return index();
-
 
147
            }
200
        }
148
        }
201
        log.info(this.reqparams);
149
        return index();
202
    }
150
    }
203
 
151
    
204
    private boolean updateItemQuantityInCart(long cartId, long itemId, long quantity){
152
    private boolean updateItemQuantityInCart(long cartId, long itemId, long quantity){
205
        try {
153
        try {
206
            UserClient userContextServiceClient = new UserClient();
154
            UserClient userContextServiceClient = new UserClient();
207
            in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
155
            in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
208
 
156
 
Line 215... Line 163...
215
        } catch (Exception e) {
163
        } catch (Exception e) {
216
            log.error("Unable to update the item quantity in the cart: ", e);
164
            log.error("Unable to update the item quantity in the cart: ", e);
217
        }
165
        }
218
        return false;
166
        return false;
219
    }
167
    }
220
    
-
 
221
    private boolean deleteItemFromCart(long cartId, long catalogItemId, long userId, boolean isSessionId){
-
 
222
        try {
-
 
223
            UserClient userContextServiceClient = new UserClient();
-
 
224
            in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
-
 
225
            
168
           
226
            userClient.deleteItemFromCart(cartId, catalogItemId);
-
 
227
            return true;
-
 
228
        } catch (ShoppingCartException e) {
-
 
229
            log.error("Unable to delete item from cart: ", e);
-
 
230
        } catch (TException e) {
-
 
231
            log.error("Unable to delete item from cart: ", e);
-
 
232
        } catch (Exception e) {
-
 
233
            log.error("Unable to delete item from cart: ", e);
-
 
234
        }
-
 
235
        
-
 
236
        return false;
-
 
237
    }
-
 
238
 
-
 
239
    private void updateUserSessionInfo(long cartId) {
-
 
240
//        UserClient userContextServiceClient = null;
-
 
241
//        try {
-
 
242
//            userContextServiceClient = new UserClient();
-
 
243
//            in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
-
 
244
//            Cart cart = userClient.getCart(cartId);
-
 
245
//            userinfo.setTotalItems(cart.getLinesSize());
-
 
246
//            userinfo.setTotalAmount(cart.getTotalPrice());
-
 
247
//        } catch (ShoppingCartException e) {
-
 
248
//            log.error("Unable to get the cart from service: ", e);
-
 
249
//        } catch (TException e) {
-
 
250
//            log.error("Unable to get the cart from service: ", e);
-
 
251
//        } catch (Exception e) {
-
 
252
//            log.error("Unable to get the cart from service: ", e);
-
 
253
//        }
-
 
254
    }
-
 
255
    
-
 
256
    public List<Map<String,String>> getCartItems() {
-
 
257
        List<Map<String,String>> items = null;
-
 
258
//
-
 
259
//        UserClient userServiceClient = null;
-
 
260
//        in.shop2020.model.v1.user.UserContextService.Client userClient = null;
-
 
261
//        CatalogClient catalogServiceClient  = null;
-
 
262
//        in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = null;
-
 
263
//
-
 
264
//        FormattingUtils formattingUtils = new FormattingUtils();
-
 
265
//        
-
 
266
//        try    {
-
 
267
//            catalogServiceClient = new CatalogClient();
-
 
268
//            catalogClient = catalogServiceClient.getClient();
-
 
269
//            userServiceClient = new UserClient();
-
 
270
//            userClient = userServiceClient.getClient();
-
 
271
//            
-
 
272
//            pincode = userClient.getDefaultPincode(userinfo.getUserId());
-
 
273
//            Cart cart = userClient.getCart(userinfo.getCartId());
-
 
274
//            List<Line> lineItems = cart.getLines();
-
 
275
//            double totalInsuranceAmount = 0.0;
-
 
276
//            boolean isAnyItemInsured = false;
-
 
277
//            if(lineItems.size() != 0)  {
-
 
278
//                items = new ArrayList<Map<String,String>>();
-
 
279
//
-
 
280
//                for (Line line : lineItems)    {
-
 
281
//                    Map<String, String> itemdetail = new HashMap<String, String>();
-
 
282
//                    
-
 
283
//                    double insuranceAmount = 0;
-
 
284
//                    Item item = catalogClient.getItemForSource(line.getItemId(), -1);
-
 
285
//                    Insurer insurer = null;
-
 
286
//                    
-
 
287
//                    if(line.getInsurer() != 0) {
-
 
288
//                        insuranceAmount = catalogClient.getInsuranceAmount(item.getId(), 
-
 
289
//                                (line.getDiscountedPrice() == 0 ? line.getActualPrice() : line.getDiscountedPrice()),
-
 
290
//                                item.getPreferredInsurer(), 
-
 
291
//                                (long)line.getQuantity());
-
 
292
//                        isAnyItemInsured = true;
-
 
293
//                    } else if (item.getPreferredInsurer() != 0) {
-
 
294
//                        insuranceAmount = catalogClient.getInsuranceAmount(item.getId(), 
-
 
295
//                                (line.getDiscountedPrice() == 0 ? line.getActualPrice() : line.getDiscountedPrice()),
-
 
296
//                                item.getPreferredInsurer(), 
-
 
297
//                                (long)line.getQuantity());
-
 
298
//                        //Insurer insurer = catalogClient.getInsurer(item.getPreferredInsurer());
-
 
299
//                    } else {
-
 
300
//                        insuranceAmount = 0;
-
 
301
//                    }
-
 
302
//                    
-
 
303
//                    String itemName = ((item.getBrand() != null) ? item.getBrand() + " " : "")
-
 
304
//                                            + ((item.getModelName() != null) ? item.getModelName() + " " : "") 
-
 
305
//                                            + (( item.getModelNumber() != null ) ? item.getModelNumber() + " " : "" );
-
 
306
//                    
-
 
307
//                    String itemColor = "";
-
 
308
//                    if(item.getColor() != null && !item.getColor().trim().equals("NA"))
-
 
309
//                        itemColor = "Color - " + item.getColor();
-
 
310
//                    
-
 
311
//                    itemdetail.put("ITEM_NAME", itemName);
-
 
312
//                    itemdetail.put("ITEM_COLOR", itemColor);
-
 
313
//                    itemdetail.put("ITEM_ID", line.getItemId() + "");
-
 
314
//                    itemdetail.put("CATALOG_ID", item.getCatalogItemId() + "");
-
 
315
//                    itemdetail.put("ITEM_QUANTITY", ((int)line.getQuantity()) + "");
-
 
316
//                    itemdetail.put("MRP", formattingUtils.formatPrice(item.getMrp()));
-
 
317
//                    itemdetail.put("SELLING_PRICE", formattingUtils.formatPrice(item.getSellingPrice()));
-
 
318
//                    itemdetail.put("TOTAL_PRICE", formattingUtils.formatPrice(((item.getSellingPrice() * line.getQuantity()))));
-
 
319
//                    itemdetail.put("SHIPPING_TIME", EstimateController.getDeliveryDateString(line.getEstimate(), DeliveryType.PREPAID));
-
 
320
//                    itemdetail.put("COD_SHIPPING_TIME", EstimateController.getDeliveryDateString(line.getEstimate(), DeliveryType.COD));
-
 
321
//                    itemdetail.put("BEST_DEAL_TEXT", item.getBestDealText());
-
 
322
//                    itemdetail.put("IS_INSURABLE", item.getPreferredInsurer() + "");
-
 
323
//                    itemdetail.put("IS_INSURED", (line.getInsurer() == 0 ? false : true) + "");
-
 
324
//                    itemdetail.put("INSURANCE_AMOUNT", insuranceAmount + "");
-
 
325
//
-
 
326
//                    totalInsuranceAmount += insuranceAmount;
-
 
327
//                    items.add(itemdetail);
-
 
328
//                }
-
 
329
//            }
-
 
330
//            
-
 
331
////            if(isAnyItemInsured == false) {
-
 
332
////                totalamount = formattingUtils.formatPrice(cart.getTotalPrice() + totalInsuranceAmount);
-
 
333
////                discountedAmount = formattingUtils.formatPrice(cart.getDiscountedPrice() + totalInsuranceAmount);
-
 
334
////            } else {
-
 
335
//                totalamount = formattingUtils.formatPrice(cart.getTotalPrice());
-
 
336
//                discountedAmount = formattingUtils.formatPrice(cart.getDiscountedPrice());
-
 
337
////            }
-
 
338
//            
-
 
339
//            couponCode = cart.getCouponCode() == null ? "" : cart.getCouponCode();
-
 
340
//        } catch (Exception e){
-
 
341
//            log.error("Unable to get the cart details becasue of: ", e);
-
 
342
//        }
-
 
343
        return items;
-
 
344
    }
-
 
345
    
-
 
346
    public String insureItem() {
169
    public String insureItem() {
347
//        //TODO : Call a method in userservice that insures the item.
170
//        //TODO : Call a method in userservice that insures the item.
348
//        insuranceResult = "";
171
//        insuranceResult = "";
349
//        try {
172
//        try {
350
//            UserContextService.Client usc = new UserClient().getClient();
173
//            UserContextService.Client usc = new UserClient().getClient();
Line 481... Line 304...
481
 
304
 
482
    public boolean getToInsure() {
305
    public boolean getToInsure() {
483
        return toInsure;
306
        return toInsure;
484
    }
307
    }
485
 
308
 
486
    public long getProductId() {
-
 
487
        return productId;
-
 
488
    }
-
 
489
 
-
 
490
    public void setProductId(long productId) {
-
 
491
        this.productId = productId;
-
 
492
    }
-
 
493
 
-
 
494
    public long getCartId() {
309
    public long getCartId() {
495
        return cartId;
310
        return cartId;
496
    }
311
    }
497
 
312
 
498
    public void setCartId(long cartId) {
313
    public void setCartId(long cartId) {
Line 506... Line 321...
506
 
321
 
507
 
322
 
508
    public String getCartPojoJson() {
323
    public String getCartPojoJson() {
509
        return cartPojoJson;
324
        return cartPojoJson;
510
    }
325
    }
-
 
326
 
-
 
327
    public void setQuantity(long quantity) {
-
 
328
        this.quantity = quantity;
-
 
329
    }
-
 
330
 
-
 
331
    public long getQuantity() {
-
 
332
        return quantity;
-
 
333
    }
511
}
334
}