fixes issue 30800: Filter on Property field in a table ref column doesn't work
authorCarlos Aristu <carlos.aristu@openbravo.com>
Fri, 30 Oct 2015 13:08:22 +0100
changeset 27817 92802596bf94
parent 27816 e1c26dc9a819
child 27818 e4c74a0ac40e
fixes issue 30800: Filter on Property field in a table ref column doesn't work
Now the value used to filter a column based on a table reference is set on the client. We make use an attribute called displayProperty which is only set for Table references. This way, the change is transparent for the rest of FK references.
By setting this value in the client, the replacement done in the server (in AvanceQueryBuilder.parseSimpleClause() method) is not needed anymore.
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/form/formitem/ob-formitem-fk-filter.js
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-grid.js
modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/reference/FKComboUIDefinition.java
modules/org.openbravo.service.json/src/org/openbravo/service/json/AdvancedQueryBuilder.java
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/form/formitem/ob-formitem-fk-filter.js	Fri Oct 23 13:28:16 2015 +0200
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/form/formitem/ob-formitem-fk-filter.js	Fri Oct 30 13:08:22 2015 +0100
@@ -66,7 +66,6 @@
     // the value for the filter and the display are the same: the identifier
     this.displayField = this.criteriaDisplayField || OB.Constants.IDENTIFIER;
     this.valueField = this.criteriaDisplayField || OB.Constants.IDENTIFIER;
-
     // if this field was being filtered by its id before being recreated, reset its filter type an its filterAuxCache
     if (this.grid && this.grid.sourceWidget && this.grid.sourceWidget.filterByIdFields && this.grid.sourceWidget.filterByIdFields.contains(this.name)) {
       this.filterType = 'id';
@@ -554,6 +553,17 @@
     }
   },
 
+  // for Table reference the displayProperty is used in the filtering criteria instead of OB.Constants.IDENTIFIER
+  // see issue https://issues.openbravo.com/view.php?id=30800
+  getDisplayProperty: function () {
+    var theGrid = this.grid,
+        name = this.name;
+    if (theGrid && name && theGrid.getField(name) && theGrid.getField(name).displayProperty) {
+      return name + OB.Constants.FIELDSEPARATOR + theGrid.getField(name).displayProperty;
+    }
+    return null;
+  },
+
   // make sure that the correct field name is used to filter the main grid
   // if this is not here then the value will be removed by smartclient as it
   // sets the criterion back into the item
@@ -563,7 +573,7 @@
     if (this.filterType === 'id') {
       return this.name;
     } else {
-      return this.criteriaField || this.name + OB.Constants.FIELDSEPARATOR + OB.Constants.IDENTIFIER;
+      return this.criteriaField || this.getDisplayProperty() || this.name + OB.Constants.FIELDSEPARATOR + OB.Constants.IDENTIFIER;
     }
   },
 
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-grid.js	Fri Oct 23 13:28:16 2015 +0200
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-grid.js	Fri Oct 30 13:08:22 2015 +0100
@@ -517,9 +517,19 @@
             continue;
           }
           fullPropName = prop;
-          if (prop.endsWith(OB.Constants.FIELDSEPARATOR + OB.Constants.IDENTIFIER)) {
+          if (prop.lastIndexOf(OB.Constants.FIELDSEPARATOR) > 0) {
             var index = prop.lastIndexOf(OB.Constants.FIELDSEPARATOR);
-            prop = prop.substring(0, index);
+            var propName = prop.substring(0, index);
+            if (prop.endsWith(OB.Constants.FIELDSEPARATOR + OB.Constants.IDENTIFIER)) {
+              prop = propName;
+            } else {
+              // for Table reference the displayProperty is used in the filtering criteria instead of OB.Constants.IDENTIFIER
+              // see issue https://issues.openbravo.com/view.php?id=30800
+              var propField = this.getEditForm().getField(propName);
+              if (propField && propField.displayProperty && prop.endsWith(OB.Constants.FIELDSEPARATOR + propField.displayProperty)) {
+                prop = propName;
+              }
+            }
           }
           var fnd = false,
               j;
--- a/modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/reference/FKComboUIDefinition.java	Fri Oct 23 13:28:16 2015 +0200
+++ b/modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/reference/FKComboUIDefinition.java	Fri Oct 30 13:08:22 2015 +0100
@@ -65,7 +65,11 @@
           Property referencedProp = KernelUtils.getInstance().getPropertyFromColumn(
               referencedTable.getDisplayedColumn());
           if (prop != null && referencedProp != null) {
-            if (isTableWithMultipleIdentifierColumns(referencedTable.getTable())) {
+            if (isTableWithMultipleIdentifierColumns(referencedTable.getTable())
+                && !isPropertyField(field)) {
+              // For property fields we do not set the criteriaField, this way we can retrieve the
+              // complete path from the client side.
+              // See issue https://issues.openbravo.com/view.php?id=30800
               criteriaField = ", criteriaField: " + "'" + prop.getName() + DalUtil.FIELDSEPARATOR
                   + referencedProp.getName() + "', criteriaDisplayField: '"
                   + referencedProp.getName() + "'";
@@ -98,6 +102,13 @@
     return false;
   }
 
+  private boolean isPropertyField(Field field) {
+    if (field.getProperty() != null) {
+      return true;
+    }
+    return false;
+  }
+
   @Override
   public String getFieldProperties(Field field, boolean getValueFromSession) {
     JSONObject value;
--- a/modules/org.openbravo.service.json/src/org/openbravo/service/json/AdvancedQueryBuilder.java	Fri Oct 23 13:28:16 2015 +0200
+++ b/modules/org.openbravo.service.json/src/org/openbravo/service/json/AdvancedQueryBuilder.java	Fri Oct 30 13:08:22 2015 +0100
@@ -528,12 +528,11 @@
       throws JSONException {
 
     // note: code duplicated in parseSingleClause
-    List<Property> properties = JsonUtils.getPropertiesOnPath(getEntity(), fieldName);
+    final List<Property> properties = JsonUtils.getPropertiesOnPath(getEntity(), fieldName);
     if (properties.isEmpty()) {
       return null;
     }
-    properties = getPropertyForTableReference(properties);
-    Property property = properties.get(properties.size() - 1);
+    final Property property = properties.get(properties.size() - 1);
 
     if (property == null) {
       return null;
@@ -632,7 +631,12 @@
           useProperty = property.getEntity().getProperty(Table.PROPERTY_NAME);
           final int index = useFieldName.indexOf(DalUtil.DOT);
           useFieldName = useFieldName.substring(0, index + 1) + useProperty.getName();
-        } else {
+        } else if (fieldName.contains(JsonConstants.IDENTIFIER)) {
+          // After solving issue https://issues.openbravo.com/view.php?id=30800, the displayed
+          // property of table references is sent from the client side when filtering.
+          // So, the property used to filter the table reference (displayed property) is replaced
+          // just in case the _identifier value is sent in some case as part of the fieldName.
+
           // read the reference to get the table reference
           final Reference reference = OBDal.getInstance().get(Reference.class,
               refProperty.getDomainType().getReference().getId());