Skip to content
This repository has been archived by the owner on May 9, 2020. It is now read-only.

Commit

Permalink
"Applied fix from trunk for revision: 1616940"
Browse files Browse the repository at this point in the history
------------------------------------------------------------------------
r1616940 | jleroux | 2014-08-09 15:05:05 +0200 (sam. 09 août 2014) | 2 lignes

The updateApprovedOrderItems defines the itemAttributesMap, itemEstimatedDeliveryDateMap and itemEstimatedShipDateMap IN parameters as optional. But if you use this service out of an event context then you cross NPEs
This is because in the context of an event the missing parameters are initialised as empty. This hid the issue so far because updateApprovedOrderItems is only used once, called by an event.
------------------------------------------------------------------------
?\026

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/branches/release11.04@1616947 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
JacquesLeRoux committed Aug 9, 2014
1 parent 3065162 commit aa1dcbb
Showing 1 changed file with 81 additions and 80 deletions.
161 changes: 81 additions & 80 deletions applications/order/src/org/ofbiz/order/order/OrderServices.java
Expand Up @@ -3556,42 +3556,32 @@ public static Map<String, Object> updateApprovedOrderItems(DispatchContext dctx,
"OrderShoppingCartEmpty", locale));
}

// 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<String, BigDecimal> itemTotals = new HashMap<String, BigDecimal>();
Iterator<String> i = itemQtyMap.keySet().iterator();
while (i.hasNext()) {
String key = i.next();
String quantityStr = itemQtyMap.get(key);
BigDecimal groupQty = BigDecimal.ZERO;
try {
groupQty = (BigDecimal) ObjectType.simpleTypeConvert(quantityStr, "BigDecimal", null, locale);
} catch (GeneralException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}

if (groupQty.compareTo(BigDecimal.ONE) < 0) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource,
"OrderItemQtyMustBePositive", locale));
}

String[] itemInfo = key.split(":");
BigDecimal tally = itemTotals.get(itemInfo[0]);
if (tally == null) {
tally = groupQty;
} else {
tally = tally.add(groupQty);
for (String key : itemQtyMap.keySet()) {
String quantityStr = itemQtyMap.get(key);
BigDecimal groupQty = BigDecimal.ZERO;
try {
groupQty = (BigDecimal) ObjectType.simpleTypeConvert(quantityStr, "BigDecimal", null, locale);
} catch (GeneralException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}

if (groupQty.compareTo(BigDecimal.ONE) < 0) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource,
"OrderItemQtyMustBePositive", locale));
}

String[] itemInfo = key.split(":");
BigDecimal tally = itemTotals.get(itemInfo[0]);
if (tally == null) {
tally = groupQty;
} else {
tally = tally.add(groupQty);
}
itemTotals.put(itemInfo[0], tally);
}
itemTotals.put(itemInfo[0], tally);
}

