2022-01-12 18:42:22 +01:00
|
|
|
package org.gcube.application.cms.concessioni.plugins;
|
|
|
|
|
2022-02-16 18:29:34 +01:00
|
|
|
import lombok.Setter;
|
2022-01-12 18:42:22 +01:00
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2022-01-27 15:02:53 +01:00
|
|
|
import org.bson.BsonDocument;
|
|
|
|
import org.bson.BsonString;
|
|
|
|
import org.bson.BsonValue;
|
|
|
|
import org.bson.Document;
|
2022-02-18 18:11:12 +01:00
|
|
|
import org.gcube.application.cms.plugins.IndexerPluginInterface;
|
2022-02-14 15:00:59 +01:00
|
|
|
import org.gcube.application.cms.plugins.MaterializationPlugin;
|
2022-02-16 18:29:34 +01:00
|
|
|
import org.gcube.application.cms.plugins.PluginManagerInterface;
|
2022-02-14 12:23:13 +01:00
|
|
|
import org.gcube.application.cms.plugins.faults.EventException;
|
2022-02-23 17:13:22 +01:00
|
|
|
import org.gcube.application.cms.plugins.model.ComparableVersion;
|
2022-02-14 15:00:59 +01:00
|
|
|
import org.gcube.application.cms.plugins.reports.*;
|
2022-02-14 12:23:13 +01:00
|
|
|
import org.gcube.application.cms.plugins.requests.EventExecutionRequest;
|
2022-02-18 18:11:12 +01:00
|
|
|
import org.gcube.application.cms.plugins.requests.IndexDocumentRequest;
|
2022-02-14 15:00:59 +01:00
|
|
|
import org.gcube.application.cms.plugins.requests.MaterializationRequest;
|
2022-02-01 15:24:39 +01:00
|
|
|
import org.gcube.application.cms.serialization.Serialization;
|
2022-01-14 12:31:11 +01:00
|
|
|
import org.gcube.application.cms.custom.gna.concessioni.model.ProfiledConcessione;
|
2022-01-12 18:42:22 +01:00
|
|
|
import org.gcube.application.cms.plugins.LifecycleManager;
|
|
|
|
import org.gcube.application.cms.plugins.faults.InitializationException;
|
|
|
|
import org.gcube.application.cms.plugins.faults.ShutDownException;
|
|
|
|
import org.gcube.application.cms.plugins.faults.StepException;
|
|
|
|
import org.gcube.application.cms.plugins.model.PluginDescriptor;
|
|
|
|
import org.gcube.application.cms.plugins.requests.StepExecutionRequest;
|
2022-01-27 15:02:53 +01:00
|
|
|
import org.gcube.application.geoportal.common.model.document.*;
|
2022-02-14 12:23:13 +01:00
|
|
|
import org.gcube.application.geoportal.common.model.document.access.Access;
|
|
|
|
import org.gcube.application.geoportal.common.model.document.access.AccessPolicy;
|
|
|
|
import org.gcube.application.geoportal.common.model.document.filesets.RegisteredFileSet;
|
|
|
|
import org.gcube.application.geoportal.common.model.document.lifecycle.LifecycleInformation;
|
|
|
|
import org.gcube.application.geoportal.common.model.document.lifecycle.TriggeredEvents;
|
2022-01-27 15:02:53 +01:00
|
|
|
import org.gcube.application.geoportal.common.model.legacy.report.ConstraintCheck;
|
2022-02-04 17:45:47 +01:00
|
|
|
import org.gcube.application.geoportal.common.model.rest.Configuration;
|
|
|
|
import org.gcube.application.geoportal.common.model.rest.ConfigurationException;
|
2022-02-16 17:55:41 +01:00
|
|
|
import org.gcube.application.geoportal.common.utils.ContextUtils;
|
2022-02-14 15:00:59 +01:00
|
|
|
import org.gcube.application.geoportal.common.utils.Files;
|
2022-01-27 15:02:53 +01:00
|
|
|
|
2022-01-12 18:42:22 +01:00
|
|
|
@Slf4j
|
|
|
|
public class ConcessioniLifeCycleManager implements LifecycleManager {
|
|
|
|
|
2022-02-18 18:11:12 +01:00
|
|
|
private static class Phases {
|
|
|
|
public static final String PENDING_APPROVAL="Pending Approval";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-12 18:42:22 +01:00
|
|
|
private static final PluginDescriptor DESCRIPTOR=new PluginDescriptor("GNA-CONCESSIONI-LC", PluginDescriptor.BaseTypes.LIFECYCLE_MANAGER);
|
|
|
|
static {
|
|
|
|
DESCRIPTOR.setDescription("GNA Concessioni. This plugin supports custom lifecycle management for the GNA Concessioni UseCase.");
|
|
|
|
DESCRIPTOR.setVersion(new ComparableVersion("1.0.0"));
|
|
|
|
}
|
|
|
|
|
2022-02-16 18:29:34 +01:00
|
|
|
@Setter
|
|
|
|
PluginManagerInterface pluginManager;
|
|
|
|
|
2022-01-12 18:42:22 +01:00
|
|
|
|
2022-02-18 18:11:12 +01:00
|
|
|
|
2022-01-12 18:42:22 +01:00
|
|
|
@Override
|
|
|
|
public InitializationReport initInContext() throws InitializationException {
|
2022-02-16 17:55:41 +01:00
|
|
|
InitializationReport report = new InitializationReport();
|
|
|
|
try{
|
2022-02-16 18:29:34 +01:00
|
|
|
report.setStatus(Report.Status.OK);
|
|
|
|
} catch (Throwable e) {
|
2022-02-16 17:55:41 +01:00
|
|
|
log.error("Unable to initialize plugins in {} ",ContextUtils.getCurrentScope(),e);
|
|
|
|
report.setStatus(Report.Status.WARNING);
|
|
|
|
report.putMessage("Unable to initialize plugins in "+ ContextUtils.getCurrentScope()+" : "+e.getMessage());
|
|
|
|
}
|
|
|
|
return report;
|
2022-01-12 18:42:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public InitializationReport init() throws InitializationException {
|
2022-02-16 17:55:41 +01:00
|
|
|
InitializationReport report = new InitializationReport();
|
|
|
|
try{
|
2022-02-16 18:29:34 +01:00
|
|
|
report.setStatus(Report.Status.OK);
|
2022-02-23 17:13:22 +01:00
|
|
|
|
2022-02-16 18:29:34 +01:00
|
|
|
} catch (Throwable e) {
|
2022-02-16 17:55:41 +01:00
|
|
|
log.error("Unable to initialize plugins ",e);
|
|
|
|
report.setStatus(Report.Status.WARNING);
|
|
|
|
report.putMessage("Unable to initialize plugins : "+e.getMessage());
|
|
|
|
}
|
|
|
|
return report;
|
2022-01-12 18:42:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void shutdown() throws ShutDownException {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-02-14 12:23:13 +01:00
|
|
|
public StepExecutionReport performStep(StepExecutionRequest request) throws StepException {
|
2022-01-14 12:31:11 +01:00
|
|
|
log.info("Serving Request {}",request);
|
2022-02-23 17:13:22 +01:00
|
|
|
StepExecutionReport report=new StepExecutionReport(request);
|
2022-02-17 16:01:39 +01:00
|
|
|
report.setStatus(Report.Status.OK);
|
|
|
|
|
|
|
|
LifecycleInformation info=report.getToSetLifecycleInformation();
|
2022-01-17 14:19:59 +01:00
|
|
|
info.setLastOperationStatus(LifecycleInformation.Status.OK);
|
2022-02-14 17:06:32 +01:00
|
|
|
info.setLastInvokedStep(request.getStep());
|
2022-01-14 12:31:11 +01:00
|
|
|
try {
|
2022-02-23 17:13:22 +01:00
|
|
|
|
|
|
|
MaterializationPlugin plugin;
|
|
|
|
|
|
|
|
IndexerPluginInterface indexerPlugin;
|
|
|
|
|
|
|
|
plugin= (MaterializationPlugin) pluginManager.getById("SDI-Default-Materializer");
|
|
|
|
indexerPlugin = (IndexerPluginInterface) pluginManager.getById("SDI-Indexer-Plugin");
|
|
|
|
|
2022-01-14 12:31:11 +01:00
|
|
|
switch (request.getStep()) {
|
2022-02-14 15:00:59 +01:00
|
|
|
case "SUBMIT-FOR-REVIEW" :{
|
2022-02-14 12:23:13 +01:00
|
|
|
|
2022-02-14 15:00:59 +01:00
|
|
|
//TODO Checks
|
2022-02-15 19:02:44 +01:00
|
|
|
if(!request.getDocument().getLifecycleInformation().getPhase().equals(LifecycleInformation.DRAFT_PHASE))
|
2022-02-14 17:06:32 +01:00
|
|
|
throw new StepException("Document is not in Draft phase");
|
2022-02-14 15:00:59 +01:00
|
|
|
//Materialize layers
|
|
|
|
MaterializationRequest matReq = new MaterializationRequest();
|
|
|
|
matReq.setDocument(request.getDocument());
|
|
|
|
matReq.setProfile(request.getProfile());
|
|
|
|
Document params = new Document();
|
|
|
|
String workspace = request.getProfile().getId() + request.getContext().getId();
|
|
|
|
params.put("workspace", Files.fixFilename(workspace));
|
|
|
|
|
2022-02-14 17:06:32 +01:00
|
|
|
|
2022-02-17 16:01:39 +01:00
|
|
|
matReq.setCallParameters(params);
|
2022-02-17 11:07:03 +01:00
|
|
|
MaterializationReport matRep = plugin.materialize(matReq);
|
2022-02-14 15:00:59 +01:00
|
|
|
|
2022-02-14 17:06:32 +01:00
|
|
|
|
2022-02-18 18:11:12 +01:00
|
|
|
report.setResultingDocument(matRep.getResultingDocument());
|
2022-02-16 17:20:40 +01:00
|
|
|
switch(matRep.getStatus()){
|
|
|
|
case OK : {
|
2022-02-18 18:11:12 +01:00
|
|
|
info.setPhase(Phases.PENDING_APPROVAL);
|
2022-02-16 17:20:40 +01:00
|
|
|
|
|
|
|
//TODO Index-confidential
|
2022-02-18 18:11:12 +01:00
|
|
|
|
|
|
|
|
2022-02-16 17:20:40 +01:00
|
|
|
//TODO Notifications
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ERROR : {
|
|
|
|
info.setLastOperationStatus(LifecycleInformation.Status.ERROR);
|
|
|
|
matRep.getMessages().forEach(s -> info.addErrorMessage(s));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case WARNING : {
|
|
|
|
info.setLastOperationStatus(LifecycleInformation.Status.WARNING);
|
|
|
|
matRep.getMessages().forEach(s -> info.addWarningMessage(s));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-02-14 12:23:13 +01:00
|
|
|
|
2022-02-16 17:20:40 +01:00
|
|
|
}
|
2022-02-14 12:23:13 +01:00
|
|
|
break;
|
|
|
|
case "REJECT-DRAFT":{
|
|
|
|
// Notification
|
|
|
|
// Set
|
2022-01-14 12:31:11 +01:00
|
|
|
break;
|
|
|
|
}
|
2022-02-14 12:23:13 +01:00
|
|
|
|
|
|
|
case "APPROVE DRAFT":{
|
2022-02-14 15:00:59 +01:00
|
|
|
// Index-published
|
2022-02-18 18:11:12 +01:00
|
|
|
|
|
|
|
if(!request.getDocument().getLifecycleInformation().getPhase()
|
|
|
|
.equals(Phases.PENDING_APPROVAL))
|
|
|
|
throw new StepException("Document is not in "+Phases.PENDING_APPROVAL+" phase");
|
|
|
|
|
|
|
|
IndexDocumentRequest indexRequest = new IndexDocumentRequest();
|
|
|
|
indexRequest.setDocument(request.getDocument());
|
|
|
|
indexRequest.setProfile(request.getProfile());
|
|
|
|
Document callParameters = new Document();
|
2022-02-24 18:09:30 +01:00
|
|
|
callParameters.put("workspace",Files.fixFilename("gna_concessioni_"+request.getContext().getId()));
|
|
|
|
callParameters.put("indexName",Files.fixFilename("gna_concessioni_centroids_"+request.getContext().getId()));
|
2022-02-18 18:11:12 +01:00
|
|
|
indexRequest.setCallParameters(callParameters);
|
|
|
|
|
|
|
|
IndexDocumentReport indexReport = indexerPlugin.index(indexRequest);
|
|
|
|
|
|
|
|
switch(indexReport.getStatus()){
|
|
|
|
case OK : {
|
|
|
|
info.setPhase("PUBLISHED");
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ERROR : {
|
|
|
|
info.setLastOperationStatus(LifecycleInformation.Status.ERROR);
|
|
|
|
indexReport.getMessages().forEach(s -> info.addErrorMessage(s));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case WARNING : {
|
|
|
|
info.setLastOperationStatus(LifecycleInformation.Status.WARNING);
|
|
|
|
indexReport.getMessages().forEach(s -> info.addWarningMessage(s));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-02-14 12:23:13 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-01-14 12:31:11 +01:00
|
|
|
default:
|
|
|
|
throw new StepException("Invalid Step " + request.getStep());
|
2022-01-12 18:42:22 +01:00
|
|
|
}
|
2022-01-14 12:31:11 +01:00
|
|
|
}catch (StepException e){
|
|
|
|
throw e;
|
|
|
|
}catch (Throwable t){
|
|
|
|
log.error("Unable to perform step "+request.getStep(),t);
|
2022-02-17 16:01:39 +01:00
|
|
|
String msg = "Unable to execute Step "+request.getStep()+". Error was "+t.getMessage();
|
|
|
|
report.setStatus(Report.Status.ERROR);
|
|
|
|
report.putMessage(msg);
|
|
|
|
info.setLastOperationStatus(LifecycleInformation.Status.ERROR);
|
|
|
|
info.addErrorMessage(msg);
|
2022-01-12 18:42:22 +01:00
|
|
|
}
|
2022-01-14 12:31:11 +01:00
|
|
|
return report;
|
2022-01-12 18:42:22 +01:00
|
|
|
}
|
|
|
|
|
2022-02-14 12:23:13 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public EventExecutionReport onEvent(EventExecutionRequest request) throws EventException {
|
|
|
|
log.info("Executing Event {}",request);
|
2022-02-23 17:13:22 +01:00
|
|
|
EventExecutionReport report=new EventExecutionReport(request);
|
2022-02-17 16:01:39 +01:00
|
|
|
|
2022-02-14 12:23:13 +01:00
|
|
|
TriggeredEvents info=new TriggeredEvents();
|
2022-02-17 16:01:39 +01:00
|
|
|
info.setEvent(request.getEvent());
|
|
|
|
info.setLastOperationStatus(LifecycleInformation.Status.OK);
|
|
|
|
report.setStatus(Report.Status.OK);
|
|
|
|
|
2022-02-14 12:23:13 +01:00
|
|
|
try {
|
|
|
|
switch(request.getEvent()){
|
|
|
|
case EventExecutionRequest.Events.ON_INIT_DOCUMENT:
|
|
|
|
// Set Defaults as for on update
|
|
|
|
case EventExecutionRequest.Events.ON_UPDATE_DOCUMENT: {
|
|
|
|
log.debug("Setting default values..");
|
2022-02-15 19:02:44 +01:00
|
|
|
// TODO implement defaults for concessione
|
|
|
|
// report.setResult(setDefaults(request.getDocument()));
|
2022-02-14 12:23:13 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case EventExecutionRequest.Events.ON_DELETE_DOCUMENT: {
|
|
|
|
//DELETE ALL
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: throw new EventException("Unexpected Event "+request.getEvent());
|
|
|
|
}
|
|
|
|
}catch (EventException e){
|
|
|
|
throw e;
|
|
|
|
}catch (Throwable t){
|
|
|
|
log.error("Unable to execute on event "+request.getEvent(),t);
|
2022-02-17 16:01:39 +01:00
|
|
|
String msg = "Unable to execute on event "+request.getEvent()+". Error was "+t.getMessage();
|
2022-02-14 12:23:13 +01:00
|
|
|
info.setLastOperationStatus(LifecycleInformation.Status.ERROR);
|
2022-02-17 16:01:39 +01:00
|
|
|
info.addErrorMessage(msg);
|
|
|
|
report.setStatus(Report.Status.ERROR);
|
|
|
|
report.putMessage(msg);
|
2022-02-14 12:23:13 +01:00
|
|
|
}finally{
|
2022-02-17 16:01:39 +01:00
|
|
|
report.getToSetLifecycleInformation().addEventReport(info);
|
2022-02-14 12:23:13 +01:00
|
|
|
}
|
|
|
|
return report;
|
|
|
|
}
|
|
|
|
|
2022-02-16 18:29:34 +01:00
|
|
|
|
|
|
|
|
2022-02-04 17:45:47 +01:00
|
|
|
@Override
|
|
|
|
public Configuration getCurrentConfiguration() throws ConfigurationException {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-01-12 18:42:22 +01:00
|
|
|
@Override
|
|
|
|
public PluginDescriptor getDescriptor() {
|
|
|
|
return DESCRIPTOR;
|
|
|
|
}
|
2022-01-14 12:31:11 +01:00
|
|
|
|
|
|
|
|
2022-02-14 12:23:13 +01:00
|
|
|
|
2022-01-14 12:31:11 +01:00
|
|
|
// STATIC ROUTINES
|
|
|
|
|
|
|
|
private static final ProfiledDocument setDefaults(ProfiledDocument document){
|
2022-01-17 13:30:21 +01:00
|
|
|
ProfiledConcessione c=Serialization.convert(document,ProfiledConcessione.class);
|
2022-01-27 15:02:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Document doc=c.getTheDocument();
|
|
|
|
doc.putIfAbsent(ProfiledConcessione.SOGGETTO,new String[]{"Research Excavation","Archaeology"});
|
|
|
|
|
|
|
|
doc.putIfAbsent(ProfiledConcessione.DSCRIZIONE_CONTENUTO,"Relazione di fine scavo e relativo abstract; selezione di immagini rappresentative;"
|
|
|
|
+ " posizionamento topografico dell'area indagata, pianta di fine scavo.");
|
|
|
|
|
|
|
|
// Super Section
|
|
|
|
c.getInfo().getAccess().setLicense(
|
|
|
|
ConstraintCheck.defaultFor(c.getInfo().getAccess().getLicense(), "CC0-1.0").evaluate());
|
|
|
|
|
|
|
|
|
|
|
|
//RELAZIONE
|
|
|
|
// TODO NB provare se object gia' presente sia usando Document che sub Object
|
|
|
|
doc.putIfAbsent(ProfiledConcessione.RELAZIONE_SCAVO,new Document());
|
|
|
|
Document rel=Serialization.convert(doc.get(ProfiledConcessione.RELAZIONE_SCAVO), Document.class);
|
|
|
|
|
|
|
|
rel.putIfAbsent(ProfiledConcessione.Sections.TITOLO,doc.getString(ProfiledConcessione.NOME)+" relazione di scavo");
|
|
|
|
rel.putIfAbsent(ProfiledConcessione.SOGGETTO,doc.get(ProfiledConcessione.SOGGETTO));
|
|
|
|
rel.putIfAbsent(RegisteredFileSet.CREATION_INFO,c.getInfo().getCreationInfo());
|
|
|
|
rel.putIfAbsent(RegisteredFileSet.ACCESS,c.getInfo().getAccess());
|
|
|
|
Access relAccess=Serialization.convert(rel.get(RegisteredFileSet.ACCESS),Access.class);
|
|
|
|
relAccess.setLicense(ConstraintCheck.defaultFor(relAccess.getLicense(),"CC-BY-4.0").evaluate());
|
|
|
|
relAccess.setPolicy(ConstraintCheck.defaultFor(relAccess.getPolicy(), AccessPolicy.OPEN).evaluate());
|
|
|
|
|
|
|
|
//ABSTRACT Relazione
|
|
|
|
doc.putIfAbsent(ProfiledConcessione.ABSTRACT_RELAZIONE,new Document());
|
|
|
|
Document abs=Serialization.convert(doc.get(ProfiledConcessione.ABSTRACT_RELAZIONE),Document.class);
|
|
|
|
abs.putIfAbsent(ProfiledConcessione.Sections.TITOLO,doc.getString(ProfiledConcessione.NOME)+" abstract relazione di scavo");
|
|
|
|
abs.putIfAbsent(RegisteredFileSet.CREATION_INFO,c.getInfo().getCreationInfo());
|
|
|
|
abs.putIfAbsent(RegisteredFileSet.ACCESS,c.getInfo().getAccess());
|
|
|
|
Access absAccess=Serialization.convert(abs.get(RegisteredFileSet.ACCESS),Access.class);
|
|
|
|
absAccess.setLicense(ConstraintCheck.defaultFor(absAccess.getLicense(),"CC-BY-4.0").evaluate());
|
|
|
|
absAccess.setPolicy(ConstraintCheck.defaultFor(absAccess.getPolicy(), AccessPolicy.OPEN).evaluate());
|
|
|
|
|
|
|
|
//TODO complete setDefaults
|
|
|
|
//IMMAGINI RAPPRESENTATIVE
|
|
|
|
|
|
|
|
|
|
|
|
if(doc.containsKey(ProfiledConcessione.IMMAGINI_RAPPRESENTATIVE)) {
|
|
|
|
for (BsonValue bsonValue : doc.toBsonDocument(null, null).
|
|
|
|
getArray(ProfiledConcessione.IMMAGINI_RAPPRESENTATIVE)) {
|
|
|
|
|
|
|
|
BsonDocument imgDocument = bsonValue.asDocument();
|
|
|
|
imgDocument.putIfAbsent(ProfiledConcessione.SOGGETTO,new BsonString(doc.getString(ProfiledConcessione.SOGGETTO)));
|
|
|
|
// imgDocument.putIfAbsent(RegisteredFileSet.CREATION_INFO,this.getInfo().getCreationInfo());
|
|
|
|
// imgDocument.putIfAbsent(RegisteredFileSet.CREATION_INFO,new BsonDocument(new Document()));
|
|
|
|
// imgDocument.putIfAbsent(RegisteredFileSet.ACCESS,this.getInfo().getAccess());
|
|
|
|
// Access absAccess=rel.get(RegisteredFileSet.ACCESS,Access.class);
|
|
|
|
// absAccess.setLicense(ConstraintCheck.defaultFor(absAccess.getLicense(),"CC-BY-4.0").evaluate());
|
|
|
|
// absAccess.setPolicy(ConstraintCheck.defaultFor(absAccess.getPolicy(), AccessPolicy.OPEN).evaluate());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-14 12:31:11 +01:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-12 18:42:22 +01:00
|
|
|
}
|