gcube-cms-suite/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/CatalogueBindingPlugin.java

321 lines
10 KiB
Java
Raw Normal View History

package org.gcube.application.cms.cataloguebinding;
2023-12-21 10:42:56 +01:00
2024-01-18 16:16:12 +01:00
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;
2024-01-19 11:12:57 +01:00
import org.gcube.application.cms.plugins.EventListenerPluginInterface;
2024-01-18 16:16:12 +01:00
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;
2023-12-21 10:42:56 +01:00
import org.gcube.application.cms.plugins.faults.InitializationException;
2024-04-15 17:01:56 +02:00
import org.gcube.application.cms.plugins.faults.InvalidProfileException;
2024-01-18 16:16:12 +01:00
import org.gcube.application.cms.plugins.faults.MaterializationException;
2023-12-21 10:42:56 +01:00
import org.gcube.application.cms.plugins.faults.ShutDownException;
2024-01-18 16:16:12 +01:00
import org.gcube.application.cms.plugins.implementations.AbstractPlugin;
2023-12-21 10:42:56 +01:00
import org.gcube.application.cms.plugins.reports.InitializationReport;
import org.gcube.application.cms.plugins.reports.Report;
2024-01-18 16:16:12 +01:00
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;
2023-12-21 10:42:56 +01:00
import org.gcube.application.geoportal.common.model.plugins.PluginDescriptor;
2024-01-18 16:16:12 +01:00
import org.gcube.application.geoportal.common.model.useCaseDescriptor.UseCaseDescriptor;
2023-12-21 10:42:56 +01:00
import com.vdurmont.semver4j.Semver;
2023-12-21 10:42:56 +01:00
2024-01-18 16:16:12 +01:00
import lombok.Data;
import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;
2023-12-21 10:42:56 +01:00
/**
* The Class CatalogueBindingPlugin.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
2024-01-19 11:12:57 +01:00
* Jan 19, 2024
*/
@Slf4j
2024-01-19 11:12:57 +01:00
public class CatalogueBindingPlugin extends AbstractPlugin implements EventListenerPluginInterface {
2024-01-19 14:36:43 +01:00
public static final String SUBSCRIBE_EVENTS_CONFIG = "subscribeEvents";
public static final String PLUGIN_ID = "Catalogue-Binding-Plugin";
public static final String PLUGIN_TYPE = "EventListener";
public static final PluginDescriptor DESCRIPTOR = new PluginDescriptor(PLUGIN_ID, PLUGIN_TYPE);
static {
DESCRIPTOR.setVersion(new Semver("1.0.0"));
DESCRIPTOR.setDescription("Manage the data binding from geoportal to catalogue");
}
/**
* Gets the descriptor.
*
* @return the descriptor
*/
2023-12-21 10:42:56 +01:00
@Override
public PluginDescriptor getDescriptor() {
2023-12-21 10:42:56 +01:00
return DESCRIPTOR;
}
2024-01-19 14:36:43 +01:00
2024-01-18 16:16:12 +01:00
@Data
2024-01-19 14:36:43 +01:00
public class SubscribeEventsConfig {
2024-01-18 16:16:12 +01:00
private String event;
}
/**
* Inits the.
*
* @return the initialization report
* @throws InitializationException the initialization exception
*/
@Override
public InitializationReport init() throws InitializationException {
log.debug("Called init");
2024-01-18 16:57:17 +01:00
InitializationReport report = null;
try {
// Creating all listeners
EventListener<ItemObserved<Project>> listenerCreated = new EventListener<ItemObserved<Project>>() {
@Override
public void updated(ItemObserved<Project> observerd) {
log.info("listenerCreated fired on item: {} " + observerd);
2024-01-18 16:16:12 +01:00
2024-01-18 16:57:17 +01:00
boolean subscribed = checkIfSubscribedEvent(observerd);
2024-01-18 16:16:12 +01:00
2024-01-18 16:57:17 +01:00
if (subscribed) {
doAction(observerd);
}
2024-01-18 16:16:12 +01:00
}
2024-01-18 16:57:17 +01:00
};
2024-01-18 16:16:12 +01:00
2024-01-18 16:57:17 +01:00
EventListener<ItemObserved<Project>> listenerUpdated = new EventListener<ItemObserved<Project>>() {
2024-01-18 16:16:12 +01:00
2024-01-18 16:57:17 +01:00
@Override
public void updated(ItemObserved<Project> observerd) {
log.info("listenerUpdated fired on item: {} " + observerd);
boolean subscribed = checkIfSubscribedEvent(observerd);
2024-01-18 16:16:12 +01:00
2024-01-18 16:57:17 +01:00
if (subscribed) {
doAction(observerd);
}
2024-01-18 16:16:12 +01:00
}
2024-01-18 16:57:17 +01:00
};
2024-01-18 16:16:12 +01:00
2024-01-18 16:57:17 +01:00
EventListener<ItemObserved<Project>> listenerDeleted = new EventListener<ItemObserved<Project>>() {
2024-01-18 16:16:12 +01:00
2024-01-18 16:57:17 +01:00
@Override
public void updated(ItemObserved<Project> observerd) {
log.info("listenerDeleted fired on item: {} " + observerd);
boolean subscribed = checkIfSubscribedEvent(observerd);
2024-01-18 16:16:12 +01:00
2024-01-18 16:57:17 +01:00
if (subscribed) {
doAction(observerd);
}
2024-01-18 16:16:12 +01:00
}
2024-01-18 16:57:17 +01:00
};
2024-01-18 16:16:12 +01:00
2024-01-18 16:57:17 +01:00
EventListener<ItemObserved<Project>> listenerLCStepPerformed = new EventListener<ItemObserved<Project>>() {
2024-01-18 16:16:12 +01:00
2024-01-18 16:57:17 +01:00
@Override
public void updated(ItemObserved<Project> observerd) {
log.info("listenerLCStepPerformed fired on item: {} " + observerd);
boolean subscribed = checkIfSubscribedEvent(observerd);
2024-01-18 16:16:12 +01:00
2024-01-18 16:57:17 +01:00
if (subscribed) {
doAction(observerd);
}
2024-01-18 16:16:12 +01:00
}
2024-01-18 16:57:17 +01:00
};
2024-01-18 16:16:12 +01:00
2024-01-18 16:57:17 +01:00
// 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);
2024-01-18 16:16:12 +01:00
2024-01-18 16:57:17 +01:00
report = new InitializationReport(Status.OK, PLUGIN_ID + " init performed");
} catch (Exception e) {
InitializationException exc = new InitializationException("Unable to initialize " + DESCRIPTOR.getId(), e);
log.error("init error: {} ", exc);
throw exc;
}
return report;
2024-01-18 16:16:12 +01:00
}
2024-01-19 14:36:43 +01:00
/**
* Inits the in context.
*
* @return the initialization report
* @throws InitializationException the initialization exception
*/
@Override
@Synchronized
public InitializationReport initInContext() throws InitializationException {
InitializationReport report = new InitializationReport();
try {
String context = UserUtils.getCurrent().getContext();
if (getCatalogueBindingMapPerContext() == null) {
log.info("Initializing in " + context);
catalogueBindingMap.put(context, new EventsSubscribed());
}
report.setStatus(Report.Status.OK);
report.putMessage("Initialized " + DESCRIPTOR.getId() + " in the " + context);
} catch (Exception e) {
InitializationException exc = new InitializationException("Unable to initialize " + DESCRIPTOR.getId(), e);
log.error("initInContext error: {} ", exc);
throw exc;
}
return report;
}
protected Map<String, EventsSubscribed> 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<String, EventsSubscribed>();
return catalogueBindingMap.get(context);
}
2024-01-18 16:16:12 +01:00
/**
* 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<Event> listEventsSubscribedPerUCD = new ArrayList<EventManager.Event>();
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);
}
2024-04-15 17:01:56 +02:00
} catch (InvalidProfileException e) {
log.warn("Unable to read configuration for {} in the UCD {}", CatalogueBindingPlugin.PLUGIN_ID,
useCaseDescriptor.getId());
2024-01-18 16:16:12 +01:00
} 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<Project> observerd) {
log.info("Checking if {} is an subscribed event", observerd.getEvent());
try {
EventsSubscribed eventsSub = readEventsSubscribedFromConfigurationInTheUCD(
observerd.getUseCaseDescriptor());
List<Event> listEvents = eventsSub.getListEventSubscribed();
log.info("List events is {}", listEvents);
if(listEvents==null) {
log.info("no event subscribed, returing false");
return false;
}
2024-01-18 16:16:12 +01:00
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.
*
* @throws ShutDownException the shut down exception
*/
2023-12-21 10:42:56 +01:00
@Override
public void shutdown() throws ShutDownException {
2023-12-21 10:42:56 +01:00
// TODO Auto-generated method stub
2023-12-21 10:42:56 +01:00
}
2024-01-19 11:12:57 +01:00
/**
* Do action.
*
* @param observerd the observerd
*/
@Override
public void doAction(ItemObserved<Project> observerd) {
new BindingAction().doAction(observerd);
2023-12-21 10:42:56 +01:00
}
2023-12-21 10:42:56 +01:00
}