details:   https://code.openbravo.com/erp/devel/pi/rev/e9d8dcaed86a
changeset: 23057:e9d8dcaed86a
user:      Guillermo Gil <guillermo.gil <at> openbravo.com>
date:      Mon May 19 12:43:51 2014 +0200
summary:   Fixed issue 26552: email sent when the role should not receive it

Alert's messages are now created taking into account if the record can be shown 
by the recipient role.

diffstat:

 src/org/openbravo/erpCommon/ad_process/AlertProcess.java |  37 +++++++++++++++-
 1 files changed, 35 insertions(+), 2 deletions(-)

diffs (100 lines):

diff -r c60e27530619 -r e9d8dcaed86a 
src/org/openbravo/erpCommon/ad_process/AlertProcess.java
--- a/src/org/openbravo/erpCommon/ad_process/AlertProcess.java  Wed May 14 
10:32:46 2014 +0200
+++ b/src/org/openbravo/erpCommon/ad_process/AlertProcess.java  Mon May 19 
12:43:51 2014 +0200
@@ -25,6 +25,7 @@
 import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Date;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Vector;
 
@@ -42,12 +43,14 @@
 import org.openbravo.erpCommon.utility.SequenceIdData;
 import org.openbravo.erpCommon.utility.Utility;
 import org.openbravo.erpCommon.utility.poc.EmailManager;
+import org.openbravo.model.ad.access.RoleOrganization;
 import org.openbravo.model.ad.access.User;
 import org.openbravo.model.ad.access.UserRoles;
 import org.openbravo.model.ad.alert.AlertRecipient;
 import org.openbravo.model.ad.alert.AlertRule;
 import org.openbravo.model.ad.system.Client;
 import org.openbravo.model.common.enterprise.EmailServerConfiguration;
+import org.openbravo.model.common.enterprise.Organization;
 import org.openbravo.scheduling.Process;
 import org.openbravo.scheduling.ProcessBundle;
 import org.openbravo.scheduling.ProcessLogger;
@@ -257,12 +260,14 @@
     // Insert
     if (alert != null && alert.length != 0) {
       int insertions = 0;
+      HashMap<String, StringBuilder> messageByOrg = new HashMap<String, 
StringBuilder>();
       StringBuilder msg = new StringBuilder();
-      ;
 
       for (int i = 0; i < alert.length; i++) {
         String adAlertId = SequenceIdData.getUUID();
 
+        StringBuilder newMsg = new StringBuilder();
+
         logger.log("Inserting alert " + adAlertId + " org:" + alert[i].adOrgId 
+ " client:"
             + alert[i].adClientId + " reference key: " + 
alert[i].referencekeyId + " created"
             + alert[i].created + "\n");
@@ -273,6 +278,14 @@
         insertions++;
 
         msg.append("\n\nAlert: " + alert[i].description + "\nRecord: " + 
alert[i].recordId);
+        newMsg.append("\n\nAlert: " + alert[i].description + "\nRecord: " + 
alert[i].recordId);
+
+        if (messageByOrg.containsKey(alert[i].adOrgId)) {
+          messageByOrg.get(alert[i].adOrgId).append(newMsg);
+        } else {
+          messageByOrg.put(alert[i].adOrgId, newMsg);
+        }
+
       }
 
       if (insertions > 0) {
@@ -379,6 +392,20 @@
                   continue;
                 }
 
+                // Create alert's message
+                final StringBuilder finalMessage = new StringBuilder();
+                for (String org : messageByOrg.keySet()) {
+                  Organization orgEntity = 
OBDal.getInstance().get(Organization.class, org);
+                  for (RoleOrganization roleOrganization : 
currentAlertRecipient.getRole()
+                      .getADRoleOrganizationList()) {
+                    if 
(OBContext.getOBContext().getOrganizationStructureProvider()
+                        .isInNaturalTree(roleOrganization.getOrganization(), 
orgEntity)) {
+                      finalMessage.append(messageByOrg.get(org));
+                      break;
+                    }
+                  }
+                }
+
                 // For each 'User', get the email parameters (to, subject, 
body, ...) and store them
                 // to send the email at the end
                 for (User targetUser : usersList) {
@@ -403,6 +430,12 @@
                   if (repeatedEmail) {
                     continue;
                   }
+
+                  // If there is no message for this user, skip it
+                  if (finalMessage.length() == 0) {
+                    continue;
+                  }
+
                   alreadySentToList.add(targetUserEmail);
 
                   final String host = mailConfig.getSmtpServer();
@@ -419,7 +452,7 @@
                   final String replyTo = null;
                   final String subject = "[OB Alert] " + alertRule.name;
                   final String content = Utility.messageBD(conn, 
"AlertMailHead",
-                      targetUserClientLanguage) + "\n" + msg;
+                      targetUserClientLanguage) + "\n" + finalMessage;
                   final String contentType = "text/plain; charset=utf-8";
                   final List<File> attachments = null;
                   final Date sentDate = null;

------------------------------------------------------------------------------
"Accelerate Dev Cycles with Automated Cross-Browser Testing - For FREE
Instantly run your Selenium tests across 300+ browser/OS combos.
Get unparalleled scalability from the best Selenium testing platform available
Simple to use. Nothing to install. Get started now for free."
http://p.sf.net/sfu/SauceLabs
_______________________________________________
Openbravo-commits mailing list
Openbravo-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to