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 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<SubscribeNotificationEvent> listNotificationEventSubscribed;
String linkToMessages;
}

View File

@ -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 "<Context,<UCD_ID,NotificationEventsSubscribedConfig>"
protected Map<String, Map<String, NotificationEventsSubscribedConfig>> 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<String, NotificationEventsSubscribedConfig>());
}
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<SubscribeNotificationEvent> 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<SubscribeNotificationEvent> 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<String, NotificationEventsSubscribedConfig> notificationEventsBindingMap = null;
/**
* Gets 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();
log.debug("Getting {} from cache map for context {}", NotificationsPlugin.PLUGIN_ID, context);
if (notificationEventsBindingMap == null)
notificationEventsBindingMap = new LinkedHashMap<String, NotificationEventsSubscribedConfig>();
if (notificationEventsBindingMap == null) {
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");
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<String, NotificationEventsSubscribedConfig> mapPerContext = getNotificationBindingMapPerContext();
List<SubscribeNotificationEvent> listNotificationEventsSubscribedPerUCD = notificationMapPerContext.getListNotificationEventSubscribed();
if (listNotificationEventsSubscribedPerUCD==null) {
notificationMapPerContext = mapPerContext.get(useCaseDescriptor.getId());
if (notificationMapPerContext == null) {
notificationMapPerContext = new NotificationEventsSubscribedConfig();
setNotificationBindingMapPerContext(context, useCaseDescriptor, notificationMapPerContext);
}
List<SubscribeNotificationEvent> listNotificationEventsSubscribedPerUCD = notificationMapPerContext
.getListNotificationEventSubscribed();
if (listNotificationEventsSubscribedPerUCD == null) {
listNotificationEventsSubscribedPerUCD = new ArrayList<SubscribeNotificationEvent>();
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<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 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> 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.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<NotificationFor> listNotificationFor = new ArrayList<NotificationFor>();