Implemented implicit filter clause, transactional filters
authorMartin Taal <martin.taal@openbravo.com>
Wed, 26 Jan 2011 06:17:52 +0100
changeset 10135 8207d391925f
parent 10134 808bd457ffbc
child 10136 9b35aa971d72
Implemented implicit filter clause, transactional filters
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/OBViewGridComponent.java
modules/org.openbravo.client.application/src/org/openbravo/client/application/window/OBViewTab.java
modules/org.openbravo.client.application/src/org/openbravo/client/application/window/StandardWindowComponent.java
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/ob-grid.js
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/ob-view-grid.js
modules/org.openbravo.service.json/src/org/openbravo/service/json/JsonConstants.java
modules/org.openbravo.service.json/src/org/openbravo/service/json/QueryBuilder.java
--- a/modules/org.openbravo.client.application/src-db/database/sourcedata/AD_MESSAGE.xml	Wed Jan 26 05:59:37 2011 +0100
+++ b/modules/org.openbravo.client.application/src-db/database/sourcedata/AD_MESSAGE.xml	Wed Jan 26 06:17:52 2011 +0100
@@ -904,4 +904,15 @@
 <!--FF8081812DB28C2D012DB299135F002A-->  <AD_MODULE_ID><![CDATA[9BA0836A3CD74EE4AB48753A47211BCC]]></AD_MODULE_ID>
 <!--FF8081812DB28C2D012DB299135F002A--></AD_MESSAGE>
 
+<!--FF8081812DC07B0C012DC082C76E0014--><AD_MESSAGE>
+<!--FF8081812DC07B0C012DC082C76E0014-->  <AD_MESSAGE_ID><![CDATA[FF8081812DC07B0C012DC082C76E0014]]></AD_MESSAGE_ID>
+<!--FF8081812DC07B0C012DC082C76E0014-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--FF8081812DC07B0C012DC082C76E0014-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--FF8081812DC07B0C012DC082C76E0014-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--FF8081812DC07B0C012DC082C76E0014-->  <VALUE><![CDATA[OBUIAPP_GridImplicitFilterIconToolTip]]></VALUE>
+<!--FF8081812DC07B0C012DC082C76E0014-->  <MSGTEXT><![CDATA[Grid is filtered using an automatic filter expression, click to clear the filter or enter your own filter criteria to override this automatic filtering.]]></MSGTEXT>
+<!--FF8081812DC07B0C012DC082C76E0014-->  <MSGTYPE><![CDATA[I]]></MSGTYPE>
+<!--FF8081812DC07B0C012DC082C76E0014-->  <AD_MODULE_ID><![CDATA[9BA0836A3CD74EE4AB48753A47211BCC]]></AD_MODULE_ID>
+<!--FF8081812DC07B0C012DC082C76E0014--></AD_MESSAGE>
+
 </data>
--- a/modules/org.openbravo.client.application/src/org/openbravo/client/application/templates/ob-view-grid.js.ftl	Wed Jan 26 05:59:37 2011 +0100
+++ b/modules/org.openbravo.client.application/src/org/openbravo/client/application/templates/ob-view-grid.js.ftl	Wed Jan 26 06:17:52 2011 +0100
@@ -39,6 +39,7 @@
     ],
     whereClause: '${data.whereClause?js_string}',
     orderByClause: '${data.orderByClause?js_string}',
