Map itemAttributesMap = (Map) context.get("itemAttributesMap");
Map<String,String> itemEstimatedShipDateMap = (Map) context.get("itemShipDateMap");
Map<String, String> itemEstimatedDeliveryDateMap = (Map) context.get("itemDeliveryDateMap");
// obtain a shopping cart object for updating
ShoppingCart cart = null;
try {
cart = loadCartForUpdate(dispatcher, delegator, userLogin, orderId);
} catch (GeneralException e) {
return ServiceUtil.returnError(e.getMessage());
}
if (cart == null) {
return ServiceUtil.returnError("ERROR: Null shopping cart object returned!");
}
// go through the item attributes map once to get a list of key names
Set<String> attributeNames =FastSet.newInstance();
Set<String> keys = itemAttributesMap.keySet();
for (String key : keys) {
String[] attributeInfo = key.split(":");
attributeNames.add(attributeInfo[0]);
}
// go through the item map and obtain the totals per item
Map itemTotals = new HashMap();
Iterator i = itemQtyMap.keySet().iterator();
while (i.hasNext()) {
String key = (String) i.next();
String quantityStr = (String) itemQtyMap.get(key);
BigDecimal groupQty = BigDecimal.ZERO;
try {
groupQty = new BigDecimal(quantityStr);
} catch (NumberFormatException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
if (groupQty.compareTo(BigDecimal.ZERO) == 0) {
return ServiceUtil.returnError("Quantity must be >0, use cancel item to cancel completely!");
}
String[] itemInfo = key.split(":");
BigDecimal tally = (BigDecimal) itemTotals.get(itemInfo[0]);
if (tally == null) {
tally = groupQty;
} else {
tally = tally.add(groupQty);
}
itemTotals.put(itemInfo[0], tally);
}
// set the items amount/price
Iterator iai = itemTotals.keySet().iterator();
while (iai.hasNext()) {
String itemSeqId = (String) iai.next();
ShoppingCartItem cartItem = cart.findCartItem(itemSeqId);
if (cartItem != null) {
BigDecimal qty = (BigDecimal) itemTotals.get(itemSeqId);
BigDecimal priceSave = cartItem.getBasePrice();
// set quantity
try {
cartItem.setQuantity(qty, dispatcher, cart, false, false); // trigger external ops, don't reset ship groups (and update prices for both PO and SO items)
} catch (CartItemModifyException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
Debug.logInfo("Set item quantity: [" + itemSeqId + "] " + qty, module);
if (cartItem.getIsModifiedPrice()) // set price
cartItem.setBasePrice(priceSave);
if (overridePriceMap.containsKey(itemSeqId)) {
String priceStr = (String) itemPriceMap.get(itemSeqId);
if (UtilValidate.isNotEmpty(priceStr)) {
BigDecimal price = new BigDecimal("-1");
price = new BigDecimal(priceStr).setScale(orderDecimals, orderRounding);
cartItem.setBasePrice(price);
cartItem.setIsModifiedPrice(true);
Debug.logInfo("Set item price: [" + itemSeqId + "] " + price, module);
}
}
// Update the item description
if (itemDescriptionMap != null && itemDescriptionMap.containsKey(itemSeqId)) {
String description = (String) itemDescriptionMap.get(itemSeqId);
if (UtilValidate.isNotEmpty(description)) {
cartItem.setName(description);
Debug.logInfo("Set item description: [" + itemSeqId + "] " + description, module);
} else {
return ServiceUtil.returnError("Item description must not be empty");
}
}
// update the order item attributes
if (itemAttributesMap != null) {
String attrValue = null;
for (String attrName : attributeNames) {
attrValue = (String) itemAttributesMap.get(attrName + ":" + itemSeqId);
if (UtilValidate.isNotEmpty(attrName)) {
cartItem.setOrderItemAttribute(attrName, attrValue);
Debug.logInfo("Set item attribute Name: [" + itemSeqId + "] " + attrName + " , Value:" + attrValue, module);
}
}
}
} else {
Debug.logInfo("Unable to locate shopping cart item for seqId #" + itemSeqId, module);
}
}
// Create Estimated Delivery dates
for (Map.Entry<String, String> entry : itemEstimatedDeliveryDateMap.entrySet()) {
String itemSeqId = entry.getKey();
String estimatedDeliveryDate = entry.getValue();
if (UtilValidate.isNotEmpty(estimatedDeliveryDate)) {
Timestamp deliveryDate = Timestamp.valueOf(estimatedDeliveryDate);
ShoppingCartItem cartItem = cart.findCartItem(itemSeqId);
cartItem.setDesiredDeliveryDate(deliveryDate);
}
}
// Create Estimated ship dates
for (Map.Entry<String, String> entry : itemEstimatedShipDateMap.entrySet()) {
String itemSeqId = entry.getKey();
String estimatedShipDate = entry.getValue();
if (UtilValidate.isNotEmpty(estimatedShipDate)) {
Timestamp shipDate = Timestamp.valueOf(estimatedShipDate);
ShoppingCartItem cartItem = cart.findCartItem(itemSeqId);
cartItem.setEstimatedShipDate(shipDate);
}
}
// update the group amounts
Iterator gai = itemQtyMap.keySet().iterator();
while (gai.hasNext()) {
String key = (String) gai.next();
String quantityStr = (String) itemQtyMap.get(key);
BigDecimal groupQty = BigDecimal.ZERO;
try {
groupQty = new BigDecimal(quantityStr);
} catch (NumberFormatException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
String[] itemInfo = key.split(":");
int groupIdx = -1;
try {
groupIdx = Integer.parseInt(itemInfo[1]);
} catch (NumberFormatException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
// set the group qty
ShoppingCartItem cartItem = cart.findCartItem(itemInfo[0]);
if (cartItem != null) {
Debug.logInfo("Shipping info (before) for group #" + (groupIdx-1) + " [" + cart.getShipmentMethodTypeId(groupIdx-1) + " / " + cart.getCarrierPartyId(groupIdx-1) + "]", module);
cart.setItemShipGroupQty(cartItem, groupQty, groupIdx - 1);
Debug.logInfo("Set ship group qty: [" + itemInfo[0] + " / " + itemInfo[1] + " (" + (groupIdx-1) + ")] " + groupQty, module);
Debug.logInfo("Shipping info (after) for group #" + (groupIdx-1) + " [" + cart.getShipmentMethodTypeId(groupIdx-1) + " / " + cart.getCarrierPartyId(groupIdx-1) + "]", module);
}
}
// save all the updated information
try {