in progess

This commit is contained in:
Francesco Mangiacrapa 2024-02-01 16:00:01 +01:00
parent a2c3f44dcd
commit a0c77b4e68
4 changed files with 120 additions and 46 deletions

View File

@ -3,6 +3,7 @@ package org.gcube.application.cms.notifications;
import java.util.List; import java.util.List;
import org.gcube.application.cms.notifications.config.SubscribeNotificationEvent; import org.gcube.application.cms.notifications.config.SubscribeNotificationEvent;
import org.gcube.application.geoportal.common.model.useCaseDescriptor.UseCaseDescriptor;
import org.gcube.com.fasterxml.jackson.annotation.JsonIgnoreProperties; import org.gcube.com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data; import lombok.Data;
@ -10,6 +11,7 @@ import lombok.Data;
@Data @Data
@JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnoreProperties(ignoreUnknown = true)
public class NotificationEventsSubscribedConfig { public class NotificationEventsSubscribedConfig {
String context; UseCaseDescriptor ucd;
List<SubscribeNotificationEvent> listNotificationEventSubscribed; List<SubscribeNotificationEvent> listNotificationEventSubscribed;
String linkToMessages;
} }

View File

@ -1,12 +1,16 @@
package org.gcube.application.cms.notifications; package org.gcube.application.cms.notifications;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
import org.bson.Document; import org.bson.Document;
import org.gcube.application.cms.implementations.utils.UserUtils; 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.config.SubscribeNotificationEvent;
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;
@ -51,6 +55,9 @@ public class NotificationsPlugin extends AbstractPlugin implements EventListener
DESCRIPTOR.setDescription("Manage the notification from Geoportal engine"); DESCRIPTOR.setDescription("Manage the notification from Geoportal engine");
} }
// This is a map "<Context,<UCD_ID,NotificationEventsSubscribedConfig>"
protected Map<String, Map<String, NotificationEventsSubscribedConfig>> notificationEventsBindingMap = null;
/** /**
* Inits the. * Inits the.
* *
@ -160,7 +167,7 @@ public class NotificationsPlugin extends AbstractPlugin implements EventListener
String context = UserUtils.getCurrent().getContext(); String context = UserUtils.getCurrent().getContext();
if (getNotificationBindingMapPerContext() == null) { if (getNotificationBindingMapPerContext() == null) {
log.info("Initializing in " + context); log.info("Initializing in " + context);
notificationEventsBindingMap.put(context, new NotificationEventsSubscribedConfig()); notificationEventsBindingMap.put(context, new HashMap<String, NotificationEventsSubscribedConfig>());
} }
report.setStatus(Report.Status.OK); report.setStatus(Report.Status.OK);
report.putMessage("Initialized " + DESCRIPTOR.getId() + " in the " + context); report.putMessage("Initialized " + DESCRIPTOR.getId() + " in the " + context);
@ -184,11 +191,17 @@ public class NotificationsPlugin extends AbstractPlugin implements EventListener
NotificationEventsSubscribedConfig eventsSub = readNotificationsSubscribedFromConfigurationInTheUCD( NotificationEventsSubscribedConfig eventsSub = readNotificationsSubscribedFromConfigurationInTheUCD(
observerd.getUseCaseDescriptor()); observerd.getUseCaseDescriptor());
List<SubscribeNotificationEvent> listEvents = eventsSub.getListNotificationEventSubscribed(); if (eventsSub != null) {
log.info("List events is {}", listEvents);
if (listEvents.contains(observerd.getEvent())) { List<SubscribeNotificationEvent> listEvents = eventsSub.getListNotificationEventSubscribed();
log.info("the event {} is subscribed from config ", observerd.getEvent()); log.info("List events is {}", listEvents);
return true; if (listEvents != null
&& listEvents.stream().anyMatch(sne -> sne.getEvent().equals(observerd.getEvent()))) {
log.info("the event {} is subscribed from config ", observerd.getEvent());
return true;
}
log.info("the event {} is not subscribed from config ", observerd.getEvent());
return false;
} }
log.info("the event {} is not subscribed from config ", observerd.getEvent()); log.info("the event {} is not subscribed from config ", observerd.getEvent());
return false; return false;
@ -200,20 +213,40 @@ public class NotificationsPlugin extends AbstractPlugin implements EventListener
} }
protected Map<String, NotificationEventsSubscribedConfig> notificationEventsBindingMap = null;
/** /**
* Gets the notification binding map per context. * Gets the notification binding map per context.
* *
* @return the notification binding map per context * @return the notification binding map per context
*/ */
protected NotificationEventsSubscribedConfig getNotificationBindingMapPerContext() { protected Map<String, NotificationEventsSubscribedConfig> getNotificationBindingMapPerContext() {
String context = UserUtils.getCurrent().getContext(); String context = UserUtils.getCurrent().getContext();
log.debug("Getting {} from cache map for context {}", NotificationsPlugin.PLUGIN_ID, context); log.debug("Getting {} from cache map for context {}", NotificationsPlugin.PLUGIN_ID, context);
if (notificationEventsBindingMap == null) if (notificationEventsBindingMap == null) {
notificationEventsBindingMap = new LinkedHashMap<String, NotificationEventsSubscribedConfig>(); notificationEventsBindingMap = new LinkedHashMap<String, Map<String, NotificationEventsSubscribedConfig>>();
}
return notificationEventsBindingMap.get(context); // read notification events binding subscribed in the context
Map<String, NotificationEventsSubscribedConfig> map = notificationEventsBindingMap.get(context);
return map == null ? new LinkedHashMap<String, NotificationEventsSubscribedConfig>() : map;
}
/**
* Sets the notification binding map per context.
*
* @param context the context
* @param ucd the ucd
* @param notification the notification
*/
private void setNotificationBindingMapPerContext(String context, UseCaseDescriptor ucd,
NotificationEventsSubscribedConfig notification) {
Map<String, NotificationEventsSubscribedConfig> mapNotificationConfig = notificationEventsBindingMap
.get(context);
if (mapNotificationConfig == null) {
mapNotificationConfig = new LinkedHashMap<String, NotificationEventsSubscribedConfig>();
}
mapNotificationConfig.put(ucd.getId(), notification);
notificationEventsBindingMap.put(context, mapNotificationConfig);
} }
/** /**
@ -228,7 +261,7 @@ public class NotificationsPlugin extends AbstractPlugin implements EventListener
log.debug("Reading subscribed events from UCD"); log.debug("Reading subscribed events from UCD");
NotificationEventsSubscribedConfig notificationMapPerContext = new NotificationEventsSubscribedConfig(); NotificationEventsSubscribedConfig notificationMapPerContext = null;
if (useCaseDescriptor == null) if (useCaseDescriptor == null)
throw new Exception("Error reading UCD null found"); throw new Exception("Error reading UCD null found");
@ -237,49 +270,61 @@ public class NotificationsPlugin extends AbstractPlugin implements EventListener
String context = UserUtils.getCurrent().getContext(); String context = UserUtils.getCurrent().getContext();
notificationMapPerContext = getNotificationBindingMapPerContext(); Map<String, NotificationEventsSubscribedConfig> mapPerContext = getNotificationBindingMapPerContext();
List<SubscribeNotificationEvent> listNotificationEventsSubscribedPerUCD = notificationMapPerContext.getListNotificationEventSubscribed(); notificationMapPerContext = mapPerContext.get(useCaseDescriptor.getId());
if (listNotificationEventsSubscribedPerUCD==null) { if (notificationMapPerContext == null) {
notificationMapPerContext = new NotificationEventsSubscribedConfig();
setNotificationBindingMapPerContext(context, useCaseDescriptor, notificationMapPerContext);
}
List<SubscribeNotificationEvent> listNotificationEventsSubscribedPerUCD = notificationMapPerContext
.getListNotificationEventSubscribed();
if (listNotificationEventsSubscribedPerUCD == null) {
listNotificationEventsSubscribedPerUCD = new ArrayList<SubscribeNotificationEvent>(); listNotificationEventsSubscribedPerUCD = new ArrayList<SubscribeNotificationEvent>();
Document profileConfiguration = getConfigurationFromProfile(useCaseDescriptor).getConfiguration(); Document profileConfiguration = getConfigurationFromProfile(useCaseDescriptor).getConfiguration();
log.debug("UseCaseDescriptor Configuration is {} ", profileConfiguration); log.debug("UseCaseDescriptor Configuration is {} ", profileConfiguration);
// JSONPathWrapper schemaNavigator = new // JSONPathWrapper schemaNavigator = new
// JSONPathWrapper(useCaseDescriptor.getSchema().toJson()); // JSONPathWrapper(useCaseDescriptor.getSchema().toJson());
for (Object fsConfigObj : profileConfiguration.get(SUBSCRIBE_NOTIFICATIONS_CONFIG, List.class)) { if (profileConfiguration != null) {
log.debug("Managing {} ", fsConfigObj); for (Object fsConfigObj : profileConfiguration.get(SUBSCRIBE_NOTIFICATIONS_CONFIG, List.class)) {
SubscribeNotificationEvent fsConfig = Serialization.convert(fsConfigObj, log.debug("Managing {} ", fsConfigObj);
SubscribeNotificationEvent.class); SubscribeNotificationEvent fsConfig = Serialization.convert(fsConfigObj,
log.debug("Converted config {}", fsConfig); SubscribeNotificationEvent.class);
String theEventSubsribed = fsConfig.getEvent(); log.debug("Converted config {}", fsConfig);
if (theEventSubsribed == null || theEventSubsribed.isEmpty()) try {
throw new MaterializationException( Event theEventSubsribed = Serialization.convert(fsConfig.getEvent(), Event.class);
"Invalid Field Definition path in configuration null or empty event : " + theEventSubsribed); if (theEventSubsribed == null)
throw new MaterializationException(
try { "Invalid Field Definition path in configuration [NO MATCH] : "
Event event = Serialization.convert(theEventSubsribed, Event.class); + theEventSubsribed);
log.debug("Added event {} to list ", event); } catch (Exception e) {
listNotificationEventsSubscribedPerUCD.add(fsConfig); log.error("MaterializationException: ", e);
}catch (Exception e) { throw new MaterializationException(
log.error("MaterializationException: ", e); "Invalid Event Definition path in configuration [NO MATCH Event] : "
throw new MaterializationException( + Event.values());
"Invalid Event Definition path in configuration [NO MATCH] : " + fsConfig.getEvent()); }
}
}
notificationMapPerContext = new NotificationEventsSubscribedConfig(); listNotificationEventsSubscribedPerUCD.add(fsConfig);
notificationMapPerContext.setContext(context); }
notificationMapPerContext.setListNotificationEventSubscribed(listNotificationEventsSubscribedPerUCD);
notificationEventsBindingMap.put(context, notificationMapPerContext); notificationMapPerContext.setUcd(useCaseDescriptor);
notificationMapPerContext
.setListNotificationEventSubscribed(listNotificationEventsSubscribedPerUCD);
String linkToMessages = profileConfiguration.get("link_to_messages", String.class);
notificationMapPerContext.setLinkToMessages(linkToMessages);
}
setNotificationBindingMapPerContext(context, useCaseDescriptor, notificationMapPerContext);
log.info("Events subscribed read from config {} ", notificationMapPerContext); log.info("Events subscribed read from config {} ", notificationMapPerContext);
} }
} catch (Exception e) { } catch (Exception e) {
log.error("Unable to read configuration for "+NotificationsPlugin.PLUGIN_ID, e); log.error("Unable to read configuration for " + NotificationsPlugin.PLUGIN_ID, e);
} catch (Throwable t) { } catch (Throwable t) {
log.error("Exception, Unable to read configuration ", t); log.error("Exception, Unable to read configuration ", t);
} }
@ -305,7 +350,32 @@ public class NotificationsPlugin extends AbstractPlugin implements EventListener
*/ */
@Override @Override
public void doAction(ItemObserved<Project> itemObserved) { public void doAction(ItemObserved<Project> itemObserved) {
// TODO Auto-generated method stub
if (checkIfSubscribedEvent(itemObserved)) {
//Map (UCD_ID, Notification)
Map<String, NotificationEventsSubscribedConfig> notificationMapPerContext = getNotificationBindingMapPerContext();
NotificationEventsSubscribedConfig subscribedConfig = notificationMapPerContext
.get(itemObserved.getUCD_Id());
String linkToFileWithMessages = subscribedConfig.getLinkToMessages();
List<SubscribeNotificationEvent> list = subscribedConfig.getListNotificationEventSubscribed();
List<SubscribeNotificationEvent> filterList = list.stream()
.filter(sne -> sne.getEvent().equals(itemObserved.getEvent())).collect(Collectors.toList());
for (SubscribeNotificationEvent subscribeNotificationEvent : filterList) {
List<NotificationFor> listNotificationFor = subscribeNotificationEvent.getNotificationFor();
UserUtils.getCurrent().getRoles();
for (NotificationFor notificationFor : listNotificationFor) {
notificationFor.getRoles();
}
}
}
} }
} }

