details:   https://code.openbravo.com/erp/devel/pi/rev/83f6116ba55b
changeset: 28079:83f6116ba55b
user:      Carlos Aristu <carlos.aristu <at> openbravo.com>
date:      Tue Dec 01 18:27:51 2015 +0100
summary:   fixes issue 31577: alert fetches with where parameter

In order to avoid the fetching of the where parameter we generate the where 
clause inside the datasource. A new datasource has been created, 
ADAlertDatasourceService, to support the requests from the Alert Management 
window.

In addition, it has been fixed a problem in the Alert Rule drop-down filter of 
the grids in the Alert Management window which was displaying alert rules from 
all the grids in the window instead of those displayed just on a particular 
grid.

diffstat:

 
modules/org.openbravo.client.application/src-db/database/sourcedata/OBSERDS_DATASOURCE.xml
                                    |   13 +
 
modules/org.openbravo.client.application/src/org/openbravo/client/application/ADAlertDatasourceService.java
                   |  205 ++++++++++
 
modules/org.openbravo.client.application/src/org/openbravo/client/application/AlertManagementActionHandler.java
               |  119 +-----
 
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/alert-management/ob-alert-grid.js
            |   82 +---
 
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/alert-management/ob-alert-management-view.js
 |   20 +-
 5 files changed, 240 insertions(+), 199 deletions(-)

diffs (truncated from 574 to 300 lines):

diff -r d6719be2b6bf -r 83f6116ba55b 
modules/org.openbravo.client.application/src-db/database/sourcedata/OBSERDS_DATASOURCE.xml
--- 
a/modules/org.openbravo.client.application/src-db/database/sourcedata/OBSERDS_DATASOURCE.xml
        Tue Dec 01 13:38:41 2015 +0000
+++ 
b/modules/org.openbravo.client.application/src-db/database/sourcedata/OBSERDS_DATASOURCE.xml
        Tue Dec 01 18:27:51 2015 +0100
@@ -56,4 +56,17 @@
 <!--C17951F970E942FD9F3771B7BE91D049-->  
<IDFKFILTERING><![CDATA[Y]]></IDFKFILTERING>
 <!--C17951F970E942FD9F3771B7BE91D049--></OBSERDS_DATASOURCE>
 
+<!--DB9F062472294F12A0291A7BD203F922--><OBSERDS_DATASOURCE>
+<!--DB9F062472294F12A0291A7BD203F922-->  
<OBSERDS_DATASOURCE_ID><![CDATA[DB9F062472294F12A0291A7BD203F922]]></OBSERDS_DATASOURCE_ID>
+<!--DB9F062472294F12A0291A7BD203F922-->  
<AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--DB9F062472294F12A0291A7BD203F922-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--DB9F062472294F12A0291A7BD203F922-->  
<AD_MODULE_ID><![CDATA[9BA0836A3CD74EE4AB48753A47211BCC]]></AD_MODULE_ID>
+<!--DB9F062472294F12A0291A7BD203F922-->  <NAME><![CDATA[AD Alert 
Datasource]]></NAME>
+<!--DB9F062472294F12A0291A7BD203F922-->  
<CLASSNAME><![CDATA[org.openbravo.client.application.ADAlertDatasourceService]]></CLASSNAME>
+<!--DB9F062472294F12A0291A7BD203F922-->  
<OBCLKER_TEMPLATE_ID><![CDATA[2BAD445C2A0343C58E455F9BD379C690]]></OBCLKER_TEMPLATE_ID>
+<!--DB9F062472294F12A0291A7BD203F922-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--DB9F062472294F12A0291A7BD203F922-->  
<USEASTABLEDATAORIGIN><![CDATA[N]]></USEASTABLEDATAORIGIN>
+<!--DB9F062472294F12A0291A7BD203F922-->  
<IDFKFILTERING><![CDATA[Y]]></IDFKFILTERING>
+<!--DB9F062472294F12A0291A7BD203F922--></OBSERDS_DATASOURCE>
+
 </data>
