From ecfd378bbe97ab37384a941470acc438367e9dd8 Mon Sep 17 00:00:00 2001
From: Lars Johansson <lars.johansson@ess.eu>
Date: Mon, 3 Jun 2024 15:39:04 +0200
Subject: [PATCH] Update notification of names and structures

Update notification of names and structures to not rely on database table
for usernames to which to send notifications.
---
 CONFIGURATION.md                              |  1 +
 .../names/service/NotificationService.java    | 49 +++++++++++++------
 .../notification/NotificationScheduler.java   | 11 ++---
 src/main/resources/application.properties     |  5 +-
 src/test/resources/application.properties     |  5 +-
 5 files changed, 48 insertions(+), 23 deletions(-)

diff --git a/CONFIGURATION.md b/CONFIGURATION.md
index f167e222..f9902829 100644
--- a/CONFIGURATION.md
+++ b/CONFIGURATION.md
@@ -15,6 +15,7 @@ Configuration may be set using SpringBoot's configuration file (`application.pro
 | NAMING_LOGGING_STACKTRACE_LENGTH | 20                                          | max number of stack trace elements to log, negative value will log entire stack trace                                  |
 | NAMING_MAIL_ADMINISTRATOR        |                                             | comma-separated list of administrator email addresses                                                                  |
 | NAMING_MAIL_NOTIFICATION         | false                                       | true/false if mail notications are to be sent                                                                          |
+| NAMING_MAIL_NOTIFICATION_NAMES   |                                             | comma-separated list of email addresses to which to send notications about changes for names                           |
 | NAMING_NOTIFY_SCHEDULE           | 0 0 5 * * *                                 | schedule for notification job, cron expression with six or seven fields to describe individual details of the schedule |
 | NAMING_SMTP_HOST                 | mail.esss.lu.se                             | DNS name of server that accepts connections for SMTP                                                                   |
 | NAMING_SMTP_PORT                 | 587                                         | port to use for SMTP, 587 for SMTP Secure                                                                              |
diff --git a/src/main/java/org/openepics/names/service/NotificationService.java b/src/main/java/org/openepics/names/service/NotificationService.java
index fc621346..a0fde73c 100644
--- a/src/main/java/org/openepics/names/service/NotificationService.java
+++ b/src/main/java/org/openepics/names/service/NotificationService.java
@@ -24,7 +24,7 @@ import java.util.List;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
-import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.compress.utils.Lists;
 import org.openepics.names.util.NameCommand;
 import org.openepics.names.util.StructureCommand;
 import org.openepics.names.util.ValidateUtil;
