in progress

This commit is contained in:
Francesco Mangiacrapa 2024-03-21 15:53:54 +01:00
parent 1e50bd74ca
commit 01bad9d30f
9 changed files with 227 additions and 33 deletions

View File

@ -13,5 +13,5 @@ import lombok.Data;
public class NotificationEventsSubscribedConfig { public class NotificationEventsSubscribedConfig {
UseCaseDescriptor ucd; UseCaseDescriptor ucd;
List<SubscribeNotificationEvent> listNotificationEventSubscribed; List<SubscribeNotificationEvent> listNotificationEventSubscribed;
String linkToMessages; String linkToNotificationsMessages;
} }

View File

@ -11,7 +11,7 @@ public class NotificationGenericConstants {
}; };
public static enum NOTIFICATION_TYPE { public static enum NOTIFICATION_TYPE {
USER_POST, EMAIL, VRE_POST USER_MESSAGE, EMAIL, VRE_POST
}; };
} }

View File

@ -1,5 +1,6 @@
package org.gcube.application.cms.notifications; package org.gcube.application.cms.notifications;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap; 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.NotificationFor;
import org.gcube.application.cms.notifications.config.SubscribeNotificationEvent; import org.gcube.application.cms.notifications.config.SubscribeNotificationEvent;
import org.gcube.application.cms.notifications.manage.ManageDoActionNotification; 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.EventListenerPluginInterface;
import org.gcube.application.cms.plugins.events.EventListener; import org.gcube.application.cms.plugins.events.EventListener;
import org.gcube.application.cms.plugins.events.EventManager; import org.gcube.application.cms.plugins.events.EventManager;
@ -314,8 +316,8 @@ public class NotificationsPlugin extends AbstractPlugin implements EventListener
notificationMapPerContext.setUcd(useCaseDescriptor); notificationMapPerContext.setUcd(useCaseDescriptor);
notificationMapPerContext notificationMapPerContext
.setListNotificationEventSubscribed(listNotificationEventsSubscribedPerUCD); .setListNotificationEventSubscribed(listNotificationEventsSubscribedPerUCD);
String linkToMessages = profileConfiguration.get("link_to_messages", String.class); String linkToMessages = profileConfiguration.get("link_to_notifications_messages", String.class);
notificationMapPerContext.setLinkToMessages(linkToMessages); notificationMapPerContext.setLinkToNotificationsMessages(linkToMessages);
} }
setNotificationBindingMapPerContext(context, useCaseDescriptor, notificationMapPerContext); setNotificationBindingMapPerContext(context, useCaseDescriptor, notificationMapPerContext);
@ -365,7 +367,7 @@ public class NotificationsPlugin extends AbstractPlugin implements EventListener
NotificationEventsSubscribedConfig subscribedConfig = notificationMapPerContext NotificationEventsSubscribedConfig subscribedConfig = notificationMapPerContext
.get(itemObserved.getUCD_Id()); .get(itemObserved.getUCD_Id());
String linkToFileWithMessages = subscribedConfig.getLinkToMessages(); String linkToFileWithMessages = subscribedConfig.getLinkToNotificationsMessages();
List<SubscribeNotificationEvent> list = subscribedConfig.getListNotificationEventSubscribed(); List<SubscribeNotificationEvent> list = subscribedConfig.getListNotificationEventSubscribed();
//Filtering list of SubscribeNotificationEvent for itemObserved Event //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()); .filter(sne -> sne.getEvent().equals(itemObserved.getEvent())).collect(Collectors.toList());
log.debug(linkToFileWithMessages); log.debug(linkToFileWithMessages);
ManageDoActionNotification mdoact = new ManageDoActionNotification(itemObserved, filterList); NMessagesPlaceholdersSubstitutorUtil nMPlaceholdersSUtil = null;
try {
for (SubscribeNotificationEvent subscribeNotificationEvent : filterList) { nMPlaceholdersSUtil = new NMessagesPlaceholdersSubstitutorUtil(linkToFileWithMessages);
List<NotificationFor> listNotificationFor = subscribeNotificationEvent.getNotificationFor(); } catch (IOException e) {
log.error("Error instancing the {}. Please check the URL {}. Returning!!", NMessagesPlaceholdersSubstitutorUtil.class.getSimpleName(), linkToFileWithMessages);
UserUtils.getCurrent().getRoles(); return;
for (NotificationFor notificationFor : listNotificationFor) {
notificationFor.getRoles();
}
} }
ManageDoActionNotification mdoact = new ManageDoActionNotification(itemObserved, nMPlaceholdersSUtil, filterList);
mdoact.manage();
// for (SubscribeNotificationEvent subscribeNotificationEvent : filterList) {
// List<NotificationFor> listNotificationFor = subscribeNotificationEvent.getNotificationFor();
//
// UserUtils.getCurrent().getRoles();
//
// for (NotificationFor notificationFor : listNotificationFor) {
// notificationFor.getRoles();
// }
// }
} }

View File

@ -25,6 +25,7 @@ public class Notify {
NOTIFICATION_TYPE type; NOTIFICATION_TYPE type;
Boolean send; Boolean send;
String placeholder_title;
String placeholder_msg; String placeholder_msg;
@JsonProperty("export_as_pdf") @JsonProperty("export_as_pdf")
ExportAsPDF export_as_pdf; ExportAsPDF export_as_pdf;

View File

@ -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<String, String> 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;
}
}

View File

