Concessioni defaults #23287

This commit is contained in:
Fabio Sinibaldi 2022-05-10 14:42:11 +02:00
parent ab91340cec
commit d1ef58e5b6
6 changed files with 213 additions and 48 deletions

View File

@ -43,10 +43,6 @@ public abstract class BasicPluginTest extends BasicTests {
System.out.println("Plugin Loading OK");
}
@Before
public void initContext(){
assumeTrue(GCubeTest.isTestInfrastructureEnabled());
TokenSetter.set(GCubeTest.getContext());
}
}

View File

@ -2,21 +2,23 @@ package org.gcube.application.cms.concessioni.plugins;
import com.vdurmont.semver4j.Semver;
import lombok.extern.slf4j.Slf4j;
import org.bson.BsonDocument;
import org.bson.BsonString;
import org.bson.BsonValue;
import org.bson.Document;
import org.gcube.application.cms.plugins.*;
import org.gcube.application.cms.plugins.implementations.Default3PhaseManager;
import org.gcube.application.cms.plugins.model.ComparableVersion;
import org.gcube.application.cms.plugins.reports.EventExecutionReport;
import org.gcube.application.cms.plugins.reports.Report;
import org.gcube.application.cms.serialization.Serialization;
import org.gcube.application.cms.custom.gna.concessioni.model.ProfiledConcessione;
import org.gcube.application.geoportal.common.model.JSONPathWrapper;
import org.gcube.application.geoportal.common.model.document.*;
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.legacy.report.ConstraintCheck;
import java.io.IOException;
import java.util.List;
@Slf4j
/** Overrides 3 Phases lifecycle with override of default values
*
@ -30,28 +32,44 @@ public class ConcessioniLifeCycleManager extends Default3PhaseManager implements
DESCRIPTOR.setVersion(new Semver("1.0.0"));
}
@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;
}
// STATIC ROUTINES
private static final Project setDefaults(Project document){
static final Project setDefaults(Project document) throws IOException {
log.info("Concessione {}, setting defaults..",document.getId());
log.debug("Full concessione is {}",document);
ProfiledConcessione c=Serialization.convert(document,ProfiledConcessione.class);
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;"
doc.putIfAbsent(ProfiledConcessione.DESCRIZIONE_CONTENUTO,"Relazione di fine scavo e relativo abstract; selezione di immagini rappresentative;"
+ " posizionamento topografico dell'area indagata, pianta di fine scavo.");
// Super Section
// TODO read from UCD
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);
//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);
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());
@ -59,37 +77,98 @@ public class ConcessioniLifeCycleManager extends Default3PhaseManager implements
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());
rel.put(RegisteredFileSet.ACCESS,relAccess);
doc.put(ProfiledConcessione.RELAZIONE_SCAVO,rel);
//ABSTRACT Relazione
doc.putIfAbsent(ProfiledConcessione.ABSTRACT_RELAZIONE,new Document());
Document abs=Serialization.convert(doc.get(ProfiledConcessione.ABSTRACT_RELAZIONE),Document.class);
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);
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());
abs.put(RegisteredFileSet.ACCESS,absAccess);
doc.put(ProfiledConcessione.ABSTRACT_RELAZIONE,abs);
//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));
// 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);
}
//TODO complete setDefaults
//IMMAGINI RAPPRESENTATIVE
JSONPathWrapper wrapper=new JSONPathWrapper(Serialization.write(doc));
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());
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);
}
}
// 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);
}
}
c.setTheDocument(Serialization.read(wrapper.getValueCTX().jsonString(),Document.class));
return c;
}

View File

@ -0,0 +1,97 @@
package org.gcube.application.cms.concessioni.plugins;
import org.bson.Document;
import org.gcube.application.cms.custom.gna.concessioni.model.ProfiledConcessione;
import org.gcube.application.cms.serialization.Serialization;
import org.gcube.application.cms.tests.TestDocuments;
import org.gcube.application.cms.tests.plugins.BasicPluginTest;
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.filesets.RegisteredFileSet;
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;
import java.util.List;
import static junit.framework.TestCase.*;
public class ConcessioniPluginTests extends BasicPluginTest {
@Test
public void testLoad(){
Assert.assertTrue(true);
}
@Test
public void testDefaults() throws IOException {
Project c= TestDocuments.documentMap.get("dummy.json");
c.getTheDocument().put(ProfiledConcessione.POSIZIONAMENTO_SCAVO,new Document());
c.getTheDocument().put(ProfiledConcessione.IMMAGINI_RAPPRESENTATIVE,new Document[]{new Document()});
c.getTheDocument().put(ProfiledConcessione.PIANTE_FINE_SCAVO,new Document[]{new Document()});
c= (ProfiledConcessione) ConcessioniLifeCycleManager.setDefaults(c);
System.out.println("Obtained "+Serialization.write(c));
assertNotNull(c.getTheDocument().get(ProfiledConcessione.SOGGETTO));
assertNotNull(c.getTheDocument().getString(ProfiledConcessione.DESCRIZIONE_CONTENUTO));
assertNotNull(c.getTheDocument().get(ProfiledConcessione.RELAZIONE_SCAVO));
Document rel = Serialization.convert(c.getTheDocument().get(ProfiledConcessione.RELAZIONE_SCAVO), Document.class);
assertNotNull(rel.get(ProfiledConcessione.Sections.TITOLO));
assertNotNull(rel.get(ProfiledConcessione.SOGGETTO));
assertNotNull(rel.get(RegisteredFileSet.CREATION_INFO));
assertNotNull(rel.get(RegisteredFileSet.ACCESS));
Document abs = Serialization.convert(c.getTheDocument().get(ProfiledConcessione.ABSTRACT_RELAZIONE), Document.class);
assertNotNull(abs.get(ProfiledConcessione.Sections.TITOLO));
assertNotNull(abs.get(RegisteredFileSet.CREATION_INFO));
assertNotNull(abs.get(RegisteredFileSet.ACCESS));
Document pos = Serialization.convert(c.getTheDocument().get(ProfiledConcessione.POSIZIONAMENTO_SCAVO), Document.class);
assertNotNull(pos.get(ProfiledConcessione.Sections.TITOLO));
assertNotNull(pos.get(ProfiledConcessione.Sections.ABSTRACT));
assertNotNull(pos.get(ProfiledConcessione.Layers.TOPIC));
assertNotNull(pos.get(ProfiledConcessione.Layers.SUB_TOPIC));
assertNotNull(pos.get(RegisteredFileSet.CREATION_INFO));
assertNotNull(pos.get(RegisteredFileSet.ACCESS));
JSONPathWrapper wrapper = new JSONPathWrapper(c.getTheDocument().toJson());
assertTrue(c.getTheDocument().containsKey(ProfiledConcessione.IMMAGINI_RAPPRESENTATIVE));
c.getTheDocument().get(ProfiledConcessione.IMMAGINI_RAPPRESENTATIVE, List.class).forEach(o -> {
try{
Document img = Serialization.asDocument(o);
assertNotNull(img.get(ProfiledConcessione.SOGGETTO));
assertNotNull(img.get(RegisteredFileSet.CREATION_INFO));
assertNotNull(img.get(RegisteredFileSet.ACCESS));
}catch (Throwable t){
System.out.println("Unable to read IMG "+o);
t.printStackTrace();
fail(t.getMessage());
}
});
assertTrue(c.getTheDocument().containsKey(ProfiledConcessione.PIANTE_FINE_SCAVO));
c.getTheDocument().get(ProfiledConcessione.PIANTE_FINE_SCAVO, List.class).forEach(o -> {
try{
Document pianta = Serialization.asDocument(o);
assertNotNull(pianta.get(ProfiledConcessione.Sections.TITOLO));
assertNotNull(pianta.get(ProfiledConcessione.Sections.ABSTRACT));
assertNotNull(pianta.get(ProfiledConcessione.Layers.TOPIC));
assertNotNull(pianta.get(ProfiledConcessione.Layers.SUB_TOPIC));
assertNotNull(pianta.get(RegisteredFileSet.CREATION_INFO));
assertNotNull(pianta.get(RegisteredFileSet.ACCESS));
}catch (Throwable t){
System.out.println("Unable to read pianta "+o);
t.printStackTrace();
fail(t.getMessage());
}
});
}
}

View File

@ -1,13 +0,0 @@
package org.gcube.application.cms.concessioni.plugins;
import org.gcube.application.cms.tests.plugins.BasicPluginTest;
import org.junit.Assert;
import org.junit.Test;
public class Tests extends BasicPluginTest {
@Test
public void testLoad(){
Assert.assertTrue(true);
}
}

View File

@ -10,7 +10,7 @@ public class ProfiledConcessione extends Project {
//Introduzione (descrizione del progetto)
public final static String INTRODUZIONE = "introduzione";
//Descrizione del contenuto
public final static String DSCRIZIONE_CONTENUTO ="descrizioneContenuto";
public final static String DESCRIZIONE_CONTENUTO ="descrizioneContenuto";
//Autori
public final static String AUTHORS= "authors";
//Soggetto che materialmente invia i dati.
@ -47,11 +47,17 @@ public class ProfiledConcessione extends Project {
public static class Sections{
public static final String TITOLO="titolo";
public static final String ABSTRACT="abstractSection";
}
public static class Layers{
public static final String TOPIC="topicCategory";
public static final String SUB_TOPIC="subTopic";
}
public static class Relazione{
public static final String RESPONSABILI= "responsabili";
}

View File

@ -14,7 +14,7 @@ public class GCubeTest {
// testContext = "/pred4s/preprod/preVRE";
testContext = "/gcube/devsec/devVRE";
// testContext = "/gcube/devsec/devVRE";
System.out.println("TEST CONTEXT = "+testContext);