@@ -60,10 +60,13 @@ public class NotificationService {
 
     private static final String ADD_FOOTER  = "addfooter";
     private static final String BACKEND_URL = "backendurl";
-    private static final String COMMA       = ",";
 
     @Value("${naming.mail.administrator}")
-    String namingMailAdministrator;
+    List<String> namingMailAdministrator;
+    @Value("${naming.mail.notification.names}")
+    List<String> namingMailNotificationNames;
+    @Value("${naming.mail.notification.structures}")
+    List<String> namingMailNotificationStructures;
     @Value("${naming.swagger.url}")
     String namingSwaggerUrl;
 
@@ -76,7 +79,7 @@ public class NotificationService {
     }
 
     /**
-     * Send notifications for names to affected users and administrators by email.
+     * Send notifications for names to administrators and affected users by email.
      *
      * @param notifications list of name notifications
      * @param nameCommand name command
@@ -92,7 +95,7 @@ public class NotificationService {
     }
 
     /**
-     * Send notifications for names to affected users and administrators by email.
+     * Send notifications for names to administrators and affected users by email.
      *
      * @param created list of notifications for created names
      * @param updated list of notifications for updated names
@@ -134,19 +137,27 @@ public class NotificationService {
         ctx.setVariable(BACKEND_URL, namingSwaggerUrl);
         TemplateEngine engine = generateTemplateEngine();
 
+        // email addresses for notification
+        //     administrators
+        //     set of users - about changes for names
+        List<String> toEmailAddresses = Lists.newArrayList();
+        if (!namingMailAdministrator.isEmpty()) {
+            toEmailAddresses.addAll(namingMailAdministrator);
+        }
+        if (!namingMailNotificationNames.isEmpty()) {
+            toEmailAddresses.addAll(namingMailNotificationNames);
+        }
+
         // send notification
-        String[] toEmailAddresses = !StringUtils.isEmpty(namingMailAdministrator)
-                ? namingMailAdministrator.split(COMMA)
-                : null;
         String[] ccEmailAddresses = null;
         String[] replyToEmailAddresses = null;
         String subject = CHANGES_NOTIFICATION_NAMES + " - " + namingSwaggerUrl;
-        mailService.sendEmail(toEmailAddresses, ccEmailAddresses, replyToEmailAddresses,
+        mailService.sendEmail(toEmailAddresses.toArray(new String[0]), ccEmailAddresses, replyToEmailAddresses,
                 subject, engine.process("templates/notification_names.html", ctx), false, null, null);
     }
 
     /**
-     * Send notifications for structures to affected users and administrators by email.
+     * Send notifications for structures to administrators and affected users by email.
      *
      * @param notifications list of structure notifications
      * @param structureCommand structure command
@@ -162,7 +173,7 @@ public class NotificationService {
     }
 
     /**
-     * Send notifications for structures to affected users and administrators by email.
+     * Send notifications for structures to administrators and affected users by email.
      *
      * @param created list of notifications for created structures
      * @param updated list of notifications for updated structures
@@ -204,14 +215,22 @@ public class NotificationService {
         ctx.setVariable(BACKEND_URL, namingSwaggerUrl);
         TemplateEngine engine = generateTemplateEngine();
 
+        // email addresses for notification
+        //     administrators
+        //     set of users - about changes for structures
+        List<String> toEmailAddresses = Lists.newArrayList();
+        if (!namingMailAdministrator.isEmpty()) {
+            toEmailAddresses.addAll(namingMailAdministrator);
+        }
+        if (!namingMailNotificationStructures.isEmpty()) {
+            toEmailAddresses.addAll(namingMailNotificationStructures);
+        }
+
         // send notification
-        String[] toEmailAddresses = !StringUtils.isEmpty(namingMailAdministrator)
-                ? namingMailAdministrator.split(COMMA)
-                : null;
         String[] ccEmailAddresses = null;
         String[] replyToEmailAddresses = null;
         String subject = CHANGES_NOTIFICATION_STRUCTURES + " - " + namingSwaggerUrl;
-        mailService.sendEmail(toEmailAddresses, ccEmailAddresses, replyToEmailAddresses,
+        mailService.sendEmail(toEmailAddresses.toArray(new String[0]), ccEmailAddresses, replyToEmailAddresses,
                 subject, engine.process("templates/notification_structures.html", ctx), false, null, null);
     }
 
diff --git a/src/main/java/org/openepics/names/util/notification/NotificationScheduler.java b/src/main/java/org/openepics/names/util/notification/NotificationScheduler.java
index ccebc8d0..1d99d0e9 100644
--- a/src/main/java/org/openepics/names/util/notification/NotificationScheduler.java
+++ b/src/main/java/org/openepics/names/util/notification/NotificationScheduler.java
@@ -66,24 +66,23 @@ public class NotificationScheduler {
     }
 
     /**
-     * Notify administrators about changes for names and structures.
+     * Notify administrators and affected users about changes for names and structures.
      * Intention is to inform about changes for names (create, update, delete).
      * It is to be checked once a day, and notifications to be generated with emails.
-     * Emails is to have limited size of entries and another email is to be generated with if size is greater than limit.
      *
      * <br/><br/>
      * Scheduling is handled with cron expression.
      */
     @Scheduled(cron = "${naming.notify.schedule}")
-    public void notifyAdministrators() {
-        notifyAdministratorsForNames();
+    public void notifyForNames() {
+        prepareAndNotifyForNames();
     }
 
     /**
      * Find out which names that are created, updated or deleted the previous day
-     * and send notification to administrators for those names.
+     * and send notification to administrators and affected users for those names.
      */
-    private void notifyAdministratorsForNames() {
+    private void prepareAndNotifyForNames() {
         // prepare
         // find names for previous day
         // find changes for names
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index 96d6f369..7655d823 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -18,11 +18,14 @@ logging.level.org.hibernate.SQL=INFO
 spring.http.log-request-details=true
 
 # mail, notification
-#     administrator mail is comma-separated list of email addresses
+#     administrator mail - comma-separated list of email addresses
+#     notification  mail - comma-separated list of email addresses
 #     notification job scheduled at 5 am every day by default
 naming.notify.schedule=${NAMING_NOTIFY_SCHEDULE:0 0 5 * * *}
 naming.mail.administrator=${NAMING_MAIL_ADMINISTRATOR:}
 naming.mail.notification=${NAMING_MAIL_NOTIFICATION:false}
+naming.mail.notification.names=${NAMING_MAIL_NOTIFICATION_NAMES:}
+naming.mail.notification.structures=${NAMING_MAIL_NOTIFICATION_STRUCTURES:}
 naming.mail.from=${NAMING_MAIL_FROM:jboss@ess.eu}
 naming.smtp.host=${NAMING_SMTP_HOST:mail.esss.lu.se}
 naming.smtp.port=${NAMING_SMTP_PORT:587}
diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties
index 5370eee3..fc49f4c1 100644
--- a/src/test/resources/application.properties
+++ b/src/test/resources/application.properties
@@ -18,11 +18,14 @@ logging.level.org.hibernate.SQL=INFO
 spring.http.log-request-details=true
 
 # mail, notification
-#     administrator mail is comma-separated list of email addresses
+#     administrator mail - comma-separated list of email addresses
+#     notification  mail - comma-separated list of email addresses
 #     notification job scheduled at 5 am every day by default
 naming.notify.schedule=${NAMING_NOTIFY_SCHEDULE:0 0 5 * * *}
 naming.mail.administrator=${NAMING_MAIL_ADMINISTRATOR:}
 naming.mail.notification=${NAMING_MAIL_NOTIFICATION:false}
+naming.mail.notification.names=${NAMING_MAIL_NOTIFICATION_NAMES:}
+naming.mail.notification.structures=${NAMING_MAIL_NOTIFICATION_STRUCTURES:}
 naming.mail.from=${NAMING_MAIL_FROM:jboss@ess.eu}
 naming.smtp.host=${NAMING_SMTP_HOST:mail.esss.lu.se}
 naming.smtp.port=${NAMING_SMTP_PORT:587}
-- 
GitLab