From 82e8505bdca14ec41d132abf11656ffd06aea0e3 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Thu, 18 Jan 2024 16:16:12 +0100 Subject: [PATCH] reviseted the classes --- catalogue-binding-plugin/README.md | 8 +- .../CatalogueBindingAbstractPlugin.java | 242 ------------------ .../CatalogueBindingPlugin.java | 226 +++++++++++++++- .../test/java/CatalogueBindingPluginTest.java | 1 - 4 files changed, 228 insertions(+), 249 deletions(-) delete mode 100644 catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/CatalogueBindingAbstractPlugin.java diff --git a/catalogue-binding-plugin/README.md b/catalogue-binding-plugin/README.md index 242bd6d..02ff5ea 100644 --- a/catalogue-binding-plugin/README.md +++ b/catalogue-binding-plugin/README.md @@ -14,7 +14,7 @@ the products registered in the Geoportal system to the D4Science Catalogue syste ## Documentation [gCube CMS Suite](../) parent module containing references, documentation, guides ad utilities. -This plugin requires an handler defined in the UCD so defined: +This plugin requires an handler registered in the UCD so defined: { "_id": "Catalogue-Binding-Plugin", @@ -31,7 +31,7 @@ This plugin requires an handler defined in the UCD so defined: } } -The supported events are: +where the supported events are: "PROJECT_CREATED" "PROJECT_UPDATED" @@ -42,6 +42,10 @@ The supported events are: ## Change log See [CHANGELOG.md](CHANGELOG.md). +## Authors + +* **Francesco Mangiacrapa** ([ORCID](https://orcid.org/0000-0002-6528-664X)) Computer Scientist at [ISTI-CNR Infrascience Group](http://nemis.isti.cnr.it/groups/infrascience) + ## License This project is licensed under the EUPL V.1.1 License - see the [LICENSE.md](LICENSE.md) file for details. diff --git a/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/CatalogueBindingAbstractPlugin.java b/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/CatalogueBindingAbstractPlugin.java deleted file mode 100644 index a36162f..0000000 --- a/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/CatalogueBindingAbstractPlugin.java +++ /dev/null @@ -1,242 +0,0 @@ -package org.gcube.application.cms.cataloguebinding; - -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import org.bson.Document; -import org.gcube.application.cms.implementations.utils.UserUtils; -import org.gcube.application.cms.plugins.InitializablePlugin; -import org.gcube.application.cms.plugins.events.EventListener; -import org.gcube.application.cms.plugins.events.EventManager; -import org.gcube.application.cms.plugins.events.EventManager.Event; -import org.gcube.application.cms.plugins.events.ItemObserved; -import org.gcube.application.cms.plugins.faults.InitializationException; -import org.gcube.application.cms.plugins.faults.MaterializationException; -import org.gcube.application.cms.plugins.implementations.AbstractPlugin; -import org.gcube.application.cms.plugins.reports.InitializationReport; -import org.gcube.application.cms.plugins.reports.Report.Status; -import org.gcube.application.cms.serialization.Serialization; -import org.gcube.application.geoportal.common.model.document.Project; -import org.gcube.application.geoportal.common.model.useCaseDescriptor.UseCaseDescriptor; - -import lombok.Data; -import lombok.extern.slf4j.Slf4j; - -/** - * The Class CatalogueBindingAbstractPlugin. - * - * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it - * - * Jan 17, 2024 - */ -@Slf4j -public abstract class CatalogueBindingAbstractPlugin extends AbstractPlugin implements InitializablePlugin { - - public static final String SUBSCRIBE_EVENTS_CONFIG = "subscribeEvents"; - public static final String PLUGIN_ID = "Catalogue-Binding-Plugin"; - public static final String PLUGIN_TYPE = "EventListener"; - - /** - * Instantiates a new materialization config. - * - * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it - * - * Jan 17, 2024 - */ - - /** - * Instantiates a new subscribe events config. - */ - @Data - public static class SubscribeEventsConfig { - private String event; - } - - protected Map catalogueBindingMap = null; - - /** - * Gets the catalogue binding map per context. - * - * @return the catalogue binding map per context - */ - protected EventsSubscribed getCatalogueBindingMapPerContext() { - String context = UserUtils.getCurrent().getContext(); - log.debug("Getting {} from cache map for context {}", CatalogueBindingAbstractPlugin.PLUGIN_ID, context); - if (catalogueBindingMap == null) - catalogueBindingMap = new LinkedHashMap(); - - return catalogueBindingMap.get(context); - } - - /** - * Inits the. - * - * @return the initialization report - * @throws InitializationException the initialization exception - */ - @Override - public InitializationReport init() throws InitializationException { - - // Creating all listeners - EventListener> listenerCreated = new EventListener>() { - - @Override - public void updated(ItemObserved observerd) { - log.info("listenerCreated fired on item: {} " + observerd); - - boolean subscribed = checkIfSubscribedEvent(observerd); - - if (subscribed) { - doAction(observerd); - } - - } - }; - - EventListener> listenerUpdated = new EventListener>() { - - @Override - public void updated(ItemObserved observerd) { - log.info("listenerUpdated fired on item: {} " + observerd); - boolean subscribed = checkIfSubscribedEvent(observerd); - - if (subscribed) { - doAction(observerd); - } - - } - }; - - EventListener> listenerDeleted = new EventListener>() { - - @Override - public void updated(ItemObserved observerd) { - log.info("listenerDeleted fired on item: {} " + observerd); - boolean subscribed = checkIfSubscribedEvent(observerd); - - if (subscribed) { - doAction(observerd); - } - } - }; - - EventListener> listenerLCStepPerformed = new EventListener>() { - - @Override - public void updated(ItemObserved observerd) { - log.info("listenerLCStepPerformed fired on item: {} " + observerd); - boolean subscribed = checkIfSubscribedEvent(observerd); - - if (subscribed) { - doAction(observerd); - } - } - }; - - // Subscribing all events - EventManager eventMngInst = EventManager.getInstance(); - eventMngInst.subscribe(Event.PROJECT_CREATED, listenerCreated); - eventMngInst.subscribe(Event.PROJECT_UPDATED, listenerUpdated); - eventMngInst.subscribe(Event.PROJECT_DELETED, listenerDeleted); - eventMngInst.subscribe(Event.LIFECYCLE_STEP_PERFORMED, listenerLCStepPerformed); - - return new InitializationReport(Status.OK, PLUGIN_ID + " init performed"); - - } - - /** - * Read events subscribed from configuration in the UCD. - * - * @param useCaseDescriptor the use case descriptor - * @return the events subscribed - * @throws Exception the exception - */ - public EventsSubscribed readEventsSubscribedFromConfigurationInTheUCD(UseCaseDescriptor useCaseDescriptor) - throws Exception { - - log.debug("Reading subscribed events from UCD"); - - EventsSubscribed eventsSubscrInTheUCD = new EventsSubscribed(); - - if (useCaseDescriptor == null) - throw new Exception("Error reading UCD null found"); - - try { - - String context = UserUtils.getCurrent().getContext(); - - eventsSubscrInTheUCD = getCatalogueBindingMapPerContext(); - - List listEventsSubscribedPerUCD = new ArrayList(); - if (eventsSubscrInTheUCD == null || listEventsSubscribedPerUCD.isEmpty()) { - - Document profileConfiguration = getConfigurationFromProfile(useCaseDescriptor).getConfiguration(); - log.debug("UseCaseDescriptor Configuration is {} ", profileConfiguration); - // JSONPathWrapper schemaNavigator = new - // JSONPathWrapper(useCaseDescriptor.getSchema().toJson()); - - for (Object fsConfigObj : profileConfiguration.get(SUBSCRIBE_EVENTS_CONFIG, List.class)) { - log.debug("Managing {} ", fsConfigObj); - SubscribeEventsConfig fsConfig = Serialization.convert(fsConfigObj, SubscribeEventsConfig.class); - log.debug("Converted config {}", fsConfig); - String theEventSubsribed = fsConfig.getEvent(); - if (theEventSubsribed == null || theEventSubsribed.isEmpty()) - throw new MaterializationException( - "Invalid Field Definition path in configuration [NO MATCH] : " + fsConfig.getEvent()); - - Event event = Serialization.convert(theEventSubsribed, Event.class); - log.debug("Added event {} to list ", event); - listEventsSubscribedPerUCD.add(event); - } - - eventsSubscrInTheUCD = new EventsSubscribed(); - eventsSubscrInTheUCD.setContext(context); - eventsSubscrInTheUCD.setListEventSubscribed(listEventsSubscribedPerUCD); - catalogueBindingMap.put(context, eventsSubscrInTheUCD); - - log.info("Events subscribed read from config {} ", eventsSubscrInTheUCD); - - } - - } catch (Exception e) { - log.error("Unable to read configuration ", e); - } catch (Throwable t) { - log.error("Exception, Unable to read configuration ", t); - } - - return eventsSubscrInTheUCD; - } - - /** - * Check if subscribed event. - * - * @param observerd the observerd - * @return true, if successful - */ - public boolean checkIfSubscribedEvent(ItemObserved observerd) { - log.info("Checking if {} is an subscribed event", observerd.getEvent()); - try { - EventsSubscribed eventsSub = readEventsSubscribedFromConfigurationInTheUCD( - observerd.getUseCaseDescriptor()); - - List listEvents = eventsSub.getListEventSubscribed(); - log.info("List events is {}", listEvents); - if (listEvents.contains(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; - - } catch (Exception e) { - log.error("Exception, Error on checking subscribed events", e); - return false; - } - - } - - public abstract void doAction(ItemObserved observerd); - -} diff --git a/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/CatalogueBindingPlugin.java b/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/CatalogueBindingPlugin.java index d93cc46..90b60c9 100644 --- a/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/CatalogueBindingPlugin.java +++ b/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/CatalogueBindingPlugin.java @@ -1,17 +1,33 @@ package org.gcube.application.cms.cataloguebinding; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.bson.Document; import org.gcube.application.cms.cataloguebinding.doaction.BindingAction; import org.gcube.application.cms.implementations.utils.UserUtils; +import org.gcube.application.cms.plugins.InitializablePlugin; +import org.gcube.application.cms.plugins.events.EventListener; +import org.gcube.application.cms.plugins.events.EventManager; +import org.gcube.application.cms.plugins.events.EventManager.Event; import org.gcube.application.cms.plugins.events.ItemObserved; import org.gcube.application.cms.plugins.faults.InitializationException; +import org.gcube.application.cms.plugins.faults.MaterializationException; import org.gcube.application.cms.plugins.faults.ShutDownException; +import org.gcube.application.cms.plugins.implementations.AbstractPlugin; import org.gcube.application.cms.plugins.reports.InitializationReport; import org.gcube.application.cms.plugins.reports.Report; +import org.gcube.application.cms.plugins.reports.Report.Status; +import org.gcube.application.cms.serialization.Serialization; import org.gcube.application.geoportal.common.model.document.Project; import org.gcube.application.geoportal.common.model.plugins.PluginDescriptor; +import org.gcube.application.geoportal.common.model.useCaseDescriptor.UseCaseDescriptor; import com.vdurmont.semver4j.Semver; +import lombok.Data; import lombok.Synchronized; import lombok.extern.slf4j.Slf4j; @@ -23,10 +39,10 @@ import lombok.extern.slf4j.Slf4j; * Jan 17, 2024 */ @Slf4j -public class CatalogueBindingPlugin extends CatalogueBindingAbstractPlugin { +public class CatalogueBindingPlugin extends AbstractPlugin implements InitializablePlugin { - public static final PluginDescriptor DESCRIPTOR = new PluginDescriptor(CatalogueBindingAbstractPlugin.PLUGIN_ID, - CatalogueBindingAbstractPlugin.PLUGIN_TYPE); + public static final PluginDescriptor DESCRIPTOR = new PluginDescriptor(CatalogueBindingPlugin.PLUGIN_ID, + CatalogueBindingPlugin.PLUGIN_TYPE); static { DESCRIPTOR.setVersion(new Semver("1.0.0")); @@ -68,6 +84,209 @@ public class CatalogueBindingPlugin extends CatalogueBindingAbstractPlugin { return report; } + public static final String SUBSCRIBE_EVENTS_CONFIG = "subscribeEvents"; + public static final String PLUGIN_ID = "Catalogue-Binding-Plugin"; + public static final String PLUGIN_TYPE = "EventListener"; + + /** + * Instantiates a new materialization config. + * + * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it + * + * Jan 17, 2024 + */ + + /** + * Instantiates a new subscribe events config. + */ + @Data + public static class SubscribeEventsConfig { + private String event; + } + + protected Map catalogueBindingMap = null; + + /** + * Gets the catalogue binding map per context. + * + * @return the catalogue binding map per context + */ + protected EventsSubscribed getCatalogueBindingMapPerContext() { + String context = UserUtils.getCurrent().getContext(); + log.debug("Getting {} from cache map for context {}", CatalogueBindingPlugin.PLUGIN_ID, context); + if (catalogueBindingMap == null) + catalogueBindingMap = new LinkedHashMap(); + + return catalogueBindingMap.get(context); + } + + /** + * Inits the. + * + * @return the initialization report + * @throws InitializationException the initialization exception + */ + @Override + public InitializationReport init() throws InitializationException { + log.debug("Called init"); + // Creating all listeners + EventListener> listenerCreated = new EventListener>() { + + @Override + public void updated(ItemObserved observerd) { + log.info("listenerCreated fired on item: {} " + observerd); + + boolean subscribed = checkIfSubscribedEvent(observerd); + + if (subscribed) { + doAction(observerd); + } + + } + }; + + EventListener> listenerUpdated = new EventListener>() { + + @Override + public void updated(ItemObserved observerd) { + log.info("listenerUpdated fired on item: {} " + observerd); + boolean subscribed = checkIfSubscribedEvent(observerd); + + if (subscribed) { + doAction(observerd); + } + + } + }; + + EventListener> listenerDeleted = new EventListener>() { + + @Override + public void updated(ItemObserved observerd) { + log.info("listenerDeleted fired on item: {} " + observerd); + boolean subscribed = checkIfSubscribedEvent(observerd); + + if (subscribed) { + doAction(observerd); + } + } + }; + + EventListener> listenerLCStepPerformed = new EventListener>() { + + @Override + public void updated(ItemObserved observerd) { + log.info("listenerLCStepPerformed fired on item: {} " + observerd); + boolean subscribed = checkIfSubscribedEvent(observerd); + + if (subscribed) { + doAction(observerd); + } + } + }; + + // Subscribing all events + EventManager eventMngInst = EventManager.getInstance(); + eventMngInst.subscribe(Event.PROJECT_CREATED, listenerCreated); + eventMngInst.subscribe(Event.PROJECT_UPDATED, listenerUpdated); + eventMngInst.subscribe(Event.PROJECT_DELETED, listenerDeleted); + eventMngInst.subscribe(Event.LIFECYCLE_STEP_PERFORMED, listenerLCStepPerformed); + + return new InitializationReport(Status.OK, PLUGIN_ID + " init performed"); + + } + + /** + * Read events subscribed from configuration in the UCD. + * + * @param useCaseDescriptor the use case descriptor + * @return the events subscribed + * @throws Exception the exception + */ + public EventsSubscribed readEventsSubscribedFromConfigurationInTheUCD(UseCaseDescriptor useCaseDescriptor) + throws Exception { + + log.debug("Reading subscribed events from UCD"); + + EventsSubscribed eventsSubscrInTheUCD = new EventsSubscribed(); + + if (useCaseDescriptor == null) + throw new Exception("Error reading UCD null found"); + + try { + + String context = UserUtils.getCurrent().getContext(); + + eventsSubscrInTheUCD = getCatalogueBindingMapPerContext(); + + List listEventsSubscribedPerUCD = new ArrayList(); + if (eventsSubscrInTheUCD == null || listEventsSubscribedPerUCD.isEmpty()) { + + Document profileConfiguration = getConfigurationFromProfile(useCaseDescriptor).getConfiguration(); + log.debug("UseCaseDescriptor Configuration is {} ", profileConfiguration); + // JSONPathWrapper schemaNavigator = new + // JSONPathWrapper(useCaseDescriptor.getSchema().toJson()); + + for (Object fsConfigObj : profileConfiguration.get(SUBSCRIBE_EVENTS_CONFIG, List.class)) { + log.debug("Managing {} ", fsConfigObj); + SubscribeEventsConfig fsConfig = Serialization.convert(fsConfigObj, SubscribeEventsConfig.class); + log.debug("Converted config {}", fsConfig); + String theEventSubsribed = fsConfig.getEvent(); + if (theEventSubsribed == null || theEventSubsribed.isEmpty()) + throw new MaterializationException( + "Invalid Field Definition path in configuration [NO MATCH] : " + fsConfig.getEvent()); + + Event event = Serialization.convert(theEventSubsribed, Event.class); + log.debug("Added event {} to list ", event); + listEventsSubscribedPerUCD.add(event); + } + + eventsSubscrInTheUCD = new EventsSubscribed(); + eventsSubscrInTheUCD.setContext(context); + eventsSubscrInTheUCD.setListEventSubscribed(listEventsSubscribedPerUCD); + catalogueBindingMap.put(context, eventsSubscrInTheUCD); + + log.info("Events subscribed read from config {} ", eventsSubscrInTheUCD); + + } + + } catch (Exception e) { + log.error("Unable to read configuration ", e); + } catch (Throwable t) { + log.error("Exception, Unable to read configuration ", t); + } + + return eventsSubscrInTheUCD; + } + + /** + * Check if subscribed event. + * + * @param observerd the observerd + * @return true, if successful + */ + public boolean checkIfSubscribedEvent(ItemObserved observerd) { + log.info("Checking if {} is an subscribed event", observerd.getEvent()); + try { + EventsSubscribed eventsSub = readEventsSubscribedFromConfigurationInTheUCD( + observerd.getUseCaseDescriptor()); + + List listEvents = eventsSub.getListEventSubscribed(); + log.info("List events is {}", listEvents); + if (listEvents.contains(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; + + } catch (Exception e) { + log.error("Exception, Error on checking subscribed events", e); + return false; + } + + } + /** * Shutdown. * @@ -79,7 +298,6 @@ public class CatalogueBindingPlugin extends CatalogueBindingAbstractPlugin { } - @Override public void doAction(ItemObserved observerd) { new BindingAction().doAction(observerd); diff --git a/catalogue-binding-plugin/src/test/java/CatalogueBindingPluginTest.java b/catalogue-binding-plugin/src/test/java/CatalogueBindingPluginTest.java index 64052fd..8bf2071 100644 --- a/catalogue-binding-plugin/src/test/java/CatalogueBindingPluginTest.java +++ b/catalogue-binding-plugin/src/test/java/CatalogueBindingPluginTest.java @@ -6,7 +6,6 @@ import org.gcube.application.cms.tests.TestProfiles; import org.gcube.application.cms.tests.plugins.BasicPluginTest; import org.gcube.application.geoportal.common.model.useCaseDescriptor.UseCaseDescriptor; import org.gcube.application.geoportal.common.utils.tests.GCubeTest; -import org.junit.Test; public class CatalogueBindingPluginTest extends BasicPluginTest {