diff --git a/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/NotificationEventsSubscribedConfig.java b/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/NotificationEventsSubscribedConfig.java index d2815ef..b8b47c4 100644 --- a/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/NotificationEventsSubscribedConfig.java +++ b/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/NotificationEventsSubscribedConfig.java @@ -13,5 +13,5 @@ import lombok.Data; public class NotificationEventsSubscribedConfig { UseCaseDescriptor ucd; List listNotificationEventSubscribed; - String linkToMessages; + String linkToNotificationsMessages; } 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 08053bf..569282d 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_POST, EMAIL, VRE_POST + USER_MESSAGE, EMAIL, VRE_POST }; } diff --git a/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/NotificationsPlugin.java b/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/NotificationsPlugin.java index e05b6e9..5e382e5 100644 --- a/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/NotificationsPlugin.java +++ b/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/NotificationsPlugin.java @@ -1,5 +1,6 @@ package org.gcube.application.cms.notifications; +import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashMap; @@ -12,6 +13,7 @@ import org.gcube.application.cms.implementations.utils.UserUtils; import org.gcube.application.cms.notifications.config.NotificationFor; import org.gcube.application.cms.notifications.config.SubscribeNotificationEvent; import org.gcube.application.cms.notifications.manage.ManageDoActionNotification; +import org.gcube.application.cms.notifications.substitutor.NMessagesPlaceholdersSubstitutorUtil; import org.gcube.application.cms.plugins.EventListenerPluginInterface; import org.gcube.application.cms.plugins.events.EventListener; import org.gcube.application.cms.plugins.events.EventManager; @@ -314,8 +316,8 @@ public class NotificationsPlugin extends AbstractPlugin implements EventListener notificationMapPerContext.setUcd(useCaseDescriptor); notificationMapPerContext .setListNotificationEventSubscribed(listNotificationEventsSubscribedPerUCD); - String linkToMessages = profileConfiguration.get("link_to_messages", String.class); - notificationMapPerContext.setLinkToMessages(linkToMessages); + String linkToMessages = profileConfiguration.get("link_to_notifications_messages", String.class); + notificationMapPerContext.setLinkToNotificationsMessages(linkToMessages); } setNotificationBindingMapPerContext(context, useCaseDescriptor, notificationMapPerContext); @@ -365,7 +367,7 @@ public class NotificationsPlugin extends AbstractPlugin implements EventListener NotificationEventsSubscribedConfig subscribedConfig = notificationMapPerContext .get(itemObserved.getUCD_Id()); - String linkToFileWithMessages = subscribedConfig.getLinkToMessages(); + String linkToFileWithMessages = subscribedConfig.getLinkToNotificationsMessages(); List list = subscribedConfig.getListNotificationEventSubscribed(); //Filtering list of SubscribeNotificationEvent for itemObserved Event @@ -373,17 +375,26 @@ public class NotificationsPlugin extends AbstractPlugin implements EventListener .filter(sne -> sne.getEvent().equals(itemObserved.getEvent())).collect(Collectors.toList()); log.debug(linkToFileWithMessages); - ManageDoActionNotification mdoact = new ManageDoActionNotification(itemObserved, filterList); - - for (SubscribeNotificationEvent subscribeNotificationEvent : filterList) { - List listNotificationFor = subscribeNotificationEvent.getNotificationFor(); - - UserUtils.getCurrent().getRoles(); - - for (NotificationFor notificationFor : listNotificationFor) { - notificationFor.getRoles(); - } + NMessagesPlaceholdersSubstitutorUtil nMPlaceholdersSUtil = null; + try { + nMPlaceholdersSUtil = new NMessagesPlaceholdersSubstitutorUtil(linkToFileWithMessages); + } catch (IOException e) { + log.error("Error instancing the {}. Please check the URL {}. Returning!!", NMessagesPlaceholdersSubstitutorUtil.class.getSimpleName(), linkToFileWithMessages); + return; } + + ManageDoActionNotification mdoact = new ManageDoActionNotification(itemObserved, nMPlaceholdersSUtil, filterList); + mdoact.manage(); + +// for (SubscribeNotificationEvent subscribeNotificationEvent : filterList) { +// List listNotificationFor = subscribeNotificationEvent.getNotificationFor(); +// +// UserUtils.getCurrent().getRoles(); +// +// for (NotificationFor notificationFor : listNotificationFor) { +// notificationFor.getRoles(); +// } +// } } diff --git a/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/config/Notify.java b/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/config/Notify.java index c892f35..8768a6f 100644 --- a/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/config/Notify.java +++ b/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/config/Notify.java @@ -25,6 +25,7 @@ public class Notify { NOTIFICATION_TYPE type; Boolean send; + String placeholder_title; String placeholder_msg; @JsonProperty("export_as_pdf") ExportAsPDF export_as_pdf; diff --git a/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/gis/GisClient.java b/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/gis/GisClient.java new file mode 100644 index 0000000..d2330e0 --- /dev/null +++ b/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/gis/GisClient.java @@ -0,0 +1,37 @@ +package org.gcube.application.cms.notifications.gis; + +import java.util.Map; + +import org.gcube.portlets.user.uriresolvermanager.UriResolverManager; +import org.gcube.portlets.user.uriresolvermanager.resolvers.query.GeoportalResolverQueryStringBuilder; +import org.gcube.portlets.user.uriresolvermanager.resolvers.query.GeoportalResolverQueryStringBuilder.RESOLVE_AS; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class GisClient { + + public static String gisLink(RESOLVE_AS resolveAs, String profileID, String projectID, String scope, boolean shortLink) { + log.debug("called gisLink as {} for scope {}, profileId {}, projectId {} ", resolveAs, scope, profileID, + projectID); + + String theLink = ""; + try { + log.debug("Trying to generate Geoportal Gis Link..."); + // Contacting the Geoportal-Resolver via UriResolverManager + UriResolverManager uriResolverManager = new UriResolverManager("GEO"); + GeoportalResolverQueryStringBuilder builder = new GeoportalResolverQueryStringBuilder(profileID, projectID); + builder.scope(scope); + builder.resolverAs(resolveAs); + Map params = builder.buildQueryParameters(); + theLink = uriResolverManager.getLink(params, shortLink); + log.info("Geoportal GisViewer link is {} ", theLink); + + } catch (Exception e) { + log.error("Error on creating the Geoportal GisViewer link for project id {}", projectID, e); + } + + return theLink; + } + +} 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 e47e158..b823553 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 @@ -2,6 +2,7 @@ package org.gcube.application.cms.notifications.manage; import java.util.ArrayList; import java.util.HashSet; +import java.util.LinkedHashMap; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -11,10 +12,14 @@ import org.gcube.application.cms.notifications.NotificationGenericConstants; 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.social.SocialClients; +import org.gcube.application.cms.notifications.substitutor.NMessagesPlaceholdersSubstitutorUtil; +import org.gcube.application.cms.notifications.substitutor.SubstitutorMessagesMap; 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.portlets.user.uriresolvermanager.resolvers.query.GeoportalResolverQueryStringBuilder.RESOLVE_AS; import lombok.extern.slf4j.Slf4j; @@ -30,16 +35,35 @@ public class ManageDoActionNotification { private List listSubscribedNotification; private ItemObserved itemObserved; + private NMessagesPlaceholdersSubstitutorUtil nMPlaceholdersSUtil; + private SubstitutorMessagesMap placeholderMapValues; /** * Instantiates a new manage do action notification. * - * @param itemObserved the item observed - * @param filterList the filter list + * @param itemObserved the item observed + * @param nMPlaceholdersSUtil the n M placeholders S util + * @param filterList the filter list */ - public ManageDoActionNotification(ItemObserved itemObserved, List filterList) { + public ManageDoActionNotification(ItemObserved itemObserved, + NMessagesPlaceholdersSubstitutorUtil nMPlaceholdersSUtil, List filterList) { this.listSubscribedNotification = filterList; + this.nMPlaceholdersSUtil = nMPlaceholdersSUtil; this.itemObserved = itemObserved; + + // Creating the map for replacing the placeholders with their values + placeholderMapValues = new SubstitutorMessagesMap(); + String gisLink = GisClient.gisLink(RESOLVE_AS.PRIVATE, itemObserved.getUCD_Id(), itemObserved.getProjectId(), + itemObserved.getContext().getName(), true); + placeholderMapValues.putGisLink(gisLink); + placeholderMapValues.putProjectId(itemObserved.getProjectId()); + LinkedHashMap docAsMap = itemObserved.getDocumentEntries(1); + String projectName = docAsMap.size() > 1 ? docAsMap.values().stream().findFirst().get() + "" + : itemObserved.getProjectId(); + placeholderMapValues.putProjectName(projectName); + + nMPlaceholdersSUtil.setPlaceholderMapValues(placeholderMapValues); + } /** @@ -74,7 +98,7 @@ public class ManageDoActionNotification { Thread notificationT = new Thread() { public void run() { - new ManageNotifyUser(notificationFor, user); + new ManageNotifyUser(notificationFor, nMPlaceholdersSUtil, user); }; }; @@ -93,7 +117,7 @@ public class ManageDoActionNotification { * Gets the list user compliant to roles. * * @param notificationFor the notification for - * @param itemObserved + * @param itemObserved the item observed * @return the list user compliant to roles */ // GET LIST USER COMPLIANT TO ROLES diff --git a/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/manage/ManageNotifyUser.java b/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/manage/ManageNotifyUser.java index 6dd757f..3e1bc6f 100644 --- a/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/manage/ManageNotifyUser.java +++ b/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/manage/ManageNotifyUser.java @@ -5,10 +5,12 @@ import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; +import org.gcube.application.cms.notifications.NotificationGenericConstants.NOTIFICATION_TYPE; import org.gcube.application.cms.notifications.config.NotificationFor; import org.gcube.application.cms.notifications.config.NotificationWhen; 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.portal.databook.shared.Post; import org.gcube.social_networking.socialnetworking.model.beans.MessageInputBean; @@ -22,32 +24,45 @@ public class ManageNotifyUser { private NotificationFor notificationFor; private User recipientUser; + private NMessagesPlaceholdersSubstitutorUtil nMPlaceholdersSUtil; - - public ManageNotifyUser(NotificationFor notificationFor, User recipientUser) { + public ManageNotifyUser(NotificationFor notificationFor, NMessagesPlaceholdersSubstitutorUtil nMPlaceholdersSUtil, + User recipientUser) { this.notificationFor = notificationFor; this.recipientUser = recipientUser; + this.nMPlaceholdersSUtil = nMPlaceholdersSUtil; } public void send() throws Exception { SocialClients socialClients = new SocialClients(); List listNotifies = toNotify(); - //TODO - //NotificationClient client = new NotificationClient(); - //client.send ?????? - + // TODO + // NotificationClient client = new NotificationClient(); + // client.send ?????? + for (Notify notify : listNotifies) { switch (notify.getType()) { - case USER_POST: - String placeholderMessage = notify.getPlaceholder_msg(); - List rec = Arrays.asList(new Recipient(recipientUser.getUsername())); - MessageInputBean message = new MessageInputBean("Test message", - "Sending message via client " + System.currentTimeMillis(), new ArrayList(rec)); - String idMessage = socialClients.writeMessage(message); + case USER_MESSAGE: + log.info("Notification type {}, send: {}", notify.getType(), notify.getSend()); + if (notify.getSend()) { + log.info("Sending message..."); + String subject = "Message"; + String body = null; + + subject = nMPlaceholdersSUtil.replacePlaceholder(notify.getPlaceholder_title()); + body = nMPlaceholdersSUtil.replacePlaceholder(notify.getPlaceholder_msg()); + log.debug("Sending subject: {}", subject); + log.debug("Sending body: {}", body); + List rec = Arrays.asList(new Recipient(recipientUser.getUsername())); + log.info("Sending message..."); + 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: - + log.info("Notification type {}, send: {}", notify.getType(), notify.getSend()); PostInputBean toWrite = null; Post thePost = socialClients.writeUserPost(toWrite); log.info("{} post created: {} ", notify.getType(), thePost); 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 new file mode 100644 index 0000000..e4f6b8e --- /dev/null +++ b/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/substitutor/NMessagesPlaceholdersSubstitutorUtil.java @@ -0,0 +1,67 @@ +package org.gcube.application.cms.notifications.substitutor; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.net.URL; +import java.util.HashMap; +import java.util.Properties; + +import org.apache.commons.text.StringSubstitutor; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class NMessagesPlaceholdersSubstitutorUtil { + + private String linkToNotificationMessages; + private Properties notificationMessagesProperties; + + private SubstitutorMessagesMap placeholderMapValues; + + private StringSubstitutor stringSub; + + public NMessagesPlaceholdersSubstitutorUtil(String linkToNotificationMessages) throws IOException { + this.linkToNotificationMessages = linkToNotificationMessages; + + try (BufferedInputStream in = new BufferedInputStream(new URL(linkToNotificationMessages).openStream())) { + + notificationMessagesProperties = new Properties(); + // load a properties file + notificationMessagesProperties.load(in); + + } catch (IOException e) { + log.error("Error on reading notificationMessagesFileURL for input: {}", linkToNotificationMessages, e); + throw e; + } + + } + + public String getLinkToNotificationMessages() { + return linkToNotificationMessages; + } + + public void setPlaceholderMapValues(SubstitutorMessagesMap placeholderMapValues) { + this.placeholderMapValues = placeholderMapValues; + stringSub = new StringSubstitutor(placeholderMapValues); + } + + public String replacePlaceholder(String notificationPlaceholder) { + + if (notificationPlaceholder == null || notificationPlaceholder.isEmpty()) + return notificationPlaceholder; + + if (stringSub != null) { + + log.info("notificationPlaceholder: {}", notificationPlaceholder); + String templateString = notificationMessagesProperties.getProperty(notificationPlaceholder); + log.debug("templateString: {}", templateString); + // Replace + String resolvedString = stringSub.replace(templateString); + log.info("resolvedString: {}", resolvedString); + return resolvedString; + } + log.info("placeholderMapValues is null, returning source: {}", notificationPlaceholder); + return notificationPlaceholder; + } + +} 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 new file mode 100644 index 0000000..bd4b18c --- /dev/null +++ b/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/substitutor/SubstitutorMessagesMap.java @@ -0,0 +1,39 @@ +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"; + public static final String USER = "user"; + + private static final long serialVersionUID = 1L; + + public SubstitutorMessagesMap() { + super(); + } + + @Override + public String put(String key, String value) { + return super.put(key, value); + } + + public String putUser(String username) { + return super.put(USER, username); + } + + public String putProjectName(String projectName) { + return super.put(PROJECT_NAME, projectName); + } + + public String putGisLink(String gisLink) { + return super.put(GIS_LINK, gisLink); + } + + public String putProjectId(String projectId) { + return super.put(PROJECT_ID, projectId); + } +} +