Fixes issue 24403: Datasource returns to grid the minimum amount of properties
authorAugusto Mauch <augusto.mauch@openbravo.com>
Tue, 30 Jul 2013 13:06:50 +0200
changeset 20851 1724b65a331a
parent 20850 cf8dd73ff55a
child 20852 be6378d2dd4a
child 20959 a0563a002814
Fixes issue 24403: Datasource returns to grid the minimum amount of properties

When a grid in a generated window makes a fetch request to its datasource, the datasource now returns the minimum amount of properties, that is:
- Properties that are part of the entity identifier
- Properties currently displayed in the grid
- Properties that are part of the display logic of toolbar buttons

Summary of changes:
- ob-view-grid.js.ftl: It contains the list of properties the always have to be fetched from the datasource regardless of the properties shown in the grid
- OBViewFieldHandler.java: Stores the list of properties that are part of the display logic of buttons
- OBViewGridComponent.java: Returns the list of properties that are part of the entity identifier or that are part of the display logic of a button.
- ob-view-form: in the call to the FIC, include the list of fields that are shown in the grid.
- FormInitializationComponent.java: In the setValuesInRequest method,
* fetch from the database the values of all columns, not only of the columns that have an associated field
* do not try to overwrite the values of the properties not shown in the grid with the values stored in the request
- ob-view-grid.js:
* When a new column in shown in the grid make a request to the datasource
* Do not allow to show a new column in the grid if it is being edited
* When making a request to the datasource, include the required properties in the request. This is not applied if the datasource is being called to export the grid.
* Added methods to be able to process the FIC return (used in ob-standard-view.js)
- ob-standard-view.js
* When a grid record is edited, make a call to the FIC in EDIT mode (it used to be done in SETSESSION mode). It needs to be done in EDIT mode to obtain the values of the records that are not shown in the grid but are shown in the form.
modules/org.openbravo.client.application/src-db/database/sourcedata/AD_MESSAGE.xml
modules/org.openbravo.client.application/src/org/openbravo/client/application/templates/ob-view-grid.js.ftl
modules/org.openbravo.client.application/src/org/openbravo/client/application/window/FormInitializationComponent.java
modules/org.openbravo.client.application/src/org/openbravo/client/application/window/OBViewFieldHandler.java
modules/org.openbravo.client.application/src/org/openbravo/client/application/window/OBViewGridComponent.java
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/form/ob-view-form.js
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-grid.js
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-view-grid.js
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/main/ob-standard-view.js
--- a/modules/org.openbravo.client.application/src-db/database/sourcedata/AD_MESSAGE.xml	Tue Aug 06 16:42:40 2013 +0200
+++ b/modules/org.openbravo.client.application/src-db/database/sourcedata/AD_MESSAGE.xml	Tue Jul 30 13:06:50 2013 +0200
@@ -1253,6 +1253,18 @@
 <!--B8C1706AB7C34C0EA750048CD68E314A-->  <ISINCLUDEINI18N><![CDATA[N]]></ISINCLUDEINI18N>
 <!--B8C1706AB7C34C0EA750048CD68E314A--></AD_MESSAGE>
 
+<!--BCA6318A99B54DB5A27D319C6569316B--><AD_MESSAGE>
+<!--BCA6318A99B54DB5A27D319C6569316B-->  <AD_MESSAGE_ID><![CDATA[BCA6318A99B54DB5A27D319C6569316B]]></AD_MESSAGE_ID>
+<!--BCA6318A99B54DB5A27D319C6569316B-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--BCA6318A99B54DB5A27D319C6569316B-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--BCA6318A99B54DB5A27D319C6569316B-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--BCA6318A99B54DB5A27D319C6569316B-->  <VALUE><![CDATA[OBUIAPP_NotAddingFieldsWhileGridEditing]]></VALUE>
+<!--BCA6318A99B54DB5A27D319C6569316B-->  <MSGTEXT><![CDATA[New fields cannot be added to the grid while it is being edited.]]></MSGTEXT>
+<!--BCA6318A99B54DB5A27D319C6569316B-->  <MSGTYPE><![CDATA[E]]></MSGTYPE>
+<!--BCA6318A99B54DB5A27D319C6569316B-->  <AD_MODULE_ID><![CDATA[9BA0836A3CD74EE4AB48753A47211BCC]]></AD_MODULE_ID>
+<!--BCA6318A99B54DB5A27D319C6569316B-->  <ISINCLUDEINI18N><![CDATA[N]]></ISINCLUDEINI18N>
+<!--BCA6318A99B54DB5A27D319C6569316B--></AD_MESSAGE>
+
 <!--C415C626CAE14E35A7F05C42F11DA114--><AD_MESSAGE>
 <!--C415C626CAE14E35A7F05C42F11DA114-->  <AD_MESSAGE_ID><![CDATA[C415C626CAE14E35A7F05C42F11DA114]]></AD_MESSAGE_ID>
 <!--C415C626CAE14E35A7F05C42F11DA114-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
