gcube-cms-suite/concessioni-lifecycle/src/main/java/org/gcube/application/cms/concessioni/plugins/ConcessioniLifeCycleManager...

273 lines
16 KiB
Java
Raw Normal View History

2022-01-12 18:42:22 +01:00
package org.gcube.application.cms.concessioni.plugins;
2022-04-01 19:11:11 +02:00
import com.vdurmont.semver4j.Semver;
2022-01-12 18:42:22 +01:00
import lombok.extern.slf4j.Slf4j;
2022-01-27 15:02:53 +01:00
import org.bson.Document;
2022-09-26 16:17:37 +02:00
import org.gcube.application.cms.custom.gna.concessioni.model.ProfiledConcessione;
2022-10-27 17:17:44 +02:00
import org.gcube.application.cms.implementations.ImplementationProvider;
import org.gcube.application.cms.implementations.ProjectAccess;
import org.gcube.application.cms.implementations.faults.InvalidUserRoleException;
import org.gcube.application.cms.implementations.faults.ProjectNotFoundException;
import org.gcube.application.cms.implementations.faults.RegistrationException;
import org.gcube.application.cms.implementations.faults.UnauthorizedAccess;
import org.gcube.application.cms.plugins.IndexerPluginInterface;
2022-09-26 16:17:37 +02:00
import org.gcube.application.cms.plugins.LifecycleManager;
2022-10-27 17:31:30 +02:00
import org.gcube.application.cms.plugins.faults.IndexingException;
2022-03-28 16:35:30 +02:00
import org.gcube.application.cms.plugins.implementations.Default3PhaseManager;
2022-05-10 14:42:11 +02:00
import org.gcube.application.cms.plugins.reports.EventExecutionReport;
import org.gcube.application.cms.plugins.reports.Report;
2022-10-27 17:17:44 +02:00
import org.gcube.application.cms.plugins.reports.StepExecutionReport;
import org.gcube.application.cms.plugins.requests.BaseRequest;
import org.gcube.application.cms.plugins.requests.IndexDocumentRequest;
2022-02-01 15:24:39 +01:00
import org.gcube.application.cms.serialization.Serialization;
2022-05-10 14:42:11 +02:00
import org.gcube.application.geoportal.common.model.JSONPathWrapper;
2022-09-26 16:17:37 +02:00
import org.gcube.application.geoportal.common.model.document.Project;
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;
2022-10-27 17:17:44 +02:00
import org.gcube.application.geoportal.common.model.document.lifecycle.LifecycleInformation;
import org.gcube.application.geoportal.common.model.document.relationships.RelationshipNavigationObject;
2022-01-27 15:02:53 +01:00
import org.gcube.application.geoportal.common.model.legacy.report.ConstraintCheck;
2022-10-27 17:17:44 +02:00
import org.gcube.application.geoportal.common.model.rest.ConfigurationException;
2022-01-27 15:02:53 +01:00
2022-05-10 14:42:11 +02:00
import java.io.IOException;
2022-10-27 17:17:44 +02:00
import java.util.ArrayList;
2022-05-10 14:42:11 +02:00
import java.util.List;
2022-01-12 18:42:22 +01:00
@Slf4j
2022-03-28 16:35:30 +02:00
/** Overrides 3 Phases lifecycle with override of default values
*
*/
public class ConcessioniLifeCycleManager extends Default3PhaseManager implements LifecycleManager {
2022-01-12 18:42:22 +01:00
2022-02-18 18:11:12 +01:00
2022-10-27 17:17:44 +02:00
private static final String FOLLOWS="follows";
private static final String PRECEDES="precedes";
2022-03-28 16:35:30 +02:00
public ConcessioniLifeCycleManager() {
DESCRIPTOR.setId("GNA-CONCESSIONI-LC");
2022-01-12 18:42:22 +01:00
DESCRIPTOR.setDescription("GNA Concessioni. This plugin supports custom lifecycle management for the GNA Concessioni UseCase.");
2022-04-01 19:11:11 +02:00
DESCRIPTOR.setVersion(new Semver("1.0.0"));
2022-01-12 18:42:22 +01:00
}
2022-05-10 14:42:11 +02:00
@Override
public EventExecutionReport setDefault(EventExecutionReport currentReport) {
EventExecutionReport report = super.setDefault(currentReport);
try{
report.setResultingDocument(setDefaults(report.getTheRequest().getDocument()).getTheDocument());
}catch (Throwable t){
log.error("Unable to evaluate defaults for concessione "+currentReport.getTheRequest().getDocument().getId(),t);
log.debug("Object was {} ",report.getTheRequest().getDocument());
report.setStatus(Report.Status.ERROR );
report.getMessages().add("Unable to evaluate defaults : "+t.getMessage());
}
return report;
}
2022-03-17 17:54:00 +01:00
2022-02-14 12:23:13 +01:00
2022-01-14 12:31:11 +01:00
// STATIC ROUTINES
2022-05-10 14:42:11 +02:00
static final Project setDefaults(Project document) throws IOException {
2022-08-31 18:06:19 +02:00
log.info("Concessione ID {}, setting defaults..",document.getId());
2022-05-10 14:42:11 +02:00
log.debug("Full concessione is {}",document);
2022-01-17 13:30:21 +01:00
ProfiledConcessione c=Serialization.convert(document,ProfiledConcessione.class);
2022-03-28 16:35:30 +02:00
Document doc=c.getTheDocument();
2022-01-27 15:02:53 +01:00
doc.putIfAbsent(ProfiledConcessione.SOGGETTO,new String[]{"Research Excavation","Archaeology"});
2022-05-10 14:42:11 +02:00
doc.putIfAbsent(ProfiledConcessione.DESCRIZIONE_CONTENUTO,"Relazione di fine scavo e relativo abstract; selezione di immagini rappresentative;"
2022-01-27 15:02:53 +01:00
+ " posizionamento topografico dell'area indagata, pianta di fine scavo.");
// Super Section
2022-05-10 14:42:11 +02:00
// TODO read from UCD
2022-01-27 15:02:53 +01:00
c.getInfo().getAccess().setLicense(
ConstraintCheck.defaultFor(c.getInfo().getAccess().getLicense(), "CC0-1.0").evaluate());
2022-05-10 14:42:11 +02:00
//RELAZIONE
Document rel = doc.containsKey(ProfiledConcessione.RELAZIONE_SCAVO)?
Serialization.convert(doc.get(ProfiledConcessione.RELAZIONE_SCAVO), Document.class):new Document();
log.debug("Concessione {}, managing relazione {}",document.getId(),rel);
2022-01-27 15:02:53 +01:00
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());
2022-05-10 14:42:11 +02:00
rel.put(RegisteredFileSet.ACCESS,relAccess);
doc.put(ProfiledConcessione.RELAZIONE_SCAVO,rel);
2022-01-27 15:02:53 +01:00
//ABSTRACT Relazione
2022-05-10 14:42:11 +02:00
Document abs=doc.containsKey(ProfiledConcessione.ABSTRACT_RELAZIONE)?
Serialization.convert(doc.get(ProfiledConcessione.ABSTRACT_RELAZIONE), Document.class):new Document();
log.debug("Concessione {}, managing abstract relazione {}",document.getId(),abs);
2022-01-27 15:02:53 +01:00
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());
2022-05-10 14:42:11 +02:00
abs.put(RegisteredFileSet.ACCESS,absAccess);
doc.put(ProfiledConcessione.ABSTRACT_RELAZIONE,abs);
2022-01-27 15:02:53 +01:00
2022-05-10 14:42:11 +02:00
//Posizionamento scavo
if(doc.containsKey(ProfiledConcessione.POSIZIONAMENTO_SCAVO)){
Document pos = Serialization.convert(doc.get(ProfiledConcessione.POSIZIONAMENTO_SCAVO), Document.class);
log.debug("Concessione {}, managing posizionamento scavo {}",document.getId(),pos);
pos.putIfAbsent(ProfiledConcessione.Sections.TITOLO,doc.getString(ProfiledConcessione.NOME)+" posizionamento scavo");
pos.putIfAbsent(ProfiledConcessione.Sections.ABSTRACT,"Posizionamento topografico georeferenziato dellarea interessata dalle indagini");
pos.putIfAbsent(ProfiledConcessione.Layers.TOPIC,"Society");
pos.putIfAbsent(ProfiledConcessione.Layers.SUB_TOPIC,"Archeology");
pos.putIfAbsent(ProfiledConcessione.PAROLE_CHIAVE_LIBERE,doc.get(ProfiledConcessione.PAROLE_CHIAVE_LIBERE));
pos.putIfAbsent(ProfiledConcessione.PAREOLE_CHIAVE_ICCD,doc.get(ProfiledConcessione.PAREOLE_CHIAVE_ICCD));
2022-01-27 15:02:53 +01:00
2022-05-10 14:42:11 +02:00
// BBOX in registered filesets
pos.put(ProfiledConcessione.RESPONSABILE,doc.get(ProfiledConcessione.RESPONSABILE));
pos.putIfAbsent(RegisteredFileSet.CREATION_INFO,c.getInfo().getCreationInfo());
pos.putIfAbsent(RegisteredFileSet.ACCESS,c.getInfo().getAccess());
Access posAccess=Serialization.convert(rel.get(RegisteredFileSet.ACCESS),Access.class);
posAccess.setLicense(ConstraintCheck.defaultFor(posAccess.getLicense(),"CC-BY-4.0").evaluate());
posAccess.setPolicy(ConstraintCheck.defaultFor(posAccess.getPolicy(), AccessPolicy.OPEN).evaluate());
pos.put(RegisteredFileSet.ACCESS,posAccess);
doc.put(ProfiledConcessione.POSIZIONAMENTO_SCAVO,pos);
}
//IMMAGINI RAPPRESENTATIVE
JSONPathWrapper wrapper=new JSONPathWrapper(Serialization.write(doc));
2022-01-27 15:02:53 +01:00
if(doc.containsKey(ProfiledConcessione.IMMAGINI_RAPPRESENTATIVE)) {
2022-05-10 14:42:11 +02:00
List imgs=wrapper.getByPath("$." + ProfiledConcessione.IMMAGINI_RAPPRESENTATIVE, List.class).get(0);
log.debug("Concessione {}, found imgs {}",document.getId(),imgs.size());
for (int i = 0; i <imgs.size() ; i++) {
Document imgDoc=Serialization.asDocument(imgs.get(i));
imgDoc.putIfAbsent(ProfiledConcessione.SOGGETTO,doc.get(ProfiledConcessione.SOGGETTO));
imgDoc.putIfAbsent(RegisteredFileSet.CREATION_INFO,c.getInfo().getCreationInfo());
imgDoc.putIfAbsent(RegisteredFileSet.ACCESS,c.getInfo().getAccess());
Access imgAccess=Serialization.convert(imgDoc.get(RegisteredFileSet.ACCESS),Access.class);
imgAccess.setLicense(ConstraintCheck.defaultFor(imgAccess.getLicense(),"CC-BY-4.0").evaluate());
imgAccess.setPolicy(ConstraintCheck.defaultFor(imgAccess.getPolicy(), AccessPolicy.OPEN).evaluate());
imgDoc.put(RegisteredFileSet.ACCESS,imgAccess);
wrapper.setElement("$."+ProfiledConcessione.IMMAGINI_RAPPRESENTATIVE+"["+i+"]",imgDoc);
2022-01-27 15:02:53 +01:00
}
}
2022-05-10 14:42:11 +02:00
// Layers
if(doc.containsKey(ProfiledConcessione.PIANTE_FINE_SCAVO)) {
List piante=wrapper.getByPath("$." + ProfiledConcessione.PIANTE_FINE_SCAVO, List.class).get(0);
log.debug("Concessione {}, found piante {}",document.getId(),piante.size());
for (int i = 0; i <piante.size() ; i++) {
Document piantaDoc=Serialization.asDocument(piante.get(i));
log.debug("Concessione {}, managing pianta {}",document.getId(),piantaDoc);
piantaDoc.putIfAbsent(ProfiledConcessione.Sections.TITOLO,doc.getString(ProfiledConcessione.NOME)+" pianta fine scavo");
piantaDoc.putIfAbsent(ProfiledConcessione.Sections.ABSTRACT,"Planimetria georeferenziata dell'area indagata al termine delle attività");
piantaDoc.putIfAbsent(ProfiledConcessione.Layers.TOPIC,"Society");
piantaDoc.putIfAbsent(ProfiledConcessione.Layers.SUB_TOPIC,"Archeology");
piantaDoc.putIfAbsent(ProfiledConcessione.PAROLE_CHIAVE_LIBERE,doc.get(ProfiledConcessione.PAROLE_CHIAVE_LIBERE));
piantaDoc.putIfAbsent(ProfiledConcessione.PAREOLE_CHIAVE_ICCD,doc.get(ProfiledConcessione.PAREOLE_CHIAVE_ICCD));
// BBOX in registered filesets
piantaDoc.put(ProfiledConcessione.RESPONSABILE,doc.get(ProfiledConcessione.RESPONSABILE));
piantaDoc.putIfAbsent(RegisteredFileSet.CREATION_INFO,c.getInfo().getCreationInfo());
piantaDoc.putIfAbsent(RegisteredFileSet.ACCESS,c.getInfo().getAccess());
Access posAccess=Serialization.convert(rel.get(RegisteredFileSet.ACCESS),Access.class);
posAccess.setLicense(ConstraintCheck.defaultFor(posAccess.getLicense(),"CC-BY-4.0").evaluate());
posAccess.setPolicy(ConstraintCheck.defaultFor(posAccess.getPolicy(), AccessPolicy.OPEN).evaluate());
piantaDoc.put(RegisteredFileSet.ACCESS,posAccess);
wrapper.setElement("$."+ProfiledConcessione.PIANTE_FINE_SCAVO+"["+i+"]",piantaDoc);
}
}
2022-01-27 15:02:53 +01:00
2022-05-10 14:42:11 +02:00
c.setTheDocument(Serialization.read(wrapper.getValueCTX().jsonString(),Document.class));
2022-01-14 12:31:11 +01:00
return c;
}
2022-10-27 17:17:44 +02:00
@Override
2022-10-27 17:31:30 +02:00
protected Document evaluateAdditionalIndexParameters(IndexDocumentRequest request) throws IndexingException {
2022-10-27 17:17:44 +02:00
Document toReturn = super.evaluateAdditionalIndexParameters(request);
2022-10-27 18:03:05 +02:00
if(toReturn == null) toReturn = new Document();
2022-10-27 17:17:44 +02:00
Project indexingProject = request.getDocument();
// Evaluate to display project IDs
log.info("Evaluating Last ID in relationship chain. Current Concessione ID is {}",indexingProject.getId());
try {
ArrayList<Project> projects = new ArrayList<>();
ProjectAccess access = ImplementationProvider.get().getProvidedObjectByClass(ProjectAccess.class);
// get Last ID in relation chain
projects.add(indexingProject);
if(!indexingProject.getRelationshipsByName(PRECEDES).isEmpty())
scanRelation(projects,access.getRelations(indexingProject.getProfileID(), indexingProject.getId(), PRECEDES,true).get(0),false);
if(!indexingProject.getRelationshipsByName(FOLLOWS).isEmpty())
scanRelation(projects,access.getRelations(indexingProject.getProfileID(), indexingProject.getId(), FOLLOWS,true).get(0),false);
2022-11-09 18:35:44 +01:00
log.debug("Produced full chain [size : {}] from {}, evaluating last available for PHASE {} ",projects.size(),indexingProject.getId(),
2022-10-27 17:17:44 +02:00
indexingProject.getLifecycleInformation().getPhase());
List<String> toDisplayId = new ArrayList<>();
List<String> toHideIds = new ArrayList<>();
log.trace("Checking from LAST.. ");
2022-11-09 18:35:44 +01:00
// Projects is time -ordered so we scan from last
2022-10-27 17:17:44 +02:00
for(int i = projects.size()-1;i>=0;i--) {
Project p = projects.get(i);
log.debug("Currently checking {} : {}",p.getId(),p.getTheDocument().get("nome"));
if(!toDisplayId.isEmpty())
toHideIds.add(p.getId());
else {
//Still need to find candidate for displaying feature
2022-11-09 18:35:44 +01:00
//Our currently indexing project is always a good candidate
if(p.getId().equals(indexingProject.getId()))
toDisplayId.add(p.getId());
//We check PHASE in order to skip projects not yet in the PHASE we are indexing
else switch(indexingProject.getLifecycleInformation().getPhase()){
2022-10-27 17:17:44 +02:00
case Phases.PENDING_APPROVAL:{
2022-11-09 18:35:44 +01:00
if ((p.getLifecycleInformation().getPhase().equals(Phases.PENDING_APPROVAL)||
p.getLifecycleInformation().getPhase().equals(Phases.PUBLISHED)))
2022-10-27 17:17:44 +02:00
toDisplayId.add(p.getId());
break;
}
case Phases.PUBLISHED:{
if (p.getLifecycleInformation().getPhase().equals(Phases.PUBLISHED))
toDisplayId.add(p.getId());
break;
}
}
2022-11-09 18:35:44 +01:00
if(!toDisplayId.isEmpty())
2022-10-27 17:17:44 +02:00
log.debug("Found last concessioni candidate for displaying. ID {}, PHASE {} ",toDisplayId,p.getLifecycleInformation().getPhase());
else toHideIds.add(p.getId()); // Still not found
}
}
toReturn.put("_toHideIds",toHideIds);
toReturn.put("_toDisplayIds",toDisplayId);
2022-11-09 18:35:44 +01:00
log.info("Indexing request for Concessione [ID {}] with to HIDE {} and toDisplay {} ",indexingProject.getId(),toHideIds,toDisplayId);
2022-10-27 17:17:44 +02:00
return toReturn;
} catch (Exception e) {
log.error("Unable to evaluate to Hide and Display Ids ",e);
2022-10-27 17:31:30 +02:00
throw new IndexingException("Unable to evaluate chain ids to hide / display",e);
2022-10-27 17:17:44 +02:00
}
}
private static void scanRelation(ArrayList chain,RelationshipNavigationObject obj, boolean putBefore){
if(putBefore)chain.add(0,obj.getTarget());
else chain.add(obj.getTarget());
2022-11-22 14:57:44 +01:00
if(obj.getChildren()!=null)
obj.getChildren().forEach(r-> scanRelation(chain,r,putBefore));
2022-10-27 17:17:44 +02:00
}
2022-01-12 18:42:22 +01:00
}