This repository has been archived on 2021-09-09. You can view files and clone it, but cannot push or open issues or pull requests.
geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/mongo/ConcessioniMongoManager.java

278 lines
10 KiB
Java
Raw Normal View History

2020-12-17 18:27:45 +01:00
package org.gcube.application.geoportal.service.engine.mongo;
import java.io.IOException;
2020-12-18 18:24:20 +01:00
import java.time.LocalDateTime;
2020-12-17 18:27:45 +01:00
import java.util.ArrayList;
import java.util.List;
2020-12-22 18:29:10 +01:00
import java.util.function.Consumer;
2020-12-17 18:27:45 +01:00
import org.bson.Document;
import org.bson.types.ObjectId;
2020-12-18 18:24:20 +01:00
import org.gcube.application.geoportal.common.model.legacy.AssociatedContent;
2020-12-17 18:27:45 +01:00
import org.gcube.application.geoportal.common.model.legacy.Concessione;
2020-12-22 18:22:56 +01:00
import org.gcube.application.geoportal.common.model.legacy.GeoServerContent;
2020-12-18 18:24:20 +01:00
import org.gcube.application.geoportal.common.model.legacy.LayerConcessione;
import org.gcube.application.geoportal.common.model.legacy.OtherContent;
import org.gcube.application.geoportal.common.model.legacy.PersistedContent;
import org.gcube.application.geoportal.common.model.legacy.RelazioneScavo;
import org.gcube.application.geoportal.common.model.legacy.SDILayerDescriptor;
import org.gcube.application.geoportal.common.model.legacy.UploadedImage;
import org.gcube.application.geoportal.common.model.legacy.WorkspaceContent;
2020-12-22 18:22:56 +01:00
import org.gcube.application.geoportal.common.model.legacy.report.ValidationReport;
import org.gcube.application.geoportal.common.model.legacy.report.ValidationReport.ValidationStatus;
2020-12-18 18:24:20 +01:00
import org.gcube.application.geoportal.common.rest.TempFile;
2020-12-22 18:22:56 +01:00
import org.gcube.application.geoportal.common.utils.Files;
2020-12-17 18:27:45 +01:00
import org.gcube.application.geoportal.model.fault.ConfigurationException;
2020-12-22 18:22:56 +01:00
import org.gcube.application.geoportal.model.fault.PublishException;
2020-12-18 18:24:20 +01:00
import org.gcube.application.geoportal.service.engine.ImplementationProvider;
2020-12-22 18:22:56 +01:00
import org.gcube.application.geoportal.service.engine.SDIManager;
2020-12-18 18:24:20 +01:00
import org.gcube.application.geoportal.service.engine.StorageClientProvider;
import org.gcube.application.geoportal.service.engine.WorkspaceManager;
import org.gcube.application.geoportal.service.engine.WorkspaceManager.FileOptions;
import org.gcube.application.geoportal.service.engine.WorkspaceManager.FolderOptions;
2020-12-22 18:22:56 +01:00
import org.gcube.application.geoportal.service.model.internal.faults.InvalidStateException;
import org.gcube.application.geoportal.service.model.internal.faults.SDIInteractionException;
2020-12-17 18:27:45 +01:00
import org.gcube.application.geoportal.service.utils.Serialization;
2020-12-18 18:24:20 +01:00
import org.gcube.common.storagehub.client.dsl.FolderContainer;
import org.gcube.common.storagehub.model.exceptions.StorageHubException;
2020-12-17 18:27:45 +01:00
import com.fasterxml.jackson.core.JsonProcessingException;
import com.mongodb.client.MongoDatabase;
import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class ConcessioniMongoManager extends MongoManager{
2020-12-22 18:22:56 +01:00
2020-12-17 18:27:45 +01:00
public ConcessioniMongoManager() throws ConfigurationException {
super();
// TODO Auto-generated constructor stub
}
2020-12-18 12:40:46 +01:00
private static final String collectionName="legacyConcessioni";
2020-12-17 18:27:45 +01:00
private static final String DB_NAME="gna_dev";
2020-12-22 18:22:56 +01:00
2020-12-17 18:27:45 +01:00
private MongoDatabase db=null;
2020-12-22 18:22:56 +01:00
2020-12-17 18:27:45 +01:00
@Override
@Synchronized
protected MongoDatabase getDatabase() {
if(db==null) {
db=client.getDatabase(DB_NAME);
}
return db;
}
2020-12-22 18:22:56 +01:00
2020-12-17 18:27:45 +01:00
protected static Document asDocument (Concessione c) throws JsonProcessingException {
2020-12-18 12:40:46 +01:00
Document toReturn=Document.parse(Serialization.write(c));
if(c.getMongo_id()!=null&&!c.getMongo_id().isEmpty())
2020-12-22 18:22:56 +01:00
toReturn.append(ID, asId(c.getMongo_id()));
2020-12-18 12:40:46 +01:00
return toReturn;
2020-12-17 18:27:45 +01:00
}
2020-12-22 18:22:56 +01:00
2020-12-17 18:27:45 +01:00
protected static Concessione asConcessione (Document d) throws JsonProcessingException, IOException {
return Serialization.read(d.toJson(), Concessione.class);
}
2020-12-22 18:22:56 +01:00
// *** PUBLIC METHODS
2020-12-17 18:27:45 +01:00
public Concessione registerNew(Concessione toRegister) throws IOException {
2020-12-22 18:22:56 +01:00
log.trace("Registering {} ",toRegister);
toRegister.setDefaults();
2020-12-17 18:27:45 +01:00
ObjectId id=insert(asDocument(toRegister), collectionName);
2020-12-22 18:22:56 +01:00
2020-12-17 18:27:45 +01:00
Concessione toReturn=asConcessione(getById(id,collectionName));
2020-12-22 18:22:56 +01:00
toReturn.setMongo_id(asString(id));
2020-12-18 18:24:20 +01:00
return asConcessione(replace(asDocument(toReturn),collectionName));
2020-12-17 18:27:45 +01:00
}
2020-12-22 18:22:56 +01:00
public Concessione replace(Concessione toRegister) throws IOException {
log.trace("Replacing {} ",toRegister);
toRegister.setDefaults();
2020-12-18 18:24:20 +01:00
return asConcessione(replace(asDocument(toRegister),collectionName));
2020-12-17 18:27:45 +01:00
}
2020-12-22 18:22:56 +01:00
public Concessione update(String id,String json) throws IOException {
log.trace("Updating id {} with {} ",id,json);
Concessione toReturn=asConcessione(update(asId(id),asDoc(json),collectionName));
log.debug("Refreshing defaults..");
toReturn.setDefaults();
return asConcessione(replace(asDocument(toReturn),collectionName));
}
2020-12-22 18:29:10 +01:00
2020-12-17 18:27:45 +01:00
public List<Concessione> list(){
ArrayList<Concessione> toReturn=new ArrayList<>();
2020-12-22 18:29:10 +01:00
iterate(null, collectionName).forEach(
new Consumer<Document>() {
@Override
public void accept(Document d) {
try {
toReturn.add(asConcessione(d));
}catch(Throwable t) {
log.error("Unable to read Document as concessione ",t);
log.debug("Document was "+d.toJson());
}
}
});
2020-12-17 18:27:45 +01:00
return toReturn;
}
2020-12-22 18:22:56 +01:00
2020-12-17 18:27:45 +01:00
public Concessione getById(String id) throws JsonProcessingException, IOException {
2020-12-18 12:40:46 +01:00
log.debug("Loading by ID "+id);
2020-12-22 18:22:56 +01:00
return asConcessione(getById(asId(id),collectionName));
2020-12-17 18:27:45 +01:00
}
public void deleteById(String id) {
2020-12-22 18:22:56 +01:00
delete(asId(id), collectionName);
2020-12-17 18:27:45 +01:00
}
2020-12-22 18:22:56 +01:00
public Concessione publish(String id) throws JsonProcessingException, IOException, InvalidStateException{
Concessione toReturn=asConcessione(getById(asId(id),collectionName));
2020-12-17 18:27:45 +01:00
toReturn.setDefaults();
toReturn.validate();
2020-12-22 18:22:56 +01:00
// MATERIALIZE LAYERS
toReturn=publish(toReturn);
// replace(asDocument(toReturn),collectionName);
// CREATE INDEXES
toReturn=index(toReturn);
// replace(asDocument(toReturn),collectionName);
2020-12-18 18:24:20 +01:00
return asConcessione(replace(asDocument(toReturn),collectionName));
}
2020-12-22 18:22:56 +01:00
public Concessione persistContent(String id, String destinationPath, List<TempFile> files) throws Exception{
log.info("Persisting {} files for path {} in concessione ",files.size(),destinationPath,id);
try{
Concessione c = getById(id);
WorkspaceManager ws=new WorkspaceManager();
//Check Init Base folder
FolderContainer baseFolder=null;
if(c.getFolderId()==null) {
String folderName=Files.fixFilename("mConcessione"+"_"+c.getNome()+"_"+Serialization.FULL_FORMATTER.format(LocalDateTime.now()));
log.info("Creating folder {} for Concessione ID {} ",folderName,id);
FolderContainer folder=ws.createFolder(new FolderOptions(folderName, "Base Folder for "+c.getNome(),null));
c.setFolderId(folder.getId());
}
log.debug("Folder id is : "+c.getFolderId());
baseFolder=ws.getFolderById(c.getFolderId());
AssociatedContent section=c.getContentByPath(destinationPath);
log.debug("Found section {} for path {}",section,destinationPath);
store(section,files,ws,baseFolder);
log.debug("Updating dafults for {} ",c);
c.setDefaults();
return asConcessione(replace(asDocument(c),collectionName));
}catch(Exception e) {
throw new Exception("Unable to save file.",e);
2020-12-18 18:24:20 +01:00
}
2020-12-17 18:27:45 +01:00
}
2020-12-22 18:22:56 +01:00
private static Concessione index(Concessione record) {
log.info("Indexing {} ",record.getId());
ValidationReport report= new ValidationReport("Index Report ");
PostgisIndex index;
try {
index = new PostgisIndex(record);
index.registerCentroid();
report.addMessage(ValidationStatus.PASSED, "Registered centroid");
} catch (SDIInteractionException | PublishException e) {
log.error("Unable to index {} ",record,e);
report.addMessage(ValidationStatus.WARNING, "Internal error while indexing.");
}
return record;
}
private static Concessione publish(Concessione conc) {
// CHECK CONDITION BY PROFILE
log.debug("Publishing "+conc.getNome());
ValidationReport report=new ValidationReport("Publish report");
try {
SDIManager sdiManager=new SDIManager();
ArrayList<AssociatedContent> list=new ArrayList<AssociatedContent>();
//Concessione
String workspace= sdiManager.createWorkspace("gna_conc_"+conc.getMongo_id());
list.add(conc.getPosizionamentoScavo());
list.addAll(conc.getPianteFineScavo());
for(AssociatedContent c:list) {
if(c instanceof LayerConcessione) {
try {
List<PersistedContent> p=c.getActualContent();
GeoServerContent geoserverPersisted=sdiManager.pushShapeLayerFileSet((SDILayerDescriptor)c, workspace, conc.getMongo_id());
// geoserverPersisted.setAssociated(c);
p.add(geoserverPersisted);
c.setActualContent(p);
}catch(SDIInteractionException e) {
log.warn("Unable to publish layers.",e);
report.addMessage(ValidationStatus.WARNING, "Layer "+c.getTitolo()+" non pubblicato.");
}
report.addMessage(ValidationStatus.PASSED, "Pubblicato layer "+c.getTitolo());
}
}
} catch (SDIInteractionException e1) {
report.addMessage(ValidationStatus.WARNING, "Unable to publish layers "+e1.getMessage());
}
conc.setReport(report);
return conc;
2020-12-17 18:27:45 +01:00
}
2020-12-22 18:22:56 +01:00
2020-12-18 18:24:20 +01:00
private static final void store(AssociatedContent content,List<TempFile> files, WorkspaceManager ws, FolderContainer base) throws Exception {
FolderContainer sectionParent=null;
2020-12-22 18:22:56 +01:00
2020-12-18 18:24:20 +01:00
if(content instanceof RelazioneScavo)
sectionParent = ws .createFolder(new FolderOptions(
"relazione","Relazione di scavo : "+content.getTitolo(),base));
2020-12-22 18:22:56 +01:00
2020-12-18 18:24:20 +01:00
else if (content instanceof UploadedImage)
sectionParent = ws .createFolder(new FolderOptions(
"imgs","Immagini rappresentative : "+content.getTitolo(),base));
2020-12-22 18:22:56 +01:00
2020-12-18 18:24:20 +01:00
else if (content instanceof SDILayerDescriptor)
//SDI Section
if(content instanceof LayerConcessione)
sectionParent = ws .createFolder(new FolderOptions(
content.getTitolo(),"Layer Concessione : "+content.getTitolo(),ws.getSubFolder(base,"layers")));
else throw new Exception("Invalid SDI Content "+content);
else if (content instanceof OtherContent )
sectionParent = ws .createFolder(new FolderOptions(
content.getTitolo(),"Relazione di scavo : "+content.getTitolo(),ws.getSubFolder(base,"other")));
else throw new Exception("Invalid Content "+content);
2020-12-22 18:22:56 +01:00
2020-12-18 18:24:20 +01:00
content.setActualContent(new ArrayList<PersistedContent>());
StorageClientProvider storage=ImplementationProvider.get().getStorageProvider();
for(TempFile f : files) {
WorkspaceContent wsContent=ws.storeToWS(new FileOptions(f.getFilename(), storage.open(f.getId()), "Imported via GeoPortal", sectionParent));
log.debug("Registered "+wsContent+" for "+content);
content.getActualContent().add(wsContent);
}
2021-01-04 16:58:40 +01:00
content.setMongo_id(asString(new ObjectId()));
2020-12-18 18:24:20 +01:00
}
2020-12-22 18:22:56 +01:00
2020-12-17 18:27:45 +01:00
}