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