// set the items amount/price
Iterator<String> iai = itemTotals.keySet().iterator();
Expand Down Expand Up @@ -3647,6 +3637,14 @@ public static Map<String, Object> updateApprovedOrderItems(DispatchContext dctx,

// update the order item attributes
if (itemAttributesMap != null) {
// 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]);
}

String attrValue = null;
for (String attrName : attributeNames) {
attrValue = itemAttributesMap.get(attrName + ":" + itemSeqId);
Expand All @@ -3662,66 +3660,69 @@ public static Map<String, Object> updateApprovedOrderItems(DispatchContext dctx,
}
}
// Create Estimated Delivery dates
for (Map.Entry<String, String> entry : itemEstimatedDeliveryDateMap.entrySet()) {
String itemSeqId = entry.getKey();

// ignore internationalised variant of dates
if (!itemSeqId.endsWith("_i18n")) {
String estimatedDeliveryDate = entry.getValue();
if (UtilValidate.isNotEmpty(estimatedDeliveryDate)) {
Timestamp deliveryDate = Timestamp.valueOf(estimatedDeliveryDate);
ShoppingCartItem cartItem = cart.findCartItem(itemSeqId);
cartItem.setDesiredDeliveryDate(deliveryDate);
if (null != itemEstimatedDeliveryDateMap) {
for (Map.Entry<String, String> entry : itemEstimatedDeliveryDateMap.entrySet()) {
String itemSeqId = entry.getKey();

// ignore internationalised variant of dates
if (!itemSeqId.endsWith("_i18n")) {
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();

// ignore internationalised variant of dates
if (!itemSeqId.endsWith("_i18n")) {
String estimatedShipDate = entry.getValue();
if (UtilValidate.isNotEmpty(estimatedShipDate)) {
Timestamp shipDate = Timestamp.valueOf(estimatedShipDate);
ShoppingCartItem cartItem = cart.findCartItem(itemSeqId);
cartItem.setEstimatedShipDate(shipDate);
if (null != itemEstimatedShipDateMap) {
for (Map.Entry<String, String> entry : itemEstimatedShipDateMap.entrySet()) {
String itemSeqId = entry.getKey();

// ignore internationalised variant of dates
if (!itemSeqId.endsWith("_i18n")) {
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<String> gai = itemQtyMap.keySet().iterator();
while (gai.hasNext()) {
String key = gai.next();
String quantityStr = itemQtyMap.get(key);
BigDecimal groupQty = BigDecimal.ZERO;
try {
groupQty = (BigDecimal) ObjectType.simpleTypeConvert(quantityStr, "BigDecimal", null, locale);
} catch (GeneralException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
for (String key : itemQtyMap.keySet()) {
String quantityStr = itemQtyMap.get(key);
BigDecimal groupQty = BigDecimal.ZERO;
try {
groupQty = (BigDecimal) ObjectType.simpleTypeConvert(quantityStr, "BigDecimal", null, locale);
} catch (GeneralException 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());
}
String[] itemInfo = key.split(":");
@SuppressWarnings("unused")
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) {
// 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 {
Expand Down Expand Up @@ -3988,7 +3989,7 @@ private static void saveUpdatedCartToOrder(LocalDispatcher dispatcher, Delegator
cart.setItemShipGroupEstimate(shippingTotal, gi);
}

// calc the sales tax
// calc the sales tax
CheckOutHelper coh = new CheckOutHelper(dispatcher, delegator, cart);
if (calcTax) {
try {
Expand Down Expand Up @@ -4038,7 +4039,7 @@ private static void saveUpdatedCartToOrder(LocalDispatcher dispatcher, Delegator
for (GenericValue stored: toStore) {
if ("OrderAdjustment".equals(stored.getEntityName())) {
if (("SHIPPING_CHARGES".equals(stored.get("orderAdjustmentTypeId")) ||
"SALES_TAX".equals(stored.get("orderAdjustmentTypeId"))) &&
"SALES_TAX".equals(stored.get("orderAdjustmentTypeId"))) &&
stored.get("orderId").equals(orderId) &&
stored.get("shipGroupSeqId").equals(shipGroupSeqId)) {
// Removing objects from toStore list for old Shipping and Handling Charges Adjustment and Sales Tax Adjustment.
Expand Down Expand Up @@ -4094,10 +4095,10 @@ private static void saveUpdatedCartToOrder(LocalDispatcher dispatcher, Delegator

// get the promo uses and codes
for (String promoCodeEntered : cart.getProductPromoCodesEntered()) {
GenericValue orderProductPromoCode = delegator.makeValue("OrderProductPromoCode");
GenericValue orderProductPromoCode = delegator.makeValue("OrderProductPromoCode");
orderProductPromoCode.set("orderId", orderId);
orderProductPromoCode.set("productPromoCodeId", promoCodeEntered);
toStore.add(orderProductPromoCode);
toStore.add(orderProductPromoCode);
}
for (GenericValue promoUse : cart.makeProductPromoUses()) {
promoUse.set("orderId", orderId);
Expand Down

0 comments on commit aa1dcbb

Please sign in to comment.