| Line 437... |
Line 437... |
| 437 |
if(StringUtils.isEmpty(cartMap)){
|
437 |
if(StringUtils.isEmpty(cartMap)){
|
| 438 |
jsonObj = new JSONObject("{\"cartItems\":[]}");
|
438 |
jsonObj = new JSONObject("{\"cartItems\":[]}");
|
| 439 |
}else {
|
439 |
}else {
|
| 440 |
jsonObj = new JSONObject(cartMap);
|
440 |
jsonObj = new JSONObject(cartMap);
|
| 441 |
}
|
441 |
}
|
| 442 |
try{
|
442 |
try{
|
| 443 |
List<ItemQuantity> itemQuantities = new ArrayList<ItemQuantity>();
|
443 |
List<ItemQuantity> itemQuantities = new ArrayList<ItemQuantity>();
|
| 444 |
JSONArray cartItems = jsonObj.getJSONArray("cartItems");
|
444 |
JSONArray cartItems = jsonObj.getJSONArray("cartItems");
|
| 445 |
List<Integer> itemsList = new ArrayList<Integer>();
|
445 |
List<Integer> itemsList = new ArrayList<Integer>();
|
| 446 |
for (int i=0; i< cartItems.length(); i++) {
|
446 |
for (int i=0; i< cartItems.length(); i++) {
|
| 447 |
JSONObject obj = cartItems.getJSONObject(i);
|
447 |
JSONObject obj = cartItems.getJSONObject(i);
|
| 448 |
//Just to avoid garage values
|
448 |
//Just to avoid garage values
|
| 449 |
int itemId = 0;
|
449 |
int itemId = 0;
|
| 450 |
try{
|
450 |
try{
|
| 451 |
itemId = obj.getInt("itemId");
|
451 |
itemId = obj.getInt("itemId");
|
| 452 |
} catch(Exception e){
|
452 |
} catch(Exception e){
|
| 453 |
continue;
|
453 |
continue;
|
| 454 |
}
|
454 |
}
|
| 455 |
Integer quantity = obj.getInt("quantity");
|
455 |
Integer quantity = obj.getInt("quantity");
|
| 456 |
ItemQuantity iq = new ItemQuantity(itemId, quantity);
|
456 |
ItemQuantity iq = new ItemQuantity(itemId, quantity);
|
| 457 |
itemQuantities.add(iq);
|
457 |
itemQuantities.add(iq);
|
| 458 |
itemsList.add(itemId);
|
458 |
itemsList.add(itemId);
|
| 459 |
}
|
459 |
}
|
| 460 |
userClient = getClient();
|
460 |
userClient = getClient();
|
| 461 |
if (itemsList.size()==0 || userClient.addItemsToCart(id, itemQuantities, jsonObj.has("couponCode")? jsonObj.getString("couponCode"):null)){
|
461 |
if (itemsList.size()==0 || userClient.addItemsToCart(id, itemQuantities, jsonObj.has("couponCode")? jsonObj.getString("couponCode"):null)){
|
| 462 |
log.info("Items added to cart Successfully");
|
462 |
log.info("Items added to cart Successfully");
|
| 463 |
//Now validate cart and provide appropriate response.
|
463 |
//Now validate cart and provide appropriate response.
|
| 464 |
String cartString = userClient.validateCartNew(id, userinfo.getPincode(), -1);
|
464 |
String cartString = userClient.validateCartNew(id, userinfo.getPincode(), -1);
|
| 465 |
JSONObject cartObj = new JSONObject(cartString);
|
465 |
JSONObject cartObj = new JSONObject(cartString);
|
| 466 |
JSONArray arr = cartObj.getJSONArray("cartItems");
|
466 |
JSONArray arr = cartObj.getJSONArray("cartItems");
|
| 467 |
int maxEstimate=-1;
|
467 |
int maxEstimate=-1;
|
| 468 |
boolean allSame=true;
|
468 |
boolean allSame=true;
|
| 469 |
int removedCount = 0;
|
469 |
int removedCount = 0;
|
| 470 |
cartObj.put("cartMessagesMerged", 0);
|
470 |
cartObj.put("cartMessagesMerged", 0);
|
| 471 |
for (int j=0; j<arr.length(); j++){
|
471 |
for (int j=0; j<arr.length(); j++){
|
| 472 |
JSONObject itemObj = arr.getJSONObject(j-removedCount);
|
472 |
JSONObject itemObj = arr.getJSONObject(j-removedCount);
|
| 473 |
if(!itemsList.contains(itemObj.getInt("itemId"))){
|
473 |
if(!itemsList.contains(itemObj.getInt("itemId"))){
|
| 474 |
if(itemObj.getInt("quantity")==0){
|
474 |
if(itemObj.getInt("quantity")==0){
|
| 475 |
arr.remove(j-removedCount);
|
475 |
arr.remove(j-removedCount);
|
| 476 |
removedCount++;
|
476 |
removedCount++;
|
| 477 |
if (itemObj.getInt("estimate")==-1){
|
477 |
if (itemObj.getInt("estimate")==-1){
|
| 478 |
cartObj.put("cartMessageUndeliverable", cartObj.getInt("cartMessageUndeliverable") - 1);
|
478 |
cartObj.put("cartMessageUndeliverable", cartObj.getInt("cartMessageUndeliverable") - 1);
|
| 479 |
} else {
|
- |
|
| 480 |
cartObj.put("cartMessageOOS", cartObj.getInt("cartMessageOOS") - 1);
|
- |
|
| 481 |
}
|
- |
|
| 482 |
continue;
|
- |
|
| 483 |
} else {
|
- |
|
| 484 |
JSONArray messagesArray = new JSONArray();
|
- |
|
| 485 |
JSONObject message=new JSONObject();
|
- |
|
| 486 |
message.put("type","info");
|
- |
|
| 487 |
message.put("messageText","Added from earlier cart");
|
- |
|
| 488 |
messagesArray.put(message);
|
- |
|
| 489 |
itemObj.put("cartItemMessages", messagesArray);
|
- |
|
| 490 |
cartObj.put("cartMessagesMerged", cartObj.getInt("cartMessagesMerged") + 1);
|
- |
|
| 491 |
}
|
- |
|
| 492 |
}
|
- |
|
| 493 |
ProductPojo pp = PojoPopulator.getShortContent(itemObj.getLong("catalogItemId"));
|
- |
|
| 494 |
if(allSame){
|
- |
|
| 495 |
allSame = maxEstimate==-1 || maxEstimate==itemObj.getInt("estimate");
|
- |
|
| 496 |
}
|
- |
|
| 497 |
if(itemObj.getInt("estimate")>maxEstimate){
|
- |
|
| 498 |
maxEstimate = itemObj.getInt("estimate");
|
- |
|
| 499 |
}
|
- |
|
| 500 |
itemObj.put("imageUrl", pp.getImageUrl());
|
- |
|
| 501 |
itemObj.put("title", pp.getTitle());
|
- |
|
| 502 |
}
|
- |
|
| 503 |
|
- |
|
| 504 |
ArrayList<String> listdata = new ArrayList<String>();
|
- |
|
| 505 |
if (arr != null) {
|
- |
|
| 506 |
for (int i=0;i<arr.length();i++){
|
- |
|
| 507 |
JSONArray itemMessages = arr.getJSONObject(i).optJSONArray(("cartItemMessages"));
|
- |
|
| 508 |
if(itemMessages!=null && itemMessages.length()>0){
|
- |
|
| 509 |
listdata.add(arr.get(i).toString());
|
- |
|
| 510 |
} else {
|
- |
|
| 511 |
listdata.add(0,arr.get(i).toString());
|
- |
|
| 512 |
}
|
- |
|
| 513 |
}
|
- |
|
| 514 |
}
|
- |
|
| 515 |
arr = new JSONArray(listdata);
|
- |
|
| 516 |
cartObj.put("cartItems", arr);
|
- |
|
| 517 |
JSONArray cartMessagesArray = new JSONArray();
|
- |
|
| 518 |
for (String message :Arrays.asList("cartMessageOOS", "cartMessageUndeliverable", "cartMessageChanged","cartMessagesMerged")){
|
- |
|
| 519 |
int count = cartObj.getInt(message);
|
- |
|
| 520 |
if (count>0) {
|
- |
|
| 521 |
String type="danger";
|
- |
|
| 522 |
JSONObject cartMessage=new JSONObject();
|
- |
|
| 523 |
if (message.equals("cartMessagesMerged")) {
|
- |
|
| 524 |
type = "info";
|
- |
|
| 525 |
if (count==1){
|
- |
|
| 526 |
cartMessage.put("messageText","1 item is added from earlier cart");
|
- |
|
| 527 |
}else {
|
- |
|
| 528 |
cartMessage.put("messageText","Few items are added from earlier cart");
|
- |
|
| 529 |
}
|
- |
|
| 530 |
} else if (message.equals("cartMessageOOS")) {
|
- |
|
| 531 |
if (count==1){
|
- |
|
| 532 |
cartMessage.put("messageText","One item is currently Out of Stock");
|
- |
|
| 533 |
}else {
|
- |
|
| 534 |
cartMessage.put("messageText","Few items are currently Out of Stock");
|
- |
|
| 535 |
}
|
- |
|
| 536 |
} else if (message.equals("cartMessageUndeliverable")) {
|
- |
|
| 537 |
if (count==1){
|
- |
|
| 538 |
cartMessage.put("messageText","One item is undeliverable");
|
- |
|
| 539 |
}else {
|
- |
|
| 540 |
cartMessage.put("messageText","Few items are underiverable");
|
- |
|
| 541 |
}
|
- |
|
| 542 |
} else {
|
479 |
} else {
|
| 543 |
if (count==1){
|
- |
|
| 544 |
cartMessage.put("messageText","One item qty has changed");
|
480 |
cartObj.put("cartMessageOOS", cartObj.getInt("cartMessageOOS") - 1);
|
| 545 |
}else {
|
- |
|
| 546 |
cartMessage.put("messageText","Few items qty have changed");
|
- |
|
| 547 |
}
|
- |
|
| 548 |
}
|
481 |
}
|
| - |
|
482 |
continue;
|
| - |
|
483 |
} else {
|
| - |
|
484 |
JSONArray messagesArray = new JSONArray();
|
| - |
|
485 |
JSONObject message=new JSONObject();
|
| 549 |
cartMessage.put("type",type);
|
486 |
message.put("type","info");
|
| - |
|
487 |
message.put("messageText","Added from earlier cart");
|
| 550 |
cartMessagesArray.put(cartMessage);
|
488 |
messagesArray.put(message);
|
| - |
|
489 |
itemObj.put("cartItemMessages", messagesArray);
|
| - |
|
490 |
cartObj.put("cartMessagesMerged", cartObj.getInt("cartMessagesMerged") + 1);
|
| 551 |
}
|
491 |
}
|
| - |
|
492 |
}
|
| - |
|
493 |
ProductPojo pp = PojoPopulator.getShortContent(itemObj.getLong("catalogItemId"));
|
| - |
|
494 |
if(allSame){
|
| - |
|
495 |
allSame = maxEstimate==-1 || maxEstimate==itemObj.getInt("estimate");
|
| - |
|
496 |
}
|
| - |
|
497 |
if(itemObj.getInt("estimate")>maxEstimate){
|
| - |
|
498 |
maxEstimate = itemObj.getInt("estimate");
|
| 552 |
}
|
499 |
}
|
| - |
|
500 |
itemObj.put("imageUrl", pp.getImageUrl());
|
| - |
|
501 |
itemObj.put("title", pp.getTitle());
|
| - |
|
502 |
}
|
| - |
|
503 |
|
| - |
|
504 |
ArrayList<String> listdata = new ArrayList<String>();
|
| - |
|
505 |
if (arr != null) {
|
| - |
|
506 |
for (int i=0;i<arr.length();i++){
|
| - |
|
507 |
JSONArray itemMessages = arr.getJSONObject(i).optJSONArray(("cartItemMessages"));
|
| - |
|
508 |
if(itemMessages!=null && itemMessages.length()>0){
|
| - |
|
509 |
listdata.add(arr.get(i).toString());
|
| - |
|
510 |
} else {
|
| - |
|
511 |
listdata.add(0,arr.get(i).toString());
|
| - |
|
512 |
}
|
| - |
|
513 |
}
|
| - |
|
514 |
}
|
| - |
|
515 |
arr = new JSONArray();
|
| - |
|
516 |
for(int i=0;i<listdata.size();i++){
|
| - |
|
517 |
arr.put(listdata.get(i));
|
| - |
|
518 |
}
|
| 553 |
cartObj.put("cartMessages", cartMessagesArray);
|
519 |
cartObj.put("cartItems", arr);
|
| - |
|
520 |
JSONArray cartMessagesArray = new JSONArray();
|
| - |
|
521 |
for (String message :Arrays.asList("cartMessageOOS", "cartMessageUndeliverable", "cartMessageChanged","cartMessagesMerged")){
|
| - |
|
522 |
int count = cartObj.getInt(message);
|
| - |
|
523 |
if (count>0) {
|
| - |
|
524 |
String type="danger";
|
| - |
|
525 |
JSONObject cartMessage=new JSONObject();
|
| - |
|
526 |
if (message.equals("cartMessagesMerged")) {
|
| - |
|
527 |
type = "info";
|
| - |
|
528 |
if (count==1){
|
| - |
|
529 |
cartMessage.put("messageText","1 item is added from earlier cart");
|
| - |
|
530 |
}else {
|
| - |
|
531 |
cartMessage.put("messageText","Few items are added from earlier cart");
|
| - |
|
532 |
}
|
| - |
|
533 |
} else if (message.equals("cartMessageOOS")) {
|
| - |
|
534 |
if (count==1){
|
| - |
|
535 |
cartMessage.put("messageText","One item is currently Out of Stock");
|
| - |
|
536 |
}else {
|
| - |
|
537 |
cartMessage.put("messageText","Few items are currently Out of Stock");
|
| 554 |
|
538 |
}
|
| - |
|
539 |
} else if (message.equals("cartMessageUndeliverable")) {
|
| 555 |
if (maxEstimate==-1){
|
540 |
if (count==1){
|
| 556 |
cartObj.put("estimateString", "Can't ship here");
|
541 |
cartMessage.put("messageText","One item is undeliverable");
|
| - |
|
542 |
}else {
|
| - |
|
543 |
cartMessage.put("messageText","Few items are underiverable");
|
| - |
|
544 |
}
|
| 557 |
} else {
|
545 |
} else {
|
| 558 |
if(itemsList.size() == 0){
|
546 |
if (count==1){
|
| - |
|
547 |
cartMessage.put("messageText","One item qty has changed");
|
| - |
|
548 |
}else {
|
| 559 |
cartObj.put("cod", getClient().showCODOption(id, 0, userinfo.getPincode()));
|
549 |
cartMessage.put("messageText","Few items qty have changed");
|
| - |
|
550 |
}
|
| 560 |
}
|
551 |
}
|
| - |
|
552 |
cartMessage.put("type",type);
|
| 561 |
cartObj.put("estimateString", LogisticsService.getDeliveryDateString(maxEstimate,DeliveryType.COD));
|
553 |
cartMessagesArray.put(cartMessage);
|
| 562 |
}
|
554 |
}
|
| - |
|
555 |
}
|
| 563 |
cartObj.put("maxEstimate", maxEstimate);
|
556 |
cartObj.put("cartMessages", cartMessagesArray);
|
| - |
|
557 |
|
| - |
|
558 |
if (maxEstimate==-1){
|
| 564 |
cartPojoJson = cartObj.toString();
|
559 |
cartObj.put("estimateString", "Can't ship here");
|
| 565 |
} else {
|
560 |
} else {
|
| - |
|
561 |
if(itemsList.size() == 0){
|
| - |
|
562 |
cartObj.put("cod", getClient().showCODOption(id, 0, userinfo.getPincode()));
|
| - |
|
563 |
}
|
| 566 |
cartPojoJson = "{\"response\":\"error\", \"message\":\"Problem occurred while updating cart\"}";
|
564 |
cartObj.put("estimateString", LogisticsService.getDeliveryDateString(maxEstimate,DeliveryType.COD));
|
| 567 |
}
|
565 |
}
|
| - |
|
566 |
cartObj.put("maxEstimate", maxEstimate);
|
| - |
|
567 |
cartPojoJson = cartObj.toString();
|
| - |
|
568 |
} else {
|
| - |
|
569 |
cartPojoJson = "{\"response\":\"error\", \"message\":\"Problem occurred while updating cart\"}";
|
| - |
|
570 |
}
|
| 568 |
}catch(Exception e) {
|
571 |
}catch(Exception e) {
|
| 569 |
cartPojoJson = "{\"response\":\"error\", \"message\":\"Something went wrong while validation\"}";
|
572 |
cartPojoJson = "{\"response\":\"error\", \"message\":\"Something went wrong while validation\"}";
|
| 570 |
e.printStackTrace();
|
573 |
e.printStackTrace();
|
| 571 |
}
|
574 |
}
|
| 572 |
}
|
575 |
}
|
| 573 |
setResultJson(cartPojoJson);
|
576 |
setResultJson(cartPojoJson);
|
| 574 |
return "index";
|
577 |
return "index";
|
| 575 |
}
|
578 |
}
|
| 576 |
|
579 |
|