[WidgetInForm] Change querylist-widgets for use in WidgetInForm
authorStefan Hühner <stefan.huehner@openbravo.com>
Mon, 01 Aug 2011 17:37:17 +0200
changeset 13369 ef714224f3f7
parent 13368 ef6e09f8f68d
child 13370 06348d284aeb
[WidgetInForm] Change querylist-widgets for use in WidgetInForm
- disable height recalculation if widget displayed in a generated window
as height is constraint by field layout
- Rewrite parameter handling of querylist
- Do not pass widget instance id and read params from database
- Instead pass current parameters from client to the backend on each
datasource request. This allows for runtime modificatin of parameter
values which is needed to allows widgets to use values of form fields
- Note: Fixed parameters are handled by the backend as before
- For each request evaluate all non-fixed parameters to allow usage of
${formValues.field} type expressions to connect to current record
modules/org.openbravo.client.querylist/src/org/openbravo/client/querylist/QueryListDataSource.java
modules/org.openbravo.client.querylist/web/org.openbravo.client.querylist/js/ob-querylist-widget.js
--- a/modules/org.openbravo.client.querylist/src/org/openbravo/client/querylist/QueryListDataSource.java	Mon Aug 01 17:31:40 2011 +0200
+++ b/modules/org.openbravo.client.querylist/src/org/openbravo/client/querylist/QueryListDataSource.java	Mon Aug 01 17:37:17 2011 +0200
@@ -38,6 +38,7 @@
 import org.codehaus.jettison.json.JSONException;
 import org.codehaus.jettison.json.JSONObject;
 import org.hibernate.Query;
+import org.openbravo.base.exception.OBException;
 import org.openbravo.base.model.ModelProvider;
 import org.openbravo.base.model.domaintype.BigDecimalDomainType;
 import org.openbravo.base.model.domaintype.DateDomainType;
@@ -46,14 +47,12 @@
 import org.openbravo.base.model.domaintype.PrimitiveDomainType;
 import org.openbravo.client.application.Parameter;
 import org.openbravo.client.application.ParameterUtils;
-import org.openbravo.client.application.ParameterValue;
 import org.openbravo.client.kernel.reference.ForeignKeyUIDefinition;
 import org.openbravo.client.kernel.reference.NumberUIDefinition;
 import org.openbravo.client.kernel.reference.UIDefinition;
 import org.openbravo.client.kernel.reference.UIDefinitionController;
 import org.openbravo.client.kernel.reference.YesNoUIDefinition;
 import org.openbravo.client.myob.WidgetClass;
