Subversion Repositories SmartDukaan

Rev

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

Rev 9570 Rev 9576
Line 1... Line 1...
1
package in.shop2020.serving.controllers;
1
package in.shop2020.mobileapi.serving.controllers;
2
 
2
 
3
import in.shop2020.datalogger.EventType;
3
import in.shop2020.datalogger.EventType;
4
import in.shop2020.logistics.DeliveryType;
4
import in.shop2020.logistics.DeliveryType;
5
import in.shop2020.model.v1.catalog.CatalogService.Client;
5
import in.shop2020.model.v1.catalog.CatalogService.Client;
6
import in.shop2020.model.v1.catalog.Insurer;
6
import in.shop2020.model.v1.catalog.Insurer;
7
import in.shop2020.model.v1.catalog.Item;
7
import in.shop2020.model.v1.catalog.Item;
8
import in.shop2020.model.v1.user.Cart;
8
import in.shop2020.model.v1.user.Cart;
9
import in.shop2020.model.v1.user.Line;
9
import in.shop2020.model.v1.user.Line;
10
import in.shop2020.model.v1.user.ShoppingCartException;
10
import in.shop2020.model.v1.user.ShoppingCartException;
11
import in.shop2020.model.v1.user.UserContextService;
11
import in.shop2020.model.v1.user.UserContextService;
12
import in.shop2020.serving.services.ContentServingService;
12
import in.shop2020.mobileapi.serving.services.ContentServingService;
13
import in.shop2020.serving.utils.FormattingUtils;
13
import in.shop2020.mobileapi.serving.utils.FormattingUtils;
14
import in.shop2020.serving.utils.SnippetType;
14
import in.shop2020.mobileapi.serving.utils.SnippetType;
15
import in.shop2020.thrift.clients.CatalogClient;
15
import in.shop2020.thrift.clients.CatalogClient;
16
import in.shop2020.thrift.clients.UserClient;
16
import in.shop2020.thrift.clients.UserClient;
17
import in.shop2020.utils.DataLogger;
17
import in.shop2020.utils.DataLogger;
18
 
18
 
19
import java.util.ArrayList;
19
import java.util.ArrayList;
Line 153... Line 153...
153
    }       
153
    }       
154
 
154
 
155
 
155
 
156
    // DELETE /entity
156
    // DELETE /entity
157
    public String destroy() {
157
    public String destroy() {
158
        log.info("CartController.destroy");
158
//        log.info("CartController.destroy");
159
        printParams();
159
//        printParams();
160
        log.info("item id is " + this.request.getParameter("productid"));
160
//        log.info("item id is " + this.request.getParameter("productid"));
161
        
161
//        
162
        String itemIdString = this.request.getParameter("productid");
162
//        String itemIdString = this.request.getParameter("productid");
163
        itemId = Long.parseLong(itemIdString);
163
//        itemId = Long.parseLong(itemIdString);
164
 
164
//
165
        if(userinfo.getCartId() == -1)  {
165
//        if(userinfo.getCartId() == -1)  {
166
            log.info("Cart does not exist. Nothing to delete.");
166
//            log.info("Cart does not exist. Nothing to delete.");
167
        } else  {
167
//        } else  {
168
            if(deleteItemFromCart(userinfo.getCartId(), itemId, userinfo.getUserId(), userinfo.isSessionId()))  {
168
//            if(deleteItemFromCart(userinfo.getCartId(), itemId, userinfo.getUserId(), userinfo.isSessionId()))  {
169
                updateUserSessionInfo(userinfo.getCartId());
169
//                updateUserSessionInfo(userinfo.getCartId());
170
                DataLogger.logData(EventType.DELETE_FROM_CART, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
170
//                DataLogger.logData(EventType.DELETE_FROM_CART, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
171
                        Long.toString(userinfo.getCartId()), itemIdString);
171
//                        Long.toString(userinfo.getCartId()), itemIdString);
172
                return "redirect";
172
//                return "redirect";
173
            }
173
//            }
174
        }
174
//        }
175
        return "redirect";
175
        return "redirect";
176
    }
176
    }
177
 
177
 
178
    // DELETE /entity
178
    // DELETE /entity
