[OBUIAPP] Move methods to ParameterUtils, improvements on getParameterValue method.
authorGorka Ion Damián <gorkaion.damian@openbravo.com>
Wed, 29 Dec 2010 19:35:07 +0100
changeset 9463 1e06cf360a62
parent 9462 a265147dd5ae
child 9464 636e7cd39d7f
[OBUIAPP] Move methods to ParameterUtils, improvements on getParameterValue method.
modules/org.openbravo.client.application/src/org/openbravo/client/application/ApplicationUtils.java
modules/org.openbravo.client.application/src/org/openbravo/client/application/JSExpressionCallout.java
modules/org.openbravo.client.application/src/org/openbravo/client/application/ParameterUtils.java
--- a/modules/org.openbravo.client.application/src/org/openbravo/client/application/ApplicationUtils.java	Wed Dec 29 19:47:17 2010 +0100
+++ b/modules/org.openbravo.client.application/src/org/openbravo/client/application/ApplicationUtils.java	Wed Dec 29 19:35:07 2010 +0100
@@ -20,12 +20,7 @@
 
 import java.util.Collections;
 import java.util.List;
-import java.util.Map;
 
-import javax.script.ScriptEngine;
-import javax.script.ScriptEngineManager;
-import javax.script.ScriptException;
-import javax.servlet.http.HttpSession;
 
 import org.apache.log4j.Logger;
 import org.hibernate.criterion.Expression;
@@ -96,60 +91,4 @@
     }
     return Collections.emptyList();
   }
-
-  /**
-   * Returns an Object with the Value of the Parameter Value. This object can be a String, a
-   * java.util.Data or a BigDecimal.
-   * 
-   * @param parameterValue
-   *          the Parameter Value we want to get the Value from.
-   * @return the Value of the Parameter Value.
-   */
-  public static Object getParameterValue(ParameterValue parameterValue) {
-    if (parameterValue.getValueDate() != null) {
-      return parameterValue.getValueDate();
-    } else if (parameterValue.getValueNumber() != null) {
-      return parameterValue.getValueNumber();
-    } else if (parameterValue.getValueString() != null) {
-      return parameterValue.getValueString();
-    }
-    return null;
-  }
-
-  /**
-   * Returns the Fixed value of the given parameter. If the value is a JS expression it returns the
-   * result of the expression based on the parameters passed in from the request.
-   * 
-   * @param parameters
-   *          the parameters passed in from the request
-   * @param parameter
-   *          the parameter we want to get the Fixed Value from
-   * @return the Fixed Value of the parameter
-   */
-  public static String getParameterFixedValue(Map<String, String> parameters, Parameter parameter) {
-    if (parameter.isEvaluateFixedValue()) {
-      try {
-        return (String) getJSExpressionResult(parameters, null, parameter.getFixedValue());
-      } catch (Exception e) {
-        log.error(e.getMessage(), e);
-        return null;
-      }
-    } else {
-      return parameter.getFixedValue();
-    }
-  }
-
-  public static Object getJSExpressionResult(Map<String, String> parameters, HttpSession session,
-      String expression) throws ScriptException {
-    final ScriptEngineManager manager = new ScriptEngineManager();
-    final ScriptEngine engine = manager.getEngineByName("js");
-
-    if (session != null) {
-      engine.put("OB", new OBBindings(OBContext.getOBContext(), parameters, session));
-    } else {
-      engine.put("OB", new OBBindings(OBContext.getOBContext(), parameters));
-    }
-
-    return engine.eval(expression);
-  }
 }
