Author: lektran
Date: Mon Aug 3 04:53:48 2009
New Revision: 800197
URL:
http://svn.apache.org/viewvc?rev=800197&view=revLog:
Add a convenience method for converting uoms
Modified:
ofbiz/trunk/framework/common/src/org/ofbiz/common/uom/UomWorker.java
Modified: ofbiz/trunk/framework/common/src/org/ofbiz/common/uom/UomWorker.java
URL:
http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/src/org/ofbiz/common/uom/UomWorker.java?rev=800197&r1=800196&r2=800197&view=diff==============================================================================
--- ofbiz/trunk/framework/common/src/org/ofbiz/common/uom/UomWorker.java (original)
+++ ofbiz/trunk/framework/common/src/org/ofbiz/common/uom/UomWorker.java Mon Aug 3 04:53:48 2009
@@ -19,10 +19,19 @@
package org.ofbiz.common.uom;
+import org.ofbiz.base.util.Debug;
import org.ofbiz.entity.GenericDelegator;
+import org.ofbiz.service.GenericServiceException;
+import org.ofbiz.service.LocalDispatcher;
+import org.ofbiz.service.ModelService;
import com.ibm.icu.util.Calendar;
+
+import java.math.BigDecimal;
import java.sql.Timestamp;
+import java.util.Map;
+
+import javolution.util.FastMap;
/**
* UomWorker
@@ -82,4 +91,31 @@
public static Calendar addUomTime(Timestamp startTime, String uomId, int value) {
return addUomTime(null, startTime, uomId, value);
}
+
+ /*
+ * Convenience method to call the convertUom service
+ */
+ public static BigDecimal convertUom(BigDecimal originalValue, String uomId, String uomIdTo, LocalDispatcher dispatcher) {
+ if (originalValue == null || uomId == null || uomIdTo == null) return null;
+ if (uomId.equals(uomIdTo)) return originalValue;
+
+ Map<String, Object> svcInMap = FastMap.newInstance();
+ svcInMap.put("originalValue", originalValue);
+ svcInMap.put("uomId", uomId);
+ svcInMap.put("uomIdTo", uomIdTo);
+
+ Map<String, Object> svcOutMap = FastMap.newInstance();
+ try {
+ svcOutMap = dispatcher.runSync("convertUom", svcInMap);
+ } catch (GenericServiceException ex) {
+ Debug.logError(ex, module);
+ return null;
+ }
+
+ if (svcOutMap.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_SUCCESS) && svcOutMap.get("convertedValue") != null) {
+ return (BigDecimal) svcOutMap.get("convertedValue");
+ }
+ Debug.logError("Failed to perform conversion for value [" + originalValue.toPlainString() + "] from Uom [" + uomId + "] to Uom [" + uomIdTo + "]",module);
+ return null;
+ }
}