fixed bug 27533: context as a query string in defaults for process definition
authorAsier Lostalé <asier.lostale@openbravo.com>
Wed, 03 Sep 2014 11:11:20 +0200
changeset 24371 02687eeace5e
parent 24370 ebeb0fa75f2b
child 24372 63ea0df119a5
child 24373 dac0cfbd526b
child 24857 77ea1cf6f335
child 25332 fab7e95904c3
fixed bug 27533: context as a query string in defaults for process definition

Now context information is sent as form data with the same format used when
the actual process is invoked.
modules/org.openbravo.client.application/src/org/openbravo/client/application/process/BaseProcessActionHandler.java
modules/org.openbravo.client.application/src/org/openbravo/client/application/process/DefaultsProcessActionHandler.java
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/process/ob-parameter-window-view.js
--- a/modules/org.openbravo.client.application/src/org/openbravo/client/application/process/BaseProcessActionHandler.java	Wed Sep 03 02:15:56 2014 +0000
+++ b/modules/org.openbravo.client.application/src/org/openbravo/client/application/process/BaseProcessActionHandler.java	Wed Sep 03 11:11:20 2014 +0200
@@ -22,7 +22,9 @@
 import java.util.Map;
 import java.util.Map.Entry;
 
+import org.apache.commons.lang.StringUtils;
 import org.apache.log4j.Logger;
+import org.codehaus.jettison.json.JSONException;
 import org.codehaus.jettison.json.JSONObject;
 import org.hibernate.criterion.Restrictions;
 import org.openbravo.base.util.Check;
@@ -77,11 +79,19 @@
         return jsonRequest;
       }
 
+      JSONObject context = null;
+      if (StringUtils.isNotEmpty(content)) {
+        try {
+          context = new JSONObject(content);
+        } catch (JSONException e) {
+          log.error("Error getting context for process definition " + processDefinition, e);
+        }
+      }
       for (Parameter param : processDefinition.getOBUIAPPParameterList()) {
         if (param.isFixed()) {
           if (param.isEvaluateFixedValue()) {
             parameters.put(param.getDBColumnName(),
-                ParameterUtils.getParameterFixedValue(fixRequestMap(parameters), param));
+                ParameterUtils.getParameterFixedValue(fixRequestMap(parameters, context), param));
           } else {
             parameters.put(param.getDBColumnName(), param.getFixedValue());
           }
@@ -200,11 +210,21 @@
     return qAccess.count() > 0;
   }
 
-  /*
+  /**
    * The request map is <String, Object> because includes the HTTP request and HTTP session, is not
    * required to handle process parameters
+   * 
+   * @deprecated use {@link BaseProcessActionHandler#fixRequestMap(Map, JSONObject)}
    */
   protected Map<String, String> fixRequestMap(Map<String, Object> parameters) {
+    return fixRequestMap(parameters, null);
+  }
+
+  /**
+   * Fixes the request map adding an "context" key to include context info in order to make it
+   * available to be evaluated by FilterExpression
+   */
+  protected Map<String, String> fixRequestMap(Map<String, Object> parameters, JSONObject context) {
     final Map<String, String> retval = new HashMap<String, String>();
     for (Entry<String, Object> entries : parameters.entrySet()) {
       if (entries.getKey().equals(KernelConstants.HTTP_REQUEST)
@@ -213,6 +233,9 @@
       }
       retval.put(entries.getKey(), entries.getValue().toString());
     }
+    if (context != null) {
+      retval.put("context", context.toString());
+    }
     return retval;
   }
 
--- a/modules/org.openbravo.client.application/src/org/openbravo/client/application/process/DefaultsProcessActionHandler.java	Wed Sep 03 02:15:56 2014 +0000
+++ b/modules/org.openbravo.client.application/src/org/openbravo/client/application/process/DefaultsProcessActionHandler.java	Wed Sep 03 11:11:20 2014 +0200
@@ -24,6 +24,7 @@
 
 import javax.servlet.http.HttpSession;
 
+import org.apache.commons.lang.StringUtils;
 import org.apache.log4j.Logger;
 import org.codehaus.jettison.json.JSONException;
 import org.codehaus.jettison.json.JSONObject;
@@ -67,12 +68,17 @@
       OBContext.setAdminMode(true);
 
       final String processId = (String) parameters.get("processId");
+      final Process processDefinition = OBDal.getInstance().get(Process.class, processId);
 
       JSONObject context = null;
-      if (parameters.get("context") != null) {
-        context = new JSONObject((String) parameters.get("context"));
+      if (StringUtils.isNotEmpty(content)) {
+        try {
+          context = new JSONObject(content);
+        } catch (JSONException e) {
+          log.error("Error getting context for process definition " + processDefinition, e);
+        }
       }
-      final Process processDefinition = OBDal.getInstance().get(Process.class, processId);
+
       JSONObject defaults = new JSONObject();
       JSONObject filterExpressions = new JSONObject();
       final List<Parameter> orderedParams = new ArrayList<Parameter>();
@@ -105,7 +111,7 @@
               defaultValue = context.get(inpName);
               inpName = "inp" + Sqlc.TransformaNombreColumna(param.getDBColumnName());
             } else {
-              Map<String, String> requestMap = fixRequestMap(parameters);
+              Map<String, String> requestMap = fixRequestMap(parameters, context);
               requestMap.put("currentParam", param.getDBColumnName());
               defaultValue = ParameterUtils.getJSExpressionResult(requestMap,
                   (HttpSession) parameters.get(KernelConstants.HTTP_SESSION), rawDefaultValue);
@@ -156,7 +162,7 @@
                   Object defaultExpression;
                   parameters.put("filterExpressionColumnName", field.getColumn().getDBColumnName());
                   defaultExpression = ParameterUtils.getJSExpressionResult(
-                      fixRequestMap(parameters),
+                      fixRequestMap(parameters, context),
                       (HttpSession) parameters.get(KernelConstants.HTTP_SESSION),
                       rawDefaultExpression);
 
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/process/ob-parameter-window-view.js	Wed Sep 03 02:15:56 2014 +0000
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/process/ob-parameter-window-view.js	Wed Sep 03 11:11:20 2014 +0200
@@ -51,7 +51,7 @@
     var i, field, items = [],
         buttonLayout = [],
         newButton, cancelButton, view = this,
-        newShowIf, params, updatedExpandSection;
+        newShowIf, context, updatedExpandSection;
 
     // this flag can be used by Selenium to determine when defaults are set
     this.defaultsAreSet = false;
@@ -289,16 +289,12 @@
     this.members.push(this.loading);
     this.Super('initWidget', arguments);
 
-    params = {
+    context = this.sourceView ? this.sourceView.getContextInfo(false, true, true, true) : {};
+
+    OB.RemoteCallManager.call('org.openbravo.client.application.process.DefaultsProcessActionHandler', context, {
       processId: this.processId,
       windowId: this.windowId
-    };
-
-    if (this.sourceView) {
-      params.context = this.sourceView.getContextInfo(false, true, true, true);
-    }
-
-    OB.RemoteCallManager.call('org.openbravo.client.application.process.DefaultsProcessActionHandler', {}, params, function (rpcResponse, data, rpcRequest) {
+    }, function (rpcResponse, data, rpcRequest) {
       view.handleDefaults(data);
     });