2022-03-10 18:15:10 +01:00
|
|
|
package org.gcube.application.cms.usecases;
|
|
|
|
|
2022-05-10 18:06:39 +02:00
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import org.bson.Document;
|
|
|
|
import org.gcube.application.cms.custom.gna.concessioni.model.ProfiledConcessione;
|
|
|
|
import org.gcube.application.cms.tests.TokenSetter;
|
|
|
|
import org.gcube.application.geoportal.client.utils.Serialization;
|
|
|
|
import org.gcube.application.geoportal.common.model.document.Project;
|
|
|
|
import org.gcube.application.geoportal.common.model.legacy.*;
|
|
|
|
import org.gcube.application.geoportal.common.model.rest.TempFile;
|
|
|
|
import org.gcube.application.geoportal.common.model.useCaseDescriptor.Field;
|
|
|
|
import org.gcube.application.geoportal.common.rest.Projects;
|
|
|
|
import org.gcube.application.geoportal.common.utils.FileSets;
|
|
|
|
import org.gcube.application.geoportal.common.utils.Files;
|
|
|
|
import org.gcube.application.geoportal.common.utils.StorageUtils;
|
2022-05-13 18:42:11 +02:00
|
|
|
import org.gcube.common.storagehub.client.dsl.StorageHubClient;
|
2022-05-10 18:06:39 +02:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.nio.charset.Charset;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2022-05-13 18:42:11 +02:00
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
2022-05-10 18:06:39 +02:00
|
|
|
import java.util.concurrent.atomic.AtomicLong;
|
2022-05-17 11:02:49 +02:00
|
|
|
import java.util.stream.Collectors;
|
2022-05-10 18:06:39 +02:00
|
|
|
|
|
|
|
import static org.gcube.application.geoportal.client.plugins.GeoportalAbstractPlugin.projects;
|
|
|
|
|
|
|
|
@Slf4j
|
2022-05-11 15:57:53 +02:00
|
|
|
public class ExportConcessioniAsProjects {
|
2022-05-10 18:06:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-05-13 18:42:11 +02:00
|
|
|
public static void main(String[] args) throws InterruptedException {
|
2022-05-10 18:06:39 +02:00
|
|
|
// read from imported folder
|
2022-05-11 15:57:53 +02:00
|
|
|
File dir= new File("/Users/fabioisti/git/gcube-cms-suite/import1652276569045");
|
2022-05-10 18:06:39 +02:00
|
|
|
String targetContext="/gcube/devsec/devVRE";
|
|
|
|
|
|
|
|
|
|
|
|
Projects<Project> client=projects("profiledConcessioni").build();;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ArrayList<Concessione> found=new ArrayList<>();
|
|
|
|
for(File elementFolder:dir.listFiles())
|
|
|
|
for(File jsonFile:elementFolder.listFiles((dir1, name) -> {return name.endsWith(".json");}))
|
|
|
|
try {
|
|
|
|
log.info("Reading "+jsonFile.getAbsolutePath());
|
|
|
|
String json= Files.readFileAsString(jsonFile.getAbsolutePath(), Charset.defaultCharset());
|
|
|
|
found.add(Serialization.read(json,Concessione.class));
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
System.out.println("Loaded "+found.size()+" elements from "+dir.getAbsolutePath());
|
|
|
|
|
|
|
|
TokenSetter.set(targetContext);
|
|
|
|
|
2022-05-17 11:02:49 +02:00
|
|
|
// StorageUtils storage = new StorageUtils();
|
2022-05-10 18:06:39 +02:00
|
|
|
|
|
|
|
AtomicLong count = new AtomicLong(0);
|
|
|
|
AtomicLong warnCount = new AtomicLong(0);
|
|
|
|
AtomicLong errCount = new AtomicLong(0);
|
|
|
|
|
2022-05-13 18:42:11 +02:00
|
|
|
ExecutorService service = Executors.newFixedThreadPool(3);
|
|
|
|
|
|
|
|
long startProcess = System.currentTimeMillis();
|
|
|
|
|
|
|
|
found.forEach(c -> {
|
|
|
|
service.submit(new Runnable(){
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2022-05-17 11:02:49 +02:00
|
|
|
publish(c,client,errCount,count);
|
2022-05-13 18:42:11 +02:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
});
|
|
|
|
|
|
|
|
while (!service.awaitTermination(1, TimeUnit.MINUTES)) {
|
|
|
|
log.info("Waiting .. completed {}, out of {} ",count.get(),found.size());
|
|
|
|
if(found.size()==count.get()) service.shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
System.out.println("Completed "+count.get()+" [elapsedTime = "+(System.currentTimeMillis()-startProcess)/1000+" sec] [warn : "+warnCount.get()+", err : "+errCount.get()+"]");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-17 11:02:49 +02:00
|
|
|
// public static List<TempFile> upload(List<PersistedContent> contents,StorageUtils storage) throws IOException {
|
|
|
|
// ArrayList toReturn = new ArrayList();
|
|
|
|
// for (PersistedContent content : contents)
|
|
|
|
// if (content instanceof WorkspaceContent) {
|
|
|
|
// WorkspaceContent wc = (WorkspaceContent) content;
|
|
|
|
// toReturn.add(storage.putOntoStorage(new URL(wc.getLink()).openStream(), wc.getName()));
|
|
|
|
// }
|
|
|
|
// return toReturn;
|
|
|
|
// }
|
2022-05-10 18:06:39 +02:00
|
|
|
|
|
|
|
|
2022-05-17 11:02:49 +02:00
|
|
|
private static void publish(Concessione c, Projects<Project> client, AtomicLong errCount,AtomicLong count){
|
2022-05-13 18:42:11 +02:00
|
|
|
try {
|
|
|
|
long startTime = System.currentTimeMillis();
|
|
|
|
FileSets.RequestBuilder req=null;
|
|
|
|
|
|
|
|
log.info("Using {} {}",c.getNome(),c.getMongo_id());
|
|
|
|
|
|
|
|
//Copy core fields
|
|
|
|
|
|
|
|
Document doc=new Document();
|
|
|
|
doc.put(ProfiledConcessione.NOME,c.getNome());
|
|
|
|
doc.put(ProfiledConcessione.INTRODUZIONE,c.getIntroduzione());
|
|
|
|
doc.put(ProfiledConcessione.DESCRIZIONE_CONTENUTO,c.getDescrizioneContenuto());
|
|
|
|
doc.put(ProfiledConcessione.AUTHORS,c.getAuthors());
|
|
|
|
doc.put(ProfiledConcessione.CONTRIBUTORE,c.getContributore());
|
|
|
|
doc.put(ProfiledConcessione.TITOLARI,c.getTitolari());
|
|
|
|
doc.put(ProfiledConcessione.RESPONSABILE,c.getResponsabile());
|
|
|
|
doc.put(ProfiledConcessione.EDITORS,c.getEditore());
|
|
|
|
doc.put(ProfiledConcessione.FONTI_FINANZIAMENTO,c.getFontiFinanziamento());
|
|
|
|
doc.put(ProfiledConcessione.SOGGETTO,c.getSoggetto());
|
|
|
|
doc.put(ProfiledConcessione.RISORSE_CORRELATE,c.getRisorseCorrelate());
|
|
|
|
doc.put(ProfiledConcessione.DATA_INZIO_PROGETTO,c.getDataInizioProgetto());
|
|
|
|
doc.put(ProfiledConcessione.DATA_FINE_PROGETTO,c.getDataFineProgetto());
|
|
|
|
doc.put(ProfiledConcessione.TITOLARE_LICENZA,c.getTitolareLicenza());
|
|
|
|
doc.put(ProfiledConcessione.TITOLARE_COPYRIGHT,c.getTitolareCopyright());
|
|
|
|
doc.put(ProfiledConcessione.PAROLE_CHIAVE_LIBERE,c.getParoleChiaveLibere());
|
|
|
|
doc.put(ProfiledConcessione.PAREOLE_CHIAVE_ICCD,c.getParoleChiaveICCD());
|
|
|
|
|
|
|
|
// CREATE Project
|
|
|
|
Project project = client.createNew(doc);
|
|
|
|
|
|
|
|
// Relazione
|
|
|
|
if(c.getRelazioneScavo()!=null) {
|
|
|
|
RelazioneScavo rel=c.getRelazioneScavo();
|
|
|
|
Document relDoc= new Document();
|
|
|
|
relDoc.put(ProfiledConcessione.Sections.TITOLO,rel.getTitolo());
|
|
|
|
relDoc.put(ProfiledConcessione.Relazione.RESPONSABILI,rel.getResponsabili());
|
|
|
|
relDoc.put(ProfiledConcessione.SOGGETTO,rel.getSoggetto());
|
|
|
|
|
|
|
|
Document updated =project.getTheDocument();
|
|
|
|
updated.put(ProfiledConcessione.RELAZIONE_SCAVO,relDoc);
|
|
|
|
project = client.updateDocument(project.getId(),updated);
|
|
|
|
|
|
|
|
List<PersistedContent> content=rel.getActualContent();
|
|
|
|
if(content!=null && !content.isEmpty()) {
|
|
|
|
req = FileSets.build("$." + ProfiledConcessione.RELAZIONE_SCAVO, "fileset",
|
|
|
|
"$." + ProfiledConcessione.RELAZIONE_SCAVO + "." + Field.CHILDREN
|
|
|
|
+ "[?(@.fileset)]");
|
2022-05-17 11:02:49 +02:00
|
|
|
req.addAll(content.stream().
|
|
|
|
filter(p -> p instanceof WorkspaceContent)
|
|
|
|
.map(p -> new TempFile(null, ((WorkspaceContent) p).getLink(), ((WorkspaceContent) p).getName()))
|
|
|
|
.collect(Collectors.toList()));
|
2022-05-13 18:42:11 +02:00
|
|
|
project = client.registerFileSet(project.getId(),req.getTheRequest());
|
|
|
|
}
|
|
|
|
// upload fileset
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(c.getAbstractRelazione()!=null) {
|
|
|
|
AbstractRelazione abs=c.getAbstractRelazione();
|
|
|
|
Document absDoc= new Document();
|
|
|
|
absDoc.put(ProfiledConcessione.Sections.TITOLO,abs.getTitolo());
|
|
|
|
absDoc.put(ProfiledConcessione.AbstractRelazione.ABSTRACT_ENG,abs.getAbstractEng());
|
|
|
|
absDoc.put(ProfiledConcessione.AbstractRelazione.ABSTRACT_ITA,abs.getAbstractIta());
|
2022-05-10 18:06:39 +02:00
|
|
|
|
|
|
|
|
2022-05-13 18:42:11 +02:00
|
|
|
Document updated =project.getTheDocument();
|
|
|
|
updated.put(ProfiledConcessione.ABSTRACT_RELAZIONE,absDoc);
|
|
|
|
project = client.updateDocument(project.getId(),updated);
|
2022-05-10 18:06:39 +02:00
|
|
|
|
2022-05-13 18:42:11 +02:00
|
|
|
//TODO ask Fra about mapping
|
|
|
|
// list persited content -> absIta, absEng
|
2022-05-10 18:06:39 +02:00
|
|
|
|
|
|
|
// List<PersistedContent> content=abs.getActualContent();
|
|
|
|
// if(content!=null && !content.isEmpty()) {
|
|
|
|
// req = FileSets.build("$." + ProfiledConcessione.RELAZIONE_SCAVO, "fileset",
|
|
|
|
// "$." + ProfiledConcessione.RELAZIONE_SCAVO + "." + Field.CHILDREN
|
|
|
|
// + "[?(@.fileset)]");
|
|
|
|
// content.forEach(uploader);
|
|
|
|
// project = client.registerFileSet(project.getId(),req.getTheRequest());
|
|
|
|
// }
|
|
|
|
|
2022-05-13 18:42:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//Immagini rappresentative
|
|
|
|
if(c.getImmaginiRappresentative()!=null){
|
|
|
|
List<Document> imgs=new ArrayList<>();
|
|
|
|
for (int i = 0; i < c.getImmaginiRappresentative().size(); i++) {
|
|
|
|
UploadedImage img=c.getImmaginiRappresentative().get(i);
|
|
|
|
Document imgDoc=new Document();
|
|
|
|
imgDoc.put(ProfiledConcessione.Sections.TITOLO,img.getTitolo());
|
|
|
|
imgDoc.put(ProfiledConcessione.Immagini.DIDASCALIA,img.getDidascalia());
|
|
|
|
imgDoc.put(ProfiledConcessione.Relazione.RESPONSABILI,img.getResponsabili());
|
|
|
|
imgDoc.put(ProfiledConcessione.SOGGETTO,img.getSoggetto());
|
|
|
|
imgs.add(imgDoc);
|
2022-05-10 18:06:39 +02:00
|
|
|
}
|
|
|
|
|
2022-05-13 18:42:11 +02:00
|
|
|
Document updated =project.getTheDocument();
|
|
|
|
updated.put(ProfiledConcessione.IMMAGINI_RAPPRESENTATIVE,imgs);
|
|
|
|
project = client.updateDocument(project.getId(),updated);
|
2022-05-10 18:06:39 +02:00
|
|
|
|
2022-05-13 18:42:11 +02:00
|
|
|
// FileSets
|
|
|
|
for (int i = 0; i < c.getImmaginiRappresentative().size(); i++) {
|
|
|
|
UploadedImage img=c.getImmaginiRappresentative().get(i);
|
|
|
|
List<PersistedContent> content=img.getActualContent();
|
|
|
|
if(content!=null && !content.isEmpty()) {
|
|
|
|
req = FileSets.build("$." + ProfiledConcessione.IMMAGINI_RAPPRESENTATIVE+"["+i+"]", "fileset",
|
|
|
|
"$." + ProfiledConcessione.IMMAGINI_RAPPRESENTATIVE + "." + Field.CHILDREN
|
|
|
|
+ "[?(@.fileset)]");
|
2022-05-17 11:02:49 +02:00
|
|
|
req.addAll(content.stream().
|
|
|
|
filter(p -> p instanceof WorkspaceContent)
|
|
|
|
.map(p -> new TempFile(null, ((WorkspaceContent) p).getLink(), ((WorkspaceContent) p).getName()))
|
|
|
|
.collect(Collectors.toList()));
|
2022-05-13 18:42:11 +02:00
|
|
|
project = client.registerFileSet(project.getId(),req.getTheRequest());
|
2022-05-10 18:06:39 +02:00
|
|
|
}
|
|
|
|
}
|
2022-05-13 18:42:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// POS
|
|
|
|
if(c.getPosizionamentoScavo()!=null) {
|
|
|
|
LayerConcessione l=c.getPosizionamentoScavo();
|
|
|
|
Document lDoc= new Document();
|
|
|
|
lDoc.put(ProfiledConcessione.Sections.TITOLO,l.getTitolo());
|
|
|
|
lDoc.put(ProfiledConcessione.RESPONSABILE,l.getResponsabile());
|
|
|
|
lDoc.put(ProfiledConcessione.AUTHORS,l.getAuthors());
|
|
|
|
lDoc.put(ProfiledConcessione.PAREOLE_CHIAVE_ICCD,l.getParoleChiaveICCD());
|
|
|
|
lDoc.put(ProfiledConcessione.PAROLE_CHIAVE_LIBERE,l.getParoleChiaveLibere());
|
|
|
|
lDoc.put(ProfiledConcessione.Layers.TOPIC,l.getTopicCategory());
|
|
|
|
lDoc.put(ProfiledConcessione.Layers.SUB_TOPIC,l.getSubTopic());
|
|
|
|
lDoc.put(ProfiledConcessione.Layers.METODO_RACCOLTA,l.getMetodoRaccoltaDati());
|
|
|
|
lDoc.put(ProfiledConcessione.Layers.SCALA_ACQUISIZIONE,l.getScalaAcquisizione());
|
|
|
|
lDoc.put(ProfiledConcessione.Layers.VALUTAZIONE_QUALITA,l.getValutazioneQualita());
|
|
|
|
lDoc.put(ProfiledConcessione.Sections.ABSTRACT,l.getAbstractSection());
|
|
|
|
|
|
|
|
Document updated =project.getTheDocument();
|
|
|
|
updated.put(ProfiledConcessione.POSIZIONAMENTO_SCAVO,lDoc);
|
|
|
|
project = client.updateDocument(project.getId(),updated);
|
|
|
|
|
|
|
|
List<PersistedContent> content=l.getActualContent();
|
|
|
|
if(content!=null && !content.isEmpty()) {
|
|
|
|
req = FileSets.build("$." + ProfiledConcessione.POSIZIONAMENTO_SCAVO, "fileset",
|
|
|
|
"$." + ProfiledConcessione.POSIZIONAMENTO_SCAVO + "." + Field.CHILDREN
|
|
|
|
+ "[?(@.fileset)]");
|
2022-05-17 11:02:49 +02:00
|
|
|
req.addAll(content.stream().
|
|
|
|
filter(p -> p instanceof WorkspaceContent)
|
|
|
|
.map(p -> new TempFile(null, ((WorkspaceContent) p).getLink(), ((WorkspaceContent) p).getName()))
|
|
|
|
.collect(Collectors.toList()));
|
2022-05-13 18:42:11 +02:00
|
|
|
project = client.registerFileSet(project.getId(),req.getTheRequest());
|
|
|
|
}
|
|
|
|
// upload fileset
|
|
|
|
}
|
2022-05-10 18:06:39 +02:00
|
|
|
|
2022-05-13 18:42:11 +02:00
|
|
|
// Piante
|
|
|
|
if(c.getPianteFineScavo()!=null){
|
|
|
|
List<Document> piante=new ArrayList<>();
|
|
|
|
for (int i = 0; i < c.getPianteFineScavo().size(); i++) {
|
|
|
|
LayerConcessione l=c.getPianteFineScavo().get(i);
|
2022-05-10 18:06:39 +02:00
|
|
|
Document lDoc= new Document();
|
|
|
|
lDoc.put(ProfiledConcessione.Sections.TITOLO,l.getTitolo());
|
|
|
|
lDoc.put(ProfiledConcessione.RESPONSABILE,l.getResponsabile());
|
|
|
|
lDoc.put(ProfiledConcessione.AUTHORS,l.getAuthors());
|
|
|
|
lDoc.put(ProfiledConcessione.PAREOLE_CHIAVE_ICCD,l.getParoleChiaveICCD());
|
|
|
|
lDoc.put(ProfiledConcessione.PAROLE_CHIAVE_LIBERE,l.getParoleChiaveLibere());
|
|
|
|
lDoc.put(ProfiledConcessione.Layers.TOPIC,l.getTopicCategory());
|
|
|
|
lDoc.put(ProfiledConcessione.Layers.SUB_TOPIC,l.getSubTopic());
|
|
|
|
lDoc.put(ProfiledConcessione.Layers.METODO_RACCOLTA,l.getMetodoRaccoltaDati());
|
|
|
|
lDoc.put(ProfiledConcessione.Layers.SCALA_ACQUISIZIONE,l.getScalaAcquisizione());
|
|
|
|
lDoc.put(ProfiledConcessione.Layers.VALUTAZIONE_QUALITA,l.getValutazioneQualita());
|
|
|
|
lDoc.put(ProfiledConcessione.Sections.ABSTRACT,l.getAbstractSection());
|
2022-05-13 18:42:11 +02:00
|
|
|
piante.add(lDoc);
|
|
|
|
}
|
2022-05-10 18:06:39 +02:00
|
|
|
|
2022-05-13 18:42:11 +02:00
|
|
|
Document updated =project.getTheDocument();
|
|
|
|
updated.put(ProfiledConcessione.PIANTE_FINE_SCAVO,piante);
|
|
|
|
project = client.updateDocument(project.getId(),updated);
|
2022-05-10 18:06:39 +02:00
|
|
|
|
2022-05-13 18:42:11 +02:00
|
|
|
// FileSets
|
|
|
|
for (int i = 0; i < c.getPianteFineScavo().size(); i++) {
|
|
|
|
LayerConcessione img=c.getPianteFineScavo().get(i);
|
|
|
|
List<PersistedContent> content=img.getActualContent();
|
2022-05-10 18:06:39 +02:00
|
|
|
if(content!=null && !content.isEmpty()) {
|
2022-05-13 18:42:11 +02:00
|
|
|
req = FileSets.build("$." + ProfiledConcessione.PIANTE_FINE_SCAVO+"["+i+"]", "fileset",
|
|
|
|
"$." + ProfiledConcessione.PIANTE_FINE_SCAVO + "." + Field.CHILDREN
|
2022-05-10 18:06:39 +02:00
|
|
|
+ "[?(@.fileset)]");
|
2022-05-17 11:02:49 +02:00
|
|
|
req.addAll(content.stream().
|
|
|
|
filter(p -> p instanceof WorkspaceContent)
|
|
|
|
.map(p -> new TempFile(null, ((WorkspaceContent) p).getLink(), ((WorkspaceContent) p).getName()))
|
|
|
|
.collect(Collectors.toList()));
|
2022-05-10 18:06:39 +02:00
|
|
|
project = client.registerFileSet(project.getId(),req.getTheRequest());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-13 18:42:11 +02:00
|
|
|
System.out.println("Done "+c.getId()+" in "+(System.currentTimeMillis()-startTime)/1000+" sec");
|
|
|
|
} catch (Throwable throwable) {
|
|
|
|
System.err.println(throwable);
|
|
|
|
errCount.incrementAndGet();
|
|
|
|
} finally {
|
|
|
|
count.incrementAndGet();
|
|
|
|
}
|
2022-05-10 18:06:39 +02:00
|
|
|
}
|
|
|
|
|
2022-03-10 18:15:10 +01:00
|
|
|
}
|