fixed bug 27233: incorrect combos if selected value is not in 1st page
authorAsier Lostalé <asier.lostale@openbravo.com>
Wed, 30 Jul 2014 15:31:26 +0200
changeset 24139 a496e0ae760c
parent 24138 67abd792f3d3
child 24140 687d91cddde8
fixed bug 27233: incorrect combos if selected value is not in 1st page
modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/ComboTableDatasourceService.java
src/org/openbravo/erpCommon/utility/ComboTableData.java
--- a/modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/ComboTableDatasourceService.java	Wed Jul 30 13:40:16 2014 +0530
+++ b/modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/ComboTableDatasourceService.java	Wed Jul 30 15:31:26 2014 +0200
@@ -160,8 +160,14 @@
       if (!StringUtils.isEmpty(filterString)) {
         newParameters.put("FILTER_VALUE", filterString);
       }
-      fps = comboTableData.select(new DalConnectionProvider(false), newParameters, true, startRow,
-          endRow);
+
+      boolean optionalFieldNonFirstPage = !column.isMandatory() && startRow > 0
+          && StringUtils.isEmpty(filterString);
+      // non-mandatory fields add a blank record at the beginning of 1st page, this needs to be
+      // taken into account in subsequent pages
+
+      fps = comboTableData.select(new DalConnectionProvider(false), newParameters, true, startRow
+          - (optionalFieldNonFirstPage ? 1 : 0), endRow - (optionalFieldNonFirstPage ? 1 : 0));
 
       ArrayList<JSONObject> comboEntries = new ArrayList<JSONObject>();
       // If column is mandatory we add an initial blank value in the first page if not filtered
--- a/src/org/openbravo/erpCommon/utility/ComboTableData.java	Wed Jul 30 13:40:16 2014 +0530
+++ b/src/org/openbravo/erpCommon/utility/ComboTableData.java	Wed Jul 30 15:31:26 2014 +0200
@@ -1124,6 +1124,7 @@
         }
 
         if (includeActual && actual != null && !actual.equals("")) {
+
           String[] discard = { "filter", "orderBy", "CLIENT_LIST", "ORG_LIST" };
           String strSqlDisc = getQuery(true, discard, null, null, null, null, false);
           PreparedStatement stInactive = conn.getPreparedStatement(strSqlDisc);
@@ -1188,24 +1189,40 @@
       result.close();
 
       if (includeActual && actual != null && !actual.equals("") && !idFound) {
-        conn.releasePreparedStatement(st);
-        String[] discard = { "filter", "orderBy", "CLIENT_LIST", "ORG_LIST" };
-        strSql = getQuery(true, discard, null, null, null, null, false);
-        if (log4j.isDebugEnabled())
-          log4j.debug("SQL Actual ID: " + strSql);
-        st = conn.getPreparedStatement(strSql);
-        iParameter = setSQLParameters(st, lparameters, 0, discard);
-        UtilSql.setValue(st, ++iParameter, 12, null, actual);
-        result = st.executeQuery();
-        while (result.next()) {
-          SQLReturnObject sqlReturnObject = new SQLReturnObject();
-          sqlReturnObject.setData("ID", UtilSql.getValue(result, "ID"));
-          String strName = UtilSql.getValue(result, "NAME");
-          if (!strName.startsWith(INACTIVE_DATA))
-            strName = INACTIVE_DATA + strName;
-          sqlReturnObject.setData("NAME", strName);
-          vector.addElement(sqlReturnObject);
-          idFound = true;
+        boolean allDataInSinglePage = startRow == 0 && vector.size() < endRow - startRow;
+        if (!allDataInSinglePage) {
+          // retrieved a partial set of data, checking if current id is in a page different that the
+          // served applying the same criteria, if so, do not add it again to the list (it will
+          // appear in its own page)
+          conn.releasePreparedStatement(st);
+          strSql = getQuery(true, null, null, 0, 1, conn, !StringUtils.isEmpty(filterValue));
+          log4j.debug("SQL to check if actual ID is in another page: " + strSql);
+          st = conn.getPreparedStatement(strSql);
+          setSQLParameters(st, lparameters, 0, null, actual, filterValue);
+          result = st.executeQuery();
+          idFound = result.next();
+          result.close();
+        }
+        if (!idFound) {
+          conn.releasePreparedStatement(st);
+          String[] discard = { "filter", "orderBy", "CLIENT_LIST", "ORG_LIST" };
+          strSql = getQuery(true, discard, null, null, null, null, false);
+          if (log4j.isDebugEnabled())
+            log4j.debug("SQL Actual ID: " + strSql);
+          st = conn.getPreparedStatement(strSql);
+          iParameter = setSQLParameters(st, lparameters, 0, discard);
+          UtilSql.setValue(st, ++iParameter, 12, null, actual);
+          result = st.executeQuery();
+          if (result.next()) {
+            SQLReturnObject sqlReturnObject = new SQLReturnObject();
+            sqlReturnObject.setData("ID", UtilSql.getValue(result, "ID"));
+            String strName = UtilSql.getValue(result, "NAME");
+            if (!strName.startsWith(INACTIVE_DATA))
+              strName = INACTIVE_DATA + strName;
+            sqlReturnObject.setData("NAME", strName);
+            vector.addElement(sqlReturnObject);
+            idFound = true;
+          }
         }
         result.close();
         if (!idFound) {