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 9756b09..d2815ef 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 @@ -3,6 +3,7 @@ package org.gcube.application.cms.notifications; import java.util.List; 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 lombok.Data; @@ -10,6 +11,7 @@ import lombok.Data; @Data @JsonIgnoreProperties(ignoreUnknown = true) public class NotificationEventsSubscribedConfig { - String context; + UseCaseDescriptor ucd; List listNotificationEventSubscribed; + String linkToMessages; } 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 b9c9422..1e77564 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,12 +1,16 @@ package org.gcube.application.cms.notifications; import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import org.bson.Document; 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.plugins.EventListenerPluginInterface; 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"); } + // This is a map "" + protected Map> notificationEventsBindingMap = null; + /** * Inits the. * @@ -160,7 +167,7 @@ public class NotificationsPlugin extends AbstractPlugin implements EventListener String context = UserUtils.getCurrent().getContext(); if (getNotificationBindingMapPerContext() == null) { log.info("Initializing in " + context); - notificationEventsBindingMap.put(context, new NotificationEventsSubscribedConfig()); + notificationEventsBindingMap.put(context, new HashMap()); } report.setStatus(Report.Status.OK); report.putMessage("Initialized " + DESCRIPTOR.getId() + " in the " + context); @@ -184,11 +191,17 @@ public class NotificationsPlugin extends AbstractPlugin implements EventListener NotificationEventsSubscribedConfig eventsSub = readNotificationsSubscribedFromConfigurationInTheUCD( observerd.getUseCaseDescriptor()); - List listEvents = eventsSub.getListNotificationEventSubscribed(); - log.info("List events is {}", listEvents); - if (listEvents.contains(observerd.getEvent())) { - log.info("the event {} is subscribed from config ", observerd.getEvent()); - return true; + if (eventsSub != null) { + + List listEvents = eventsSub.getListNotificationEventSubscribed(); + log.info("List events is {}", listEvents); + 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()); return false; @@ -200,20 +213,40 @@ public class NotificationsPlugin extends AbstractPlugin implements EventListener } - protected Map notificationEventsBindingMap = null; - /** * Gets the notification binding map per context. * * @return the notification binding map per context */ - protected NotificationEventsSubscribedConfig getNotificationBindingMapPerContext() { + protected Map getNotificationBindingMapPerContext() { String context = UserUtils.getCurrent().getContext(); log.debug("Getting {} from cache map for context {}", NotificationsPlugin.PLUGIN_ID, context); - if (notificationEventsBindingMap == null) - notificationEventsBindingMap = new LinkedHashMap(); + if (notificationEventsBindingMap == null) { + notificationEventsBindingMap = new LinkedHashMap>(); + } - return notificationEventsBindingMap.get(context); + // read notification events binding subscribed in the context + Map map = notificationEventsBindingMap.get(context); + return map == null ? new LinkedHashMap() : 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 mapNotificationConfig = notificationEventsBindingMap + .get(context); + if (mapNotificationConfig == null) { + mapNotificationConfig = new LinkedHashMap(); + } + + 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"); - NotificationEventsSubscribedConfig notificationMapPerContext = new NotificationEventsSubscribedConfig(); + NotificationEventsSubscribedConfig notificationMapPerContext = null; if (useCaseDescriptor == null) throw new Exception("Error reading UCD null found"); @@ -237,49 +270,61 @@ public class NotificationsPlugin extends AbstractPlugin implements EventListener String context = UserUtils.getCurrent().getContext(); - notificationMapPerContext = getNotificationBindingMapPerContext(); + Map mapPerContext = getNotificationBindingMapPerContext(); - List listNotificationEventsSubscribedPerUCD = notificationMapPerContext.getListNotificationEventSubscribed(); - - if (listNotificationEventsSubscribedPerUCD==null) { + notificationMapPerContext = mapPerContext.get(useCaseDescriptor.getId()); + + if (notificationMapPerContext == null) { + notificationMapPerContext = new NotificationEventsSubscribedConfig(); + setNotificationBindingMapPerContext(context, useCaseDescriptor, notificationMapPerContext); + } + + List listNotificationEventsSubscribedPerUCD = notificationMapPerContext + .getListNotificationEventSubscribed(); + + if (listNotificationEventsSubscribedPerUCD == null) { listNotificationEventsSubscribedPerUCD = new ArrayList(); Document profileConfiguration = getConfigurationFromProfile(useCaseDescriptor).getConfiguration(); log.debug("UseCaseDescriptor Configuration is {} ", profileConfiguration); // JSONPathWrapper schemaNavigator = new // JSONPathWrapper(useCaseDescriptor.getSchema().toJson()); - for (Object fsConfigObj : profileConfiguration.get(SUBSCRIBE_NOTIFICATIONS_CONFIG, List.class)) { - log.debug("Managing {} ", fsConfigObj); - SubscribeNotificationEvent fsConfig = Serialization.convert(fsConfigObj, - SubscribeNotificationEvent.class); - log.debug("Converted config {}", fsConfig); - String theEventSubsribed = fsConfig.getEvent(); - if (theEventSubsribed == null || theEventSubsribed.isEmpty()) - throw new MaterializationException( - "Invalid Field Definition path in configuration null or empty event : " + theEventSubsribed); - - try { - Event event = Serialization.convert(theEventSubsribed, Event.class); - log.debug("Added event {} to list ", event); - listNotificationEventsSubscribedPerUCD.add(fsConfig); - }catch (Exception e) { - log.error("MaterializationException: ", e); - throw new MaterializationException( - "Invalid Event Definition path in configuration [NO MATCH] : " + fsConfig.getEvent()); - } - } + if (profileConfiguration != null) { + for (Object fsConfigObj : profileConfiguration.get(SUBSCRIBE_NOTIFICATIONS_CONFIG, List.class)) { + log.debug("Managing {} ", fsConfigObj); + SubscribeNotificationEvent fsConfig = Serialization.convert(fsConfigObj, + SubscribeNotificationEvent.class); + log.debug("Converted config {}", fsConfig); + try { + Event theEventSubsribed = Serialization.convert(fsConfig.getEvent(), Event.class); + if (theEventSubsribed == null) + throw new MaterializationException( + "Invalid Field Definition path in configuration [NO MATCH] : " + + theEventSubsribed); + } catch (Exception e) { + log.error("MaterializationException: ", e); + throw new MaterializationException( + "Invalid Event Definition path in configuration [NO MATCH Event] : " + + Event.values()); + } - notificationMapPerContext = new NotificationEventsSubscribedConfig(); - notificationMapPerContext.setContext(context); - notificationMapPerContext.setListNotificationEventSubscribed(listNotificationEventsSubscribedPerUCD); - notificationEventsBindingMap.put(context, notificationMapPerContext); + listNotificationEventsSubscribedPerUCD.add(fsConfig); + } + + 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); } } 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) { log.error("Exception, Unable to read configuration ", t); } @@ -305,7 +350,32 @@ public class NotificationsPlugin extends AbstractPlugin implements EventListener */ @Override public void doAction(ItemObserved itemObserved) { - // TODO Auto-generated method stub + + if (checkIfSubscribedEvent(itemObserved)) { + + //Map (UCD_ID, Notification) + Map notificationMapPerContext = getNotificationBindingMapPerContext(); + + NotificationEventsSubscribedConfig subscribedConfig = notificationMapPerContext + .get(itemObserved.getUCD_Id()); + + String linkToFileWithMessages = subscribedConfig.getLinkToMessages(); + List list = subscribedConfig.getListNotificationEventSubscribed(); + + List filterList = list.stream() + .filter(sne -> sne.getEvent().equals(itemObserved.getEvent())).collect(Collectors.toList()); + + 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/SubscribeNotificationEvent.java b/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/config/SubscribeNotificationEvent.java index a99bf4e..722e878 100644 --- a/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/config/SubscribeNotificationEvent.java +++ b/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/config/SubscribeNotificationEvent.java @@ -2,6 +2,7 @@ package org.gcube.application.cms.notifications.config; import java.util.List; +import org.gcube.application.cms.plugins.events.EventManager.Event; import org.gcube.com.fasterxml.jackson.annotation.JsonIgnoreProperties; import lombok.Data; @@ -66,6 +67,6 @@ import lombok.Data; @JsonIgnoreProperties(ignoreUnknown = true) public class SubscribeNotificationEvent { - String event; + Event event; List notificationFor; } diff --git a/notifications-plugins/src/test/java/CatalogueBindingPluginTest.java b/notifications-plugins/src/test/java/CatalogueBindingPluginTest.java index b0be403..39d7cc0 100644 --- a/notifications-plugins/src/test/java/CatalogueBindingPluginTest.java +++ b/notifications-plugins/src/test/java/CatalogueBindingPluginTest.java @@ -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.NOTIFICATION_TYPE; 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.serialization.Serialization; import org.gcube.application.cms.tests.TestProfiles; @@ -76,7 +77,7 @@ public class CatalogueBindingPluginTest extends BasicPluginTest { org.junit.Assume.assumeTrue(GCubeTest.isTestInfrastructureEnabled()); SubscribeNotificationEvent sne = new SubscribeNotificationEvent(); - sne.setEvent("LIFECYCLE_STEP_PERFORMED"); + sne.setEvent(Event.LIFECYCLE_STEP_PERFORMED); List listNotificationFor = new ArrayList();