in progess on defining the config

This commit is contained in:
Francesco Mangiacrapa 2024-02-01 12:22:34 +01:00
parent 49f7b248fb
commit a2c3f44dcd
5 changed files with 167 additions and 78 deletions

View File

@ -228,7 +228,7 @@ public class NotificationsPlugin extends AbstractPlugin implements EventListener
log.debug("Reading subscribed events from UCD"); log.debug("Reading subscribed events from UCD");
NotificationEventsSubscribedConfig eventsSubscrInTheUCD = new NotificationEventsSubscribedConfig(); NotificationEventsSubscribedConfig notificationMapPerContext = new NotificationEventsSubscribedConfig();
if (useCaseDescriptor == null) if (useCaseDescriptor == null)
throw new Exception("Error reading UCD null found"); throw new Exception("Error reading UCD null found");
@ -237,11 +237,12 @@ public class NotificationsPlugin extends AbstractPlugin implements EventListener
String context = UserUtils.getCurrent().getContext(); String context = UserUtils.getCurrent().getContext();
eventsSubscrInTheUCD = getNotificationBindingMapPerContext(); notificationMapPerContext = getNotificationBindingMapPerContext();
List<SubscribeNotificationEvent> listNotificationEventsSubscribedPerUCD = new ArrayList<SubscribeNotificationEvent>();
if (eventsSubscrInTheUCD == null || listNotificationEventsSubscribedPerUCD.isEmpty()) {
List<SubscribeNotificationEvent> listNotificationEventsSubscribedPerUCD = notificationMapPerContext.getListNotificationEventSubscribed();
if (listNotificationEventsSubscribedPerUCD==null) {
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
@ -249,26 +250,31 @@ public class NotificationsPlugin extends AbstractPlugin implements EventListener
for (Object fsConfigObj : profileConfiguration.get(SUBSCRIBE_NOTIFICATIONS_CONFIG, List.class)) { for (Object fsConfigObj : profileConfiguration.get(SUBSCRIBE_NOTIFICATIONS_CONFIG, List.class)) {
log.debug("Managing {} ", fsConfigObj); log.debug("Managing {} ", fsConfigObj);
SubscribeNotificationEvent fsConfig = Serialization.read(fsConfigObj.toString(), SubscribeNotificationEvent fsConfig = Serialization.convert(fsConfigObj,
SubscribeNotificationEvent.class); SubscribeNotificationEvent.class);
log.debug("Converted config {}", fsConfig); log.debug("Converted config {}", fsConfig);
String theEventSubsribed = fsConfig.getEvent(); String theEventSubsribed = fsConfig.getEvent();
if (theEventSubsribed == null || theEventSubsribed.isEmpty()) if (theEventSubsribed == null || theEventSubsribed.isEmpty())
throw new MaterializationException( throw new MaterializationException(
"Invalid Field Definition path in configuration [NO MATCH] : " + fsConfig.getEvent()); "Invalid Field Definition path in configuration null or empty event : " + theEventSubsribed);
SubscribeNotificationEvent event = Serialization.convert(theEventSubsribed, try {
SubscribeNotificationEvent.class); Event event = Serialization.convert(theEventSubsribed, Event.class);
log.debug("Added event {} to list ", event); log.debug("Added event {} to list ", event);
listNotificationEventsSubscribedPerUCD.add(event); listNotificationEventsSubscribedPerUCD.add(fsConfig);
}catch (Exception e) {
log.error("MaterializationException: ", e);
throw new MaterializationException(
"Invalid Event Definition path in configuration [NO MATCH] : " + fsConfig.getEvent());
}
} }
eventsSubscrInTheUCD = new NotificationEventsSubscribedConfig(); notificationMapPerContext = new NotificationEventsSubscribedConfig();
eventsSubscrInTheUCD.setContext(context); notificationMapPerContext.setContext(context);
eventsSubscrInTheUCD.setListNotificationEventSubscribed(listNotificationEventsSubscribedPerUCD); notificationMapPerContext.setListNotificationEventSubscribed(listNotificationEventsSubscribedPerUCD);
notificationEventsBindingMap.put(context, eventsSubscrInTheUCD); notificationEventsBindingMap.put(context, notificationMapPerContext);
log.info("Events subscribed read from config {} ", eventsSubscrInTheUCD); log.info("Events subscribed read from config {} ", notificationMapPerContext);
} }
@ -278,7 +284,7 @@ public class NotificationsPlugin extends AbstractPlugin implements EventListener
log.error("Exception, Unable to read configuration ", t); log.error("Exception, Unable to read configuration ", t);
} }
return eventsSubscrInTheUCD; return notificationMapPerContext;
} }
/** /**

View File

@ -14,6 +14,6 @@ import lombok.Data;
public class NotificationWhen { public class NotificationWhen {
List<String> target_phase; List<String> target_phase;
Notify notify; List<Notify> notify;
} }

View File

@ -9,11 +9,10 @@ import lombok.Data;
* *
* *
{ {
"send_personal_nofitication": false, "type": "NOTIFICATION_TYPE",
"send_email": true, "send": "true/false",
"send_pdf_document": false, "attach_pdf_document": "true/false",
"post_on_vre": false, "message": "PLACEHOLDER_MESSAGE_1"
"message": "ID_MESSAGE_1"
} }
* *
*/ */
@ -21,9 +20,9 @@ import lombok.Data;
@JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnoreProperties(ignoreUnknown = true)
public class Notify { public class Notify {
Boolean send_personal_nofitication; public static enum NOTIFICATION_TYPE {PERSONAL_POST, EMAIL, VRE_POST, ANY}
Boolean send_email; NOTIFICATION_TYPE type;
Boolean send_pdf_document; Boolean send;
Boolean post_on_vre; Boolean attach_pdf_document;
String message; String message;
} }

