package org.gcube.application.cms.usecases.mocks; import com.opencsv.CSVReader; import lombok.extern.slf4j.Slf4j; import org.gcube.application.cms.tests.model.TestModel; import org.gcube.application.cms.tests.TokenSetter; import org.gcube.application.geoportal.client.utils.Serialization; import org.gcube.application.geoportal.common.utils.FileSets; import org.gcube.application.geoportal.common.model.legacy.Concessione; import org.gcube.application.geoportal.common.model.legacy.report.ValidationReport; import org.gcube.application.geoportal.common.rest.MongoConcessioni; import org.gcube.application.geoportal.common.utils.Files; import org.gcube.application.geoportal.common.utils.StorageUtils; import java.io.*; import java.util.*; import static org.gcube.application.geoportal.client.GeoportalAbstractPlugin.mongoConcessioni; import static org.junit.Assert.assertTrue; @Slf4j public class MockFromFolder { public static void main(String[] args) throws Exception { //PARAMS String context= "/gcube/devsec/devVRE"; File descriptorsBaseFolder=new File(TestModel.getBaseFolder(),"packages"); //Concessioni 04-03 String packageBaseDir="/Users/fabioisti/Documents/Concessioni 04-03/"; String csvDescriptor="concessioni04-03_filtered.csv"; //DATASET_GNA_01 // String packageBaseDir="/Users/fabioisti/Documents/DATASET_GNA_01"; // String csvDescriptor="DATASET_GNA_01.csv"; //DATASET_GNA_02 //String packageBaseDir="/Users/fabioisti/Documents/DATASET_GNA_02"; //String csvDescriptor="DATASET_GNA_02.csv"; // invio_08_02 // String packageBaseDir="/Users/fabioisti/Documents/invio_08_05"; // String csvDescriptor="invio_08_05.csv"; // concessioni 23_04 // String packageBaseDir="/Users/fabioisti/Documents/Concessioni_23_04"; // String csvDescriptor="concessioni_23_04.csv"; TokenSetter.set(context); MongoConcessioni client=mongoConcessioni().build(); StorageUtils storage=new StorageUtils(); long publishedCount=0l; long successcount=0l; long entrycount=0l; //Parse CSV descriptor File baseDir=new File(packageBaseDir); ArrayList pushed=new ArrayList<>(); CSVReader reader = new CSVReader(new FileReader(new File(descriptorsBaseFolder,csvDescriptor))); String [] nextLine; //reads one line at a time while ((nextLine = reader.readNext()) != null) { entrycount++; //Create new String projectName = nextLine[0]; String positionPath = nextLine[1]; try { //NB raggruppa per file Map.Entry> posSets = Files.getAllShapeSet(new File(baseDir,positionPath),true). entrySet().stream().findFirst().get(); Map> pianteSets = Files.getAllShapeSet(new File(baseDir,projectName),true); if(pianteSets.size()>1) pianteSets.remove(posSets.getKey()); log.debug("Entry {} pos Size {} piante {} ",projectName,posSets.getValue().size(),pianteSets.size()); Concessione c = createMock(projectName,baseDir.getName(), pianteSets, posSets.getValue(), client, storage); publishedCount++; if (c.getReport().getStatus().equals(ValidationReport.ValidationStatus.PASSED)) successcount++; pushed.add(c); }catch(Throwable t){ System.err.println("Problematic entry "+projectName); t.printStackTrace(System.err); } } System.out.println("Done "+publishedCount+" [SUCCESS : "+successcount+"] \t OUT OF :"+entrycount+" entries"); pushed.forEach(c -> { try{ System.out.println(c.getNome()+"\t"+c.getMongo_id()+"\t"+c.getReport().getStatus()); }catch(Throwable t){ System.out.println(c.getNome()+"\t"+c.getMongo_id()+"\t PROBLEMATIC, NO REPORT"); } }); } private static Concessione createMock(String baseName,String packageName,Map> piante, List pos, MongoConcessioni client, StorageUtils storage) throws Exception { Concessione c= TestModel.prepareConcessione(piante.size(), 2); c.setNome("Mock for "+baseName+" ("+packageName+")"); c= client.createNew(c); String mongoId=c.getMongo_id(); // TEST DATA, DO NOT CARE client.registerFileSet(mongoId, FileSets.prepareRequest(storage, Concessione.Paths.RELAZIONE,new File (TestModel.getBaseFolder(),"relazione.pdf"))); client.registerFileSet(mongoId, FileSets.prepareRequest(storage, Concessione.Paths.imgByIndex(0),new File(TestModel.getBaseFolder(),"immagine.png"))); // POSIZIONAMENTO client.registerFileSet(mongoId, FileSets.prepareRequest(storage, Concessione.Paths.POSIZIONAMENTO,pos.toArray(new File[pos.size()]))); // PIANTE Map.Entry>[] entries= piante.entrySet().toArray(new Map.Entry[0]); for( int i= 0; i< piante.size();i++) { // Set layer name c=client.getById(mongoId); String path=Concessione.Paths.piantaByIndex(i); c.getContentByPath(path).setTitolo(" Pianta from "+entries[i].getKey()); client.update(mongoId, Serialization.write(c)); //Set fileset client.registerFileSet(mongoId, FileSets.prepareRequest(storage,path, entries[i].getValue().toArray(new File[0]))); } c=client.publish(mongoId); System.out.println("@@@ Concessione "+c.getNome()+"\t STATUS : "+ c.getReport().getStatus()); return c; } }