fixed bug 26914: Process Definition fail if they don't have a single grid param
authorAsier Lostalé <asier.lostale@openbravo.com>
Fri, 20 Jun 2014 15:59:37 +0200
changeset 23853 d615b5db09cd
parent 23852 032fb3d55363
child 23855 ae57ca1f99dc
fixed bug 26914: Process Definition fail if they don't have a single grid param

-Do not assume 1st param is a grid
-Do not assume there is a grid at all
-Do not do the conversion if there are several grids
-In case of failure don't do the conversion but continue the flow
modules/org.openbravo.client.application/src/org/openbravo/client/application/process/BaseProcessActionHandler.java
--- a/modules/org.openbravo.client.application/src/org/openbravo/client/application/process/BaseProcessActionHandler.java	Fri Jun 20 11:42:57 2014 +0200
+++ b/modules/org.openbravo.client.application/src/org/openbravo/client/application/process/BaseProcessActionHandler.java	Fri Jun 20 15:59:37 2014 +0200
@@ -32,6 +32,7 @@
 import org.openbravo.client.application.ProcessAccess;
 import org.openbravo.client.kernel.BaseActionHandler;
 import org.openbravo.client.kernel.KernelConstants;
+import org.openbravo.dal.core.DalUtil;
 import org.openbravo.dal.core.OBContext;
 import org.openbravo.dal.service.OBCriteria;
 import org.openbravo.dal.service.OBDal;
@@ -50,6 +51,8 @@
 
   private static final Logger log = Logger.getLogger(BaseProcessActionHandler.class);
 
+  private static final String GRID_REFERENCE_ID = "FF80818132D8F0F30132D9BC395D0038";
+
   @Override
   protected final JSONObject execute(Map<String, Object> parameters, String content) {
 
@@ -95,14 +98,45 @@
       Process process = OBDal.getInstance().get(Process.class, processId);
       String updatedContent = content;
       if (process.isGridlegacy()) {
+        log.warn("Process "
+            + process.getName()
+            + " is marked as Grid Legacy, you should consider migrating it to prevent parameter conversion");
+
         JSONObject jsonRequest = new JSONObject(content);
         if (!jsonRequest.isNull("_params")) {
-          JSONObject jsonparams = jsonRequest.getJSONObject("_params");
-          String gridParamName = jsonparams.names().getString(0);
-          JSONObject jsongrid = jsonparams.getJSONObject(gridParamName);
-          jsonRequest.put("_selection", jsongrid.getJSONArray("_selection"));
-          jsonRequest.put("_allRows", jsongrid.getJSONArray("_allRows"));
-          updatedContent = jsonRequest.toString();
+          try {
+            Parameter gridParameter = null;
+            boolean shouldConvert = false;
+            for (Parameter param : process.getOBUIAPPParameterList()) {
+              if (GRID_REFERENCE_ID.equals(DalUtil.getId(param.getReference()))) {
+                if (gridParameter != null) {
+                  log.error("Error while trying to conver parameters to legacy mode. There are more than one grid parameter. Not converting it.");
+                  shouldConvert = false;
+                } else {
+                  gridParameter = param;
+                  shouldConvert = true;
+                }
+              }
+            }
+
+            if (gridParameter == null) {
+              log.info("There is no grid parameter in proces " + process.getName()
+                  + ". No conversion is needed so Grid Legacy can be safelly unflagged.");
+            }
+
+            if (shouldConvert) {
+              JSONObject jsonparams = jsonRequest.getJSONObject("_params");
+              if (jsonparams.has(gridParameter.getDBColumnName())
+                  && !jsonparams.isNull(gridParameter.getDBColumnName())) {
+                JSONObject jsongrid = jsonparams.getJSONObject(gridParameter.getDBColumnName());
+                jsonRequest.put("_selection", jsongrid.getJSONArray("_selection"));
+                jsonRequest.put("_allRows", jsongrid.getJSONArray("_allRows"));
+              }
+              updatedContent = jsonRequest.toString();
+            }
+          } catch (Exception e) {
+            log.error("Error while converting parameters. Sending them without conversion", e);
+          }
         }
       }
       return doExecute(parameters, updatedContent);