details:   https://code.openbravo.com/erp/devel/pi/rev/8725b521c897
changeset: 33402:8725b521c897
user:      Augusto Mauch <augusto.mauch <at> openbravo.com>
date:      Tue Feb 06 19:08:50 2018 +0100
summary:   Fixes issue 37451: Fixes filtering of fks that dont use ids as key

The problem was that the foreign keys that did not use and id column as key 
(i.e. ad_country.ad_language) were not being properly
filtered locally in smartclient, because the criteria used the id of the 
referenced record, that was not available in the browser (it
was available in the query done in the back end, that is why only the 
client-side filtering was not working).

For instance, if the ad_language foreign key reference uses as key 
ad_language.ad_language instead of ad_language.ad_language_id, the criteria
will no longer be:

{
  fieldName: 'language',
  operator: 'equals',
  value: '192'
}

Now it will be:

{
  fieldName: 'language',
  operator: 'equals',
  value: 'en_US'
}

The AdvancedQueryBuilder has been adapted to support the new type of criteria.

There are also changes on ob-grid.js and ob-formitem-fk-filter to ensure that 
the criteria is properly displayed in the filter input.

details:   https://code.openbravo.com/erp/devel/pi/rev/e15a97093931
changeset: 33403:e15a97093931
user:      Augusto Mauch <augusto.mauch <at> openbravo.com>
date:      Tue Feb 06 19:10:21 2018 +0100
summary:   Related with issue 37451: Adds tests

Tests have been added to check that the datasource returns properly the results 
using the updated criteria.

Selenium tests with the current infrastructure are not possible, since when 
they filter the grid, they do not
select the checkboxes of the foreign key combo boxes, they just enter the text 
in the filter input, and if done
that way the issue is not reproducible.

diffstat:

 
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/form/formitem/ob-formitem-fk-filter.js
 |  17 +-
 
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-grid.js
                        |  18 +-
 
modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/reference/ForeignKeyUIDefinition.java
               |  13 +-
 
modules/org.openbravo.service.json/src/org/openbravo/service/json/AdvancedQueryBuilder.java
                             |   8 +-
 src-test/src/org/openbravo/test/AllWebserviceTests.java                        
                                         |   4 +-
 src-test/src/org/openbravo/test/datasource/NonIdForeignKeyFilters.java         
                                         |  94 ++++++++++
 6 files changed, 138 insertions(+), 16 deletions(-)

diffs (289 lines):

diff -r 756a8987324d -r e15a97093931 
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/form/formitem/ob-formitem-fk-filter.js
--- 
a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/form/formitem/ob-formitem-fk-filter.js
   Tue Feb 06 12:59:15 2018 +0100
+++ 
b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/form/formitem/ob-formitem-fk-filter.js
   Tue Feb 06 19:10:21 2018 +0100
@@ -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) 2011-2017 Openbravo SLU
+ * All portions are Copyright (C) 2011-2018 Openbravo SLU
  * All Rights Reserved.
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -66,6 +66,7 @@
     // 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;
+    this.keyProperty = this.keyProperty || OB.Constants.ID;
     // 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';
@@ -733,17 +734,17 @@
     }
 
     for (i = 0; i < records.length; i++) {
-      recordIds.add(records[i][OB.Constants.ID]);
+      recordIds.add(records[i][this.keyProperty]);
     }
     return recordIds;
   },
 