View File

@ -2,6 +2,7 @@ package org.gcube.application.cms.notifications.config;
import java.util.List; import java.util.List;
import org.gcube.application.cms.plugins.events.EventManager.Event;
import org.gcube.com.fasterxml.jackson.annotation.JsonIgnoreProperties; import org.gcube.com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data; import lombok.Data;
@ -66,6 +67,6 @@ import lombok.Data;
@JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnoreProperties(ignoreUnknown = true)
public class SubscribeNotificationEvent { public class SubscribeNotificationEvent {
String event; Event event;
List<NotificationFor> notificationFor; List<NotificationFor> notificationFor;
} }

View File

@ -11,6 +11,7 @@ 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.config.Notify.NOTIFICATION_TYPE; import org.gcube.application.cms.notifications.config.Notify.NOTIFICATION_TYPE;
import org.gcube.application.cms.notifications.config.SubscribeNotificationEvent; import org.gcube.application.cms.notifications.config.SubscribeNotificationEvent;
import org.gcube.application.cms.plugins.events.EventManager.Event;
import org.gcube.application.cms.plugins.faults.InitializationException; import org.gcube.application.cms.plugins.faults.InitializationException;
import org.gcube.application.cms.serialization.Serialization; import org.gcube.application.cms.serialization.Serialization;
import org.gcube.application.cms.tests.TestProfiles; import org.gcube.application.cms.tests.TestProfiles;
@ -76,7 +77,7 @@ public class CatalogueBindingPluginTest extends BasicPluginTest {
org.junit.Assume.assumeTrue(GCubeTest.isTestInfrastructureEnabled()); org.junit.Assume.assumeTrue(GCubeTest.isTestInfrastructureEnabled());
SubscribeNotificationEvent sne = new SubscribeNotificationEvent(); SubscribeNotificationEvent sne = new SubscribeNotificationEvent();
sne.setEvent("LIFECYCLE_STEP_PERFORMED"); sne.setEvent(Event.LIFECYCLE_STEP_PERFORMED);
List<NotificationFor> listNotificationFor = new ArrayList<NotificationFor>(); List<NotificationFor> listNotificationFor = new ArrayList<NotificationFor>();