+    filterClause: '${data.filterClause?js_string}',
     
     foreignKeyFieldNames:[
     <#list data.foreignKeyFields as field>
--- a/modules/org.openbravo.client.application/src/org/openbravo/client/application/window/OBViewGridComponent.java	Wed Jan 26 05:59:37 2011 +0100
+++ b/modules/org.openbravo.client.application/src/org/openbravo/client/application/window/OBViewGridComponent.java	Wed Jan 26 06:17:52 2011 +0100
@@ -23,6 +23,8 @@
 import java.util.Comparator;
 import java.util.List;
 
+import org.openbravo.base.model.Entity;
+import org.openbravo.base.model.ModelProvider;
 import org.openbravo.base.model.Property;
 import org.openbravo.client.application.ApplicationUtils;
 import org.openbravo.client.kernel.BaseTemplateComponent;
@@ -31,10 +33,12 @@
 import org.openbravo.client.kernel.reference.StringUIDefinition;
 import org.openbravo.client.kernel.reference.UIDefinition;
 import org.openbravo.client.kernel.reference.UIDefinitionController;
+import org.openbravo.dal.core.DalUtil;
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.data.Sqlc;
 import org.openbravo.model.ad.ui.Field;
 import org.openbravo.model.ad.ui.Tab;
+import org.openbravo.model.common.order.Order;
 import org.openbravo.service.json.JsonConstants;
 
 /**
@@ -47,6 +51,7 @@
   private static final String TEMPLATE_ID = "91DD63545B674BE8801E1FA4F48FF4C6";
   private static Long ZERO = new Long(0);
 
+  private boolean applyTransactionalFilter = false;
   private Tab tab;
   private List<LocalField> fields = null;
 
@@ -76,6 +81,31 @@
     return JsonConstants.IDENTIFIER;
   }
 
+  public String getFilterClause() {
+    if (tab.getHqlfilterclause() != null) {
+      return addTransactionalFilter(tab.getHqlfilterclause());
+    }
+    return addTransactionalFilter("");
+  }
+
+  private String addTransactionalFilter(String filterClause) {
+    if (!this.isApplyTransactionalFilter()) {
+      return filterClause;
+    }
+    String transactionalFilter = " e.updated > " + JsonConstants.QUERY_PARAM_TRANSACTIONAL_RANGE
+        + " ";
+    final Entity entity = ModelProvider.getInstance().getEntityByTableId(
+        (String) DalUtil.getId(tab.getTable()));
+    if (entity.hasProperty(Order.PROPERTY_PROCESSED)) {
+      transactionalFilter += " and e.processed = false ";
+    }
+
+    if (filterClause.length() > 0) {
+      return " ((" + transactionalFilter + ") and (" + filterClause + ")) ";
+    }
+    return transactionalFilter;
+  }
+
   public List<String> getForeignKeyFields() {
     final List<String> fkFields = new ArrayList<String>();
     for (LocalField field : getFields()) {
@@ -302,4 +332,12 @@
     return windowEntities;
   }
 
+  public boolean isApplyTransactionalFilter() {
+    return applyTransactionalFilter;
+  }
+
+  public void setApplyTransactionalFilter(boolean applyTransactionalFilter) {
+    this.applyTransactionalFilter = applyTransactionalFilter;
+  }
+
 }
--- a/modules/org.openbravo.client.application/src/org/openbravo/client/application/window/OBViewTab.java	Wed Jan 26 05:59:37 2011 +0100
+++ b/modules/org.openbravo.client.application/src/org/openbravo/client/application/window/OBViewTab.java	Wed Jan 26 06:17:52 2011 +0100
@@ -67,6 +67,7 @@
   private Field keyField;
   private Column keyColumn;
   private boolean buttonSessionLogic;
+  private boolean isRootTab;
 
   protected Template getComponentTemplate() {
     return OBDal.getInstance().get(Template.class, TEMPLATE_ID);
@@ -201,6 +202,8 @@
     final OBViewGridComponent viewGridComponent = createComponent(OBViewGridComponent.class);
     viewGridComponent.setParameters(getParameters());
     viewGridComponent.setTab(tab);
+    viewGridComponent.setApplyTransactionalFilter(isRootTab()
+        && this.tab.getWindow().getWindowType().equals("T"));
     return viewGridComponent.generate();
   }
 
@@ -584,4 +587,12 @@
     }
   }
 
+  public boolean isRootTab() {
+    return isRootTab;
+  }
+
+  public void setRootTab(boolean isRootTab) {
+    this.isRootTab = isRootTab;
+  }
+
 }
--- a/modules/org.openbravo.client.application/src/org/openbravo/client/application/window/StandardWindowComponent.java	Wed Jan 26 05:59:37 2011 +0100
+++ b/modules/org.openbravo.client.application/src/org/openbravo/client/application/window/StandardWindowComponent.java	Wed Jan 26 06:17:52 2011 +0100
@@ -200,7 +200,9 @@
         }
       }
     }
-
+    if (rootTabComponent != null) {
+      rootTabComponent.setRootTab(true);
+    }
     return rootTabComponent;
   }
 }
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/ob-grid.js	Wed Jan 26 05:59:37 2011 +0100
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/ob-grid.js	Wed Jan 26 06:17:52 2011 +0100
@@ -141,9 +141,15 @@
         initWidget: function(){
           thisGrid.filterImage = this;
           this.recordEditor.filterImage = this;
+          if (thisGrid.implicitFilteringOn) {
+            this.prompt = OB.I18N.getLabel('OBUIAPP_GridImplicitFilterIconToolTip');      
+            this.visibility = 'inherit';
+          }
           return this.Super('initWidget', arguments);
         },
         click: function(){
+          // get rid of the initial filter clause
+          delete this.filterClause;
           this.recordEditor.getEditForm().clearValues();
           this.recordEditor.performAction();
         }
@@ -183,8 +189,27 @@
   
   // show or hide the filter button
   filterEditorSubmit: function(criteria){
+    // get rid of the initial filter clause
+    delete this.filterClause;
+
+    this.filterImage.prompt = OB.I18N.getLabel('OBUIAPP_GridFilterIconToolTip');
+    
     this.checkShowFilterFunnelIcon(criteria);
   },
+
+  setImplicitFilteringIconOn: function(state, criteria) {
+    if (!this.filterImage) {
+      this.implicitFilteringOn = true;
+      return;
+    }
+    if (state) {
+      this.filterImage.prompt = OB.I18N.getLabel('OBUIAPP_GridImplicitFilterIconToolTip');      
+      this.filterImage.show(true);
+    } else {
+      this.filterImage.prompt = OB.I18N.getLabel('OBUIAPP_GridFilterIconToolTip');
+      this.checkShowFilterFunnelIcon(criteria);
+    }
+  },
   
   checkShowFilterFunnelIcon: function (criteria) {
     if (this.isGridFiltered(criteria)) {
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/ob-view-grid.js	Wed Jan 26 05:59:37 2011 +0100
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/ob-view-grid.js	Wed Jan 26 06:17:52 2011 +0100
@@ -557,8 +557,19 @@
       criteria[OB.Constants.ORDERBY_PARAMETER] = this.orderByClause;
     }
     
-    if (this.whereClause) {
+    if (this.filterClause) {
+      if (this.whereClause) {
+        criteria[OB.Constants.WHERE_PARAMETER] = ' ((' + this.whereClause + ') and (' + this.filterClause + ")) ";
+      } else {
+        criteria[OB.Constants.WHERE_PARAMETER] =  this.filterClause;
+      }
+      this.setImplicitFilteringIconOn(true, criteria);
+    } else if (this.whereClause) {
       criteria[OB.Constants.WHERE_PARAMETER] = this.whereClause;
+      this.setImplicitFilteringIconOn(false, criteria);
+    } else {
+      criteria[OB.Constants.WHERE_PARAMETER] = null;
+      this.setImplicitFilteringIconOn(false, criteria);
     }
     
     // add all the new session properties context info to the criteria
--- a/modules/org.openbravo.service.json/src/org/openbravo/service/json/JsonConstants.java	Wed Jan 26 05:59:37 2011 +0100
+++ b/modules/org.openbravo.service.json/src/org/openbravo/service/json/JsonConstants.java	Wed Jan 26 06:17:52 2011 +0100
@@ -99,4 +99,11 @@
   public static final String DATA = "data";
 
   public static final String IN_PARAMETER_SEPARATOR = "__;__";
+
+  public static final String SESSION_PARAM_TRANSACTIONALRANGE = "Transactional$Range".toUpperCase();
+
+  public static final String QUERY_PARAM_CLIENT = "@client@";
+  public static final String QUERY_PARAM_USER = "@user@";
+  public static final String QUERY_PARAM_TRANSACTIONAL_RANGE = "@transactionalRange@";
+
 }
--- a/modules/org.openbravo.service.json/src/org/openbravo/service/json/QueryBuilder.java	Wed Jan 26 05:59:37 2011 +0100
+++ b/modules/org.openbravo.service.json/src/org/openbravo/service/json/QueryBuilder.java	Wed Jan 26 06:17:52 2011 +0100
@@ -34,8 +34,11 @@
 import org.openbravo.base.model.Property;
 import org.openbravo.base.structure.IdentifierProvider;
 import org.openbravo.base.util.Check;
+import org.openbravo.client.kernel.RequestContext;
 import org.openbravo.dal.core.DalUtil;
 import org.openbravo.dal.core.OBContext;
+import org.openbravo.erpCommon.utility.Utility;
+import org.openbravo.service.db.DalConnectionProvider;
 
 /**
  * Encapsulates the logic to translate filter properties and values received from the client to a
@@ -99,9 +102,11 @@
     }
 
     // add some default filter parameters
-    filterParameters.put("@user@", OBContext.getOBContext().getUser().getId());
-    if (!filterParameters.containsKey("@client@")) {
-      filterParameters.put("@client@", OBContext.getOBContext().getUser().getId());
+    filterParameters
+        .put(JsonConstants.QUERY_PARAM_USER, OBContext.getOBContext().getUser().getId());
+    if (!filterParameters.containsKey(JsonConstants.QUERY_PARAM_CLIENT)) {
+      filterParameters.put(JsonConstants.QUERY_PARAM_CLIENT, OBContext.getOBContext().getUser()
+          .getId());
     }
 
     final SimpleDateFormat simpleDateFormat = JsonUtils.createDateFormat();
@@ -319,6 +324,22 @@
       whereClause = " where " + whereClause;
     }
 
+    // handle special transactional range parameter
+    if (whereClause.contains(JsonConstants.QUERY_PARAM_TRANSACTIONAL_RANGE)) {
+      final String alias = getTypedParameterAlias();
+      String windowId = RequestContext.get().getRequestParameter("windowId");
+      if (windowId == null) {
+        windowId = "";
+      }
+      final String range = Utility.getTransactionalDate(new DalConnectionProvider(false),
+          RequestContext.get().getVariablesSecureApp(), windowId);
+      final int rangeNum = Integer.parseInt(range);
+      final Calendar cal = Calendar.getInstance();
+      cal.add(Calendar.DAY_OF_MONTH, -1 * rangeNum);
+      whereClause = whereClause.replace(JsonConstants.QUERY_PARAM_TRANSACTIONAL_RANGE, alias);
+      typedParameters.add(cal.getTime());
+    }
+
     whereClause = setRequestParameters(whereClause);
 
     return whereClause;