-import org.openbravo.client.myob.WidgetInstance;
 import org.openbravo.dal.core.OBContext;
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.model.ad.domain.Reference;
@@ -92,12 +91,11 @@
 
     OBContext.setAdminMode();
     try {
-      WidgetInstance widgetInstance = OBDal.getInstance().get(WidgetInstance.class,
-          parameters.get("widgetInstanceId"));
+      WidgetClass widgetClass = OBDal.getInstance().get(WidgetClass.class,
+          parameters.get("widgetId"));
       boolean isExport = "true".equals(parameters.get("exportToFile"));
       boolean showAll = "true".equals(parameters.get("showAll"));
       String viewMode = parameters.get("viewMode");
-      WidgetClass widgetClass = widgetInstance.getWidgetClass();
       List<OBCQL_QueryColumn> columns = QueryListUtils.getColumns(widgetClass
           .getOBCQLWidgetQueryList().get(0));
 
@@ -134,7 +132,7 @@
 
       String[] params = widgetQuery.getNamedParameters();
       if (params.length > 0) {
-        HashMap<String, Object> parameterValues = getParameterValues(parameters, widgetInstance);
+        HashMap<String, Object> parameterValues = getParameterValues(parameters, widgetClass);
 
         for (int i = 0; i < params.length; i++) {
           String namedParam = params[i];
@@ -200,27 +198,42 @@
    * 
    * @param parameters
    *          the parameters passed in from the request
-   * @param widgetInstance
-   *          the widget instance owner of the parameter values
+   * @param widgetClass
+   *          the widget class to which the parameters belong to
    * @return a HashMap<String, Object> with the value of each parameter mapped by the DBColumnName
    *         of the parameter.
    */
   private HashMap<String, Object> getParameterValues(Map<String, String> parameters,
-      WidgetInstance widgetInstance) {
+      WidgetClass widgetClass) {
     HashMap<String, Object> parameterValues = new HashMap<String, Object>();
-    for (ParameterValue value : widgetInstance
-        .getOBUIAPPParameterValueEMObkmoWidgetInstanceIDList()) {
-      parameterValues.put(value.getParameter().getDBColumnName(),
-          ParameterUtils.getParameterValue(value));
+
+    // get serializedValues from request (if present)
+    String serializedParams = parameters.get("serializedParameters");
+    if (serializedParams != null) {
+      try {
+        JSONObject json = new JSONObject(serializedParams);
+        for (Parameter parameter : widgetClass.getOBUIAPPParameterEMObkmoWidgetClassIDList()) {
+          if (parameter.isFixed()) {
+            parameterValues.put(parameter.getDBColumnName(),
+                ParameterUtils.getParameterFixedValue(parameters, parameter));
+          } else {
+            if (json.has(parameter.getDBColumnName())) {
+              parameterValues.put(parameter.getDBColumnName(),
+                  json.get(parameter.getDBColumnName()));
+            } else {
+              // TODO: not fixed & value missing -> error (prepared to be handled in caller, but not
+              // yet implemented)
+            }
+          }
+        }
+      } catch (JSONException e) {
+        log.error("Error processing client parameters", e);
+      }
+    } else {
+      // data send without serializedParams (should not happen)
+      throw new OBException("Missing serializedParameters value in request");
     }
 
-    for (Parameter parameter : widgetInstance.getWidgetClass()
-        .getOBUIAPPParameterEMObkmoWidgetClassIDList()) {
-      if (!parameterValues.containsKey(parameter.getDBColumnName()) && parameter.isFixed()) {
-        parameterValues.put(parameter.getDBColumnName(),
-            ParameterUtils.getParameterFixedValue(parameters, parameter));
-      }
-    }
     return parameterValues;
   }
 
--- a/modules/org.openbravo.client.querylist/web/org.openbravo.client.querylist/js/ob-querylist-widget.js	Mon Aug 01 17:31:40 2011 +0200
+++ b/modules/org.openbravo.client.querylist/web/org.openbravo.client.querylist/js/ob-querylist-widget.js	Mon Aug 01 17:37:17 2011 +0200
@@ -64,6 +64,11 @@
   },
   
   setWidgetHeight: function (){
+    // when used inside generated window, just accept externally defined height (via rowspan) and
+	// don't override it as will break normal window layout
+    if (this.inWidgetInFormMode) {
+      return;
+    }
     var currentHeight = this.getHeight(), 
     //currentBodyHeight = this.body.getHeight(),
     edgeTop = this.edgeTop,
@@ -249,7 +254,23 @@
   },
   
   getFetchRequestParams: function(params) {
+    var localWidgetProperties, propName, propValue;
+
+    // process dynamic parameters
+    localWidgetProperties = isc.clone(this.widget.parameters);
+    delete localWidgetProperties.formValues;
+    for (propName in localWidgetProperties) {
+      if (localWidgetProperties.hasOwnProperty(propName)) {
+        propValue = localWidgetProperties[propName];
+        if (typeof propValue === 'string') {
+          localWidgetProperties[propName] = this.widget.evaluateContents(propValue);
+        }
+      }
+    }
+
     params = params || {};
+    params.serializedParameters = isc.JSON.encode(localWidgetProperties);
+    params.widgetId = this.widget.widgetId;
     params.widgetInstanceId = this.widget.dbInstanceId;
     params.rowsNumber = this.widget.parameters.RowsNumber;
     params.viewMode = this.widget.viewMode;