Author: ashish
Date: Fri Aug 27 12:39:13 2010 New Revision: 990130 URL: http://svn.apache.org/viewvc?rev=990130&view=rev Log: Applied fix from trunk for revision: 990127 === Applied patch from jira issue OFBIZ-3904 - Bug in calling methods defined in Usps Services. Calling "getPackageSplit" & "calcPackageWeight" methods from outside of the class causes compilation error, as their access modifiers are private. So fixing this bug, following points are covered in this commit. 1) Moving "getPackageSplit" and "calcPackageWeight" from UspsServices.java to ShipmentWorker.java. They are not Usps third party related methods. 2) Changing access modifiers of these methods to public. Modified: ofbiz/branches/release10.04/ (props changed) ofbiz/branches/release10.04/applications/product/src/org/ofbiz/shipment/shipment/ShipmentWorker.java ofbiz/branches/release10.04/applications/product/src/org/ofbiz/shipment/thirdparty/usps/UspsServices.java Propchange: ofbiz/branches/release10.04/ ------------------------------------------------------------------------------ --- svn:mergeinfo (original) +++ svn:mergeinfo Fri Aug 27 12:39:13 2010 @@ -1,3 +1,3 @@ /ofbiz/branches/addbirt:831210-885099,885686-886087 /ofbiz/branches/multitenant20100310:921280-927264 -/ofbiz/trunk:939988,939990,939999,940025,940053,940234,940248,940309,940401,940410,940425,940779,940815,940849,940851,941007,941047,941109,941177,941199,941261,941440,941600,941999,942084,942406,942414,942671,942883-942884,943168,943271-943272,944614,944621,944623,944647,944669,944797,944895,945010,945018,945026,945118,945573,945578,945580,945582,945610,945619,945848,945852,945857,946061,946066,946073,946075,946080,946309,946313,946320,946322,946596,947004-947005,947392,947424,947679,947988,948017,948694,949174,949710,949844,950866,950870,950893,951005,951062,951098,951251,951367,951381,951672,952232,952249,952270,953294,953671,954135,954583,954733,954956,955568,956022,956206,956340,957160,958343,958514,958521,958752,958758,958769,958953,959456,960143,960491,960997,963610,964558,965470,965916,966525,966785,967098,978806,978893,978939,979104,980641-980642,980935,981051,981104,981123,981288,983920,983930,985163,985473,985718,985856,985902,987841,989166,989389 +/ofbiz/trunk:939988,939990,939999,940025,940053,940234,940248,940309,940401,940410,940425,940779,940815,940849,940851,941007,941047,941109,941177,941199,941261,941440,941600,941999,942084,942406,942414,942671,942883-942884,943168,943271-943272,944614,944621,944623,944647,944669,944797,944895,945010,945018,945026,945118,945573,945578,945580,945582,945610,945619,945848,945852,945857,946061,946066,946073,946075,946080,946309,946313,946320,946322,946596,947004-947005,947392,947424,947679,947988,948017,948694,949174,949710,949844,950866,950870,950893,951005,951062,951098,951251,951367,951381,951672,952232,952249,952270,953294,953671,954135,954583,954733,954956,955568,956022,956206,956340,957160,958343,958514,958521,958752,958758,958769,958953,959456,960143,960491,960997,963610,964558,965470,965916,966525,966785,967098,978806,978893,978939,979104,980641-980642,980935,981051,981104,981123,981288,983920,983930,985163,985473,985718,985856,985902,987841,989166,989389,990127 Modified: ofbiz/branches/release10.04/applications/product/src/org/ofbiz/shipment/shipment/ShipmentWorker.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release10.04/applications/product/src/org/ofbiz/shipment/shipment/ShipmentWorker.java?rev=990130&r1=990129&r2=990130&view=diff ============================================================================== --- ofbiz/branches/release10.04/applications/product/src/org/ofbiz/shipment/shipment/ShipmentWorker.java (original) +++ ofbiz/branches/release10.04/applications/product/src/org/ofbiz/shipment/shipment/ShipmentWorker.java Fri Aug 27 12:39:13 2010 @@ -19,12 +19,23 @@ package org.ofbiz.shipment.shipment; import java.math.BigDecimal; +import java.math.MathContext; import java.util.List; +import java.util.Map; + +import javolution.util.FastList; +import javolution.util.FastMap; import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.UtilMisc; +import org.ofbiz.base.util.UtilProperties; import org.ofbiz.base.util.UtilValidate; import org.ofbiz.entity.GenericEntityException; import org.ofbiz.entity.GenericValue; +import org.ofbiz.service.DispatchContext; +import org.ofbiz.service.GenericServiceException; +import org.ofbiz.service.LocalDispatcher; +import org.ofbiz.service.ModelService; /** * ShipmentWorker - Worker methods for Shipment and related entities @@ -32,7 +43,7 @@ import org.ofbiz.entity.GenericValue; public class ShipmentWorker { public static final String module = ShipmentWorker.class.getName(); - + public static final MathContext generalRounding = new MathContext(10); /* * Returns the value of a given ShipmentPackageContent record. Calculated by working out the total value (from the OrderItems) of all ItemIssuances * for the ShipmentItem then dividing that by the total quantity issued for the same to get an average item value then multiplying that by the package @@ -91,5 +102,121 @@ public class ShipmentWorker { value = totalValue.divide(totalIssued, 10, BigDecimal.ROUND_HALF_EVEN).multiply(quantity); return value; } + + public static List<Map<String, BigDecimal>> getPackageSplit(DispatchContext dctx, List<Map<String, Object>> shippableItemInfo, BigDecimal maxWeight) { + // create the package list w/ the first package + List<Map<String, BigDecimal>> packages = FastList.newInstance(); + + if (shippableItemInfo != null) { + for (Map<String, Object> itemInfo: shippableItemInfo) { + long pieces = ((Long) itemInfo.get("piecesIncluded")).longValue(); + BigDecimal totalQuantity = (BigDecimal) itemInfo.get("quantity"); + BigDecimal totalWeight = (BigDecimal) itemInfo.get("weight"); + String productId = (String) itemInfo.get("productId"); + + // sanity check + if (pieces < 1) { + pieces = 1; // can NEVER be less than one + } + BigDecimal weight = totalWeight.divide(BigDecimal.valueOf(pieces), generalRounding); + + for (int z = 1; z <= totalQuantity.intValue(); z++) { + BigDecimal partialQty = pieces > 1 ? BigDecimal.ONE.divide(BigDecimal.valueOf(pieces), generalRounding) : BigDecimal.ONE; + for (long x = 0; x < pieces; x++) { + if (weight.compareTo(maxWeight) >= 0) { + Map<String, BigDecimal> newPackage = FastMap.newInstance(); + newPackage.put(productId, partialQty); + packages.add(newPackage); + } else if (totalWeight.compareTo(BigDecimal.ZERO) > 0) { + // create the first package + if (packages.size() == 0) { + packages.add(FastMap.<String, BigDecimal>newInstance()); + } + + // package loop + boolean addedToPackage = false; + for (Map<String, BigDecimal> packageMap: packages) { + if (!addedToPackage) { + BigDecimal packageWeight = calcPackageWeight(dctx, packageMap, shippableItemInfo, weight); + if (packageWeight.compareTo(maxWeight) <= 0) { + BigDecimal qty = (BigDecimal) packageMap.get(productId); + qty = qty == null ? BigDecimal.ZERO : qty; + packageMap.put(productId, qty.add(partialQty)); + addedToPackage = true; + } + } + } + if (!addedToPackage) { + Map<String, BigDecimal> packageMap = FastMap.newInstance(); + packageMap.put(productId, partialQty); + packages.add(packageMap); + } + } + } + } + } + } + return packages; + } + + public static BigDecimal calcPackageWeight(DispatchContext dctx, Map<String, BigDecimal> packageMap, List<Map<String, Object>> shippableItemInfo, BigDecimal additionalWeight) { + + LocalDispatcher dispatcher = dctx.getDispatcher(); + BigDecimal totalWeight = BigDecimal.ZERO; + String defaultWeightUomId = UtilProperties.getPropertyValue("shipment.properties", "shipment.default.weight.uom"); + if (UtilValidate.isEmpty(defaultWeightUomId)) { + Debug.logWarning("No shipment.default.weight.uom set in shipment.properties, setting it to WT_oz for USPS", module); + defaultWeightUomId = "WT_oz"; + } + + for (Map.Entry<String, BigDecimal> entry: packageMap.entrySet()) { + String productId = entry.getKey(); + Map<String, Object> productInfo = getProductItemInfo(shippableItemInfo, productId); + BigDecimal productWeight = (BigDecimal) productInfo.get("weight"); + BigDecimal quantity = (BigDecimal) packageMap.get(productId); + + // DLK - I'm not sure if this line is working. shipment_package seems to leave this value null so??? + String weightUomId = (String) productInfo.get("weight_uom_id"); + + Debug.logInfo("Product Id : " + productId.toString() + " Product Weight : " + String.valueOf(productWeight) + " Product UomId : " + weightUomId + " assuming " + defaultWeightUomId + " if null. Quantity : " + String.valueOf(quantity), module); + + if (UtilValidate.isEmpty(weightUomId)) { + weightUomId = defaultWeightUomId; + // Most shipping modules assume pounds while ProductEvents.java assumes WT_oz. - Line 720 for example. + } + if (!"WT_lb".equals(weightUomId)) { + // attempt a conversion to pounds + Map<String, Object> result = FastMap.newInstance(); + try { + result = dispatcher.runSync("convertUom", UtilMisc.<String, Object>toMap("uomId", weightUomId, "uomIdTo", "WT_lb", "originalValue", productWeight)); + } catch (GenericServiceException ex) { + Debug.logError(ex, module); + } + + if (result.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_SUCCESS) && result.get("convertedValue") != null) { + productWeight = (BigDecimal) result.get("convertedValue"); + } else { + Debug.logError("Unsupported weightUom [" + weightUomId + "] for calcPackageWeight running productId " + productId + ", could not find a conversion factor to WT_lb",module); + } + + } + + totalWeight = totalWeight.add(productWeight.multiply(quantity)); + } + Debug.logInfo("Package Weight : " + String.valueOf(totalWeight) + " lbs.", module); + return totalWeight.add(additionalWeight); + } + + public static Map<String, Object> getProductItemInfo(List<Map<String, Object>> shippableItemInfo, String productId) { + if (shippableItemInfo != null) { + for (Map<String, Object> testMap: shippableItemInfo) { + String id = (String) testMap.get("productId"); + if (productId.equals(id)) { + return testMap; + } + } + } + return null; + } } Modified: ofbiz/branches/release10.04/applications/product/src/org/ofbiz/shipment/thirdparty/usps/UspsServices.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release10.04/applications/product/src/org/ofbiz/shipment/thirdparty/usps/UspsServices.java?rev=990130&r1=990129&r2=990130&view=diff ============================================================================== --- ofbiz/branches/release10.04/applications/product/src/org/ofbiz/shipment/thirdparty/usps/UspsServices.java (original) +++ ofbiz/branches/release10.04/applications/product/src/org/ofbiz/shipment/thirdparty/usps/UspsServices.java Fri Aug 27 12:39:13 2010 @@ -77,8 +77,6 @@ public class UspsServices { public final static String module = UspsServices.class.getName(); public final static String errorResource = "ProductErrorUiLabels"; - public static final MathContext generalRounding = new MathContext(10); - private static List<String> domesticCountries = FastList.newInstance(); // Countries treated as domestic for rate enquiries static { @@ -171,13 +169,13 @@ public class UspsServices { } List<Map<String, Object>> shippableItemInfo = UtilGenerics.checkList(context.get("shippableItemInfo")); - List<Map<String, BigDecimal>> packages = getPackageSplit(dctx, shippableItemInfo, maxWeight); + List<Map<String, BigDecimal>> packages = ShipmentWorker.getPackageSplit(dctx, shippableItemInfo, maxWeight); boolean isOnePackage = packages.size() == 1; // use shippableWeight if there's only one package // TODO: Up to 25 packages can be included per request - handle more than 25 for (ListIterator<Map<String, BigDecimal>> li = packages.listIterator(); li.hasNext();) { Map<String, BigDecimal> packageMap = li.next(); - BigDecimal packageWeight = isOnePackage ? shippableWeight : calcPackageWeight(dctx, packageMap, shippableItemInfo, BigDecimal.ZERO); + BigDecimal packageWeight = isOnePackage ? shippableWeight : ShipmentWorker.calcPackageWeight(dctx, packageMap, shippableItemInfo, BigDecimal.ZERO); if (packageWeight.compareTo(BigDecimal.ZERO) == 0) { continue; } @@ -327,7 +325,7 @@ public class UspsServices { } List<Map<String, Object>> shippableItemInfo = UtilGenerics.checkList(context.get("shippableItemInfo")); - List<Map<String, BigDecimal>> packages = getPackageSplit(dctx, shippableItemInfo, maxWeight); + List<Map<String, BigDecimal>> packages = ShipmentWorker.getPackageSplit(dctx, shippableItemInfo, maxWeight); boolean isOnePackage = packages.size() == 1; // use shippableWeight if there's only one package // create the request document @@ -340,7 +338,7 @@ public class UspsServices { Element packageElement = UtilXml.addChildElement(requestDocument.getDocumentElement(), "Package", requestDocument); packageElement.setAttribute("ID", String.valueOf(li.nextIndex() - 1)); // use zero-based index (see examples) - BigDecimal packageWeight = isOnePackage ? shippableWeight : calcPackageWeight(dctx, packageMap, shippableItemInfo, BigDecimal.ZERO); + BigDecimal packageWeight = isOnePackage ? shippableWeight : ShipmentWorker.calcPackageWeight(dctx, packageMap, shippableItemInfo, BigDecimal.ZERO); if (packageWeight.compareTo(BigDecimal.ZERO) == 0) { continue; } @@ -408,123 +406,7 @@ public class UspsServices { return result; } - private static List<Map<String, BigDecimal>> getPackageSplit(DispatchContext dctx, List<Map<String, Object>> shippableItemInfo, BigDecimal maxWeight) { - // create the package list w/ the first package - List<Map<String, BigDecimal>> packages = FastList.newInstance(); - - if (shippableItemInfo != null) { - for (Map<String, Object> itemInfo: shippableItemInfo) { - long pieces = ((Long) itemInfo.get("piecesIncluded")).longValue(); - BigDecimal totalQuantity = (BigDecimal) itemInfo.get("quantity"); - BigDecimal totalWeight = (BigDecimal) itemInfo.get("weight"); - String productId = (String) itemInfo.get("productId"); - - // sanity check - if (pieces < 1) { - pieces = 1; // can NEVER be less than one - } - BigDecimal weight = totalWeight.divide(BigDecimal.valueOf(pieces), generalRounding); - - for (int z = 1; z <= totalQuantity.intValue(); z++) { - BigDecimal partialQty = pieces > 1 ? BigDecimal.ONE.divide(BigDecimal.valueOf(pieces), generalRounding) : BigDecimal.ONE; - for (long x = 0; x < pieces; x++) { - if (weight.compareTo(maxWeight) >= 0) { - Map<String, BigDecimal> newPackage = FastMap.newInstance(); - newPackage.put(productId, partialQty); - packages.add(newPackage); - } else if (totalWeight.compareTo(BigDecimal.ZERO) > 0) { - // create the first package - if (packages.size() == 0) { - packages.add(FastMap.<String, BigDecimal>newInstance()); - } - - // package loop - boolean addedToPackage = false; - for (Map<String, BigDecimal> packageMap: packages) { - if (!addedToPackage) { - BigDecimal packageWeight = calcPackageWeight(dctx, packageMap, shippableItemInfo, weight); - if (packageWeight.compareTo(maxWeight) <= 0) { - BigDecimal qty = (BigDecimal) packageMap.get(productId); - qty = qty == null ? BigDecimal.ZERO : qty; - packageMap.put(productId, qty.add(partialQty)); - addedToPackage = true; - } - } - } - if (!addedToPackage) { - Map<String, BigDecimal> packageMap = FastMap.newInstance(); - packageMap.put(productId, partialQty); - packages.add(packageMap); - } - } - } - } - } - } - return packages; - } - - private static BigDecimal calcPackageWeight(DispatchContext dctx, Map<String, BigDecimal> packageMap, List<Map<String, Object>> shippableItemInfo, BigDecimal additionalWeight) { - - LocalDispatcher dispatcher = dctx.getDispatcher(); - BigDecimal totalWeight = BigDecimal.ZERO; - String defaultWeightUomId = UtilProperties.getPropertyValue("shipment.properties", "shipment.default.weight.uom"); - if (UtilValidate.isEmpty(defaultWeightUomId)) { - Debug.logWarning("No shipment.default.weight.uom set in shipment.properties, setting it to WT_oz for USPS", module); - defaultWeightUomId = "WT_oz"; - } - - for (Map.Entry<String, BigDecimal> entry: packageMap.entrySet()) { - String productId = entry.getKey(); - Map<String, Object> productInfo = getProductItemInfo(shippableItemInfo, productId); - BigDecimal productWeight = (BigDecimal) productInfo.get("weight"); - BigDecimal quantity = (BigDecimal) packageMap.get(productId); - - // DLK - I'm not sure if this line is working. shipment_package seems to leave this value null so??? - String weightUomId = (String) productInfo.get("weight_uom_id"); - - Debug.logInfo("Product Id : " + productId.toString() + " Product Weight : " + String.valueOf(productWeight) + " Product UomId : " + weightUomId + " assuming " + defaultWeightUomId + " if null. Quantity : " + String.valueOf(quantity), module); - - if (UtilValidate.isEmpty(weightUomId)) { - weightUomId = defaultWeightUomId; - // Most shipping modules assume pounds while ProductEvents.java assumes WT_oz. - Line 720 for example. - } - if (!"WT_lb".equals(weightUomId)) { - // attempt a conversion to pounds - Map<String, Object> result = FastMap.newInstance(); - try { - result = dispatcher.runSync("convertUom", UtilMisc.<String, Object>toMap("uomId", weightUomId, "uomIdTo", "WT_lb", "originalValue", productWeight)); - } catch (GenericServiceException ex) { - Debug.logError(ex, module); - } - - if (result.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_SUCCESS) && result.get("convertedValue") != null) { - productWeight = (BigDecimal) result.get("convertedValue"); - } else { - Debug.logError("Unsupported weightUom [" + weightUomId + "] for calcPackageWeight running productId " + productId + ", could not find a conversion factor to WT_lb",module); - } - - } - - totalWeight = totalWeight.add(productWeight.multiply(quantity)); - } - Debug.logInfo("Package Weight : " + String.valueOf(totalWeight) + " lbs.", module); - return totalWeight.add(additionalWeight); - } - // lifted from UpsServices with no changes - 2004.09.06 JFE - private static Map<String, Object> getProductItemInfo(List<Map<String, Object>> shippableItemInfo, String productId) { - if (shippableItemInfo != null) { - for (Map<String, Object> testMap: shippableItemInfo) { - String id = (String) testMap.get("productId"); - if (productId.equals(id)) { - return testMap; - } - } - } - return null; - } - /* Track/Confirm Samples: (API=TrackV2) |
Free forum by Nabble | Edit this page |