fixed bug 24740: filter list reference columns in HQL defined selectors
authorAsier Lostalé <asier.lostale@openbravo.com>
Wed, 25 Sep 2013 12:33:35 +0200
changeset 21192 8ab8b14e8472
parent 21191 327a7d8af6ce
child 21193 a06bf757c7ec
fixed bug 24740: filter list reference columns in HQL defined selectors

Taking into account this case both in the view generation and in the DataSource
modules/org.openbravo.userinterface.selector/src/org/openbravo/userinterface/selector/CustomQuerySelectorDatasource.java
modules/org.openbravo.userinterface.selector/src/org/openbravo/userinterface/selector/SelectorComponent.java
--- a/modules/org.openbravo.userinterface.selector/src/org/openbravo/userinterface/selector/CustomQuerySelectorDatasource.java	Wed Sep 25 13:28:36 2013 +0530
+++ b/modules/org.openbravo.userinterface.selector/src/org/openbravo/userinterface/selector/CustomQuerySelectorDatasource.java	Wed Sep 25 12:33:35 2013 +0200
@@ -45,6 +45,7 @@
 import org.openbravo.base.model.domaintype.DomainType;
 import org.openbravo.base.model.domaintype.ForeignKeyDomainType;
 import org.openbravo.base.model.domaintype.LongDomainType;
+import org.openbravo.base.model.domaintype.StringEnumerateDomainType;
 import org.openbravo.base.model.domaintype.UniqueIdDomainType;
 import org.openbravo.client.application.ParameterUtils;
 import org.openbravo.client.kernel.RequestContext;
@@ -361,8 +362,38 @@
     } else if (domainType instanceof ForeignKeyDomainType) {
       // Assume left part definition is full object reference from HQL select
       whereClause = field.getClauseLeftPart() + ".id = '" + value + "'";
+    } else if (domainType instanceof StringEnumerateDomainType) {
+      // For enumerations value can be in two formats:
+      // 1- VAL: in this case the expression should be property='VAL'
+      // 2- ["VAL1", "VAL2"] (JSONArray): the expression should be property in ('VAL1', 'VAL2')
+      JSONArray values = null;
+      if (value.startsWith("[")) {
+        try {
+          values = new JSONArray(value);
+        } catch (JSONException ignore) {
+          // It is not a JSONArray: assuming format 1
+        }
+      }
+
+      if (values == null) {
+        // format 1
+        whereClause = field.getClauseLeftPart() + " = '" + value + "'";
+      } else {
+        // format 2
+        whereClause = field.getClauseLeftPart() + " IN (";
+        for (int i = 0; i < values.length(); i++) {
+          if (i > 0) {
+            whereClause += ", ";
+          }
+          try {
+            whereClause += "'" + values.getString(i) + "'";
+          } catch (JSONException e) {
+            log.error("Error parsing values as JSONArray:" + value, e);
+          }
+        }
+        whereClause += ")";
+      }
     } else {
-
       if ("iStartsWith".equals(operator)) {
         whereClause = "lower(" + field.getClauseLeftPart() + ") LIKE '"
             + value.toLowerCase().replaceAll(" ", "%") + "%'";
--- a/modules/org.openbravo.userinterface.selector/src/org/openbravo/userinterface/selector/SelectorComponent.java	Wed Sep 25 13:28:36 2013 +0530
+++ b/modules/org.openbravo.userinterface.selector/src/org/openbravo/userinterface/selector/SelectorComponent.java	Wed Sep 25 12:33:35 2013 +0200
@@ -11,7 +11,7 @@
  * under the License.
  * The Original Code is Openbravo ERP.
  * The Initial Developer of the Original Code is Openbravo SLU
- * All portions are Copyright (C) 2009-2012 Openbravo SLU
+ * All portions are Copyright (C) 2009-2013 Openbravo SLU
  * All Rights Reserved. 
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -846,11 +846,19 @@
             column = OBDal.getInstance().get(Column.class, property.getColumnId());
           }
         }
+
+        // getting the preference based on property column or directly set in field
+        org.openbravo.model.ad.domain.Reference reference = null;
         if (column != null && column.getReferenceSearchKey() != null) {
-          Set<String> allowedValues = DataSourceProperty.getAllowedValues(column
-              .getReferenceSearchKey());
-          List<RefListEntry> entries = DataSourceProperty.createValueMap(allowedValues, column
-              .getReferenceSearchKey().getId());
+          reference = column.getReferenceSearchKey();
+        } else if (selectorField.getReference() != null) {
+          reference = selectorField.getReference();
+        }
+
+        if (reference != null) {
+          Set<String> allowedValues = DataSourceProperty.getAllowedValues(reference);
+          List<RefListEntry> entries = DataSourceProperty.createValueMap(allowedValues,
+              reference.getId());
           JSONObject jsonValueMap = new JSONObject();
           for (RefListEntry entry : entries) {
             try {