-  getRecordIdentifierFromId: function (id) {
-    var recordIdentifier;
-    if (this.pickList && this.pickList.data.find(OB.Constants.ID, id)) {
-      recordIdentifier = this.pickList.data.find(OB.Constants.ID, 
id)[OB.Constants.IDENTIFIER];
-    } else if (this.filterAuxCache && 
this.filterAuxCache.find(OB.Constants.ID, id)) {
-      recordIdentifier = this.filterAuxCache.find(OB.Constants.ID, 
id)[OB.Constants.IDENTIFIER];
+  getRecordIdentifierFromId: function (keyValue) {
+    var recordIdentifier, keyProperty = this.keyProperty || OB.Constants.ID;
+    if (this.pickList && this.pickList.data.find(keyProperty, keyValue)) {
+      recordIdentifier = this.pickList.data.find(keyProperty, 
keyValue)[OB.Constants.IDENTIFIER];
+    } else if (this.filterAuxCache && this.filterAuxCache.find(keyProperty, 
keyValue)) {
+      recordIdentifier = this.filterAuxCache.find(keyProperty, 
keyValue)[OB.Constants.IDENTIFIER];
     }
     return recordIdentifier;
   },
diff -r 756a8987324d -r e15a97093931 
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-grid.js
--- 
a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-grid.js
  Tue Feb 06 12:59:15 2018 +0100
+++ 
b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-grid.js
  Tue Feb 06 19:10:21 2018 +0100
@@ -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) 2010-2016 Openbravo SLU
+ * All portions are Copyright (C) 2010-2018 Openbravo SLU
  * All Rights Reserved.
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -1023,7 +1023,7 @@
     var filterField, criterion, filterLength = criteria.criteria.length,
         fkFilterAuxCache = [],
         innerCache = [],
-        filterEditForm, cacheElement, i, j;
+        filterEditForm, cacheElement, i, j, keyProperty;
     if (!this.filterEditor || !this.filterEditor.getEditForm()) {
       return fkFilterAuxCache;
     }
@@ -1033,18 +1033,19 @@
       filterField = filterEditForm.getField(criterion.fieldName);
       innerCache = [];
       if (filterField && filterField.filterType === 'id') {
+        keyProperty = this.getKeyProperty(criterion.fieldName);
         if (criterion.criteria) {
           for (j = 0; j < criterion.criteria.length; j++) {
             cacheElement = {};
             cacheElement.fieldName = criterion.criteria[j].fieldName;
-            cacheElement[OB.Constants.ID] = criterion.criteria[j].value;
+            cacheElement[keyProperty] = criterion.criteria[j].value;
             cacheElement[OB.Constants.IDENTIFIER] = 
filterField.getRecordIdentifierFromId(criterion.criteria[j].value);
             innerCache.add(cacheElement);
           }
         } else {
           cacheElement = {};
           cacheElement.fieldName = criterion.fieldName;
-          cacheElement[OB.Constants.ID] = criterion.value;
+          cacheElement[keyProperty] = criterion.value;
           cacheElement[OB.Constants.IDENTIFIER] = 
filterField.getRecordIdentifierFromId(criterion.value);
           innerCache.add(cacheElement);
         }
@@ -1057,6 +1058,15 @@
     return fkFilterAuxCache;
   },
 
+  getKeyProperty: function (fieldName) {
+    var keyProperty = OB.Constants.ID,
+        field = this.getFieldByName(fieldName);
+    if (field && field.filterEditorProperties && 
field.filterEditorProperties.keyProperty) {
+      keyProperty = field.filterEditorProperties.keyProperty;
+    }
+    return keyProperty;
+  },
+
   setNewRecordFilterMessage: function () {
     var showMessageProperty, showMessage;
 
diff -r 756a8987324d -r e15a97093931 
modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/reference/ForeignKeyUIDefinition.java
--- 
a/modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/reference/ForeignKeyUIDefinition.java
 Tue Feb 06 12:59:15 2018 +0100
+++ 
b/modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/reference/ForeignKeyUIDefinition.java
 Tue Feb 06 19:10:21 2018 +0100
@@ -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) 2010-2017 Openbravo SLU 
+ * All portions are Copyright (C) 2010-2018 Openbravo SLU 
  * All Rights Reserved. 
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -83,10 +83,21 @@
         append = append + ", showFkDropdownUnfiltered: " + 
showFkDropdownUnfiltered.toString();
       }
     }
+    if (field != null) {
+      Property keyProperty = getKeyProperty(field);
+      if (keyProperty != null) {
+        append = append + ", keyProperty: '" + keyProperty.getName() + "'";
+      }
+    }
 
     return super.getFilterEditorPropertiesProperty(field) + append;
   }
 
+  private Property getKeyProperty(Field field) {
+    return KernelUtils.getInstance().getPropertyFromColumn(field.getColumn())
+        .getReferencedProperty();
+  }
+
   @Override
   public String getGridFieldProperties(Field field) {
     final Property prop = 
KernelUtils.getInstance().getPropertyFromColumn(field.getColumn());
diff -r 756a8987324d -r e15a97093931 
modules/org.openbravo.service.json/src/org/openbravo/service/json/AdvancedQueryBuilder.java
--- 
a/modules/org.openbravo.service.json/src/org/openbravo/service/json/AdvancedQueryBuilder.java
       Tue Feb 06 12:59:15 2018 +0100
+++ 
b/modules/org.openbravo.service.json/src/org/openbravo/service/json/AdvancedQueryBuilder.java
       Tue Feb 06 19:10:21 2018 +0100
@@ -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-2017 Openbravo SLU 
+ * All portions are Copyright (C) 2009-2018 Openbravo SLU 
  * All Rights Reserved. 
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -766,7 +766,7 @@
         }
       }
     } else if (!useProperty.isPrimitive()) {
-      clause = clause + ".id";
+      clause = clause + "." + getReferencedPropertyName(useProperty);
     } else if (tableReference && useProperty.isTranslatable()
         && OBContext.hasTranslationInstalled()) {
       // filtering by table reference translatable field: use translation table
@@ -785,6 +785,10 @@
     return clause;
   }
 
