From ac370aa055b68d9e5e18cec2636ce19ac379c7c2 Mon Sep 17 00:00:00 2001 From: "konstantina.galouni" Date: Fri, 26 Feb 2021 14:54:17 +0000 Subject: [PATCH] [Trunk | Admin Tools]: 1. CuratorResponse.java: Remove "email" field. 2. EmailController.java: a.Replace methods "notifyNewManagers()" (/notifyForNewManagers/{pid}) and "notifyForNewSubscribers()" (/notifyForNewSuscribers/{pid}) with "notifyManagers()" (/notifyManagers/{pid}/{newRoleType}), where newRoleType is "manager" or "subscriber". b. Method notifyManagers() finds manager emails by calling registry service - stop getting them from portals inside Email object. --- .../controllers/EmailController.java | 83 ++++++++++++------- .../entities/curator/CuratorResponse.java | 10 --- 2 files changed, 54 insertions(+), 39 deletions(-) diff --git a/src/main/java/eu/dnetlib/uoaadmintools/controllers/EmailController.java b/src/main/java/eu/dnetlib/uoaadmintools/controllers/EmailController.java index b40e5b8..13af7ca 100644 --- a/src/main/java/eu/dnetlib/uoaadmintools/controllers/EmailController.java +++ b/src/main/java/eu/dnetlib/uoaadmintools/controllers/EmailController.java @@ -1,13 +1,16 @@ package eu.dnetlib.uoaadmintools.controllers; import eu.dnetlib.uoaadmintools.dao.NotificationsDAO; +import eu.dnetlib.uoaadmintools.entities.Manager; import eu.dnetlib.uoaadmintools.entities.Notifications; +import eu.dnetlib.uoaadmintools.services.ManagerService; import eu.dnetlib.uoaadmintoolslibrary.dao.PortalDAO; import eu.dnetlib.uoaadmintoolslibrary.emailSender.EmailSender; import eu.dnetlib.uoaadmintoolslibrary.entities.email.Email; import eu.dnetlib.uoaadmintoolslibrary.entities.email.EmailRecaptcha; import eu.dnetlib.uoaadmintoolslibrary.handlers.ContentNotFoundException; import eu.dnetlib.uoaadmintoolslibrary.handlers.InvalidReCaptchaException; +import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils; import eu.dnetlib.uoaadmintoolslibrary.recaptcha.VerifyRecaptcha; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; @@ -28,6 +31,10 @@ public class EmailController { private PortalDAO portalDAO; @Autowired private VerifyRecaptcha verifyRecaptcha; + @Autowired + private ManagerService managerService; + @Autowired + private RolesUtils rolesUtils; @RequestMapping(value = "/contact", method = RequestMethod.POST) public Boolean contact(@RequestBody EmailRecaptcha form) throws InvalidReCaptchaException { @@ -66,41 +73,38 @@ public class EmailController { } - @RequestMapping(value = "/notifyForNewManagers/{pid}", method = RequestMethod.POST) - public Boolean notifyNewManagers(@PathVariable(value = "pid") String pid,@RequestBody Email email ) throws Exception { + @RequestMapping(value = "/notifyManagers/{pid}/{newRoleType}", method = RequestMethod.POST) + public Boolean notifyManagers(@PathVariable(value = "pid") String pid, + @PathVariable(value = "newRoleType") String newRoleType, + @RequestBody Email email ) throws Exception { List notifyrecipients = new ArrayList(); if(portalDAO.findByPid(pid) == null){ throw new ContentNotFoundException("Portal not found"); } - for(String user:email.getRecipients()){ - Notifications userNotifications = notificationsDAO.findByManagerEmailAndPortalPid(user,pid); - if(userNotifications == null || userNotifications.getNotifyForNewManagers()){ - notifyrecipients.add(user); - } - } - if(notifyrecipients.size() > 0){ - return emailSender.send(notifyrecipients,email.getSubject(),email.getBody(), false); - }else{ - log.debug("There are no users to notify "); - } - - return true; - } - @RequestMapping(value = "/notifyForNewSubscribers/{pid}", method = RequestMethod.POST) - public Boolean notifyNewSubscribers(@PathVariable(value = "pid") String pid,@RequestBody Email email ) throws Exception { - List notifyrecipients = new ArrayList(); - if(portalDAO.findByPid(pid) == null){ - throw new ContentNotFoundException("Portal not found"); - } - for(String user:email.getRecipients()){ - Notifications userNotifications = notificationsDAO.findByManagerEmailAndPortalPid(user,pid); - - if(userNotifications == null || userNotifications.getNotifyForNewSubscribers()){ - notifyrecipients.add(user); + Manager[] managers = managerService.getManagers(pid); + List emails = new ArrayList(); + + for(Manager manager:managers){ + String userEmail = manager.getEmail(); + emails.add(userEmail); + + // Do not send the notification to the new manager/ subscriber. + if(userEmail.equals(rolesUtils.getEmail())) { + continue; + } + + Notifications userNotifications = notificationsDAO.findByManagerEmailAndPortalPid(userEmail,pid); + + if(userNotifications == null + || (newRoleType.equals("manager") && userNotifications.getNotifyForNewManagers()) + || (newRoleType.equals("subscriber") && userNotifications.getNotifyForNewSubscribers())){ + notifyrecipients.add(userEmail); } } if(notifyrecipients.size() > 0){ + email.setBody(email.getBody().replace("((__managers__))", String.join(", ", emails))); + return emailSender.send(notifyrecipients,email.getSubject(),email.getBody(), false); }else{ log.debug("There are no users to notify "); @@ -108,6 +112,27 @@ public class EmailController { return true; } +// @RequestMapping(value = "/notifyForNewSubscribers/{pid}", method = RequestMethod.POST) +// public Boolean notifyNewSubscribers(@PathVariable(value = "pid") String pid,@RequestBody Email email ) throws Exception { +// List notifyrecipients = new ArrayList(); +// if(portalDAO.findByPid(pid) == null){ +// throw new ContentNotFoundException("Portal not found"); +// } +// for(String user:email.getRecipients()){ +// Notifications userNotifications = notificationsDAO.findByManagerEmailAndPortalPid(user,pid); +// +// if(userNotifications == null || userNotifications.getNotifyForNewSubscribers()){ +// notifyrecipients.add(user); +// } +// } +// if(notifyrecipients.size() > 0){ +// return emailSender.send(notifyrecipients,email.getSubject(),email.getBody(), false); +// }else{ +// log.debug("There are no users to notify "); +// } +// +// return true; +// } @RequestMapping(value = "/test", method = RequestMethod.GET) public String test() throws Exception { log.debug("Test mail"); @@ -121,11 +146,11 @@ public class EmailController { email.setBody("Test body"); email.setSubject("Test theme"); String response = ""; - response+=this.notifyNewManagers("ee", email); + response+=this.notifyManagers("ee", "manager", email); log.debug("Notify managers "+response); response+=" "; - response+=this.notifyNewSubscribers("ee", email); + response+=this.notifyManagers("ee", "subscriber", email); log.debug("Notify for subscr "+response); return response; diff --git a/src/main/java/eu/dnetlib/uoaadmintools/entities/curator/CuratorResponse.java b/src/main/java/eu/dnetlib/uoaadmintools/entities/curator/CuratorResponse.java index 2cac643..8ddbfd7 100644 --- a/src/main/java/eu/dnetlib/uoaadmintools/entities/curator/CuratorResponse.java +++ b/src/main/java/eu/dnetlib/uoaadmintools/entities/curator/CuratorResponse.java @@ -6,7 +6,6 @@ import java.util.List; public class CuratorResponse { - private String email; private String name; private List affiliations; private String photo; @@ -16,21 +15,12 @@ public class CuratorResponse { } public CuratorResponse(Curator curator) { - this.email = curator.getEmail(); this.name = curator.getName(); this.affiliations = curator.getAffiliations(); this.photo = curator.getPhoto(); this.bio = curator.getBio(); } - public String getEmail() { - return email; - } - - public void setEmail(String email) { - this.email = email; - } - public String getName() { return name; }