# HG changeset patch
# User Asier Lostalé <asier.lostale@openbravo.com>
# Date 1365071619 -7200
# Node ID 291096b6fc73488d7fbc78f39c13c0fa5ab4f0c3
# Parent  4a36ca94ba638ac4a7fe4e5bf373b6aa8642df56
fixed bug 23482: Alert Management fails in ORA having more than 1000 alerts
if those alerts belong to Alert Rules with a Filter Clause

diff -r 4a36ca94ba63 -r 291096b6fc73 modules/org.openbravo.client.application/web/org.openbravo.client.application/js/alert-management/ob-alert-grid.js
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/alert-management/ob-alert-grid.js	Wed Apr 03 19:56:28 2013 +0200
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/alert-management/ob-alert-grid.js	Thu Apr 04 12:33:39 2013 +0200
@@ -173,6 +173,36 @@
     requestProperties.params[OB.Constants.WHERE_PARAMETER] = this.getFilterClause();
   },
 
+  getAlertsWithFilterClause: function (alertRule) {
+    var filterClause, alerts = alertRule.alerts.split(','),
+        alertsNum = alerts.length,
+        i, chunksOfAlerts = [],
+        j, chunkSize = 1000;
+    filterClause = ' and (e.alertRule.id != \'' + alertRule.alertRuleId + '\'';
+
+    if (alertsNum <= chunkSize) {
+      filterClause += ' or e.id in (' + alertRule.alerts + '))';
+      return filterClause;
+    }
+
+    // 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
+    for (i = 0; i < alertsNum; i += chunkSize) {
+      chunksOfAlerts.push(alerts.slice(i, i + chunkSize));
+    }
+
+    for (i = 0; i < chunksOfAlerts.length; i++) {
+      filterClause += ' or e.id in (';
+      for (j = 0; j < chunksOfAlerts[i].length; j++) {
+        filterClause += j > 0 ? ',' : '';
+        filterClause += chunksOfAlerts[i][j];
+      }
+      filterClause += ')';
+    }
+    filterClause += ')';
+    return filterClause;
+  },
+
   getFilterClause: function () {
     var i, filterClause = '',
         alertRuleIds = '',
@@ -188,8 +218,7 @@
       // alerts are of a different alertRule or only the alerts predefined
       // this only happens if the alertRule has an SQL filter expression defined
       if (OB.AlertManagement.alertRules[i].alerts) {
-        filterClause += ' and (e.alertRule.id != \'' + OB.AlertManagement.alertRules[i].alertRuleId + '\'';
-        filterClause += ' or e.id in (' + OB.AlertManagement.alertRules[i].alerts + '))';
+        filterClause += this.getAlertsWithFilterClause(OB.AlertManagement.alertRules[i]);
       }
     }
     if (alertRuleIds !== '') {
