Manage display last of chain

This commit is contained in:
Fabio Sinibaldi 2022-10-27 17:17:44 +02:00
parent b811abcb54
commit 1d955fbcfa
1 changed files with 85 additions and 0 deletions

View File

@ -4,19 +4,33 @@ import com.vdurmont.semver4j.Semver;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.gcube.application.cms.custom.gna.concessioni.model.ProfiledConcessione;
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;
import org.gcube.application.cms.plugins.LifecycleManager;
import org.gcube.application.cms.plugins.implementations.Default3PhaseManager;
import org.gcube.application.cms.plugins.reports.EventExecutionReport;
import org.gcube.application.cms.plugins.reports.Report;
import org.gcube.application.cms.plugins.reports.StepExecutionReport;
import org.gcube.application.cms.plugins.requests.BaseRequest;
import org.gcube.application.cms.plugins.requests.IndexDocumentRequest;
import org.gcube.application.cms.serialization.Serialization;
import org.gcube.application.geoportal.common.model.JSONPathWrapper;
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.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.relationships.RelationshipNavigationObject;
import org.gcube.application.geoportal.common.model.legacy.report.ConstraintCheck;
import org.gcube.application.geoportal.common.model.rest.ConfigurationException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@ -26,6 +40,10 @@ import java.util.List;
public class ConcessioniLifeCycleManager extends Default3PhaseManager implements LifecycleManager {
private static final String FOLLOWS="follows";
private static final String PRECEDES="precedes";
public ConcessioniLifeCycleManager() {
DESCRIPTOR.setId("GNA-CONCESSIONI-LC");
DESCRIPTOR.setDescription("GNA Concessioni. This plugin supports custom lifecycle management for the GNA Concessioni UseCase.");
@ -173,4 +191,71 @@ public class ConcessioniLifeCycleManager extends Default3PhaseManager implements
}
@Override
protected Document evaluateAdditionalIndexParameters(IndexDocumentRequest request) throws InvalidUserRoleException, RegistrationException, ProjectNotFoundException, ConfigurationException, UnauthorizedAccess {
Document toReturn = super.evaluateAdditionalIndexParameters(request);
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);
log.debug("Produced full chain from {}, evaluating last available for PHASE {} ",indexingProject.getId(),
indexingProject.getLifecycleInformation().getPhase());
List<String> toDisplayId = new ArrayList<>();
List<String> toHideIds = new ArrayList<>();
log.trace("Checking from LAST.. ");
// recurse from last
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
// Check PHASE
switch(indexingProject.getLifecycleInformation().getPhase()){
case Phases.PENDING_APPROVAL:{
if (p.getLifecycleInformation().getPhase().equals(Phases.PENDING_APPROVAL)||
p.getLifecycleInformation().getPhase().equals(Phases.PUBLISHED))
toDisplayId.add(p.getId());
break;
}
case Phases.PUBLISHED:{
if (p.getLifecycleInformation().getPhase().equals(Phases.PUBLISHED))
toDisplayId.add(p.getId());
break;
}
}
if(toDisplayId != null)
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);
return toReturn;
} catch (Exception e) {
log.error("Unable to evaluate to Hide and Display Ids ",e);
throw e;
}
}
private static void scanRelation(ArrayList chain,RelationshipNavigationObject obj, boolean putBefore){
if(putBefore)chain.add(0,obj.getTarget());
else chain.add(obj.getTarget());
if(obj.getChildren()!=null && !obj.getChildren().isEmpty())scanRelation(chain,obj,putBefore);
}
}