--- a/modules/org.openbravo.client.application/src/org/openbravo/client/application/templates/ob-view-grid.js.ftl	Tue Aug 06 16:42:40 2013 +0200
+++ b/modules/org.openbravo.client.application/src/org/openbravo/client/application/templates/ob-view-grid.js.ftl	Tue Jul 30 13:06:50 2013 +0200
@@ -12,7 +12,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-2011 Openbravo SLU
+ * All portions are Copyright (C) 2010-2013 Openbravo SLU
  * All Rights Reserved.
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -39,6 +39,10 @@
         filterName: '${data.filterName?js_string}',
     </#if>
     
+    requiredGridProperties: [
+    <#list data.requiredGridProperties as property>
+        '${property?js_string}'<#if property_has_next>,</#if>
+    </#list>],
     // the this is the view instance
     fields: this.gridFields
 })
--- a/modules/org.openbravo.client.application/src/org/openbravo/client/application/window/FormInitializationComponent.java	Tue Aug 06 16:42:40 2013 +0200
+++ b/modules/org.openbravo.client.application/src/org/openbravo/client/application/window/FormInitializationComponent.java	Tue Jul 30 13:06:50 2013 +0200
@@ -821,13 +821,21 @@
     // database
     if (mode.equals("EDIT") && !dataSourceBasedTable) {
       // In EDIT mode we initialize them from the database
-      for (Field field : fields) {
-        if (field.getColumn() == null) {
-          continue;
-        }
-        setValueOfColumnInRequest(row, field, field.getColumn().getDBColumnName());
+      List<Column> columns = getADColumnList(tab.getTable().getId());
+      for (Column column : columns) {
+        setValueOfColumnInRequest(row, null, column.getDBColumnName());
       }
     }
+
+    List<String> gridVisibleProperties = new ArrayList<String>();
+    if (jsContent.has("_visibleProperties")) {
+      try {
+        gridVisibleProperties = convertJSONArray(jsContent.getJSONArray("_gridVisibleProperties"));
+      } catch (JSONException e) {
+        log.error("Error while retrieving _gridVisibleProperties from jsContent" + jsContent, e);
+      }
+    }
+
     // and then overwrite with what gets passed in
     if (mode.equals("EDIT") || mode.equals("CHANGE") || mode.equals("SETSESSION")) {
       // In CHANGE and SETSESSION we get them from the request
@@ -835,7 +843,14 @@
         if (field.getColumn() == null) {
           continue;
         }
+        // Do not overwrite the value of fields that are not visible in the grid, because they are
+        // empty in the request
+
         final Property prop = KernelUtils.getInstance().getPropertyFromColumn(field.getColumn());
+        if ((mode.equals("EDIT") || mode.equals("SETSESSION"))
+            && !gridVisibleProperties.contains(prop.getName())) {
+          continue;
+        }
         String inpColName = "inp"
             + Sqlc.TransformaNombreColumna(field.getColumn().getDBColumnName());
         try {
--- a/modules/org.openbravo.client.application/src/org/openbravo/client/application/window/OBViewFieldHandler.java	Tue Aug 06 16:42:40 2013 +0200
+++ b/modules/org.openbravo.client.application/src/org/openbravo/client/application/window/OBViewFieldHandler.java	Tue Jul 30 13:06:50 2013 +0200
@@ -73,6 +73,7 @@
 
   private List<String> windowEntities = null;
   private List<OBViewFieldDefinition> fields;
+  private List<String> propertiesInButtonFieldDisplayLogic = new ArrayList<String>();
 
   private List<Field> ignoredFields = new ArrayList<Field>();
 
@@ -94,7 +95,8 @@
   }
 
   public List<OBViewFieldDefinition> getFields() {
-
+    final Entity entity = ModelProvider.getInstance().getEntityByTableId(
+        getTab().getTable().getId());
     if (fields != null) {
       return fields;
     }
@@ -129,6 +131,13 @@
         if (!fieldsInDynamicExpression.contains(fieldExpression)) {
           fieldsInDynamicExpression.add(fieldExpression);
         }
+        if ("Button".equals(f.getColumn().getReference().getName())) {
+          Property property = entity.getPropertyByColumnName(fieldExpression.getColumn()
+              .getDBColumnName());
+          if (!propertiesInButtonFieldDisplayLogic.contains(property.getName())) {
+            propertiesInButtonFieldDisplayLogic.add(property.getName());
+          }
+        }
       }
     }
 
@@ -2043,4 +2052,9 @@
     getFields(); // initializes stuff
     return ignoredFields;
   }
