Integrated EventManager and notifies

This commit is contained in:
Francesco Mangiacrapa 2023-12-21 10:49:37 +01:00
parent a39f8cc8d6
commit 6a4babd6ea
1 changed files with 64 additions and 8 deletions

View File

@ -15,10 +15,15 @@ import javax.ws.rs.core.MediaType;
import org.bson.Document;
import org.gcube.application.cms.implementations.ImplementationProvider;
import org.gcube.application.cms.implementations.utils.UserUtils;
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.serialization.Serialization;
import org.gcube.application.geoportal.common.model.configuration.Configuration;
import org.gcube.application.geoportal.common.model.document.Project;
import org.gcube.application.geoportal.common.model.document.access.Access;
import org.gcube.application.geoportal.common.model.document.accounting.AccountingInfo;
import org.gcube.application.geoportal.common.model.rest.ConfigurationException;
import org.gcube.application.geoportal.common.model.rest.QueryRequest;
import org.gcube.application.geoportal.common.model.rest.RegisterFileSetRequest;
@ -38,8 +43,8 @@ import lombok.extern.slf4j.Slf4j;
* The Class ProfiledDocuments.
*
* @author created by Fabio Sinibaldi
* @author new manager/developer and mantainer - Francesco Mangiacrapa at ISTI-CNR
* francesco.mangiacrapa@isti.cnr.it
* @author new manager/developer and mantainer - Francesco Mangiacrapa at
* ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Apr 18, 2023
*/
@ -51,6 +56,7 @@ import lombok.extern.slf4j.Slf4j;
public class ProfiledDocuments {
private ProfiledMongoManager manager;
private EventManager eventManager = EventManager.getInstance(); // as singleton
/**
* Instantiates a new profiled documents.
@ -99,7 +105,7 @@ public class ProfiledDocuments {
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Project createNew(Document d) {
return new GuardedMethod<Project>() {
Project theNewProject = new GuardedMethod<Project>() {
@Override
protected Project run() throws Exception, WebApplicationException {
log.info("Creating new Project ({})", manager.getUseCaseDescriptor().getId());
@ -108,6 +114,18 @@ public class ProfiledDocuments {
return toReturn;
}
}.execute().getResult();
// notifying the Event.PROJECT_CREATED;
ItemObserved<Project> item = new ItemObserved<Project>();
EventManager.Event event = Event.PROJECT_CREATED;
AccountingInfo user = UserUtils.getCurrent().asInfo();
item.setCaller(user.getUser());
item.setContext(user.getContext());
item.setEvent(event);
item.setProject(theNewProject);
eventManager.notify(event, item);
return theNewProject;
}
/**
@ -122,13 +140,24 @@ public class ProfiledDocuments {
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Project update(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String documentId, Document d) {
return new GuardedMethod<Project>() {
Project theUpdatedProject = new GuardedMethod<Project>() {
@Override
protected Project run() throws Exception, WebApplicationException {
log.info("Updating Project ({}, ID {})", manager.getUseCaseDescriptor().getId(), documentId);
return manager.update(documentId, d);
}
}.execute().getResult();
ItemObserved<Project> item = new ItemObserved<Project>();
EventManager.Event event = Event.PROJECT_UPDATED;
AccountingInfo user = UserUtils.getCurrent().asInfo();
item.setCaller(user.getUser());
item.setContext(user.getContext());
item.setEvent(event);
item.setProject(theUpdatedProject);
eventManager.notify(event, item);
return theUpdatedProject;
}
/**
@ -165,7 +194,7 @@ public class ProfiledDocuments {
@Path("{" + InterfaceConstants.Parameters.PROJECT_ID + "}")
public Boolean delete(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id,
@DefaultValue("false") @QueryParam(InterfaceConstants.Parameters.FORCE) Boolean force) {
return new GuardedMethod<Boolean>() {
Boolean deleted = new GuardedMethod<Boolean>() {
@Override
protected Boolean run() throws Exception, WebApplicationException {
log.info("Deleting Project ({}, ID {}). Force is {}", manager.getUseCaseDescriptor().getId(), id,
@ -174,6 +203,21 @@ public class ProfiledDocuments {
return true;
}
}.execute().getResult();
ItemObserved<Project> item = new ItemObserved<Project>();
EventManager.Event event = Event.PROJECT_DELETED;
AccountingInfo user = UserUtils.getCurrent().asInfo();
item.setCaller(user.getUser());
item.setContext(user.getContext());
item.setEvent(event);
// Referencing delete project
Project deletedProject = new Project();
deletedProject.setId(id);
deletedProject.setProfileID(manager.getUseCaseDescriptor().getId());
item.setProject(deletedProject);
eventManager.notify(event, item);
return deleted;
}
/**
@ -217,7 +261,8 @@ public class ProfiledDocuments {
@Path("/" + InterfaceConstants.Methods.DELETE_FILES_PATH + "/{" + InterfaceConstants.Parameters.PROJECT_ID + "}")
public Project deleteFileSet(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id,
@DefaultValue("false") @QueryParam(InterfaceConstants.Parameters.FORCE) Boolean force,
@DefaultValue("false") @QueryParam(InterfaceConstants.Parameters.IGNORE_ERRORS) Boolean ignore_errors, String path) {
@DefaultValue("false") @QueryParam(InterfaceConstants.Parameters.IGNORE_ERRORS) Boolean ignore_errors,
String path) {
return new GuardedMethod<Project>() {
@Override
protected Project run() throws Exception, WebApplicationException {
@ -241,7 +286,7 @@ public class ProfiledDocuments {
@Path("/" + InterfaceConstants.Methods.STEP + "/{" + InterfaceConstants.Parameters.PROJECT_ID + "}")
public Project performStep(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id,
StepExecutionRequest request) {
return new GuardedMethod<Project>() {
Project theProject = new GuardedMethod<Project>() {
@Override
protected Project run() throws Exception, WebApplicationException {
log.info("Executing step {} on Project ({},ID,{}) with options {}", request.getStepID(),
@ -249,6 +294,17 @@ public class ProfiledDocuments {
return manager.performStep(id, request.getStepID(), request.getOptions());
}
}.execute().getResult();
ItemObserved<Project> item = new ItemObserved<Project>();
EventManager.Event event = Event.LIFECYCLE_STEP_PERFORMED;
AccountingInfo user = UserUtils.getCurrent().asInfo();
item.setCaller(user.getUser());
item.setContext(user.getContext());
item.setEvent(event);
item.setProject(theProject);
eventManager.notify(event, item);
return theProject;
}
/**
@ -457,4 +513,4 @@ public class ProfiledDocuments {
}.execute().getResult();
}
}
}