View File

@ -16,42 +16,49 @@ import lombok.Data;
/** /**
* Instantiates a new subscribe notification config. * Instantiates a new subscribe notification config.
* *
* { {
* "event": "EVENT_NAME", "subscribeNotifications": [
* "for": [ {
* { "event": "EVENT_NAME",
* "roles": [ "notificationFor": [
* "USER_ROLE" {
* ], "roles": [
* "when": [ "USER_ROLE"
* { ],
* "target_phase": [ "when": [
* "TARGET_PHASE" {
* ], "target_phase": [
* "notify": { "TARGET_PHASE"
* "send_personal_nofitication": "true/false", ],
* "send_email": "true/false", "notify": [
* "send_pdf_document": "true/false", {
* "post_on_vre": "true/false", "type": "NOTIFICATION_TYPE",
* "message": "ID_MESSAGE_1" "send": "true/false",
* } "attach_pdf_document": "true/false",
* }, "message": "PLACEHOLDER_MESSAGE_1"
* { }
* "target_phase": [ ]
* "TARGET_PHASE" },
* ], {
* "notify": { "target_phase": [
* "send_personal_nofitication": "true/false", "TARGET_PHASE"
* "send_email": "true/false", ],
* "send_pdf_document": "true/false", "notify": [
* "post_on_vre": "true/false", {
* "message": "ID_MESSAGE_2" "type": "NOTIFICATION_TYPE",
* } "send": "true/false",
* } "attach_pdf_document": "true/false",
* ] "message": "PLACEHOLDER_MESSAGE_2"
* } }
* ] ]
* } }
]
}
]
}
],
"link_to_messages": "link to file containing the messages as properties like ID_MESSAGE_{N} = value"
}
* *
*/ */

View File