+
+  public List<String> getPropertiesInButtonFieldDisplayLogic() {
+    return propertiesInButtonFieldDisplayLogic;
+  }
+
 }
--- a/modules/org.openbravo.client.application/src/org/openbravo/client/application/window/OBViewGridComponent.java	Tue Aug 06 16:42:40 2013 +0200
+++ b/modules/org.openbravo.client.application/src/org/openbravo/client/application/window/OBViewGridComponent.java	Tue Jul 30 13:06:50 2013 +0200
@@ -18,11 +18,14 @@
  */
 package org.openbravo.client.application.window;
 
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import org.openbravo.base.model.Entity;
 import org.openbravo.base.model.ModelProvider;
+import org.openbravo.base.model.Property;
 import org.openbravo.client.application.window.OBViewFieldHandler.OBViewField;
 import org.openbravo.client.application.window.OBViewFieldHandler.OBViewFieldDefinition;
 import org.openbravo.client.kernel.BaseTemplateComponent;
@@ -231,4 +234,27 @@
     this.viewTab = viewTab;
   }
 
+  /**
+   * Returns the string representation of an array that contains all the properties that must always
+   * be returned from the datasource when the grid asks for data: - id - all the properties that
+   * compose the identifier of the entity - all the properties that are part of the display logic of
+   * the tab buttons
+   */
+  public List<String> getRequiredGridProperties() {
+    List<String> requiredGridProperties = new ArrayList<String>();
+    requiredGridProperties.add("id");
+
+    for (Property identifierProperty : this.entity.getIdentifierProperties()) {
+      requiredGridProperties.add(identifierProperty.getName());
+    }
+
+    List<String> propertiesInButtonFieldDisplayLogic = getViewTab().getFieldHandler()
+        .getPropertiesInButtonFieldDisplayLogic();
+
+    for (String propertyName : propertiesInButtonFieldDisplayLogic) {
+      requiredGridProperties.add(propertyName);
+    }
+
+    return requiredGridProperties;
+  }
 }
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/form/ob-view-form.js	Tue Aug 06 16:42:40 2013 +0200
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/form/ob-view-form.js	Tue Jul 30 13:06:50 2013 +0200
@@ -567,7 +567,9 @@
     var parentId = this.view.getParentId(),
         i, fldNames = [],
         requestParams, allProperties, parentColumn, me = this,
-        mode, length = this.getFields().length;
+        mode, length = this.getFields().length,
+        gridVisibleProperties = [],
+        len;
 
     this.setParentDisplayInfo();
 
@@ -593,6 +595,18 @@
       requestParams[parentColumn] = parentId;
     }
 
+    if (this.view && this.view.viewGrid && this.view.viewGrid.fields) {
+      gridVisibleProperties.push('id');
+      len = this.view.viewGrid.fields.length;
+      for (i = 0; i < len; i++) {
+        if (this.view.viewGrid.fields[i].name[0] !== '_') {
+          gridVisibleProperties.push(this.view.viewGrid.fields[i].name);
+        }
+      }
+      allProperties._gridVisibleProperties = gridVisibleProperties;
+    }
+
+
     allProperties._entityName = this.view.entity;
 
     // only put the visible field names in the call
@@ -1961,4 +1975,4 @@
     }
     return true;
   }
-};
\ No newline at end of file
+};
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-grid.js	Tue Aug 06 16:42:40 2013 +0200
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-grid.js	Tue Jul 30 13:06:50 2013 +0200
@@ -711,7 +711,8 @@
         dsURL = this.dataSource.dataURL;
     var sortCriteria;
     var lcriteria = this.getCriteria();
-    var gdata = this.getData();
+    var gdata = this.getData(),
+        isExporting = true;
     if (gdata && gdata.dataSource) {
       lcriteria = gdata.dataSource.convertRelativeDates(lcriteria);
     }
@@ -728,7 +729,7 @@
       exportToFile: true,
       _textMatchStyle: 'substring',
       _UTCOffsetMiliseconds: OB.Utilities.Date.getUTCOffsetInMiliseconds()