179
    public String update() {
179
    public String update() {
180
        log.info("CartController.update");
180
//        log.info("CartController.update");
181
        printParams();
181
//        printParams();
182
        log.info("item id is " + this.request.getParameter("productid"));
182
//        log.info("item id is " + this.request.getParameter("productid"));
183
        log.info("item quantity is " + this.request.getParameter("quantity"));
183
//        log.info("item quantity is " + this.request.getParameter("quantity"));
184
        String itemIdString = this.request.getParameter("productid");
184
//        String itemIdString = this.request.getParameter("productid");
185
        String quantityString = this.request.getParameter("quantity");
185
//        String quantityString = this.request.getParameter("quantity");
186
        long itemId = Long.parseLong(itemIdString);
186
//        long itemId = Long.parseLong(itemIdString);
187
        long quantity = Long.parseLong(quantityString);
187
//        long quantity = Long.parseLong(quantityString);
188
 
188
//
189
        if(quantity <= 0)   {
189
//        if(quantity <= 0)   {
190
            log.info("Not valid item quantity. Unable to change item quantity.");
190
//            log.info("Not valid item quantity. Unable to change item quantity.");
191
        } else  {
191
//        } else  {
192
            if(updateItemQuantityInCart(userinfo.getCartId(), itemId, quantity))    {
192
//            if(updateItemQuantityInCart(userinfo.getCartId(), itemId, quantity))    {
193
                DataLogger.logData(EventType.UPDATE_CART_QUANTITY, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
193
//                DataLogger.logData(EventType.UPDATE_CART_QUANTITY, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
194
                        Long.toString(userinfo.getCartId()), Long.toString(itemId), Long.toString(quantity));
194
//                        Long.toString(userinfo.getCartId()), Long.toString(itemId), Long.toString(quantity));
195
                return "redirect";
195
//                return "redirect";
196
            }
196
//            }
197
        }
197
//        }
198
        DataLogger.logData(EventType.UPDATE_CART_QUANTITY_FAILED, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
198
//        DataLogger.logData(EventType.UPDATE_CART_QUANTITY_FAILED, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
199
                    Long.toString(userinfo.getCartId()), Long.toString(itemId), Long.toString(quantity));
199
//                    Long.toString(userinfo.getCartId()), Long.toString(itemId), Long.toString(quantity));
200
        addActionError("Unable to update the quantity");
200
//        addActionError("Unable to update the quantity");
201
        return "redirect";
201
        return "redirect";
202
    }
202
    }
203
    
203
    
204
    public void printParams(){
204
    public void printParams(){
205
        for(String param : reqparams.keySet()) {
205
        for(String param : reqparams.keySet()) {
Line 243... Line 243...
243
        
243
        
244
        return false;
244
        return false;
245
    }
245
    }
246
 
246
 
247
    private void updateUserSessionInfo(long cartId) {
247
    private void updateUserSessionInfo(long cartId) {
248
        UserClient userContextServiceClient = null;
248
//        UserClient userContextServiceClient = null;
249
        try {
249
//        try {
250
            userContextServiceClient = new UserClient();
250
//            userContextServiceClient = new UserClient();
251
            in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
251
//            in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
252
            Cart cart = userClient.getCart(cartId);
252
//            Cart cart = userClient.getCart(cartId);
253
            userinfo.setTotalItems(cart.getLinesSize());
253
//            userinfo.setTotalItems(cart.getLinesSize());
254
            userinfo.setTotalAmount(cart.getTotalPrice());
254
//            userinfo.setTotalAmount(cart.getTotalPrice());
255
        } catch (ShoppingCartException e) {
255
//        } catch (ShoppingCartException e) {
256
            log.error("Unable to get the cart from service: ", e);
256
//            log.error("Unable to get the cart from service: ", e);
257
        } catch (TException e) {
257
//        } catch (TException e) {
258
            log.error("Unable to get the cart from service: ", e);
258
//            log.error("Unable to get the cart from service: ", e);
259
        } catch (Exception e) {
259
//        } catch (Exception e) {
260
            log.error("Unable to get the cart from service: ", e);
260
//            log.error("Unable to get the cart from service: ", e);
261
        }
261
//        }
262
    }
262
    }
263
    
263
    
264
    public List<Map<String,String>> getCartItems() {
264
    public List<Map<String,String>> getCartItems() {
265
        List<Map<String,String>> items = null;
265
        List<Map<String,String>> items = null;
266
 
266
//
267
        UserClient userServiceClient = null;
267
//        UserClient userServiceClient = null;
268
        in.shop2020.model.v1.user.UserContextService.Client userClient = null;
268
//        in.shop2020.model.v1.user.UserContextService.Client userClient = null;
269
        CatalogClient catalogServiceClient  = null;
269
//        CatalogClient catalogServiceClient  = null;
270
        in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = null;
270
//        in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = null;
271
 
271
//
272
        FormattingUtils formattingUtils = new FormattingUtils();
272
//        FormattingUtils formattingUtils = new FormattingUtils();
273
        
273
//        
274
        try    {
274
//        try    {
275
            catalogServiceClient = new CatalogClient();
275
//            catalogServiceClient = new CatalogClient();
276
            catalogClient = catalogServiceClient.getClient();
276
//            catalogClient = catalogServiceClient.getClient();
277
            userServiceClient = new UserClient();
277
//            userServiceClient = new UserClient();
278
            userClient = userServiceClient.getClient();
278
//            userClient = userServiceClient.getClient();
279
            
279
//            
280
            pincode = userClient.getDefaultPincode(userinfo.getUserId());
280
//            pincode = userClient.getDefaultPincode(userinfo.getUserId());
281
            Cart cart = userClient.getCart(userinfo.getCartId());
281
//            Cart cart = userClient.getCart(userinfo.getCartId());
282
            List<Line> lineItems = cart.getLines();
282
//            List<Line> lineItems = cart.getLines();
283
            double totalInsuranceAmount = 0.0;
283
//            double totalInsuranceAmount = 0.0;
284
            boolean isAnyItemInsured = false;
284
//            boolean isAnyItemInsured = false;
285
            if(lineItems.size() != 0)  {
285
//            if(lineItems.size() != 0)  {
286
                items = new ArrayList<Map<String,String>>();
286
//                items = new ArrayList<Map<String,String>>();
287
 
287
//
288
                for (Line line : lineItems)    {
288
//                for (Line line : lineItems)    {
289
                    Map<String, String> itemdetail = new HashMap<String, String>();
289
//                    Map<String, String> itemdetail = new HashMap<String, String>();
290
                    
290
//                    
291
                    double insuranceAmount = 0;
291
//                    double insuranceAmount = 0;
292
                    Item item = catalogClient.getItemForSource(line.getItemId(), sourceId);
292
//                    Item item = catalogClient.getItemForSource(line.getItemId(), sourceId);
293
                    Insurer insurer = null;
293
//                    Insurer insurer = null;
294
                    
294
//                    
295
                    if(line.getInsurer() != 0) {
295
//                    if(line.getInsurer() != 0) {
296
                        insuranceAmount = catalogClient.getInsuranceAmount(item.getId(), 
296
//                        insuranceAmount = catalogClient.getInsuranceAmount(item.getId(), 
297
                                (line.getDiscountedPrice() == 0 ? line.getActualPrice() : line.getDiscountedPrice()),
297
//                                (line.getDiscountedPrice() == 0 ? line.getActualPrice() : line.getDiscountedPrice()),
298
                                item.getPreferredInsurer(), 
298
//                                item.getPreferredInsurer(), 
299
                                (long)line.getQuantity());
299
//                                (long)line.getQuantity());
300
                        isAnyItemInsured = true;
300
//                        isAnyItemInsured = true;
301
                    } else if (item.getPreferredInsurer() != 0) {
301
//                    } else if (item.getPreferredInsurer() != 0) {
302
                        insuranceAmount = catalogClient.getInsuranceAmount(item.getId(), 
302
//                        insuranceAmount = catalogClient.getInsuranceAmount(item.getId(), 
303
                                (line.getDiscountedPrice() == 0 ? line.getActualPrice() : line.getDiscountedPrice()),
303
//                                (line.getDiscountedPrice() == 0 ? line.getActualPrice() : line.getDiscountedPrice()),
304
                                item.getPreferredInsurer(), 
304
//                                item.getPreferredInsurer(), 
305
                                (long)line.getQuantity());
305
//                                (long)line.getQuantity());
306
                        //Insurer insurer = catalogClient.getInsurer(item.getPreferredInsurer());
306
//                        //Insurer insurer = catalogClient.getInsurer(item.getPreferredInsurer());
307
                    } else {
307
//                    } else {
308
                        insuranceAmount = 0;
308
//                        insuranceAmount = 0;
309
                    }
309
//                    }
310
                    
310
//                    
311
                    String itemName = ((item.getBrand() != null) ? item.getBrand() + " " : "")
311
//                    String itemName = ((item.getBrand() != null) ? item.getBrand() + " " : "")
312
                                            + ((item.getModelName() != null) ? item.getModelName() + " " : "") 
312
//                                            + ((item.getModelName() != null) ? item.getModelName() + " " : "") 
313
                                            + (( item.getModelNumber() != null ) ? item.getModelNumber() + " " : "" );
313
//                                            + (( item.getModelNumber() != null ) ? item.getModelNumber() + " " : "" );
314
                    
314
//                    
315
                    String itemColor = "";
315
//                    String itemColor = "";
316
                    if(item.getColor() != null && !item.getColor().trim().equals("NA"))
316
//                    if(item.getColor() != null && !item.getColor().trim().equals("NA"))
317
                        itemColor = "Color - " + item.getColor();
317
//                        itemColor = "Color - " + item.getColor();
318
                    
318
//                    
319
                    itemdetail.put("ITEM_NAME", itemName);
319
//                    itemdetail.put("ITEM_NAME", itemName);
320
                    itemdetail.put("ITEM_COLOR", itemColor);
320
//                    itemdetail.put("ITEM_COLOR", itemColor);
321
                    itemdetail.put("ITEM_ID", line.getItemId() + "");
321
//                    itemdetail.put("ITEM_ID", line.getItemId() + "");
322
                    itemdetail.put("CATALOG_ID", item.getCatalogItemId() + "");
322
//                    itemdetail.put("CATALOG_ID", item.getCatalogItemId() + "");
323
                    itemdetail.put("ITEM_QUANTITY", ((int)line.getQuantity()) + "");
323
//                    itemdetail.put("ITEM_QUANTITY", ((int)line.getQuantity()) + "");
324
                    itemdetail.put("MRP", formattingUtils.formatPrice(item.getMrp()));
324
//                    itemdetail.put("MRP", formattingUtils.formatPrice(item.getMrp()));
325
                    itemdetail.put("SELLING_PRICE", formattingUtils.formatPrice(item.getSellingPrice()));
325
//                    itemdetail.put("SELLING_PRICE", formattingUtils.formatPrice(item.getSellingPrice()));
326
                    itemdetail.put("TOTAL_PRICE", formattingUtils.formatPrice(((item.getSellingPrice() * line.getQuantity()))));
326
//                    itemdetail.put("TOTAL_PRICE", formattingUtils.formatPrice(((item.getSellingPrice() * line.getQuantity()))));
327
                    itemdetail.put("SHIPPING_TIME", EstimateController.getDeliveryDateString(line.getEstimate(), DeliveryType.PREPAID));
327
//                    itemdetail.put("SHIPPING_TIME", EstimateController.getDeliveryDateString(line.getEstimate(), DeliveryType.PREPAID));
328
                    itemdetail.put("COD_SHIPPING_TIME", EstimateController.getDeliveryDateString(line.getEstimate(), DeliveryType.COD));
328
//                    itemdetail.put("COD_SHIPPING_TIME", EstimateController.getDeliveryDateString(line.getEstimate(), DeliveryType.COD));
329
                    itemdetail.put("BEST_DEAL_TEXT", item.getBestDealText());
329
//                    itemdetail.put("BEST_DEAL_TEXT", item.getBestDealText());
330
                    itemdetail.put("IS_INSURABLE", item.getPreferredInsurer() + "");
330
//                    itemdetail.put("IS_INSURABLE", item.getPreferredInsurer() + "");
331
                    itemdetail.put("IS_INSURED", (line.getInsurer() == 0 ? false : true) + "");
331
//                    itemdetail.put("IS_INSURED", (line.getInsurer() == 0 ? false : true) + "");
332
                    itemdetail.put("INSURANCE_AMOUNT", insuranceAmount + "");
332
//                    itemdetail.put("INSURANCE_AMOUNT", insuranceAmount + "");
333
 
333
//
334
                    totalInsuranceAmount += insuranceAmount;
334
//                    totalInsuranceAmount += insuranceAmount;
335
                    items.add(itemdetail);
335
//                    items.add(itemdetail);
336
                }
336
//                }
337
            }
-
 
338
            
-
 
339
//            if(isAnyItemInsured == false) {
-
 
340
//                totalamount = formattingUtils.formatPrice(cart.getTotalPrice() + totalInsuranceAmount);
-
 
341
//                discountedAmount = formattingUtils.formatPrice(cart.getDiscountedPrice() + totalInsuranceAmount);
-
 
342
//            } else {
-
 
343
                totalamount = formattingUtils.formatPrice(cart.getTotalPrice());
-
 
344
                discountedAmount = formattingUtils.formatPrice(cart.getDiscountedPrice());
-
 
345
//            }
337
//            }
346
            
338
//            
-
 
339
////            if(isAnyItemInsured == false) {
-
 
340
////                totalamount = formattingUtils.formatPrice(cart.getTotalPrice() + totalInsuranceAmount);
-
 
341
////                discountedAmount = formattingUtils.formatPrice(cart.getDiscountedPrice() + totalInsuranceAmount);
-
 
342
////            } else {
-
 
343
//                totalamount = formattingUtils.formatPrice(cart.getTotalPrice());
-
 
344
//                discountedAmount = formattingUtils.formatPrice(cart.getDiscountedPrice());
-
 
345
////            }
-
 
346
//            
347
            couponCode = cart.getCouponCode() == null ? "" : cart.getCouponCode();
347
//            couponCode = cart.getCouponCode() == null ? "" : cart.getCouponCode();
348
        } catch (Exception e){
348
//        } catch (Exception e){
349
            log.error("Unable to get the cart details becasue of: ", e);
349
//            log.error("Unable to get the cart details becasue of: ", e);
350
        }
350
//        }
351
        return items;
351
        return items;
352
    }
352
    }
353
    
353
    
354
    public String insureItem() {
354
    public String insureItem() {
355
        //TODO : Call a method in userservice that insures the item.
355
//        //TODO : Call a method in userservice that insures the item.
356
        insuranceResult = "";
356
//        insuranceResult = "";
357
        try {
357
//        try {
358
            UserContextService.Client usc = new UserClient().getClient();
358
//            UserContextService.Client usc = new UserClient().getClient();
359
            if(usc.insureItem(productId, userinfo.getCartId(), toInsure)) {
359
//            if(usc.insureItem(productId, userinfo.getCartId(), toInsure)) {
360
                setInsuranceResult("SUCCESS");
360
//                setInsuranceResult("SUCCESS");
361
            } else {
361
//            } else {
362
                setInsuranceResult("FAILURE");
362
//                setInsuranceResult("FAILURE");
363
            }
363
//            }
364
        } catch (Exception e) {
364
//        } catch (Exception e) {
365
            log.error("Unable to insure item : " + productId + " for cart : " + userinfo.getCartId(), e);
365
//            log.error("Unable to insure item : " + productId + " for cart : " + userinfo.getCartId(), e);
366
            setInsuranceResult("FAILURE");
366
//            setInsuranceResult("FAILURE");
367
        }
367
//        }
368
        return "insurance-result";
368
        return "insurance-result";
369
    }
369
    }
370
 
370
 
371
    public long getItemId(){
371
    public long getItemId(){
372
        return this.itemId;
372
        return this.itemId;
Line 390... Line 390...
390
    
390
    
391
    public String getErrorMsg()    {
391
    public String getErrorMsg()    {
392
        return errorMsg;
392
        return errorMsg;
393
    }
393
    }
394
    
394
    
395
    public long getNumberOfItems(){
395
//    public long getNumberOfItems(){
396
        return userinfo.getTotalItems();
396
//        return userinfo.getTotalItems();
397
    }
397
//    }
398
 
398
 
399
    public String getCartMsg(){
399
    public String getCartMsg(){
400
        if(cartMsg.equals("")){
400
        if(cartMsg.equals("")){
401
            return null;
401
            return null;
402
        }
402
        }
Line 421... Line 421...
421
    @Override
421
    @Override
422
    public void setParameters(Map<String, String[]> parameters) {
422
    public void setParameters(Map<String, String[]> parameters) {
423
        this.reqparams = parameters;    
423
        this.reqparams = parameters;    
424
    }
424
    }
425
    
425
    
426
    @Override
426
//    @Override
427
    public String getHeaderSnippet() {
427
//    public String getHeaderSnippet() {
428
        String url = request.getQueryString();
428
//        String url = request.getQueryString();
429
        if (url == null) {
429
//        if (url == null) {
430
            url = "";
430
//            url = "";
431
        } else {
431
//        } else {
432
            url = "?" + url;
432
//            url = "?" + url;
433
        }
433
//        }
434
        url = request.getRequestURI() + url;
434
//        url = request.getRequestURI() + url;
435
        return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), userinfo.getTotalItems(), url , 0, false);
435
//        return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), userinfo.getTotalItems(), url , 0, false);
436
    }
436
//    }
437
    
437
    
438
    public boolean isUserLoggedIn() {
438
//    public boolean isUserLoggedIn() {
439
        return userinfo.isLoggedIn();
439
//        return userinfo.isLoggedIn();
440
    }
440
//    }
441
    
441
    
442
    public void setVariationId(String uri)  {
442
    public void setVariationId(String uri)  {
443
        if (uri.equals("/cart1"))   {
443
        if (uri.equals("/cart1"))   {
444
            this.variationId = 1;
444
            this.variationId = 1;
445
        }
445
        }