@ -9,6 +9,7 @@ import org.gcube.application.cms.notifications.NotificationsPlugin;
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.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.faults.InitializationException; import org.gcube.application.cms.plugins.faults.InitializationException;
import org.gcube.application.cms.serialization.Serialization; import org.gcube.application.cms.serialization.Serialization;
@ -42,7 +43,7 @@ public class CatalogueBindingPluginTest extends BasicPluginTest {
} }
//@Test @Test
public void checkPluginConfig() { public void checkPluginConfig() {
org.junit.Assume.assumeTrue(GCubeTest.isTestInfrastructureEnabled()); org.junit.Assume.assumeTrue(GCubeTest.isTestInfrastructureEnabled());
NotificationsPlugin plugin = (NotificationsPlugin) plugins.get(NotificationsPlugin.DESCRIPTOR.getId()); NotificationsPlugin plugin = (NotificationsPlugin) plugins.get(NotificationsPlugin.DESCRIPTOR.getId());
@ -71,7 +72,7 @@ public class CatalogueBindingPluginTest extends BasicPluginTest {
} }
//@Test //@Test
public void checkDeserialize() { public void checkSerializeDeserialize() {
org.junit.Assume.assumeTrue(GCubeTest.isTestInfrastructureEnabled()); org.junit.Assume.assumeTrue(GCubeTest.isTestInfrastructureEnabled());
SubscribeNotificationEvent sne = new SubscribeNotificationEvent(); SubscribeNotificationEvent sne = new SubscribeNotificationEvent();
@ -87,12 +88,11 @@ public class CatalogueBindingPluginTest extends BasicPluginTest {
notificationWhen.setTarget_phase(Arrays.asList("Pending Approval")); notificationWhen.setTarget_phase(Arrays.asList("Pending Approval"));
Notify notify = new Notify(); Notify notify = new Notify();
notify.setSend_personal_nofitication(false); notify.setType(NOTIFICATION_TYPE.EMAIL);
notify.setPost_on_vre(false); notify.setSend(true);
notify.setSend_email(false); notify.setAttach_pdf_document(true);
notify.setSend_personal_nofitication(false);
notify.setMessage("message"); notify.setMessage("message");
notificationWhen.setNotify(notify); notificationWhen.setNotify(Arrays.asList(notify));
listWhen.add(notificationWhen); listWhen.add(notificationWhen);
notificatioFor.setWhen(listWhen); notificatioFor.setWhen(listWhen);
@ -111,13 +111,12 @@ public class CatalogueBindingPluginTest extends BasicPluginTest {
//Test Notify //Test Notify
/* /*
String testNotify = " {\n" String testNotify = "{\n"
+ " \"send_personal_nofitication\": false,\n" + " \"type\": \"EMAIL\",\n"
+ " \"send_email\": true,\n" + " \"send\": true,\n"
+ " \"send_pdf_document\": false,\n" + " \"attach_pdf_document\": false,\n"
+ " \"post_on_vre\": false,\n" + " \"message\": \"The project ${project_name} has just been rejected by ${user}\"\n"
+ " \"message\": \"ID_MESSAGE_1\"\n" + "}";
+ " }";
System.out.println("String :"+testNotify); System.out.println("String :"+testNotify);
Notify serialize = Serialization.read(testNotify, Notify.class); Notify serialize = Serialization.read(testNotify, Notify.class);
@ -158,6 +157,84 @@ public class CatalogueBindingPluginTest extends BasicPluginTest {
System.out.println("Ser from JSON :"+serializeNotificationWhen); System.out.println("Ser from JSON :"+serializeNotificationWhen);
*/ */
String testSubribeNotifications = "{\n"
+ " \"event\": \"LIFECYCLE_STEP_PERFORMED\",\n"
+ " \"notificationFor\": [\n"
+ " {\n"
+ " \"roles\": [\n"
+ " \"Data-Manager\"\n"
+ " ],\n"
+ " \"when\": [\n"
+ " {\n"
+ " \"target_phase\": [\n"
+ " \"Pending Approval\"\n"
+ " ],\n"
+ " \"notify\": [\n"
+ " {\n"
+ " \"type\": \"EMAIL\",\n"
+ " \"send\": true,\n"
+ " \"attach_pdf_document\": false,\n"
+ " \"message\": \"${user} created the project ${project_name}. You are kindly requested to review it and decide either to APPROVE or REJECT it. See it at @link\"\n"
+ " }\n"
+ " ]\n"
+ " },\n"
+ " {\n"
+ " \"target_phase\": [\n"
+ " \"Rejected\"\n"
+ " ],\n"
+ " \"notify\": [\n"
+ " {\n"
+ " \"type\": \"EMAIL\",\n"
+ " \"send\": true,\n"
+ " \"attach_pdf_document\": false,\n"
+ " \"message\": \"The project ${project_name} has just been rejected by ${user}\"\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ " ]\n"
+ " },\n"
+ " {\n"
+ " \"roles\": [\n"
+ " \"Item_Creator\"\n"
+ " ],\n"
+ " \"when\": [\n"
+ " {\n"
+ " \"target_phase\": [\n"
+ " \"Rejected\"\n"
+ " ],\n"
+ " \"notify\": [\n"
+ " {\n"
+ " \"type\": \"EMAIL\",\n"
+ " \"send\": true,\n"
+ " \"attach_pdf_document\": false,\n"
+ " \"message\": \"The project ${project_name} has just been rejected. You are kindly requested to review it\"\n"
+ " }\n"
+ " ]\n"
+ " },\n"
+ " {\n"
+ " \"target_phase\": [\n"
+ " \"Published\"\n"
+ " ],\n"
+ " \"notify\": [\n"
+ " {\n"
+ " \"type\": \"EMAIL\",\n"
+ " \"send\": true,\n"
+ " \"attach_pdf_document\": false,\n"
+ " \"message\": \"The project ${project_name} has just been published. See it at @link\"\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ " ]\n"
+ "}";
testSubribeNotifications = "{event=LIFECYCLE_STEP_PERFORMED, notificationFor=[{roles=[Data-Manager], when=[{target_phase=[Pending Approval], notify=[{type=EMAIL, send=true, attach_pdf_document=false, message=${user} created the project ${project_name}. You are kindly requested to review it and decide either to APPROVE or REJECT it. See it at @link}]}, {target_phase=[Rejected], notify=[{type=EMAIL, send=true, attach_pdf_document=false, message=The project ${project_name} has just been rejected by ${user}}]}]}, {roles=[Item_Creator], when=[{target_phase=[Rejected], notify=[{type=EMAIL, send=true, attach_pdf_document=false, message=The project ${project_name} has just been rejected. You are kindly requested to review it}]}, {target_phase=[Published], notify=[{type=EMAIL, send=true, attach_pdf_document=false, message=The project ${project_name} has just been published. See it at @link}]}]}]}";
System.out.println("String :"+testSubribeNotifications);
SubscribeNotificationEvent serializeSubscribeNotificationEvent = Serialization.convert(testSubribeNotifications, SubscribeNotificationEvent.class);
System.out.println("Ser from JSON :"+serializeSubscribeNotificationEvent);
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {