2022-03-29 18:06:09 +02:00
|
|
|
package org.gcube.application.cms.plugins.implementations;
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import org.bson.Document;
|
|
|
|
import org.gcube.application.cms.plugins.IndexerPluginInterface;
|
|
|
|
import org.gcube.application.cms.plugins.LifecycleManager;
|
|
|
|
import org.gcube.application.cms.plugins.MaterializationPlugin;
|
|
|
|
import org.gcube.application.cms.plugins.faults.*;
|
2022-04-01 19:11:11 +02:00
|
|
|
import org.gcube.application.cms.plugins.implementations.executions.GuardedEventManager;
|
|
|
|
import org.gcube.application.cms.plugins.implementations.executions.GuardedStepExecution;
|
2022-03-29 18:06:09 +02:00
|
|
|
import org.gcube.application.cms.plugins.reports.*;
|
|
|
|
import org.gcube.application.cms.plugins.requests.*;
|
|
|
|
import org.gcube.application.geoportal.common.model.JSONPathWrapper;
|
|
|
|
import org.gcube.application.geoportal.common.model.configuration.Configuration;
|
|
|
|
import org.gcube.application.geoportal.common.model.configuration.Index;
|
|
|
|
import org.gcube.application.geoportal.common.model.document.filesets.RegisteredFileSet;
|
|
|
|
import org.gcube.application.geoportal.common.model.document.lifecycle.LifecycleInformation;
|
2022-04-01 19:11:11 +02:00
|
|
|
import org.gcube.application.geoportal.common.model.plugins.OperationDescriptor;
|
2022-03-29 18:06:09 +02:00
|
|
|
import org.gcube.application.geoportal.common.model.rest.ConfigurationException;
|
|
|
|
import org.gcube.application.geoportal.common.utils.Files;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2022-04-01 19:11:11 +02:00
|
|
|
import java.util.Collections;
|
2022-03-29 18:06:09 +02:00
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
public class SimpleLifeCycleManager extends AbstractLifeCycleManager implements LifecycleManager {
|
|
|
|
|
2022-04-01 19:11:11 +02:00
|
|
|
public SimpleLifeCycleManager() {
|
2022-04-06 17:04:56 +02:00
|
|
|
DESCRIPTOR.setId("DEFAULT-SINGLE-STEP");
|
2022-04-01 19:11:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static class Steps {
|
|
|
|
public static final OperationDescriptor PUBLISH = new OperationDescriptor("PUBLISH","Materialize & index project");
|
|
|
|
|
|
|
|
static{
|
|
|
|
PUBLISH.setAppliableToPhases(Collections.singletonList(LifecycleInformation.CommonPhases.DRAFT_PHASE));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-29 18:06:09 +02:00
|
|
|
@Override
|
|
|
|
public Configuration getCurrentConfiguration(BaseRequest req) throws ConfigurationException {
|
|
|
|
Configuration toReturn = super.getCurrentConfiguration(req);
|
|
|
|
toReturn.setIndexes(new ArrayList<>());
|
|
|
|
|
|
|
|
IndexerPluginInterface indexerPlugin;
|
|
|
|
indexerPlugin = (IndexerPluginInterface) pluginManager.getById("SDI-Indexer-Plugin");
|
|
|
|
BaseRequest indexRequest = new BaseRequest(req.getUseCaseDescriptor(),req.getCaller(),req.getContext());
|
|
|
|
|
|
|
|
// Info on Public index
|
|
|
|
try {
|
|
|
|
indexRequest.setCallParameters(getPublicIndexParams(req));
|
|
|
|
Index publicIndex = indexerPlugin.getIndex(indexRequest);
|
|
|
|
publicIndex.put("flag", "public");
|
|
|
|
toReturn.getIndexes().add(publicIndex);
|
|
|
|
}catch(ConfigurationException e){
|
|
|
|
toReturn.addErrorMessage("Unable to gather information on public GIS Centroids Index : "+e.getMessage());
|
|
|
|
log.error("Unable to gather information on public GIS Centroids Index",e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected Document getPublicIndexParams(BaseRequest req){
|
|
|
|
Document callParameters = new Document();
|
|
|
|
callParameters.put("workspace",Files.fixFilename(req.getUseCaseDescriptor().getId()+req.getContext().getName()));
|
|
|
|
callParameters.put("indexName",Files.fixFilename(req.getUseCaseDescriptor().getId()+req.getContext().getName()+"_centroids"));
|
|
|
|
return callParameters;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected Document getMaterializationParameters(BaseRequest request){
|
|
|
|
Document params = new Document();
|
|
|
|
String workspace = request.getUseCaseDescriptor().getId() + request.getContext().getId();
|
|
|
|
params.put("workspace", Files.fixFilename(workspace));
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void registerSteps() {
|
2022-04-01 19:11:11 +02:00
|
|
|
setStep(new GuardedStepExecution(Steps.PUBLISH) {
|
2022-03-29 18:06:09 +02:00
|
|
|
@Override
|
|
|
|
protected StepExecutionReport run() throws Exception {
|
|
|
|
if(!theReport.getTheRequest().getDocument().getLifecycleInformation().getPhase().equals(LifecycleInformation.CommonPhases.DRAFT_PHASE))
|
|
|
|
throw new StepException("Document is not in "+LifecycleInformation.CommonPhases.DRAFT_PHASE+" phase");
|
|
|
|
|
|
|
|
// Materialize
|
|
|
|
theReport = materializeDocument(theReport, getMaterializer(),
|
|
|
|
getMaterializationParameters(theReport.getTheRequest()));
|
|
|
|
|
|
|
|
if(theReport.getToSetLifecycleInformation().getLastOperationStatus().equals(LifecycleInformation.Status.OK)){
|
|
|
|
// Index
|
|
|
|
theReport = index(theReport,getIndexer(),
|
|
|
|
getPublicIndexParams(theReport.getTheRequest()));
|
|
|
|
// setPhase
|
|
|
|
if(theReport.getToSetLifecycleInformation().getLastOperationStatus().equals(LifecycleInformation.Status.OK))
|
|
|
|
theReport.getToSetLifecycleInformation().setPhase("PUBLISHED");
|
|
|
|
}
|
|
|
|
return theReport;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void blockNonDraft(EventExecutionReport report) throws InvalidPluginRequestException {
|
2022-04-01 19:11:11 +02:00
|
|
|
Boolean force = false;
|
|
|
|
try {
|
|
|
|
force = Boolean.parseBoolean(report.getTheRequest().getCallParameters().get("force").toString());
|
|
|
|
}catch(Throwable t){}
|
|
|
|
|
2022-03-29 18:06:09 +02:00
|
|
|
if(!report.getTheRequest().getDocument().getLifecycleInformation().getPhase().equals(LifecycleInformation.CommonPhases.DRAFT_PHASE) && ! force)
|
|
|
|
throw new InvalidPluginRequestException("Document is not in "+LifecycleInformation.CommonPhases.DRAFT_PHASE+" phase");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected EventExecutionReport onInitDocument(EventExecutionReport report) throws InvalidPluginRequestException {
|
|
|
|
blockNonDraft(report);
|
|
|
|
return super.onInitDocument(report);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected EventExecutionReport onUpdateDocument(EventExecutionReport report) {
|
|
|
|
return super.onUpdateDocument(report);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected EventExecutionReport onDeleteDocument(EventExecutionReport report) throws ConfigurationException, InvalidPluginRequestException, MaterializationException, EventException {
|
|
|
|
// dematerialize all
|
|
|
|
blockNonDraft(report);
|
|
|
|
JSONPathWrapper wrapper = new JSONPathWrapper(report.getTheRequest().getDocument().getTheDocument().toJson());
|
|
|
|
for (String s : wrapper.getMatchingPaths("..*[?(@." + RegisteredFileSet.PAYLOADS + ")]")){
|
|
|
|
log.info("Requesting dematerialization for {} ",s);
|
|
|
|
report = deMaterialize(report,getMaterializer(),new Document("fileSetPath",s));
|
|
|
|
if(!report.getToSetLifecycleInformation().getLastOperationStatus().equals(LifecycleInformation.Status.OK))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(report.getToSetLifecycleInformation().getLastOperationStatus().equals(LifecycleInformation.Status.OK)) {
|
|
|
|
report = deIndex(report,getIndexer(),getPublicIndexParams(report.getTheRequest()));
|
|
|
|
}
|
|
|
|
return report;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected EventExecutionReport onDeleteFileSet(EventExecutionReport theReport) throws ConfigurationException, InvalidPluginRequestException, MaterializationException, EventException {
|
|
|
|
// dematerialize selected
|
|
|
|
blockNonDraft(theReport);
|
|
|
|
deMaterialize(theReport,getMaterializer(),
|
|
|
|
theReport.getTheRequest().getCallParameters());
|
|
|
|
// de index
|
|
|
|
if(theReport.getToSetLifecycleInformation().getLastOperationStatus().equals(LifecycleInformation.Status.OK))
|
|
|
|
deIndex(theReport,getIndexer(),getPublicIndexParams(theReport.getTheRequest()));
|
|
|
|
return theReport;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void registerEvents() {
|
|
|
|
super.registerEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected IndexerPluginInterface getIndexer() throws ConfigurationException {
|
|
|
|
return (IndexerPluginInterface) pluginManager.getById("SDI-Indexer-Plugin");
|
|
|
|
}
|
|
|
|
|
|
|
|
protected MaterializationPlugin getMaterializer() throws ConfigurationException {
|
|
|
|
return (MaterializationPlugin) pluginManager.getById("SDI-Default-Materializer");
|
|
|
|
}
|
|
|
|
|
|
|
|
protected <T extends DocumentHandlingReport> T deIndex(T report, IndexerPluginInterface indexer, Document parameters) throws InvalidPluginRequestException {
|
|
|
|
BaseExecutionRequest request = report.getTheRequest();
|
|
|
|
IndexDocumentRequest indexRequest = new IndexDocumentRequest(
|
|
|
|
request.getUseCaseDescriptor(),request.getCaller(), request.getContext(),request.getDocument());
|
|
|
|
|
|
|
|
indexRequest.setCallParameters(parameters);
|
|
|
|
IndexDocumentReport indexReport = indexer.deindex(indexRequest);
|
|
|
|
|
|
|
|
return handleReport(indexReport,report);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
protected <T extends DocumentHandlingReport> T deMaterialize(T report, MaterializationPlugin plugin, Document parameters) throws InvalidPluginRequestException, MaterializationException {
|
|
|
|
BaseExecutionRequest request = report.getTheRequest();
|
|
|
|
MaterializationRequest matReq =
|
|
|
|
new MaterializationRequest(request.getUseCaseDescriptor(),request.getCaller(), request.getContext(), request.getDocument());
|
|
|
|
|
|
|
|
Document params = new Document();
|
|
|
|
String workspace = request.getUseCaseDescriptor().getId() + request.getContext().getId();
|
|
|
|
params.put("workspace", Files.fixFilename(workspace));
|
|
|
|
|
|
|
|
matReq.setCallParameters(params);
|
|
|
|
MaterializationReport matRep = plugin.dematerialize(matReq);
|
|
|
|
|
|
|
|
return handleReport(matRep,report);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected <T extends DocumentHandlingReport> T index(T report, IndexerPluginInterface indexer, Document parameters) throws InvalidPluginRequestException {
|
|
|
|
BaseExecutionRequest request = report.getTheRequest();
|
|
|
|
IndexDocumentRequest indexRequest = new IndexDocumentRequest(
|
|
|
|
request.getUseCaseDescriptor(),request.getCaller(), request.getContext(),request.getDocument());
|
|
|
|
|
|
|
|
indexRequest.setCallParameters(parameters);
|
|
|
|
IndexDocumentReport indexReport = indexer.index(indexRequest);
|
|
|
|
|
|
|
|
return handleReport(indexReport,report);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
protected <T extends DocumentHandlingReport> T materializeDocument(T report,MaterializationPlugin plugin,Document parameters) throws InvalidPluginRequestException, MaterializationException {
|
|
|
|
BaseExecutionRequest request = report.getTheRequest();
|
|
|
|
MaterializationRequest matReq =
|
|
|
|
new MaterializationRequest(request.getUseCaseDescriptor(),request.getCaller(), request.getContext(), request.getDocument());
|
|
|
|
|
|
|
|
matReq.setCallParameters(parameters);
|
|
|
|
MaterializationReport matRep = plugin.materialize(matReq);
|
|
|
|
|
|
|
|
return handleReport(matRep,report);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private <T extends DocumentHandlingReport> T handleReport(DocumentHandlingReport toHandle, T toUpdate){
|
|
|
|
toUpdate.setResultingDocument(toHandle.getResultingDocument());
|
|
|
|
LifecycleInformation info = toUpdate.getToSetLifecycleInformation();
|
|
|
|
switch(toHandle.getStatus()){
|
|
|
|
case OK : {
|
|
|
|
info.setLastOperationStatus(LifecycleInformation.Status.OK);
|
|
|
|
if(toHandle instanceof IndexDocumentReport)
|
|
|
|
toUpdate.setToSetSpatialReference(((IndexDocumentReport)toHandle).getToSetSpatialReference());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ERROR : {
|
|
|
|
info.setLastOperationStatus(LifecycleInformation.Status.ERROR);
|
|
|
|
toHandle.getMessages().forEach(s -> info.addErrorMessage(s));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case WARNING : {
|
|
|
|
info.setLastOperationStatus(LifecycleInformation.Status.WARNING);
|
|
|
|
toHandle.getMessages().forEach(s -> info.addWarningMessage(s));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return toUpdate;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|