--- a/modules/org.openbravo.client.application/src/org/openbravo/client/application/JSExpressionCallout.java	Wed Dec 29 19:47:17 2010 +0100
+++ b/modules/org.openbravo.client.application/src/org/openbravo/client/application/JSExpressionCallout.java	Wed Dec 29 19:35:07 2010 +0100
@@ -91,7 +91,7 @@
     sb.append(CLEAR_MSG_CODE);
 
     try {
-      Object result = ApplicationUtils.getJSExpressionResult(getParameterMap(request),
+      Object result = ParameterUtils.getJSExpressionResult(getParameterMap(request),
           request.getSession(false), expression);
 
       sb.append(", ['INFO','"
--- a/modules/org.openbravo.client.application/src/org/openbravo/client/application/ParameterUtils.java	Wed Dec 29 19:47:17 2010 +0100
+++ b/modules/org.openbravo.client.application/src/org/openbravo/client/application/ParameterUtils.java	Wed Dec 29 19:35:07 2010 +0100
@@ -21,16 +21,24 @@
 import java.math.BigDecimal;
 import java.text.SimpleDateFormat;
 import java.util.Date;
+import java.util.Map;
+
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineManager;
+import javax.script.ScriptException;
+import javax.servlet.http.HttpSession;
 
 import org.apache.log4j.Logger;
 import org.codehaus.jettison.json.JSONObject;
 import org.openbravo.base.model.ModelProvider;
 import org.openbravo.base.model.domaintype.BigDecimalDomainType;
+import org.openbravo.base.model.domaintype.BooleanDomainType;
 import org.openbravo.base.model.domaintype.DateDomainType;
 import org.openbravo.base.model.domaintype.DomainType;
 import org.openbravo.base.model.domaintype.LongDomainType;
 import org.openbravo.base.model.domaintype.StringDomainType;
 import org.openbravo.base.util.Check;
+import org.openbravo.dal.core.OBContext;
 
 /**
  * Utility class for Parameters handling
@@ -43,7 +51,7 @@
   public static void setParameterValue(Parameter parameter, ParameterValue parameterValue,
       JSONObject requestValue) {
     try {
-      setValue(parameterValue, getParameterDomainType(parameter), requestValue.getString("value"));
+      setValue(parameterValue, requestValue.getString("value"));
     } catch (Exception e) {
       log.error("Error trying to set value for paramter: "
           + parameterValue.getParameter().getName(), e);
@@ -52,12 +60,11 @@
 
   public static void setDefaultParameterValue(ParameterValue value) {
     Check.isNotNull(value, "Default value is based on Parameter defintion");
-    setValue(value, getParameterDomainType(value.getParameter()), value.getParameter()
-        .getDefaultValue());
+    setValue(value, value.getParameter().getDefaultValue());
   }
 
-  private static void setValue(ParameterValue parameterValue, DomainType domainType,
-      String stringValue) {
+  private static void setValue(ParameterValue parameterValue, String stringValue) {
+    DomainType domainType = getParameterDomainType(parameterValue.getParameter());
     final SimpleDateFormat xmlDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
     try {
       if (domainType.getClass().equals(StringDomainType.class)) {
@@ -81,4 +88,65 @@
     return ModelProvider.getInstance().getReference(parameter.getReference().getId())
         .getDomainType();
   }
+
+  /**
+   * Returns an Object with the Value of the Parameter Value. This object can be a String, a
+   * java.util.Data or a BigDecimal.
+   * 
+   * @param parameterValue
+   *          the Parameter Value we want to get the Value from.
+   * @return the Value of the Parameter Value.
+   */
+  public static Object getParameterValue(ParameterValue parameterValue) {
+    DomainType domainType = getParameterDomainType(parameterValue.getParameter());
+    if (domainType.getClass().equals(StringDomainType.class)) {
+      return parameterValue.getValueString();
+    } else if (domainType.getClass().equals(DateDomainType.class)) {
+      return parameterValue.getValueDate();
+    } else if (domainType.getClass().getSuperclass().equals(BigDecimalDomainType.class)
+        || domainType.getClass().equals(LongDomainType.class)) {
+      return parameterValue.getValueNumber();
+    } else if (domainType.getClass().equals(BooleanDomainType.class)) {
+      return "true".equals(parameterValue.getValueString());
+    } else { // default
+      return parameterValue.getValueString();
+    }
+  }
+
+  /**
+   * Returns the Fixed value of the given parameter. If the value is a JS expression it returns the
+   * result of the expression based on the parameters passed in from the request.
+   * 
+   * @param parameters
+   *          the parameters passed in from the request
+   * @param parameter
+   *          the parameter we want to get the Fixed Value from
+   * @return the Fixed Value of the parameter
+   */
+  public static String getParameterFixedValue(Map<String, String> parameters, Parameter parameter) {
+    if (parameter.isEvaluateFixedValue()) {
+      try {
+        return (String) getJSExpressionResult(parameters, null, parameter.getFixedValue());
+      } catch (Exception e) {
+        // log.error(e.getMessage(), e);
+        return null;
+      }
+    } else {
+      return parameter.getFixedValue();
+    }
+  }
+
+  public static Object getJSExpressionResult(Map<String, String> parameters, HttpSession session,
+      String expression) throws ScriptException {
+    final ScriptEngineManager manager = new ScriptEngineManager();
+    final ScriptEngine engine = manager.getEngineByName("js");
+
+    if (session != null) {
+      engine.put("OB", new OBBindings(OBContext.getOBContext(), parameters, session));
+    } else {
+      engine.put("OB", new OBBindings(OBContext.getOBContext(), parameters));
+    }
+
+    return engine.eval(expression);
+  }
 }