+  private String getReferencedPropertyName(Property property) {
+    return property.getReferencedProperty().getName();
+  }
+
   private String buildValueClause(Property property, String operator, Object 
value)
       throws JSONException {
     Object localValue = value;
diff -r 756a8987324d -r e15a97093931 
src-test/src/org/openbravo/test/AllWebserviceTests.java
--- a/src-test/src/org/openbravo/test/AllWebserviceTests.java   Tue Feb 06 
12:59:15 2018 +0100
+++ b/src-test/src/org/openbravo/test/AllWebserviceTests.java   Tue Feb 06 
19:10:21 2018 +0100
@@ -30,6 +30,7 @@
 import org.openbravo.test.datasource.FetchDSNoActiveEntityObjects;
 import org.openbravo.test.datasource.HQLDataSourceTest;
 import org.openbravo.test.datasource.LinkToParentTreeDataSourceTest;
+import org.openbravo.test.datasource.NonIdForeignKeyFilters;
 import org.openbravo.test.datasource.OrganizationSelectorDataSourceTest;
 import org.openbravo.test.datasource.OtherDatasourceRequests;
 import org.openbravo.test.datasource.ProductSelectorDataSourceTest;
@@ -88,7 +89,8 @@
     WSReadableClientsTest.class, //
     UserInfoSessionDataTest.class, //
     LinkToParentTreeDataSourceTest.class, //
-    OtherDatasourceRequests.class //
+    OtherDatasourceRequests.class, //
+    NonIdForeignKeyFilters.class //
 })
 public class AllWebserviceTests {
 }
diff -r 756a8987324d -r e15a97093931 
src-test/src/org/openbravo/test/datasource/NonIdForeignKeyFilters.java
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/src-test/src/org/openbravo/test/datasource/NonIdForeignKeyFilters.java    
Tue Feb 06 19:10:21 2018 +0100
@@ -0,0 +1,94 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
+ * Version 1.1  with a permitted attribution clause; you may not  use this
+ * file except in compliance with the License. You  may  obtain  a copy of
+ * the License at http://www.openbravo.com/legal/license.html 
+ * Software distributed under the License  is  distributed  on  an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific  language  governing  rights  and  limitations
+ * under the License. 
+ * The Original Code is Openbravo ERP. 
+ * The Initial Developer of the Original Code is Openbravo SLU 
+ * All portions are Copyright (C) 2018 Openbravo SLU 
+ * All Rights Reserved. 
+ * Contributor(s):  ______________________________________.
+ ************************************************************************
+ */
+
+package org.openbravo.test.datasource;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.junit.Assert.assertThat;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.codehaus.jettison.json.JSONException;
+import org.codehaus.jettison.json.JSONObject;
+import org.junit.Test;
+import org.openbravo.dal.core.OBContext;
+
+/**
+ * Tests the criteria that the grid builds when a foreign key that does not 
reference the id of the
+ * target table is filtered
+ */
+public class NonIdForeignKeyFilters extends BaseDataSourceTestDal {
+  private static final int STATUS_OK = 0;
+
+  @Test
+  public void testFilterWithNonIdReferencedProperty() throws Exception {
+    OBContext.setOBContext(TEST_USER_ID);
+
+    // the language FK references a non-id column
+    JSONObject response = requestCountry(getLanguageCriteria());
+
+    assertThat("Response is OK", isResponseOk(response), equalTo(true));
+    assertThat("Response contains one record", getNumberOfDataItems(response), 
equalTo(1));
+  }
+
+  @Test
+  public void testFilterWithIdReferencedProperty() throws Exception {
+    OBContext.setOBContext(TEST_USER_ID);
+
+    // the currency FK references an id column
+    JSONObject response = requestCountry(getCurrencyCriteria());
+
+    assertThat("Response is OK", isResponseOk(response), equalTo(true));
+    assertThat("Response contains one record", getNumberOfDataItems(response), 
equalTo(1));
+  }
+
+  private String getLanguageCriteria() {
+    return 
"{\"_constructor\":\"AdvancedCriteria\",\"fieldName\":\"language\",\"value\":\"sq_AL\",\"operator\":\"equals\"}";
+  }
+
+  private String getCurrencyCriteria() {
+    return 
"{\"_constructor\":\"AdvancedCriteria\",\"fieldName\":\"currency\",\"value\":\"238\",\"operator\":\"equals\"}";
+  }
+
+  private JSONObject requestCountry(String criteria) throws Exception {
+    Map<String, String> params = this.getParameters(criteria);
+
+    return new 
JSONObject(this.doRequest("/org.openbravo.service.datasource/Country", params, 
200,
+        "POST")).getJSONObject("response");
+  }
+
+  private Map<String, String> getParameters(String criteria) {
+    Map<String, String> params = new HashMap<>();
+    params.put("_operationType", "fetch");
+    params.put("_startRow", "0");
+    params.put("_endRow", "100");
+    params.put("criteria", criteria);
+    return params;
+  }
+
+  private boolean isResponseOk(JSONObject response) throws JSONException {
+    return response.getInt("status") == STATUS_OK;
+  }
+
+  private int getNumberOfDataItems(JSONObject response) throws JSONException {
+    return response.getJSONArray("data").length();
+  }
+
+}

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Openbravo-commits mailing list
Openbravo-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to