From 45164a02dab700c324f27f5f5ce8ba4960682b42 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Mon, 25 Mar 2024 11:16:40 +0100 Subject: [PATCH] add mapping to CatalogueEvent --- .../NotificationGenericConstants.java | 2 +- .../manage/ManageDoActionNotification.java | 72 +++++++------- .../cms/notifications/manage/NotifyUser.java | 97 ++++++++++++------- .../manage/mapping/MappingToCatalogue.java | 14 +-- .../notifications/social/SocialClients.java | 12 +++ .../NMessagesPlaceholdersSubstitutorUtil.java | 4 + .../substitutor/SubstitutorMessagesMap.java | 7 +- 7 files changed, 128 insertions(+), 80 deletions(-) diff --git a/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/NotificationGenericConstants.java b/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/NotificationGenericConstants.java index 569282d..08053bf 100644 --- a/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/NotificationGenericConstants.java +++ b/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/NotificationGenericConstants.java @@ -11,7 +11,7 @@ public class NotificationGenericConstants { }; public static enum NOTIFICATION_TYPE { - USER_MESSAGE, EMAIL, VRE_POST + USER_POST, EMAIL, VRE_POST }; } diff --git a/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/manage/ManageDoActionNotification.java b/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/manage/ManageDoActionNotification.java index 59c7485..96b69f4 100644 --- a/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/manage/ManageDoActionNotification.java +++ b/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/manage/ManageDoActionNotification.java @@ -13,6 +13,7 @@ import org.gcube.application.cms.notifications.config.NotificationFor; import org.gcube.application.cms.notifications.config.NotificationWhen; import org.gcube.application.cms.notifications.config.SubscribeNotificationEvent; import org.gcube.application.cms.notifications.gis.GisClient; +import org.gcube.application.cms.notifications.manage.mapping.MappingToCatalogue; import org.gcube.application.cms.notifications.social.SocialClients; import org.gcube.application.cms.notifications.substitutor.NMessagesPlaceholdersSubstitutorUtil; import org.gcube.application.cms.notifications.substitutor.SubstitutorMessagesMap; @@ -20,9 +21,8 @@ import org.gcube.application.cms.plugins.events.ItemObserved; import org.gcube.application.geoportal.common.model.document.Project; import org.gcube.application.geoportal.common.model.document.accounting.User; import org.gcube.application.geoportal.common.model.document.lifecycle.LifecycleInformation; -import org.gcube.common.authorization.utils.manager.SecretManager; -import org.gcube.common.authorization.utils.manager.SecretManagerProvider; import org.gcube.portlets.user.uriresolvermanager.resolvers.query.GeoportalResolverQueryStringBuilder.RESOLVE_AS; +import org.gcube.social_networking.socialnetworking.model.beans.catalogue.CatalogueEventType; import lombok.extern.slf4j.Slf4j; @@ -87,7 +87,7 @@ public class ManageDoActionNotification { String lastInvokedStep = lInfo.getLastInvokedStep(); log.debug("ItemObserved phase is: {}, lastInvokedStep is: {}", itemPhase, lastInvokedStep); - final SecretManager cm = SecretManagerProvider.instance.get(); + //final SecretManager cm = SecretManagerProvider.instance.get(); for (final NotificationFor notificationFor : listNotificationFor) { // Filtering notifications on 'phase' and 'lastInvokedStep' according to item @@ -100,39 +100,23 @@ public class ManageDoActionNotification { if (notificationsCompliantToPhase != null && notificationsCompliantToPhase.size() > 0) { // GET LIST USER COMPLIANT TO ROLES - List notifyUsers = getListUserCompliantToRoles(notificationFor, itemObserved); + List notifyUsers = getListUserCompliantToRoles(notificationFor, itemObserved, true); final SocialClients socialClients = new SocialClients(); - + CatalogueEventType catalogueEventType = MappingToCatalogue.toCatalogueEventType(itemPhase, lastInvokedStep); + if (notifyUsers.size() > 0) { log.info("Users compliant to role are: {}", notifyUsers.size()); if (log.isDebugEnabled()) { - notifyUsers.stream().forEach(u -> log.debug("user: " + u)); + notifyUsers.stream().forEach(u -> log.debug("notifying user: " + u)); } - - for (final User user : notifyUsers) { - try { - - NotifyUser mnu = new NotifyUser(socialClients, notificationsCompliantToPhase, - nMPlaceholdersS, user); - mnu.send(); - } catch (Exception e) { - log.error("Error occurred when send notify: ", e); - } - + + try { + NotifyUser mnu = new NotifyUser(socialClients, notificationsCompliantToPhase, + nMPlaceholdersS, notifyUsers, catalogueEventType); + mnu.send(); + } catch (Exception e) { + log.error("Error occurred when send notify: ", e); } - - /* - * for (final User user : notifyUsers) { Thread notificationT = new Thread() { - * public void run() { SecretManagerProvider.instance.set(cm); NotifyUser mnu = - * new NotifyUser(socialClients, notificationsCompliantToPhase, nMPlaceholdersS, - * user); try { mnu.send(); } catch (Exception e) { - * log.error("Error occurred when send notify: ", e); } }; - * - * }; - * - * notificationT.start(); } - */ - } } } @@ -150,7 +134,8 @@ public class ManageDoActionNotification { for (NotificationWhen notificationWhen : notificationFor.getWhen()) { log.info(++i + ") notificationWhen target phases: {} and last_invoked_step: {}", notificationWhen.getTarget_phase(), notificationWhen.getLast_invoked_step()); - if (notificationWhen.getTarget_phase().contains(itemPhase)) { + if (notificationWhen.getTarget_phase().contains(itemPhase) || notificationWhen.getTarget_phase() + .contains(NotificationGenericConstants.CUSTOM_TARGET_PHASE.Any.name())) { if (notificationWhen.getLast_invoked_step() == null) { // no filtering on lastInvokedStep filteredList.add(notificationWhen); @@ -171,24 +156,33 @@ public class ManageDoActionNotification { /** * Gets the list user compliant to roles. * - * @param notificationFor the notification for - * @param itemObserved the item observed + * @param notificationFor the notification for + * @param itemObserved the item observed + * @param ignoreUserCaller the ignore user caller * @return the list user compliant to roles */ - // GET LIST USER COMPLIANT TO ROLES - public List getListUserCompliantToRoles(NotificationFor notificationFor, ItemObserved itemObserved) { - log.debug("getListUserCompliantToRoles called for scope: {}", UserUtils.getCurrent().getContext()); + // GET LIST USER COMPLIANT TO ROLES, ignore USER caller + public List getListUserCompliantToRoles(NotificationFor notificationFor, ItemObserved itemObserved, + boolean ignoreUserCaller) { + log.debug("getListUserCompliantToRoles called for scope: {}, ignore user caller: {}", itemObserved.getContext(), + ignoreUserCaller); List notifyUserWithRoles = notificationFor.getRoles(); List notifyUsers = new ArrayList(); String anyRole = NotificationGenericConstants.CUSTOM_USER_ROLES.Any.name(); SocialClients socialClients = new SocialClients(); + User userCaller = itemObserved.getUserCaller(); if (notifyUserWithRoles.contains(anyRole)) { try { // String token = AccessTokenProvider.instance.set(s); List listUsername = socialClients.getUsernamesByScope(); notifyUsers.addAll( listUsername.stream().map(username -> new User(username, null)).collect(Collectors.toList())); + + if (ignoreUserCaller) { + notifyUsers.removeIf(u -> u.equals(userCaller)); + } + } catch (Exception e) { log.error("Error on reading users from scope {}: ", UserUtils.getCurrent().getContext(), e); } @@ -198,7 +192,6 @@ public class ManageDoActionNotification { // We need to create the list of users that must be notified for (String role : notifyUserWithRoles) { - // Reading the list of users in the VRE with specific role // Creator of the item String itemCreatorRole = NotificationGenericConstants.CUSTOM_USER_ROLES.Item_Creator.name(); if (role.compareTo(itemCreatorRole) == 0) { @@ -213,12 +206,17 @@ public class ManageDoActionNotification { UserUtils.getCurrent().getContext(), e); } } else { + // Reading the list of users in the VRE with specific role try { List listUsernameForRole = socialClients.getUsernamesByRole(role); notifyUsers.addAll(listUsernameForRole.stream() .map(username -> new User(username, Stream.of(role).collect(Collectors.toCollection(HashSet::new)))) .collect(Collectors.toList())); + + if (ignoreUserCaller) { + notifyUsers.removeIf(u -> u.equals(userCaller)); + } log.info("added {} users for role {}", notifyUsers.size(), role); } catch (Exception e) { log.error("Error on reading users from scope {}: ", UserUtils.getCurrent().getContext(), e); diff --git a/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/manage/NotifyUser.java b/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/manage/NotifyUser.java index d5d97fc..0acabb3 100644 --- a/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/manage/NotifyUser.java +++ b/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/manage/NotifyUser.java @@ -2,7 +2,6 @@ package org.gcube.application.cms.notifications.manage; import java.net.URL; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; @@ -11,11 +10,11 @@ import org.gcube.application.cms.notifications.config.Notify; import org.gcube.application.cms.notifications.social.SocialClients; import org.gcube.application.cms.notifications.substitutor.NMessagesPlaceholdersSubstitutorUtil; import org.gcube.application.geoportal.common.model.document.accounting.User; +import org.gcube.common.authorization.utils.manager.SecretManager; import org.gcube.common.authorization.utils.manager.SecretManagerProvider; import org.gcube.portal.databook.shared.Post; import org.gcube.social_networking.social_networking_client_library.NotificationClient; import org.gcube.social_networking.socialnetworking.model.beans.PostInputBean; -import org.gcube.social_networking.socialnetworking.model.beans.Recipient; import org.gcube.social_networking.socialnetworking.model.beans.catalogue.CatalogueEvent; import org.gcube.social_networking.socialnetworking.model.beans.catalogue.CatalogueEventType; @@ -24,54 +23,44 @@ import lombok.extern.slf4j.Slf4j; @Slf4j public class NotifyUser { - private User recipientUser; + private List recipientUsers; private NMessagesPlaceholdersSubstitutorUtil nMPlaceholdersSUtil; private List listNotificationWhen; private SocialClients socialClients; + private CatalogueEventType catalogueEventType; public NotifyUser(SocialClients socialClients, List listWhen, - NMessagesPlaceholdersSubstitutorUtil nMPlaceholdersSUtil, User recipientUser) { + NMessagesPlaceholdersSubstitutorUtil nMPlaceholdersSUtil, List recipientUsers, CatalogueEventType catalogueEventType) { this.socialClients = socialClients; this.listNotificationWhen = listWhen; - this.recipientUser = recipientUser; + this.recipientUsers = recipientUsers; this.nMPlaceholdersSUtil = nMPlaceholdersSUtil; + this.catalogueEventType = catalogueEventType; } + public void send() throws Exception { - log.info("send call..."); + log.info("send notify..."); List listNotifies = toNotify(); - // TODO - // NotificationClient client = new NotificationClient(); - // client.send ?????? - for (Notify notify : listNotifies) { switch (notify.getType()) { - case USER_MESSAGE: + case USER_POST: log.info("Notification type {}, send: {}", notify.getType(), notify.getSend()); if (notify.getSend()) { log.debug("Building message..."); String subject = "Message"; String body = null; -// PostClient postClient = socialClients.getPostClient(); -// PostInputBean postInputBean = new PostInputBean(); -// postInputBean.setEnablenotification(true); -// postInputBean.setText(message); -// -// socialClients.writeUserPost(null) - subject = nMPlaceholdersSUtil.replacePlaceholder(notify.getPlaceholder_title()); body = nMPlaceholdersSUtil.replacePlaceholder(notify.getPlaceholder_msg()); + log.debug("subject: {}", subject); log.debug("body: {}", body); - List rec = Arrays.asList(new Recipient(recipientUser.getUsername())); - log.info("Sending message to users: {}", rec); + log.info("Sending message to users: {}", recipientUsers); + + postMessage(subject, body, catalogueEventType); -// sendNotification(null); -// MessageInputBean message = new MessageInputBean(subject, body, new ArrayList(rec)); -// String idMessage = socialClients.writeMessage(message); -// log.info("Message sent with id: {}", idMessage); } break; case VRE_POST: @@ -92,12 +81,61 @@ public class NotifyUser { } + private List toNotify() { + List listNotifies = new ArrayList(); + for (NotificationWhen notificationWhen : listNotificationWhen) { + listNotifies.addAll(notificationWhen.getNotify().stream().filter(n -> n.getSend() == true) + .collect(Collectors.toList())); + } + return listNotifies; + } + + protected void postMessage(String subject, String bodyMessage, CatalogueEventType catalogueEventType) throws Exception { + CatalogueEvent catalogueEvent = getCatalogueEvent(subject, bodyMessage); + SecretManager secretManager = SecretManagerProvider.instance.get(); + + //GET geoportal SECRET + +// Secret secret = Constants.getCatalogueSecret(); +// if(notificationSentByGCat) { +// secretManager.startSession(secret); +// } +// try { +// sendNotification(catalogueEvent); +// }finally { +// if(notificationSentByGCat) { +// secretManager.endSession(); +// } +// } + + sendNotification(catalogueEvent); + } + + protected CatalogueEvent getCatalogueEvent(String subject, String bodyMessage) throws Exception { + CatalogueEvent catalogueEvent = new CatalogueEvent(); + catalogueEvent.setType(catalogueEventType); + catalogueEvent.setNotifyText(bodyMessage); + catalogueEvent.setItemId(subject); + String itemURL = nMPlaceholdersSUtil.getPlaceholderMapValues().getGisLink(); + if(itemURL!=null) { + catalogueEvent.setItemURL(new URL(itemURL)); + } + + // Adding recipient users + String[] usersToNotify = recipientUsers.stream().map(u -> u.getUsername()).toArray(String[]::new); + catalogueEvent.setIdsToNotify(usersToNotify); + catalogueEvent.setIdsAsGroup(false); + + return catalogueEvent; + } + + protected void sendNotification(CatalogueEvent catalogueEvent) throws Exception { Thread thread = new Thread() { public void run() { try { log.trace("{} is going to send the following notification {}", SecretManagerProvider.instance.get().getUser().getUsername(), catalogueEvent); - NotificationClient nc = new NotificationClient(); + NotificationClient nc = socialClients.getNotificationClient(); nc.sendCatalogueEvent(catalogueEvent); } catch(Exception e) { log.error("Error while sending notification.", e); @@ -122,13 +160,6 @@ public class NotifyUser { return catalogueEvent; } - private List toNotify() { - List listNotifies = new ArrayList(); - for (NotificationWhen notificationWhen : listNotificationWhen) { - listNotifies.addAll(notificationWhen.getNotify().stream().filter(n -> n.getSend() == true) - .collect(Collectors.toList())); - } - return listNotifies; - } + } diff --git a/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/manage/mapping/MappingToCatalogue.java b/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/manage/mapping/MappingToCatalogue.java index f9210d9..d75c8b2 100644 --- a/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/manage/mapping/MappingToCatalogue.java +++ b/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/manage/mapping/MappingToCatalogue.java @@ -11,10 +11,10 @@ public class MappingToCatalogue { // SimpleLifeCycleManager if (itemPhase.equals(SimpleLifeCycleManager.Phases.PUBLISHED) - && lastInvokedStep.equals(SimpleLifeCycleManager.Steps.PUBLISH.getId())) { + && (lastInvokedStep == null || lastInvokedStep.equals(SimpleLifeCycleManager.Steps.PUBLISH.getId()))) { return CatalogueEventType.ITEM_SUBMITTED; - } else if (itemPhase.equals(SimpleLifeCycleManager.Phases.UNPUBLISHED) - && lastInvokedStep.equals(SimpleLifeCycleManager.Steps.UNPUBLISH.getId())) { + } else if (itemPhase.equals(SimpleLifeCycleManager.Phases.UNPUBLISHED) && (lastInvokedStep == null + || lastInvokedStep.equals(SimpleLifeCycleManager.Steps.UNPUBLISH.getId()))) { // TODO // Missing ITEM UNPUBLISHED; @@ -22,16 +22,16 @@ public class MappingToCatalogue { // Default3PhaseManager if (itemPhase.equals(Default3PhaseManager.Phases.PENDING_APPROVAL) - && lastInvokedStep.equals(Default3PhaseManager.STEPS.SUBMIT.getId())) { + && (lastInvokedStep == null || lastInvokedStep.equals(Default3PhaseManager.STEPS.SUBMIT.getId()))) { return CatalogueEventType.ITEM_SUBMITTED; } else if (itemPhase.equals(Default3PhaseManager.Phases.PUBLISHED) - && lastInvokedStep.equals(Default3PhaseManager.STEPS.APPROVE.getId())) { + && (lastInvokedStep == null || lastInvokedStep.equals(Default3PhaseManager.STEPS.APPROVE.getId()))) { return CatalogueEventType.ITEM_PUBLISHED; } else if (itemPhase.equals(LifecycleInformation.CommonPhases.DRAFT_PHASE) - && lastInvokedStep.equals(Default3PhaseManager.STEPS.REJECT.getId())) { + && (lastInvokedStep == null || lastInvokedStep.equals(Default3PhaseManager.STEPS.REJECT.getId()))) { return CatalogueEventType.ITEM_REJECTED; } else if (itemPhase.equals(LifecycleInformation.CommonPhases.DRAFT_PHASE) - && lastInvokedStep.equals(Default3PhaseManager.STEPS.UNPUBLISH.getId())) { + && (lastInvokedStep == null || lastInvokedStep.equals(Default3PhaseManager.STEPS.UNPUBLISH.getId()))) { // TODO // Missing ITEM UNPUBLISHED; } diff --git a/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/social/SocialClients.java b/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/social/SocialClients.java index 2589a9d..207188a 100644 --- a/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/social/SocialClients.java +++ b/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/social/SocialClients.java @@ -5,6 +5,7 @@ import java.util.List; import org.gcube.portal.databook.shared.Post; import org.gcube.social_networking.social_networking_client_library.MessageClient; +import org.gcube.social_networking.social_networking_client_library.NotificationClient; import org.gcube.social_networking.social_networking_client_library.PostClient; import org.gcube.social_networking.social_networking_client_library.UserClient; import org.gcube.social_networking.socialnetworking.model.beans.MessageInputBean; @@ -25,6 +26,7 @@ public class SocialClients { private PostClient postClient; private MessageClient messagesClient; private UserClient userClient; + private NotificationClient notificationClient; /** * Instantiates a new social util. @@ -47,6 +49,12 @@ public class SocialClients { } catch (Exception e) { log.error("Error on instacing {}: ", UserClient.class.getSimpleName(), e); } + + try { + this.notificationClient = new NotificationClient(); + } catch (Exception e) { + log.error("Error on instacing {}: ", NotificationClient.class.getSimpleName(), e); + } } /** @@ -106,5 +114,9 @@ public class SocialClients { public UserClient getUserClient() { return userClient; } + + public NotificationClient getNotificationClient() { + return notificationClient; + } } diff --git a/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/substitutor/NMessagesPlaceholdersSubstitutorUtil.java b/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/substitutor/NMessagesPlaceholdersSubstitutorUtil.java index 50f1c04..0b338bc 100644 --- a/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/substitutor/NMessagesPlaceholdersSubstitutorUtil.java +++ b/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/substitutor/NMessagesPlaceholdersSubstitutorUtil.java @@ -64,5 +64,9 @@ public class NMessagesPlaceholdersSubstitutorUtil { log.info("placeholderMapValues is null, returning source: {}", notificationPlaceholder); return notificationPlaceholder; } + + public SubstitutorMessagesMap getPlaceholderMapValues() { + return placeholderMapValues; + } } diff --git a/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/substitutor/SubstitutorMessagesMap.java b/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/substitutor/SubstitutorMessagesMap.java index bd4b18c..a4b57cf 100644 --- a/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/substitutor/SubstitutorMessagesMap.java +++ b/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/substitutor/SubstitutorMessagesMap.java @@ -3,7 +3,7 @@ package org.gcube.application.cms.notifications.substitutor; import java.util.HashMap; public class SubstitutorMessagesMap extends HashMap { - + public static final String PROJECT_ID = "project_id"; public static final String GIS_LINK = "gis_link"; public static final String PROJECT_NAME = "project_name"; @@ -35,5 +35,8 @@ public class SubstitutorMessagesMap extends HashMap { public String putProjectId(String projectId) { return super.put(PROJECT_ID, projectId); } -} + public String getGisLink() { + return super.get(GIS_LINK); + } +}