geoportal-data-entry-app/src/main/java/org/gcube/portlets/user/geoportaldataentry/server/GeoportalDataEntryServiceIm...

263 lines
11 KiB
Java
Raw Normal View History

2020-10-07 17:04:13 +02:00
package org.gcube.portlets.user.geoportaldataentry.server;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.gcube.application.geoportal.managers.ConcessioneManager;
import org.gcube.application.geoportal.managers.ManagerFactory;
import org.gcube.application.geoportal.model.InputStreamDescriptor;
import org.gcube.application.geoportal.model.concessioni.Concessione;
import org.gcube.application.geoportal.model.concessioni.LayerConcessione;
import org.gcube.application.geoportal.model.concessioni.RelazioneScavo;
import org.gcube.application.geoportal.model.content.UploadedImage;
import org.gcube.application.geoportal.model.content.WorkspaceContent;
import org.gcube.application.geoportal.model.fault.PublishException;
import org.gcube.application.geoportal.model.fault.ValidationException;
import org.gcube.application.geoportal.utils.Serialization;
import org.gcube.portlets.user.geoportaldataentry.client.ConcessioniFormCardTitle;
2020-10-22 16:50:14 +02:00
import org.gcube.portlets.user.geoportaldataentry.client.GeoportalDataEntryService;
import org.gcube.portlets.user.geoportaldataentry.shared.CommitReport;
import org.gcube.portlets.user.geoportaldataentry.shared.CommitReport.STATE;
import org.gcube.portlets.user.geoportaldataentry.shared.GeoNaFormDataObject;
import org.gcube.portlets.user.geoportaldataentry.shared.GeonaISConfig;
import org.gcube.portlets.widgets.mpformbuilder.shared.GenericDatasetBean;
import org.gcube.portlets.widgets.mpformbuilder.shared.upload.FileUploaded;
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2020-10-07 17:04:13 +02:00
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
/**
* The server side implementation of the RPC service.
*/
@SuppressWarnings("serial")
2020-10-22 16:50:14 +02:00
public class GeoportalDataEntryServiceImpl extends RemoteServiceServlet implements GeoportalDataEntryService {
public static final String GEONA_GENERIC_RESOURCE_SECONDARY_TYPE = "GEONA_GENERIC_RESOURCE_SECONDARY_TYPE";
2020-10-22 16:50:14 +02:00
private static final Logger LOG = LoggerFactory.getLogger(GeoportalDataEntryServiceImpl.class);
@Override
public CommitReport saveGeonaDataForms(List<GeoNaFormDataObject> listGeonaFormObjects) throws Exception {
LOG.info("saveGeonaDataForms called");
HashMap<ConcessioniFormCardTitle, List<GeoNaFormDataObject>> toMap =
new HashMap<ConcessioniFormCardTitle, List<GeoNaFormDataObject>>();
for (GeoNaFormDataObject geoNaFormDataObject : listGeonaFormObjects) {
ConcessioniFormCardTitle theType = geoNaFormDataObject.getTheType();
List<GeoNaFormDataObject> listForm = toMap.get(theType);
if (listForm == null) {
listForm = new ArrayList<GeoNaFormDataObject>();
}
listForm.add(geoNaFormDataObject);
toMap.put(theType, listForm);
}
if(LOG.isDebugEnabled())
prettyPrintClientDataEntryMap(toMap);
LOG.debug("Built map with form data: "+toMap);
2020-10-20 15:25:26 +02:00
GCubeUser user = SessionUtil.getCurrentUser(this.getThreadLocalRequest());
ConcessioneManager manager;
try {
//Saving Data
//Informazioni di Progetto
List<GeoNaFormDataObject> list = toMap.get(ConcessioniFormCardTitle.INFORMAZIONI_DI_PROGETTO);
if(list==null || list.isEmpty() || list.get(0)==null || list.get(0).getListGDB()==null)
throw new Exception("Error: no data found for "+ConcessioniFormCardTitle.INFORMAZIONI_DI_PROGETTO.getTitle());
List<GenericDatasetBean> listGDB = list.get(0).getListGDB();
GenericDatasetBean idp = listGDB.get(0);
LOG.debug("\n\n");
LOG.debug(ConcessioniFormCardTitle.INFORMAZIONI_DI_PROGETTO+" building with client obj: "+idp);
Concessione concessione = ConvertToServiceModel.toConcessione(idp, user);
LOG.debug("Built "+ConcessioniFormCardTitle.INFORMAZIONI_DI_PROGETTO+" as server obj: "+concessione);
//saving into back-end
manager = ManagerFactory.registerNew(concessione);
//RELAZIONE_DI_SCAVO
list = toMap.get(ConcessioniFormCardTitle.RELAZIONE_DI_SCAVO);
if(list==null || list.isEmpty() || list.get(0)==null || list.get(0).getListGDB()==null)
throw new Exception("Error: no data found for "+ConcessioniFormCardTitle.RELAZIONE_DI_SCAVO.getTitle());
listGDB = list.get(0).getListGDB();
GenericDatasetBean rds = listGDB.get(0);
LOG.debug("\n\n");
LOG.debug(ConcessioniFormCardTitle.RELAZIONE_DI_SCAVO+" building with client obj: "+rds);
RelazioneScavo relazioneDiScavo = ConvertToServiceModel.toRelazioneScavo(rds);
LOG.debug("Built "+ConcessioniFormCardTitle.RELAZIONE_DI_SCAVO+" as server obj: "+relazioneDiScavo);
//Should be only one
for (FileUploaded fileUploaded : rds.getFilesUploaded()) {
FileInputStream fis = new FileInputStream(fileUploaded.getTempSystemPath());
InputStreamDescriptor isd = new InputStreamDescriptor(fis, fileUploaded.getFileName());
//saving into back-end
manager.setRelazioneScavo(relazioneDiScavo, isd);
}
//IMMAGINI_RAPPRESENTATIVE
list = toMap.get(ConcessioniFormCardTitle.IMMAGINI_RAPPRESENTATIVE);
if(list==null || list.isEmpty() || list.get(0)==null || list.get(0).getListGDB()==null)
throw new Exception("Error: no data found for "+ConcessioniFormCardTitle.IMMAGINI_RAPPRESENTATIVE.getTitle());
List<GenericDatasetBean> imgRaprsLst = list.get(0).getListGDB();
for (GenericDatasetBean imgRapr : imgRaprsLst) {
LOG.debug("\n\n");
LOG.debug(ConcessioniFormCardTitle.IMMAGINI_RAPPRESENTATIVE+" building with client obj: "+imgRapr);
UploadedImage img = ConvertToServiceModel.toImmaginiRappresentative(imgRapr);
LOG.debug("Built "+ConcessioniFormCardTitle.IMMAGINI_RAPPRESENTATIVE+" as server obj: "+img);
for (FileUploaded fileUploaded : imgRapr.getFilesUploaded()) {
LOG.debug("Adding uploaded file: "+fileUploaded.getFileName() + " to "+img);
FileInputStream fis = new FileInputStream(fileUploaded.getTempSystemPath());
InputStreamDescriptor isd = new InputStreamDescriptor(fis, fileUploaded.getFileName());
//saving into back-end
manager.addImmagineRappresentativa(img,isd);
}
}
//POSIZIONAMENTO_DELL_AREA_DI_INDAGINE
list = toMap.get(ConcessioniFormCardTitle.POSIZIONAMENTO_DELL_AREA_DI_INDAGINE);
if(list==null || list.isEmpty() || list.get(0)==null || list.get(0).getListGDB()==null)
throw new Exception("Error: no data found for "+ConcessioniFormCardTitle.POSIZIONAMENTO_DELL_AREA_DI_INDAGINE.getTitle());
listGDB = list.get(0).getListGDB();
GenericDatasetBean pad = listGDB.get(0);
LOG.debug("\n\n");
LOG.debug(ConcessioniFormCardTitle.POSIZIONAMENTO_DELL_AREA_DI_INDAGINE+" building with client obj: "+pad);
LayerConcessione layerPad = ConvertToServiceModel.toLayerConcessione(pad);
LOG.debug("Built "+ConcessioniFormCardTitle.POSIZIONAMENTO_DELL_AREA_DI_INDAGINE+" as server obj: "+layerPad);
for (FileUploaded fileUploaded : pad.getFilesUploaded()) {
LOG.debug("Adding uploaded file: "+fileUploaded.getFileName() + " to "+layerPad);
FileInputStream fis = new FileInputStream(fileUploaded.getTempSystemPath());
InputStreamDescriptor isd = new InputStreamDescriptor(fis, fileUploaded.getFileName());
//saving into back-end
manager.setPosizionamento(layerPad, isd);
}
//PIANTA_DI_FINE_SCAVO
list = toMap.get(ConcessioniFormCardTitle.PIANTA_DI_FINE_SCAVO);
if(list==null || list.isEmpty() || list.get(0)==null || list.get(0).getListGDB()==null)
throw new Exception("Error: no data found for "+ConcessioniFormCardTitle.PIANTA_DI_FINE_SCAVO.getTitle());
listGDB = list.get(0).getListGDB();
GenericDatasetBean pfs = listGDB.get(0);
LOG.debug("\n\n");
LOG.debug(ConcessioniFormCardTitle.PIANTA_DI_FINE_SCAVO+" building with client obj: "+pfs);
LayerConcessione layerPfs = ConvertToServiceModel.toLayerConcessione(pfs);
LOG.debug("Built "+ConcessioniFormCardTitle.PIANTA_DI_FINE_SCAVO+" as server obj: "+layerPfs);
for (FileUploaded fileUploaded : pfs.getFilesUploaded()) {
LOG.debug("Adding uploaded file: "+fileUploaded.getFileName() + " to "+layerPfs);
FileInputStream fis = new FileInputStream(fileUploaded.getTempSystemPath());
InputStreamDescriptor isd = new InputStreamDescriptor(fis, fileUploaded.getFileName());
//saving into back-end
manager.addPiantaFineScavo(layerPfs, isd);
}
}catch (Exception e) {
LOG.error("Error on converting form data: ",e);
throw new Exception("Error occurred on saving data, try again or contact the support. Error: "+e.getMessage());
}
try {
//If true -> data are published into the SDI
Boolean publish=true;
Concessione registered=manager.commit(publish);
LOG.info("Registered: "+registered);
String link = null;
try {
link = ((WorkspaceContent)registered.getRelazioneScavo().getActualContent().get(0)).getLink();
}catch (Exception e) {
LOG.warn("Not able to check public link ",e);
}
//String link = "toTest";
CommitReport cRep = new CommitReport();
cRep.setRecordId(registered.getId());
if(link!=null) {
cRep.setState(STATE.OK);
cRep.setMsg("Project saved successfully");
}
else {
cRep.setState(STATE.WARN);
cRep.setMsg("Project saved with errors");
}
return cRep;
}catch (ValidationException e1){
LOG.error("Error on validating data: ",e1);
throw new Exception("Error on validating data: "+Serialization.prettyPrint(e1.getReport()));
}catch (PublishException e2) {
LOG.error("Error on publishing data: ",e2);
throw new Exception("Error on publishing data: "+e2.getMessage());
}catch (Exception e) {
LOG.error("Error on commiting data: ",e);
throw new Exception("Error occurred on saving data, try again or contact the support. Error: "+e.getMessage());
}finally {
// if(manager!=null) {
// try {
// manager.shutdown();
// }catch (Exception e) {
// //silent
// }
//
// }
}
}
@Override
public GeonaISConfig getGeonaInitConfig() {
LOG.info("getConfig called");
String scope = SessionUtil.getCurrentContext(this.getThreadLocalRequest(), false);
String theSecondaryType;
try {
theSecondaryType = this.getServletContext().getInitParameter(GEONA_GENERIC_RESOURCE_SECONDARY_TYPE);
} catch (Exception e) {
LOG.error("I cannot read the init parameter for: " + GEONA_GENERIC_RESOURCE_SECONDARY_TYPE, e);
theSecondaryType = "GeoNaMetadata";
}
2020-11-03 12:14:57 +01:00
// LOG.warn("\n\n\nHARD-CABLING THE SCOPE, PLEASE REMOTE IT!!!!\n\n\n");
// scope = "/gcube/devsec/devVRE";
GeonaISConfig configs = new GeonaISConfig(theSecondaryType, scope);
LOG.info("returning config: "+configs);
return configs;
}
private void prettyPrintClientDataEntryMap(HashMap<ConcessioniFormCardTitle, List<GeoNaFormDataObject>> toMap) {
for (ConcessioniFormCardTitle theType : toMap.keySet()) {
2020-10-20 15:25:26 +02:00
LOG.debug("\n\n");
LOG.debug(theType.toString());
List<GeoNaFormDataObject> list = toMap.get(theType);
for (GeoNaFormDataObject geoNaFormDataObject : list) {
LOG.debug("\t has "+geoNaFormDataObject.getListGDB().size() +" data bean/s");
int i = 0;
for (GenericDatasetBean gbd : geoNaFormDataObject.getListGDB()) {
LOG.debug("\t "+ ++i +") "+GenericDatasetBean.class.getSimpleName()+" entries:");
Map<String, List<String>> map = gbd.getFormDataEntryFields();
for (String keyEntry : map.keySet()) {
LOG.debug("\t "+keyEntry + ": "+map.get(keyEntry));
}
for (FileUploaded fup : gbd.getFilesUploaded()) {
LOG.debug("\t "+fup);
}
}
}
}
}
2020-10-07 17:04:13 +02:00
}