-    }, lcriteria, this.getFetchRequestParams());
+    }, lcriteria, this.getFetchRequestParams(null, isExporting));
     if (this.getSortField()) {
       sortCriteria = this.getSort();
       if (sortCriteria && sortCriteria.length > 0) {
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-view-grid.js	Tue Aug 06 16:42:40 2013 +0200
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-view-grid.js	Tue Jul 30 13:06:50 2013 +0200
@@ -964,12 +964,19 @@
 
   showField: function (field, suppressRelayout) {
     var res;
+    // Do not allow to add a new field while the grid is being edited. Adding a new field implies a grid refresh, 
+    // and the refresh toolbar button is disabled while the grid/form is being edited
+    if (this.view.isEditingGrid) {
+      this.view.messageBar.setMessage(isc.OBMessageBar.TYPE_ERROR, OB.I18N.getLabel('OBUIAPP_Error'), OB.I18N.getLabel('OBUIAPP_NotAddingFieldsWhileGridEditing'));
+      return;
+    }
     this._showingField = true;
     this._savedEditValues = this.getEditValues(this.getEditRow());
     res = this.Super('showField', arguments);
     delete this._savedEditValues;
     delete this._showingField;
     this.view.standardWindow.storeViewState();
+    this.invalidateCache();
     this.refreshContents();
     return res;
   },
@@ -1949,7 +1956,8 @@
     requestProperties.params = this.getFetchRequestParams(requestProperties.params);
   },
 
-  getFetchRequestParams: function (params) {
+  getFetchRequestParams: function (params, isExporting) {
+    var i, len, first, selectedProperties;
     params = params || {};
 
     if (this.targetRecordId) {
@@ -1993,6 +2001,29 @@
     } else {
       params[OB.Constants.WHERE_PARAMETER] = null;
     }
+
+    if (!isExporting) {
+      first = true;
+      selectedProperties = '';
+      len = this.requiredGridProperties.length;
+      for (i = 0; i < len; i++) {
+        if (first) {
+          first = false;
+          selectedProperties = selectedProperties + this.requiredGridProperties[i];
+        } else {
+          selectedProperties = selectedProperties + ',' + this.requiredGridProperties[i];
+        }
+      }
+
+      len = this.fields.length;
+      for (i = 0; i < len; i++) {
+        if (this.fields[i].name[0] !== '_') {
+          selectedProperties = selectedProperties + ',';
+          selectedProperties = selectedProperties + this.fields[i].name;
+        }
+      }
+      params._selectedProperties = selectedProperties;
+    }
     return params;
   },
 
@@ -3501,19 +3532,50 @@
   },
 
   getFieldFromColumnName: function (columnName) {
-    var i, field, length, fields = this.completeFields;
+    var i, field, length, fields = this.view.propertyToColumns;
 
     length = fields.length;
 
     for (i = 0; i < fields.length; i++) {
-      if (fields[i].columnName === columnName) {
+      if (fields[i].dbColumn === columnName) {
         field = fields[i];
         break;
       }
     }
     return field;
+  },
+
+  processColumnValue: function (rowNum, columnName, columnValue) {
+    var field, newValue;
+    if (!columnValue) {
+      return;
+    }
+    field = this.getFieldFromColumnName(columnName);
+    if (!field) {
+      return;
+    }
+    newValue = {};
+    newValue[field.property] = columnValue.value;
+    this.setEditValue(rowNum, field.property, columnValue.value);
+  },
+
+  processFICReturn: function (response, data, request) {
+    var context = response && response.clientContext,
+        rowNum = context && context.rowNum,
+        grid = context && context.grid,
+        columnValues, prop, value, undef;
+
+    if (rowNum === undef || !data || !data.columnValues) {
+      return;
+    }
+    columnValues = data.columnValues;
+
+    for (prop in columnValues) {
+      if (columnValues.hasOwnProperty(prop)) {
+        grid.processColumnValue(rowNum, prop, columnValues[prop]);
+      }
+    }
   }
-
 });
 
 // = OBGridToolStripIcon =
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/main/ob-standard-view.js	Tue Aug 06 16:42:40 2013 +0200
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/main/ob-standard-view.js	Tue Jul 30 13:06:50 2013 +0200
@@ -2151,6 +2151,7 @@
   },
 
   setContextInfo: function (sessionProperties, callbackFunction, forced) {
+    var newCallback, me = this;
     // no need to set the context in this case
     if (!forced && (this.isEditingGrid || this.isShowingForm)) {
       if (callbackFunction) {
@@ -2163,12 +2164,23 @@
       sessionProperties = this.getContextInfo(true, true, false, true);
     }
 
+    newCallback = function (response, data, request) {
+      var context = {}, grid = me.viewGrid;
+      context.rowNum = grid.getRecordIndex(grid.getSelectedRecord());
+      context.grid = grid;
+      response.clientContext = context;
+      grid.processFICReturn (response, data, request);
+      if (callbackFunction) {
+        callbackFunction();
+      }
+    };
+
     OB.RemoteCallManager.call('org.openbravo.client.application.window.FormInitializationComponent', sessionProperties, {
-      MODE: 'SETSESSION',
+      MODE: 'EDIT',
       TAB_ID: this.tabId,
       PARENT_ID: this.getParentId(),
       ROW_ID: this.viewGrid.getSelectedRecord() ? this.viewGrid.getSelectedRecord().id : this.getCurrentValues().id
-    }, callbackFunction);
+    }, newCallback);
 
   },