diff -r d6719be2b6bf -r 83f6116ba55b 
modules/org.openbravo.client.application/src/org/openbravo/client/application/ADAlertDatasourceService.java
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ 
b/modules/org.openbravo.client.application/src/org/openbravo/client/application/ADAlertDatasourceService.java
       Tue Dec 01 18:27:51 2015 +0100
@@ -0,0 +1,205 @@
+/*
+ *************************************************************************
+ * 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) 2015 Openbravo SLU
+ * All Rights Reserved.
+ * Contributor(s):  ______________________________________.
+ ************************************************************************
+ */
+package org.openbravo.client.application;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import javax.servlet.ServletException;
+
+import org.apache.commons.lang.StringEscapeUtils;
+import org.apache.commons.lang.StringUtils;
+import org.hibernate.SQLQuery;
+import org.hibernate.exception.SQLGrammarException;
+import org.openbravo.base.exception.OBException;
+import org.openbravo.base.model.Entity;
+import org.openbravo.base.model.ModelProvider;
+import org.openbravo.client.kernel.RequestContext;
+import org.openbravo.dal.core.DalUtil;
+import org.openbravo.dal.core.OBContext;
+import org.openbravo.dal.service.OBDal;
+import org.openbravo.dal.service.OBQuery;
+import org.openbravo.erpCommon.utility.UsedByLink;
+import org.openbravo.model.ad.alert.Alert;
+import org.openbravo.model.ad.alert.AlertRecipient;
+import org.openbravo.model.ad.alert.AlertRule;
+import org.openbravo.service.datasource.DefaultDataSourceService;
+import org.openbravo.service.json.JsonConstants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Datasource used by the Alert Management window
+ */
+public class ADAlertDatasourceService extends DefaultDataSourceService {
+  private static final String AD_TABLE_ID = "594";
+  private static final String ALERT_STATUS = "_alertStatus";
+  private static final String ALERT_RULE_TAB = "alertRule.tab.id";
+  private static final Logger log = 
LoggerFactory.getLogger(ADAlertDatasourceService.class);
+
+  @Override
+  public Entity getEntity() {
+    return ModelProvider.getInstance().getEntityByTableId(AD_TABLE_ID);
+  }
+
+  @Override
+  public String fetch(Map<String, String> parameters) {
+    long t = System.currentTimeMillis();
+    String alertStatus = "";
+    try {
+      // Retrieve the information from the request parameters
+      if (parameters.get(JsonConstants.WHERE_PARAMETER) != null) {
+        log.warn("_where parameter is not allowed, ignoring it");
+      }
+      alertStatus = parameters.get(ALERT_STATUS);
+      alertStatus = StringUtils.isEmpty(alertStatus) ? "" : 
alertStatus.toUpperCase();
+
+      List<String> alertList = getAlertIds();
+
+      String whereClause = buildWhereClause(alertStatus, alertList);
+      parameters.put(JsonConstants.WHERE_PARAMETER, whereClause);
+
+      if (parameters.get(JsonConstants.DISTINCT_PARAMETER) == null) {
+        // Also return the tab id of the alert rule, just when loading the 
grid from the server.
+        // This is used in the Alert Management window to navigate to the 
record related to an alert
+        parameters.put(JsonConstants.ADDITIONAL_PROPERTIES_PARAMETER, 
ALERT_RULE_TAB);
+      }
+
+      return super.fetch(parameters, true);
+    } catch (Exception ex) {
+      log.error("Error while fetching alert data", ex);
+      throw new OBException(ex);
+    } finally {
+      log.debug("Alert list with status {} retrieved in {} ms", alertStatus,
+          System.currentTimeMillis() - t);
+    }
+  }
+
+  private List<String> getAlertIds() {
+    // Get alert rules visible for context's the role/user.
+    StringBuffer whereClause = new StringBuffer();
+    whereClause.append(" as ar ");
+    whereClause.append("\nwhere exists (select 1 from ar."
+        + AlertRule.PROPERTY_ADALERTRECIPIENTLIST + " as arr");
+    whereClause.append("\n    where arr." + 
AlertRecipient.PROPERTY_USERCONTACT + ".id = :user");
+    whereClause.append("\n      or (");
+    whereClause.append("arr." + AlertRecipient.PROPERTY_USERCONTACT + " is 
null");
+    whereClause.append("\n          and arr." + AlertRecipient.PROPERTY_ROLE + 
".id = :role))");
+
+    OBQuery<AlertRule> alertRulesQuery = 
OBDal.getInstance().createQuery(AlertRule.class,
+        whereClause.toString());
+    alertRulesQuery.setNamedParameter("user", 
DalUtil.getId(OBContext.getOBContext().getUser()));
+    alertRulesQuery.setNamedParameter("role", 
DalUtil.getId(OBContext.getOBContext().getRole()));
+
+    return getAlertIdsFromAlertRules(alertRulesQuery.list());
+  }
+
+  private List<String> getAlertIdsFromAlertRules(List<AlertRule> alertRules) {
+    List<String> alertIds = new ArrayList<String>();
+    for (AlertRule alertRule : alertRules) {
+      // Adding alert rule if it has not filter clause. In case it has, it 
will be added only in
+      // case it returns data after applying the filter clause.
+      if (alertRule.getFilterClause() == null) {
+        for (Alert alert : alertRule.getADAlertList()) {
+          alertIds.add((String) DalUtil.getId(alert));
+        }
+      }
+
+      String filterClause = null;
+      if (alertRule.getFilterClause() != null) {
+        try {
+          filterClause = new UsedByLink().getWhereClause(RequestContext.get()
+              .getVariablesSecureApp(), "", alertRule.getFilterClause());
+        } catch (ServletException e) {
+          throw new IllegalStateException(e);
+        }
+        final String sql = "select * from AD_ALERT where ISACTIVE='Y'" + " AND 
AD_CLIENT_ID "
+            + OBDal.getInstance().getReadableClientsInClause() + " AND 
AD_ORG_ID "
+            + OBDal.getInstance().getReadableOrganizationsInClause() + " AND 
AD_ALERTRULE_ID = ? "
+            + (filterClause == null ? "" : filterClause);
+        final SQLQuery sqlQuery = 
OBDal.getInstance().getSession().createSQLQuery(sql)
+            .addEntity(Alert.ENTITY_NAME);
+        sqlQuery.setParameter(0, alertRule.getId());
+
+        try {
+          @SuppressWarnings("unchecked")
+          List<Alert> alertsWithFilterClause = sqlQuery.list();
+          log.debug("Alert " + alertRule.getName() + " (" + alertRule.getId() 
+ ") - SQL:'" + sql
+              + "' - Rows: " + alertsWithFilterClause.size());
+          for (Alert alert : alertsWithFilterClause) {
+            alertIds.add((String) DalUtil.getId(alert));
+          }
+        } catch (SQLGrammarException e) {
+          log.error("An error has ocurred when trying to process the alerts: " 
+ e.getMessage(), e);
+        }
+      }
+    }
+    return alertIds;
+  }
+
+  private String buildWhereClause(String alertStatus, List<String> alertList) {
+    int chunkSize = 1000;
+    String filterClause;
+    String whereClause = "coalesce(to_char(status), 'NEW') = upper('"
+        + StringEscapeUtils.escapeSql(alertStatus) + "')";
+    ArrayList<String> alertListToRemove;
+
+    if (alertList.size() == 0) {
+      return "1 = 2";
+    }
+
+    if (alertList.size() <= chunkSize) {
+      whereClause += " and e.id in (" + toStringList(alertList) + ")";
+      return whereClause;
+    }
+
+    // There are more than 1000 alerts to include in the where clause, Oracle 
doesn't
+    // support it, so let's split them in chunks with <=1000 elements each
+    alertListToRemove = new ArrayList<String>();
+    filterClause = "";
+    while (alertList.size() > chunkSize) {
+      alertListToRemove = new ArrayList<String>(alertList.subList(0, chunkSize 
- 1));
+      if (StringUtils.isEmpty(filterClause)) {
+        filterClause = " and (e.id in (" + toStringList(alertListToRemove) + 
")";
+      } else {
+        filterClause += " or e.id in (" + toStringList(alertListToRemove) + 
")";
+      }
+      alertList.removeAll(alertListToRemove);
+    }
+    if (!alertList.isEmpty()) {
+      filterClause += " or e.id in (" + toStringList(alertList) + "))";
+    } else {
+      filterClause += ")";
+    }
+    whereClause += filterClause;
+    return whereClause;
+  }
+
+  private String toStringList(List<String> list) {
+    String result = "";
+    for (String s : list) {
+      if (!StringUtils.isEmpty(result)) {
+        result += ", ";
+      }
+      result += "'" + s + "'";
+    }
+    return result;
+  }
+}
\ No newline at end of file
diff -r d6719be2b6bf -r 83f6116ba55b 
modules/org.openbravo.client.application/src/org/openbravo/client/application/AlertManagementActionHandler.java
--- 
a/modules/org.openbravo.client.application/src/org/openbravo/client/application/AlertManagementActionHandler.java
   Tue Dec 01 13:38:41 2015 +0000
+++ 
b/modules/org.openbravo.client.application/src/org/openbravo/client/application/AlertManagementActionHandler.java
   Tue Dec 01 18:27:51 2015 +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 Openbravo SLU
+ * All portions are Copyright (C) 2015 Openbravo SLU
  * All Rights Reserved.
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -22,25 +22,17 @@
 import java.util.Map;
 
 import javax.enterprise.context.ApplicationScoped;
-import javax.servlet.ServletException;
 
 import org.apache.commons.lang.StringUtils;
-import org.apache.log4j.Logger;
-import org.codehaus.jettison.json.JSONArray;
 import org.codehaus.jettison.json.JSONException;
 import org.codehaus.jettison.json.JSONObject;
-import org.hibernate.SQLQuery;
-import org.hibernate.exception.SQLGrammarException;
 import org.openbravo.client.kernel.BaseActionHandler;
-import org.openbravo.client.kernel.RequestContext;
 import org.openbravo.dal.core.OBContext;
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.dal.service.OBDao;
-import org.openbravo.dal.service.OBQuery;
-import org.openbravo.erpCommon.utility.UsedByLink;
 import org.openbravo.model.ad.alert.Alert;
-import org.openbravo.model.ad.alert.AlertRecipient;
-import org.openbravo.model.ad.alert.AlertRule;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * @author gorkaion
@@ -48,10 +40,8 @@
  */
 @ApplicationScoped
 public class AlertManagementActionHandler extends BaseActionHandler {
-  private static final Logger log = 
Logger.getLogger(AlertManagementActionHandler.class);
-  private static final String GET_ALERT_RULES = "getAlertRules";
+  private static final Logger log = 
LoggerFactory.getLogger(AlertManagementActionHandler.class);
   private static final String MOVE_TO_STATUS = "moveToStatus";
-  private static final Logger log4j = 
Logger.getLogger(AlertManagementActionHandler.class);
 
   /*
    * (non-Javadoc)
@@ -66,9 +56,7 @@
     try {
       JSONObject o = new JSONObject(content);
       final String strEventType = o.getString("eventType");
-      if (GET_ALERT_RULES.equals(strEventType)) {
-        object.put("alertRules", getAlertRules());
-      } else if (MOVE_TO_STATUS.equals(strEventType)) {
+      if (MOVE_TO_STATUS.equals(strEventType)) {
         final String alertIDs = o.getString("alertIDs");
         final String oldStatus = o.getString("oldStatus");
         final String newStatus = o.getString("newStatus");
@@ -88,103 +76,6 @@
     return object;
   }
 
-  private JSONArray getAlertRules() {
-    // Get alert rules visible for context's the role/user.

------------------------------------------------------------------------------
Go from Idea to Many App Stores Faster with Intel(R) XDK
Give your users amazing mobile app experiences with Intel(R) XDK.
Use one codebase in this all-in-one HTML5 development environment.
Design, debug & build mobile apps & 2D/3D high-impact games for multiple OSs.
http://pubads.g.doubleclick.net/gampad/clk?id=254741911&iu=/4140
_______________________________________________
Openbravo-commits mailing list
Openbravo-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to