@ -2,6 +2,7 @@ package org.gcube.application.cms.notifications.manage;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; 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.NotificationFor;
import org.gcube.application.cms.notifications.config.NotificationWhen; import org.gcube.application.cms.notifications.config.NotificationWhen;
import org.gcube.application.cms.notifications.config.SubscribeNotificationEvent; 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.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.cms.plugins.events.ItemObserved;
import org.gcube.application.geoportal.common.model.document.Project; 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.accounting.User;
import org.gcube.portlets.user.uriresolvermanager.resolvers.query.GeoportalResolverQueryStringBuilder.RESOLVE_AS;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -30,16 +35,35 @@ public class ManageDoActionNotification {
private List<SubscribeNotificationEvent> listSubscribedNotification; private List<SubscribeNotificationEvent> listSubscribedNotification;
private ItemObserved<Project> itemObserved; private ItemObserved<Project> itemObserved;
private NMessagesPlaceholdersSubstitutorUtil nMPlaceholdersSUtil;
private SubstitutorMessagesMap placeholderMapValues;
/** /**
* Instantiates a new manage do action notification. * Instantiates a new manage do action notification.
* *
* @param itemObserved the item observed * @param itemObserved the item observed
* @param filterList the filter list * @param nMPlaceholdersSUtil the n M placeholders S util
* @param filterList the filter list
*/ */
public ManageDoActionNotification(ItemObserved<Project> itemObserved, List<SubscribeNotificationEvent> filterList) { public ManageDoActionNotification(ItemObserved<Project> itemObserved,
NMessagesPlaceholdersSubstitutorUtil nMPlaceholdersSUtil, List<SubscribeNotificationEvent> filterList) {
this.listSubscribedNotification = filterList; this.listSubscribedNotification = filterList;
this.nMPlaceholdersSUtil = nMPlaceholdersSUtil;
this.itemObserved = itemObserved; 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<String, Object> 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() { Thread notificationT = new Thread() {
public void run() { 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. * Gets the list user compliant to roles.
* *
* @param notificationFor the notification for * @param notificationFor the notification for
* @param itemObserved * @param itemObserved the item observed
* @return the list user compliant to roles * @return the list user compliant to roles
*/ */
// GET LIST USER COMPLIANT TO ROLES // GET LIST USER COMPLIANT TO ROLES

View File

@ -5,10 +5,12 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; 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.NotificationFor;
import org.gcube.application.cms.notifications.config.NotificationWhen; import org.gcube.application.cms.notifications.config.NotificationWhen;
import org.gcube.application.cms.notifications.config.Notify; import org.gcube.application.cms.notifications.config.Notify;
import org.gcube.application.cms.notifications.social.SocialClients; 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.application.geoportal.common.model.document.accounting.User;
import org.gcube.portal.databook.shared.Post; import org.gcube.portal.databook.shared.Post;
import org.gcube.social_networking.socialnetworking.model.beans.MessageInputBean; import org.gcube.social_networking.socialnetworking.model.beans.MessageInputBean;
@ -22,32 +24,45 @@ public class ManageNotifyUser {
private NotificationFor notificationFor; private NotificationFor notificationFor;
private User recipientUser; private User recipientUser;
private NMessagesPlaceholdersSubstitutorUtil nMPlaceholdersSUtil;
public ManageNotifyUser(NotificationFor notificationFor, NMessagesPlaceholdersSubstitutorUtil nMPlaceholdersSUtil,
public ManageNotifyUser(NotificationFor notificationFor, User recipientUser) { User recipientUser) {
this.notificationFor = notificationFor; this.notificationFor = notificationFor;
this.recipientUser = recipientUser; this.recipientUser = recipientUser;
this.nMPlaceholdersSUtil = nMPlaceholdersSUtil;
} }
public void send() throws Exception { public void send() throws Exception {
SocialClients socialClients = new SocialClients(); SocialClients socialClients = new SocialClients();
List<Notify> listNotifies = toNotify(); List<Notify> listNotifies = toNotify();
//TODO // TODO
//NotificationClient client = new NotificationClient(); // NotificationClient client = new NotificationClient();
//client.send ?????? // client.send ??????
for (Notify notify : listNotifies) { for (Notify notify : listNotifies) {
switch (notify.getType()) { switch (notify.getType()) {
case USER_POST: case USER_MESSAGE:
String placeholderMessage = notify.getPlaceholder_msg(); log.info("Notification type {}, send: {}", notify.getType(), notify.getSend());
List<Recipient> rec = Arrays.asList(new Recipient(recipientUser.getUsername())); if (notify.getSend()) {
MessageInputBean message = new MessageInputBean("Test message", log.info("Sending message...");
"Sending message via client " + System.currentTimeMillis(), new ArrayList<Recipient>(rec)); String subject = "Message";
String idMessage = socialClients.writeMessage(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<Recipient> rec = Arrays.asList(new Recipient(recipientUser.getUsername()));
log.info("Sending message...");
MessageInputBean message = new MessageInputBean(subject, body, new ArrayList<Recipient>(rec));
String idMessage = socialClients.writeMessage(message);
log.info("Message sent with id: {}", idMessage);
}
break; break;
case VRE_POST: case VRE_POST:
log.info("Notification type {}, send: {}", notify.getType(), notify.getSend());
PostInputBean toWrite = null; PostInputBean toWrite = null;
Post thePost = socialClients.writeUserPost(toWrite); Post thePost = socialClients.writeUserPost(toWrite);
log.info("{} post created: {} ", notify.getType(), thePost); log.info("{} post created: {} ", notify.getType(), thePost);

View File

@ -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;
}
}

View File

@ -0,0 +1,39 @@
package org.gcube.application.cms.notifications.substitutor;
import java.util.HashMap;
public class SubstitutorMessagesMap extends HashMap<String, String> {
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);
}
}