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

331 lines
13 KiB
Java

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.Record;
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.report.PublicationReport;
import org.gcube.application.geoportalcommon.GeoportalCommon;
import org.gcube.application.geoportalcommon.shared.GeoNaItemRef;
import org.gcube.portlets.user.geoportaldataentry.client.ConcessioniFormCardTitle;
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;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
/**
* The server side implementation of the RPC service.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
*
* Dec 1, 2020
*/
@SuppressWarnings("serial")
public class GeoportalDataEntryServiceImpl extends RemoteServiceServlet implements GeoportalDataEntryService {
public static final String GEONA_GENERIC_RESOURCE_SECONDARY_TYPE = "GEONA_GENERIC_RESOURCE_SECONDARY_TYPE";
private static final Logger LOG = LoggerFactory.getLogger(GeoportalDataEntryServiceImpl.class);
/**
* Save geona data forms.
*
* @param listGeonaFormObjects the list geona form objects
* @return the commit report
* @throws Exception the exception
*/
@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);
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
//SessionUtil.getCurrentContext(this.getThreadLocalRequest(), true);
manager = ManagerFactory.registerNew(concessione);
// ABSTRACT_RELAZIONE_DI_SCAVO
list = toMap.get(ConcessioniFormCardTitle.ABSTRACT_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.ABSTRACT_RELAZIONE_DI_SCAVO.getTitle());
listGDB = list.get(0).getListGDB();
GenericDatasetBean ards = listGDB.get(0);
LOG.debug("\n\n");
LOG.debug(ConcessioniFormCardTitle.ABSTRACT_RELAZIONE_DI_SCAVO + " building with client obj: " + ards);
RelazioneScavo abstractRelazioneDiScavo = ConvertToServiceModel.toAbstractRelazioneScavo(ards);
// 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(abstractRelazioneDiScavo, 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;
PublicationReport report = manager.commitSafely(publish);
LOG.info("PublicationReport: " + report);
Record registered = report.getTheRecord();
// String link = "toTest";
CommitReport cRep = new CommitReport();
if(registered!=null)
cRep.setRecordId(registered.getId());
cRep.setReport(report.prettyPrint());
switch (report.getStatus()) {
case PASSED:
cRep.setState(STATE.OK);
if(cRep.getRecordId()==null) {
LOG.error("The record id is null!!!");
throw new Exception("Invalid record id");
}
break;
case WARNING:
cRep.setState(STATE.WARN);
cRep.setMsg(report.getWarningMessages().toString());
break;
case ERROR:
cRep.setState(STATE.ERROR);
cRep.setMsg(report.getErrorMessages().toString());
break;
default:
cRep.setState(STATE.UNKNOWN);
break;
}
return cRep;
} catch (Exception e) {
LOG.error("Error on commiting data: ", e);
throw new Exception(
"Error occurred on saving data. Error: " + e.getMessage());
} finally {
// if(manager!=null) {
// try {
// manager.shutdown();
// }catch (Exception e) {
// //silent
// }
//
// }
}
}
/**
* Gets the geona init config.
*
* @return the geona init config
*/
@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";
}
// 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;
}
/**
* Gets the links for.
*
* @param itemId the item id
* @param recordType the record type
* @return the links for
* @throws Exception the exception
*/
@Override
public GeoNaItemRef getLinksFor(Long itemId, String recordType) throws Exception {
LOG.info("getLinksFor called");
SessionUtil.getCurrentContext(this.getThreadLocalRequest(), true);
GeoportalCommon gc = new GeoportalCommon();
GeoNaItemRef item = new GeoNaItemRef(itemId, recordType);
item = gc.getPublicLinksFor(item);
LOG.info("Returning: " + item);
return item;
}
/**
* Pretty print client data entry map.
*
* @param toMap the to map
*/
private void prettyPrintClientDataEntryMap(HashMap<ConcessioniFormCardTitle, List<GeoNaFormDataObject>> toMap) {
for (ConcessioniFormCardTitle theType : toMap.keySet()) {
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);
}
}
}
}
}
}