diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/GeoportalDataViewer.java b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/GeoportalDataViewer.java index a5cd49c..d095be6 100644 --- a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/GeoportalDataViewer.java +++ b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/GeoportalDataViewer.java @@ -9,7 +9,7 @@ import org.gcube.application.geoportalcommon.shared.GNADataViewerConfigProfile; import org.gcube.application.geoportalcommon.shared.GeoNaItemRef; import org.gcube.application.geoportalcommon.shared.geoportal.DocumentDV; import org.gcube.application.geoportalcommon.shared.geoportal.config.ItemFieldDV; -import org.gcube.application.geoportalcommon.shared.geoportal.materialization.IndexLayer; +import org.gcube.application.geoportalcommon.shared.geoportal.materialization.IndexLayerDV; import org.gcube.application.geoportalcommon.shared.geoportal.view.ProjectView; import org.gcube.application.geoportalcommon.shared.products.ConcessioneDV; import org.gcube.application.geoportalcommon.shared.products.model.RecordDV; @@ -361,7 +361,7 @@ public class GeoportalDataViewer implements EntryPoint { } // TODO Get Default Index Layer // For now we just take the first - only 1 is expected - IndexLayer layer = toOpen.getIndexes().get(0); + IndexLayerDV layer = toOpen.getIndexes().get(0); // Open Index Layer layerManager.addIndexLayer(layer); @@ -390,7 +390,7 @@ public class GeoportalDataViewer implements EntryPoint { // } // // TODO Get Default Index Layer // // For now we just take the first - only 1 is expected -// IndexLayer layer = toOpen.getIndexes().get(0); +// IndexLayerDV layer = toOpen.getIndexes().get(0); // // Open Index Layer // // layerManager.addIndexLayer(layer); @@ -421,7 +421,8 @@ public class GeoportalDataViewer implements EntryPoint { @Override public void onFailure(Throwable caught) { - // TODO Auto-generated method stub + Window.alert(caught.getMessage()); + mainPanel.hidePanelDetails(); } diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/LayerManager.java b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/LayerManager.java index 89b078f..d8e69fb 100644 --- a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/LayerManager.java +++ b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/LayerManager.java @@ -9,7 +9,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import org.gcube.application.geoportalcommon.shared.geoportal.materialization.IndexLayer; +import org.gcube.application.geoportalcommon.shared.geoportal.materialization.IndexLayerDV; import org.gcube.application.geoportalcommon.shared.gis.BoundsMap; import org.gcube.application.geoportalcommon.shared.products.content.WorkspaceContentDV; import org.gcube.application.geoportalcommon.shared.products.model.UploadedImageDV; @@ -542,7 +542,7 @@ public class LayerManager { * * @param layer the layer */ - public void addIndexLayer(IndexLayer layer) { + public void addIndexLayer(IndexLayerDV layer) { addLayer(LayerObjectType.INDEX_LAYER, null, null, layer.getLayer().getOgcLinks().get("wms"), false, false, null, false, null, null, null, null); diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/ui/cms/gallery/ImagesSectionGallery.java b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/ui/cms/gallery/ImagesSectionGallery.java index 7105c2f..0c24ecd 100644 --- a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/ui/cms/gallery/ImagesSectionGallery.java +++ b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/ui/cms/gallery/ImagesSectionGallery.java @@ -2,8 +2,8 @@ package org.gcube.portlets.user.geoportaldataviewer.client.ui.cms.gallery; import java.util.Set; -import org.gcube.application.geoportalcommon.shared.geoportal.view.FilesetDV; -import org.gcube.application.geoportalcommon.shared.geoportal.view.PayloadDV; +import org.gcube.application.geoportalcommon.shared.geoportal.materialization.innerobject.FilesetDV; +import org.gcube.application.geoportalcommon.shared.geoportal.materialization.innerobject.PayloadDV; import org.gcube.application.geoportalcommon.shared.geoportal.view.SectionView; import org.gcube.application.geoportalcommon.shared.geoportal.view.SubDocumentView; diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/ui/cms/project/ProjectViewer.java b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/ui/cms/project/ProjectViewer.java index a779604..d3d7781 100644 --- a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/ui/cms/project/ProjectViewer.java +++ b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/ui/cms/project/ProjectViewer.java @@ -136,8 +136,10 @@ public class ProjectViewer extends Composite { for (SectionView sectionView : projectView.getListSections()) { - SectionViewer sectionViewer = new SectionViewer(sectionView); - pageViewDetails.add(sectionViewer); + if(!sectionView.isEmpty()) { + SectionViewer sectionViewer = new SectionViewer(sectionView); + pageViewDetails.add(sectionViewer); + } } } diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/ui/cms/project/SectionViewer.java b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/ui/cms/project/SectionViewer.java index 6cb6524..824abdf 100644 --- a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/ui/cms/project/SectionViewer.java +++ b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/ui/cms/project/SectionViewer.java @@ -2,8 +2,8 @@ package org.gcube.portlets.user.geoportaldataviewer.client.ui.cms.project; import java.util.List; -import org.gcube.application.geoportalcommon.shared.geoportal.view.FilesetDV; -import org.gcube.application.geoportalcommon.shared.geoportal.view.PayloadDV; +import org.gcube.application.geoportalcommon.shared.geoportal.materialization.innerobject.FilesetDV; +import org.gcube.application.geoportalcommon.shared.geoportal.materialization.innerobject.PayloadDV; import org.gcube.application.geoportalcommon.shared.geoportal.view.SectionView; import org.gcube.application.geoportalcommon.shared.geoportal.view.SubDocumentView; import org.gcube.portlets.user.geoportaldataviewer.client.GeoportalDataViewerConstants; @@ -51,18 +51,31 @@ public class SectionViewer extends Composite { } } - //Displaying the whole section as a Gallery + boolean displayAsMapOfLayers = false; + for (SubDocumentView subDocumentView : subDocuments) { + if (subDocumentView.getListLayers() != null && subDocumentView.getListLayers().size() > 0) { + displayAsMapOfLayers = true; + break; + } + } + + // Displaying the whole section as a Gallery if (displayAsGallery) { ImagesSectionGallery sectionGallery = new ImagesSectionGallery(sectionView); sectionPanelContainer.add(sectionGallery.getGalleryPanel()); sectionGallery.fillGallery(); - } else { + + // Displaying the whole section as a Map of Layers + } else if (displayAsMapOfLayers) { + + + }else { for (SubDocumentView subDocumentView : subDocuments) { String table = GeoportalDataViewerConstants.jsonToTableHTML(subDocumentView.getMetadataAsJSON()); sectionPanelContainer.add(new HTML(table)); - + List files = subDocumentView.getListFiles(); - if(files!=null) { + if (files != null) { for (FilesetDV fileset : files) { showLinkToDownloadWsContent(fileset.getName(), fileset.getListPayload()); } @@ -71,7 +84,7 @@ public class SectionViewer extends Composite { } } - + private void showLinkToDownloadWsContent(String title, List listPayloads) { if (listPayloads != null) { @@ -83,16 +96,16 @@ public class SectionViewer extends Composite { if (i > 0) { fieldLabel = ""; } - + String downloadLabel = "download"; - if(payload.getName()!=null) { + if (payload.getName() != null) { downloadLabel = payload.getName(); } - customTable.addNextKeyWidget(fieldLabel, new HTML( - "" + downloadLabel + "")); + customTable.addNextKeyWidget(fieldLabel, + new HTML("" + downloadLabel + "")); } - + sectionPanelContainer.add(customTable); } } diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/GeoportalDataViewerServiceImpl.java b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/GeoportalDataViewerServiceImpl.java index 2c41412..dfdda3b 100644 --- a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/GeoportalDataViewerServiceImpl.java +++ b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/GeoportalDataViewerServiceImpl.java @@ -3,6 +3,7 @@ package org.gcube.portlets.user.geoportaldataviewer.server; import static org.gcube.application.geoportal.client.plugins.GeoportalAbstractPlugin.projects; import static org.gcube.application.geoportal.client.plugins.GeoportalAbstractPlugin.useCaseDescriptors; +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -42,14 +43,17 @@ import org.gcube.application.geoportalcommon.shared.geoportal.ResultDocumentDV; import org.gcube.application.geoportalcommon.shared.geoportal.config.FilePathDV; import org.gcube.application.geoportalcommon.shared.geoportal.config.GcubeProfileDV; import org.gcube.application.geoportalcommon.shared.geoportal.config.ItemFieldDV; -import org.gcube.application.geoportalcommon.shared.geoportal.materialization.IndexLayer; +import org.gcube.application.geoportalcommon.shared.geoportal.materialization.GCubeSDILayer; +import org.gcube.application.geoportalcommon.shared.geoportal.materialization.GCubeSDIViewerLayerDV; +import org.gcube.application.geoportalcommon.shared.geoportal.materialization.IndexLayerDV; +import org.gcube.application.geoportalcommon.shared.geoportal.materialization.innerobject.BBOXDV; +import org.gcube.application.geoportalcommon.shared.geoportal.materialization.innerobject.FilesetDV; +import org.gcube.application.geoportalcommon.shared.geoportal.materialization.innerobject.PayloadDV; import org.gcube.application.geoportalcommon.shared.geoportal.project.ProjectDV; import org.gcube.application.geoportalcommon.shared.geoportal.ucd.GEOPORTAL_CONFIGURATION_TYPE; import org.gcube.application.geoportalcommon.shared.geoportal.ucd.GEOPORTAL_DATA_HANDLER; import org.gcube.application.geoportalcommon.shared.geoportal.ucd.HandlerDeclarationDV; import org.gcube.application.geoportalcommon.shared.geoportal.ucd.UseCaseDescriptorDV; -import org.gcube.application.geoportalcommon.shared.geoportal.view.FilesetDV; -import org.gcube.application.geoportalcommon.shared.geoportal.view.PayloadDV; import org.gcube.application.geoportalcommon.shared.geoportal.view.ProjectView; import org.gcube.application.geoportalcommon.shared.geoportal.view.SectionView; import org.gcube.application.geoportalcommon.shared.geoportal.view.SubDocumentView; @@ -83,15 +87,24 @@ import org.gcube.spatial.data.geoutility.bean.LayerZAxis; import org.gcube.spatial.data.geoutility.bean.WmsParameters; import org.gcube.vomanagement.usermanagement.model.GCubeUser; import org.json.JSONArray; +import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gwt.user.server.rpc.RemoteServiceServlet; import com.jayway.jsonpath.JsonPath; +import com.jayway.jsonpath.TypeRef; +import com.jayway.jsonpath.spi.json.GsonJsonProvider; import com.jayway.jsonpath.spi.json.JsonOrgJsonProvider; /** @@ -110,8 +123,6 @@ public class GeoportalDataViewerServiceImpl extends RemoteServiceServlet impleme private static final String CACHE_IMAGE_PREVIEW_FOR_CONCESSIONE = "MAP_IMAGE_PREVIEW_FOR_CONCESSIONE"; - public static final String JSON_$_POINTER = "$"; - public static enum COMMON_IMAGES_FORMAT { gif, png, jpeg, jpg, bmp, tif, tiff, svg, avif, webp } @@ -753,6 +764,7 @@ public class GeoportalDataViewerServiceImpl extends RemoteServiceServlet impleme LOG.debug("Checking configuration for collection " + u.getId()); Projects p = projects(u.getId()).build(); + UseCaseDescriptorDV ucd = ConvertToDataValueObjectModel.toUseCaseDescriptorDV(u, null); Configuration ucdConfig = p.getConfiguration(); GCubeCollection coll = new GCubeCollection(); @@ -771,7 +783,7 @@ public class GeoportalDataViewerServiceImpl extends RemoteServiceServlet impleme for (Index index : ucdConfig.getIndexes()) { try { - IndexLayer toAdd = ConvertToDataValueObjectModel.convert(index); + IndexLayerDV toAdd = ConvertToDataValueObjectModel.convert(index); if (toAdd.getFlag().equals(toCheckFlag)) { coll.getIndexes().add(toAdd); } @@ -902,46 +914,6 @@ public class GeoportalDataViewerServiceImpl extends RemoteServiceServlet impleme super.onBeforeRequestDeserialized(serializedRequest); } -// /** -// * Gets the list concessioni. -// * -// * @param start the start -// * @param limit the limit -// * @param filter the filter -// * @param reloadFromService the reload from service -// * @return the list concessioni -// * @throws Exception the exception -// */ -// @Override -// public ResultSetPaginatedData getListConcessioni(Integer start, Integer limit, SearchingFilter filter, -// boolean reloadFromService) throws Exception { -// LOG.info("getListConcessioni called wit start: " + start + ", limit: " + limit + ", filter: " + filter); -// -// try { -// -// throw new Exception("getListConcessioni must be revisited!!!!"); -// /* -// * -// * // setting identity as D4S User or KC client new -// * GeoportalServiceIdentityProxy(this.getThreadLocalRequest()); -// * SessionUtil.getCurrentContext(getThreadLocalRequest(), true); -// * MongoServiceCommon serviceCommon = new MongoServiceCommon(); // TODO MUST BE -// * REPLACED BY COUNT List listOfConcessioni = -// * SessionUtil.getListOfConcessioni(getThreadLocalRequest(), reloadFromService); -// * int listConcessioniSize = listOfConcessioni.size(); -// * -// * ResultSetPaginatedData searchedData = -// * serviceCommon.queryOnMongo(listConcessioniSize, start, limit, filter, -// * "concessione"); return searchedData; -// */ -// -// } catch (Exception e) { -// LOG.error("Error on loading paginated and filtered list of concessioni: ", e); -// throw new Exception("Error occurred on loading list of Concessioni. Error: " + e.getMessage()); -// } -// -// } - /** * Gets the list projects. * @@ -1125,11 +1097,12 @@ public class GeoportalDataViewerServiceImpl extends RemoteServiceServlet impleme GeoportalServiceIdentityProxy cms = new GeoportalServiceIdentityProxy(this.getThreadLocalRequest()); ProjectDV theProjectDV = cms.getProjectByID(profileID, projectID); - ProjectView projectView = loadProjectView(theProjectDV, scope, userName); + ProjectView projectView = Geoportal_JSON_Mapper.loadProjectView(theProjectDV, scope, userName); + + if (LOG.isDebugEnabled()) { + Geoportal_JSON_Mapper.prettyPrintProjectView(projectView); + } - if(LOG.isDebugEnabled()) - prettyPrintProjectView(projectView); - LOG.info("returning project view for id: " + projectView.getTheProjectDV().getId()); return projectView; @@ -1141,369 +1114,4 @@ public class GeoportalDataViewerServiceImpl extends RemoteServiceServlet impleme } - public ProjectView loadProjectView(ProjectDV theProjectDV, String scope, String username) throws Exception { - - String theWholeProjectAsJSON = theProjectDV.getTheDocument().getDocumentAsJSON(); - - LOG.debug("theProjectDV as JSON: " + theWholeProjectAsJSON); - LOG.debug("theProjectDV as MAP: " + theProjectDV.getTheDocument().getDocumentAsMap()); - - ProjectView projectView = new ProjectView(); - projectView.setTheProjectDV(theProjectDV); - - LinkedHashMap> linkedMap_UCDId_gCubeProfiles = GcubeProfilesPerUCDIdCache - .get(scope); - - // NO UCD defined, applying default - if (linkedMap_UCDId_gCubeProfiles.size() == 0) { - LOG.warn("No " + GEOPORTAL_CONFIGURATION_TYPE.gcube_profiles + " found in the UCD"); - LOG.info("Applying default business logic to display the project"); - SectionView sectionView = new SectionView(); - sectionView.setSectionTitle("Document"); - SubDocumentView subDocumentView = new SubDocumentView(); - - Document sectionDoc = Document.parse(theProjectDV.getTheDocument().getDocumentAsJSON()); - boolean isAccessibleSection = isAccessibleSectionAccordingToPolicy(sectionDoc, JSON_$_POINTER, username); - - // If is accessible - if (isAccessibleSection) { - - // Creating one Project with one SectionView and SubDocumentView - String wholeSectionDoc = sectionDoc.toJson(); - subDocumentView.setMetadataAsJSON(wholeSectionDoc); - - List listFiles = new ArrayList(); - List listImages = new ArrayList(); - String filesetJSONPath = String.format("%s.%s", JSON_$_POINTER, "fileset"); - List listPayloads = readPayloadsForFileset(filesetJSONPath, wholeSectionDoc); - FilesetDV filesetDV = new FilesetDV(); - filesetDV.setName("fileset"); - for (Payload payload : listPayloads) { - PayloadDV payloadDV = ConvertToDataValueObjectModel.toPayloadDV(payload); - filesetDV.addPayloadDV(payloadDV); - boolean isImage = ImageDetector.isImage(payload.getMimetype()); - - if (isImage) { - listImages.add(filesetDV); - } else { - listFiles.add(filesetDV); - } - } - - // TODO LAYERS - subDocumentView.setListImages(listImages); - subDocumentView.setListFiles(listFiles); - sectionView.addSubDocument(subDocumentView); - projectView.addSectionView(sectionView); - - } - - } - - List listProfilesBean = linkedMap_UCDId_gCubeProfiles - .get(theProjectDV.getProfileID()); - - com.jayway.jsonpath.Configuration configuration = com.jayway.jsonpath.Configuration.builder() - .jsonProvider(new JsonOrgJsonProvider()).build(); - - // Reading the Project according to list of Profile defined in the UCD - for (GcubeProfilesMetadataForUCD gcubeProfileMetaForUCD : listProfilesBean) { - - GcubeProfileDV gcubeProfileDV = gcubeProfileMetaForUCD.getGcubeProfile(); - SectionView sectionView = new SectionView(); - sectionView.setSectionTitle(gcubeProfileDV.getSectionTitle()); - LOG.debug("\n\nThe profile is: " + gcubeProfileDV); - // Building JSON/section full PATH and section name - String sectionJSONPath = ""; - String parentPathFromProfile = gcubeProfileDV.getParentName() == null ? "" : gcubeProfileDV.getParentName(); - String theSectionName = gcubeProfileDV.getSectionName(); - - if (theSectionName.compareTo(JSON_$_POINTER) == 0 || theSectionName.compareTo(JSON_$_POINTER + ".") == 0) { - sectionJSONPath = JSON_$_POINTER; - theSectionName = ""; - } else { - sectionJSONPath = String.format("%s%s", - parentPathFromProfile.endsWith(".") ? parentPathFromProfile : parentPathFromProfile + ".", - theSectionName); - } - - LOG.debug("The sectionJSONPath is: " + sectionJSONPath); - - JsonPath theSectionJsonPath = null; - Object data = null; - try { - theSectionJsonPath = JsonPath.compile(sectionJSONPath); - data = theSectionJsonPath.read(theWholeProjectAsJSON, configuration); - }catch (Exception e) { - LOG.warn("Error on searching the section " + sectionJSONPath +" in the JSON Project: "+theWholeProjectAsJSON); - continue; - } - - LOG.debug("Data is instace of: " + data.getClass()); - LOG.debug("data to string: " + data.toString()); - - // Splitting the General Document in bson.Document according to list of - // GcubeProfiles - List listBSONDocument = new ArrayList(); - if (data instanceof org.json.JSONObject) { - String jsonString = data.toString(); - LOG.debug("the JSON to string: " + jsonString); - Document sectionDoc = Document.parse(jsonString); - boolean isAccessibleSection = isAccessibleSectionAccordingToPolicy(sectionDoc, sectionJSONPath, - username); - if (isAccessibleSection) { - listBSONDocument.add(sectionDoc); - } - - } else if (data instanceof org.json.JSONArray) { - org.json.JSONArray dataArray = (org.json.JSONArray) data; - for (int i = 0; i < dataArray.length(); i++) { - String jsonString = dataArray.get(i).toString(); - LOG.debug("the array " + i + " JSON to string: " + jsonString); - Document sectionDoc = Document.parse(jsonString); - boolean isAccessibleSection = isAccessibleSectionAccordingToPolicy(sectionDoc, sectionJSONPath, - username); - if (isAccessibleSection) { - listBSONDocument.add(sectionDoc); - } - - } - } - - LOG.debug("Result for " + gcubeProfileDV.getSectionName() + " is: " + listBSONDocument); - List theProfileBeans = gcubeProfileMetaForUCD.getListMetadataProfileBean(); - MetaDataProfileBean theProfileBean = theProfileBeans.get(0); - - // For each bson.Document creating the SubDocumentView - for (int i = 0; i < listBSONDocument.size(); i++) { - Document fromSectionDoc = listBSONDocument.get(i); - SubDocumentView subDocumentView = new SubDocumentView(); - Document toSectionDoc = new Document(); - // Filling the SubDocumentView metadata with the metadataField.getFieldName() - // read from the Profile - for (MetadataFieldWrapper metadataField : theProfileBean.getMetadataFields()) { - - String theFieldName = metadataField.getFieldId() != null ? metadataField.getFieldId() - : metadataField.getFieldName(); - LOG.debug("reading theFieldName: " + theFieldName); - Object theOBJFieldValue = fromSectionDoc.get(theFieldName); - - // NB: Using ALWAYS THE metadataField.getFieldName() as LABEL - toSectionDoc = sanitizeDocumentValue(toSectionDoc, metadataField.getFieldName(), theOBJFieldValue); - - } - String subToJSON = toSectionDoc.toJson(); - LOG.debug("theSubSetionDoc is: " + subToJSON); - subDocumentView.setMetadataAsJSON(toSectionDoc.toJson()); - - // Reading filePaths - List filePaths = gcubeProfileDV.getFilePaths(); - - // READING fileset* field ACCORDING TO filePaths OF THE 'gcubeProfiles' CONFIG - if (filePaths != null) { - String fromSectionDocJSON = fromSectionDoc.toJson(); - List listFiles = new ArrayList(); - List listImages = new ArrayList(); - for (FilePathDV filePath : filePaths) { - - String filesetJSONPath = String.format("%s.%s", JSON_$_POINTER, filePath.getFieldName()); - List listPayloads = readPayloadsForFileset(filesetJSONPath, fromSectionDocJSON); - FilesetDV filesetDV = new FilesetDV(); - filesetDV.setName(filePath.getGcubeProfileFieldName()); - for (Payload payload : listPayloads) { - PayloadDV payloadDV = ConvertToDataValueObjectModel.toPayloadDV(payload); - filesetDV.addPayloadDV(payloadDV); - boolean isImage = ImageDetector.isImage(payload.getMimetype()); - - if (isImage) { - listImages.add(filesetDV); - } else { - listFiles.add(filesetDV); - } - } - - } - subDocumentView.setListFiles(listFiles); - subDocumentView.setListImages(listImages); - } - - sectionView.addSubDocument(subDocumentView); - - } - - projectView.addSectionView(sectionView); - } - - return projectView; - } - - public void prettyPrintProjectView(ProjectView projectView) { - - for (SectionView section : projectView.getListSections()) { - System.out.println("\n\n###### Section Title: " + section.getSectionTitle() + " ######"); - int i = 1; - for (SubDocumentView subDocument : section.getListSubDocuments()) { - System.out.println("## " + SubDocumentView.class.getSimpleName() + " n." + i); - System.out.println("***** Metadata"); - System.out.println(prettyPrintJSON(subDocument.getMetadataAsJSON())); - System.out.println("***** Files"); - if (subDocument.getListFiles() != null) { - for (FilesetDV filesetDV : subDocument.getListFiles()) { - System.out.println("******* File Fileset name: " + filesetDV.getName()); - for (PayloadDV payload : filesetDV.getListPayload()) { - System.out.println("********* Payload: " + payload); - } - } - } - System.out.println("***** Images"); - if (subDocument.getListImages() != null) { - for (FilesetDV filesetDV : subDocument.getListImages()) { - System.out.println("******* Image Fileset name: " + filesetDV.getName()); - for (PayloadDV payload : filesetDV.getListPayload()) { - System.out.println("********* Payload: " + payload); - } - } - } - i++; - } - - } - } - - /** - * Read payloads for fileset. - * - * @param filesetJSONPath the fileset JSON path - * @param sectionJSONDocument the section JSON document - * @return the list - */ - private List readPayloadsForFileset(String filesetJSONPath, String sectionJSONDocument) { - LOG.debug("readPayloadsForFileset called"); - - List listPayloads = new ArrayList(); - String _payloadsJSONPath = String.format("%s.%s", filesetJSONPath, "_payloads"); - try { - com.jayway.jsonpath.Configuration configuration = com.jayway.jsonpath.Configuration.builder() - .jsonProvider(new JsonOrgJsonProvider()).build(); - - LOG.info("Reading sectionPath at {} into section document {}", _payloadsJSONPath, sectionJSONDocument); - JsonPath theSectionPolycJsonPath = JsonPath.compile(_payloadsJSONPath); - Object _payloads = theSectionPolycJsonPath.read(sectionJSONDocument, configuration).toString(); - - if (_payloads instanceof String) { - String toStringPayloads = (String) _payloads; - LOG.trace("The _payloads is a String {}", toStringPayloads); - JSONArray jsonArray = new JSONArray(toStringPayloads); - for (int i = 0; i < jsonArray.length(); i++) { - Payload payloadDV = Serialization.read(jsonArray.getJSONObject(i).toString(), Payload.class); - listPayloads.add(payloadDV); - } - } - - LOG.info("returning list of payloads {}", listPayloads); - } catch (Exception e) { - LOG.warn("Error on reading the JSON Path "+_payloadsJSONPath+" in the doc "+sectionJSONDocument, e); - } - - return listPayloads; - - } - - /** - * Checks if is accessible section according to policy. - * - * @param section the section - * @param sectionJSONPath the section JSON path - * @param myLogin the my login - * @return true, if is accessible section according to policy - */ - private boolean isAccessibleSectionAccordingToPolicy(Document section, String sectionJSONPath, String myLogin) { - LOG.debug("isAccessibleSectionAccordingToPolicy called"); - boolean isAccessible = true; - - // Skipping the root, going to check the access_policy of subsections - if (sectionJSONPath.compareTo(JSON_$_POINTER) != 0) { - isAccessible = checkAccessPolicy(section.toJson(), myLogin); - } - - return isAccessible; - } - - /** - * Check access policy. - * - * @param sectionDocumentJSON the section document JSON - * @param myLogin the my login - * @return true, if successful - */ - private boolean checkAccessPolicy(String sectionDocumentJSON, String myLogin) { - LOG.debug("checkAccessPolicy called"); - // CHECKING THE POLICY - String accessPolicyPath = JSON_$_POINTER + "._access._policy"; - boolean isAccessible = true; - try { - com.jayway.jsonpath.Configuration configuration = com.jayway.jsonpath.Configuration.builder() - .jsonProvider(new JsonOrgJsonProvider()).build(); - - LOG.info("Reading access policy at {} into section document {}", accessPolicyPath, sectionDocumentJSON); - JsonPath theSectionPolycJsonPath = JsonPath.compile(accessPolicyPath); - String _policy = theSectionPolycJsonPath.read(sectionDocumentJSON, configuration).toString(); - LOG.info("The section {} has policy {}", accessPolicyPath, _policy); - isAccessible = GeportalCheckAccessPolicy.isAccessible(_policy, myLogin); - } catch (Exception e) { - LOG.error(accessPolicyPath + " not found. Check OK"); - } - LOG.info("Is the section {} accessible? {}", sectionDocumentJSON, isAccessible); - return isAccessible; - } - - /** - * Pretty print JSON. - * - * @param jsonString the json string - * @return the string - */ - private String prettyPrintJSON(String jsonString) { - - Gson gson = new GsonBuilder().setPrettyPrinting().create(); - JsonObject jsonObject = new JsonParser().parse(jsonString).getAsJsonObject(); - return gson.toJson(jsonObject); - } - - /** - * Sanitize document value. - * - * @param toDoc the to doc - * @param fieldLabel the field label - * @param theObjectFieldValue the the object field value - * @return the document - */ - private Document sanitizeDocumentValue(Document toDoc, String fieldLabel, Object theObjectFieldValue) { - - if (theObjectFieldValue != null) { - if (theObjectFieldValue instanceof String) { - String toString = (String) theObjectFieldValue; - if (toString != null && !toString.isEmpty()) { - toDoc.append(fieldLabel, theObjectFieldValue); - } else { - LOG.debug("Skipping String field " + fieldLabel + " its value is null or empty"); - } - - } else if (theObjectFieldValue instanceof ArrayList) { - ArrayList toArrayList = (ArrayList) theObjectFieldValue; - if (toArrayList != null && !toArrayList.isEmpty()) { - toDoc.append(fieldLabel, theObjectFieldValue); - } else { - LOG.debug("Skipping ArrayList field " + fieldLabel + " its value is null or empty"); - } - } else { - toDoc.append(fieldLabel, theObjectFieldValue); - } - } else { - LOG.debug("Skipping field " + fieldLabel + " its value is null or empty"); - } - - return toDoc; - } - } diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/Geoportal_JSON_Mapper.java b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/Geoportal_JSON_Mapper.java new file mode 100644 index 0000000..f25bf07 --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/Geoportal_JSON_Mapper.java @@ -0,0 +1,503 @@ +package org.gcube.portlets.user.geoportaldataviewer.server; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; + +import org.bson.Document; +import org.gcube.application.geoportal.client.utils.Serialization; +import org.gcube.application.geoportalcommon.ConvertToDataValueObjectModel; +import org.gcube.application.geoportalcommon.geoportal.access.GeportalCheckAccessPolicy; +import org.gcube.application.geoportalcommon.geoportal.serdes.Payload; +import org.gcube.application.geoportalcommon.shared.geoportal.config.FilePathDV; +import org.gcube.application.geoportalcommon.shared.geoportal.config.GcubeProfileDV; +import org.gcube.application.geoportalcommon.shared.geoportal.materialization.GCubeSDIViewerLayerDV; +import org.gcube.application.geoportalcommon.shared.geoportal.materialization.innerobject.BBOXDV; +import org.gcube.application.geoportalcommon.shared.geoportal.materialization.innerobject.FilesetDV; +import org.gcube.application.geoportalcommon.shared.geoportal.materialization.innerobject.PayloadDV; +import org.gcube.application.geoportalcommon.shared.geoportal.project.ProjectDV; +import org.gcube.application.geoportalcommon.shared.geoportal.ucd.GEOPORTAL_CONFIGURATION_TYPE; +import org.gcube.application.geoportalcommon.shared.geoportal.view.ProjectView; +import org.gcube.application.geoportalcommon.shared.geoportal.view.SectionView; +import org.gcube.application.geoportalcommon.shared.geoportal.view.SubDocumentView; +import org.gcube.portlets.user.geoportaldataviewer.server.GeoportalDataViewerServiceImpl.ImageDetector; +import org.gcube.portlets.widgets.mpformbuilder.shared.metadata.MetaDataProfileBean; +import org.gcube.portlets.widgets.mpformbuilder.shared.metadata.MetadataFieldWrapper; +import org.json.JSONArray; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.jayway.jsonpath.JsonPath; +import com.jayway.jsonpath.spi.json.JsonOrgJsonProvider; + +public class Geoportal_JSON_Mapper { + + private static final Logger LOG = LoggerFactory.getLogger(Geoportal_JSON_Mapper.class); + + public static final String FILESET = "fileset"; + + public static final String _OGC_LINKS_WMS = "_ogcLinks.wms"; + + public static final String _BBOX = "_bbox"; + + public static final String _TYPE = "_type"; + + public static final String JSON_$_POINTER = "$"; + + public static ProjectView loadProjectView(ProjectDV theProjectDV, String scope, String username) throws Exception { + + String theWholeProjectAsJSON = theProjectDV.getTheDocument().getDocumentAsJSON(); + + LOG.debug("theProjectDV as JSON: " + theWholeProjectAsJSON); + LOG.debug("theProjectDV as MAP: " + theProjectDV.getTheDocument().getDocumentAsMap()); + + ProjectView projectView = new ProjectView(); + projectView.setTheProjectDV(theProjectDV); + + LinkedHashMap> linkedMap_UCDId_gCubeProfiles = GcubeProfilesPerUCDIdCache + .get(scope); + + // NO UCD defined, applying default + if (linkedMap_UCDId_gCubeProfiles.size() == 0) { + LOG.warn("No " + GEOPORTAL_CONFIGURATION_TYPE.gcube_profiles + " found in the UCD"); + LOG.info("Applying default business logic to display the project"); + SectionView sectionView = new SectionView(); + sectionView.setSectionTitle("Document"); + SubDocumentView subDocumentView = new SubDocumentView(); + + Document sectionDoc = Document.parse(theProjectDV.getTheDocument().getDocumentAsJSON()); + boolean isAccessibleSection = isAccessibleSectionAccordingToPolicy(sectionDoc, JSON_$_POINTER, username); + + // If is accessible + if (isAccessibleSection) { + + // Creating one Project with one SectionView and SubDocumentView + String wholeSectionDoc = sectionDoc.toJson(); + subDocumentView.setMetadataAsJSON(wholeSectionDoc); + + List listFiles = new ArrayList(); + List listImages = new ArrayList(); + List listLayers = new ArrayList(); + + // Reading Fileset _payloads + String filesetJSONPath = String.format("%s.%s", JSON_$_POINTER, FILESET); + List listPayloads = readPayloadsForFileset(filesetJSONPath, wholeSectionDoc); + FilesetDV filesetDV = new FilesetDV(); + filesetDV.setName(FILESET); + for (Payload payload : listPayloads) { + PayloadDV payloadDV = ConvertToDataValueObjectModel.toPayloadDV(payload); + filesetDV.addPayloadDV(payloadDV); + boolean isImage = ImageDetector.isImage(payload.getMimetype()); + + if (isImage) { + listImages.add(filesetDV); + } else { + listFiles.add(filesetDV); + } + } + + // Reading Fileset _materializations + listLayers = readGcubeSDILayersForFileset(filesetJSONPath, wholeSectionDoc); + + // TODO LAYERS + subDocumentView.setListImages(listImages); + subDocumentView.setListFiles(listFiles); + subDocumentView.setListLayers(listLayers); + sectionView.addSubDocument(subDocumentView); + projectView.addSectionView(sectionView); + + } + + } + + List listProfilesBean = linkedMap_UCDId_gCubeProfiles + .get(theProjectDV.getProfileID()); + + com.jayway.jsonpath.Configuration configuration = com.jayway.jsonpath.Configuration.builder() + .jsonProvider(new JsonOrgJsonProvider()).build(); + + // Reading the Project according to list of Profile defined in the UCD + for (GcubeProfilesMetadataForUCD gcubeProfileMetaForUCD : listProfilesBean) { + + GcubeProfileDV gcubeProfileDV = gcubeProfileMetaForUCD.getGcubeProfile(); + SectionView sectionView = new SectionView(); + sectionView.setSectionTitle(gcubeProfileDV.getSectionTitle()); + LOG.debug("\n\nThe profile is: " + gcubeProfileDV); + // Building JSON/section full PATH and section name + String sectionJSONPath = ""; + String parentPathFromProfile = gcubeProfileDV.getParentName() == null ? "" : gcubeProfileDV.getParentName(); + String theSectionName = gcubeProfileDV.getSectionName(); + + if (theSectionName.compareTo(JSON_$_POINTER) == 0 || theSectionName.compareTo(JSON_$_POINTER + ".") == 0) { + sectionJSONPath = JSON_$_POINTER; + theSectionName = ""; + } else { + sectionJSONPath = String.format("%s%s", + parentPathFromProfile.endsWith(".") ? parentPathFromProfile : parentPathFromProfile + ".", + theSectionName); + } + + LOG.debug("The sectionJSONPath is: " + sectionJSONPath); + + JsonPath theSectionJsonPath = null; + Object data = null; + try { + theSectionJsonPath = JsonPath.compile(sectionJSONPath); + data = theSectionJsonPath.read(theWholeProjectAsJSON, configuration); + } catch (Exception e) { + LOG.warn("Error on searching the section " + sectionJSONPath + " in the JSON Project: " + + theWholeProjectAsJSON); + continue; + } + + LOG.debug("Data is instace of: " + data.getClass()); + LOG.debug("data to string: " + data.toString()); + + // Splitting the General Document in bson.Document according to list of + // GcubeProfiles + List listBSONDocument = new ArrayList(); + if (data instanceof org.json.JSONObject) { + String jsonString = data.toString(); + LOG.debug("the JSON to string: " + jsonString); + Document sectionDoc = Document.parse(jsonString); + boolean isAccessibleSection = isAccessibleSectionAccordingToPolicy(sectionDoc, sectionJSONPath, + username); + if (isAccessibleSection) { + listBSONDocument.add(sectionDoc); + } + + } else if (data instanceof org.json.JSONArray) { + org.json.JSONArray dataArray = (org.json.JSONArray) data; + for (int i = 0; i < dataArray.length(); i++) { + String jsonString = dataArray.get(i).toString(); + LOG.debug("the array " + i + " JSON to string: " + jsonString); + Document sectionDoc = Document.parse(jsonString); + boolean isAccessibleSection = isAccessibleSectionAccordingToPolicy(sectionDoc, sectionJSONPath, + username); + if (isAccessibleSection) { + listBSONDocument.add(sectionDoc); + } + + } + } + + LOG.debug("Result for " + gcubeProfileDV.getSectionName() + " is: " + listBSONDocument); + List theProfileBeans = gcubeProfileMetaForUCD.getListMetadataProfileBean(); + MetaDataProfileBean theProfileBean = theProfileBeans.get(0); + + // For each bson.Document creating the SubDocumentView + for (int i = 0; i < listBSONDocument.size(); i++) { + Document fromSectionDoc = listBSONDocument.get(i); + SubDocumentView subDocumentView = new SubDocumentView(); + Document toSectionDoc = new Document(); + // Filling the SubDocumentView metadata with the metadataField.getFieldName() + // read from the Profile + for (MetadataFieldWrapper metadataField : theProfileBean.getMetadataFields()) { + + String theFieldName = metadataField.getFieldId() != null ? metadataField.getFieldId() + : metadataField.getFieldName(); + LOG.debug("reading theFieldName: " + theFieldName); + Object theOBJFieldValue = fromSectionDoc.get(theFieldName); + + // NB: Using ALWAYS THE metadataField.getFieldName() as LABEL + toSectionDoc = sanitizeDocumentValue(toSectionDoc, metadataField.getFieldName(), theOBJFieldValue); + + } + String subToJSON = toSectionDoc.toJson(); + LOG.debug("theSubSetionDoc is: " + subToJSON); + subDocumentView.setMetadataAsJSON(toSectionDoc.toJson()); + + // Reading filePaths + List filePaths = gcubeProfileDV.getFilePaths(); + + // READING fileset* field ACCORDING TO filePaths OF THE 'gcubeProfiles' CONFIG + if (filePaths != null) { + String fromSectionDocJSON = fromSectionDoc.toJson(); + List listFiles = new ArrayList(); + List listImages = new ArrayList(); + List listLayers = new ArrayList(); + for (FilePathDV filePath : filePaths) { + + // Reading Fileset _payloads + String filesetJSONPath = String.format("%s.%s", JSON_$_POINTER, filePath.getFieldName()); + List listPayloads = readPayloadsForFileset(filesetJSONPath, fromSectionDocJSON); + FilesetDV filesetDV = new FilesetDV(); + filesetDV.setName(filePath.getGcubeProfileFieldName()); + for (Payload payload : listPayloads) { + PayloadDV payloadDV = ConvertToDataValueObjectModel.toPayloadDV(payload); + filesetDV.addPayloadDV(payloadDV); + boolean isImage = ImageDetector.isImage(payload.getMimetype()); + + if (isImage) { + listImages.add(filesetDV); + } else { + listFiles.add(filesetDV); + } + } + + // Reading Fileset _materializations + listLayers = readGcubeSDILayersForFileset(filesetJSONPath, fromSectionDocJSON); + + } + subDocumentView.setListFiles(listFiles); + subDocumentView.setListImages(listImages); + } + + sectionView.addSubDocument(subDocumentView); + + } + + projectView.addSectionView(sectionView); + } + + return projectView; + } + + /** + * Read payloads for fileset. + * + * @param filesetJSONPath the fileset JSON path + * @param sectionJSONDocument the section JSON document + * @return the list + */ + private static List readPayloadsForFileset(String filesetJSONPath, String sectionJSONDocument) { + LOG.debug("readPayloadsForFileset called"); + + List listPayloads = new ArrayList(); + String _payloadsJSONPath = String.format("%s.%s", filesetJSONPath, "_payloads"); + try { + com.jayway.jsonpath.Configuration configuration = com.jayway.jsonpath.Configuration.builder() + .jsonProvider(new JsonOrgJsonProvider()).build(); + + LOG.info("Reading sectionPath at {} into section document {}", _payloadsJSONPath, sectionJSONDocument); + JsonPath theSectionPolycJsonPath = JsonPath.compile(_payloadsJSONPath); + Object _payloads = theSectionPolycJsonPath.read(sectionJSONDocument, configuration).toString(); + + if (_payloads instanceof String) { + String toStringPayloads = (String) _payloads; + LOG.trace("The _payloads is a String {}", toStringPayloads); + JSONArray jsonArray = new JSONArray(toStringPayloads); + for (int i = 0; i < jsonArray.length(); i++) { + Payload payloadDV = Serialization.read(jsonArray.getJSONObject(i).toString(), Payload.class); + listPayloads.add(payloadDV); + } + } + + LOG.info("returning list of payloads {}", listPayloads); + } catch (Exception e) { + LOG.warn("Error on reading the JSON Path " + _payloadsJSONPath + " in the doc " + sectionJSONDocument, e); + } + + return listPayloads; + + } + + private static List readGcubeSDILayersForFileset(String materializationJSONPath, + String sectionJSONDocument) { + LOG.debug("readGcubeSDILayersForFileset called"); + + List listSDILayers = new ArrayList(); + String _materializationsJSONPath = String.format("%s.%s", materializationJSONPath, "_materializations"); + try { + com.jayway.jsonpath.Configuration configurationJSONSmart = com.jayway.jsonpath.Configuration.builder() + .jsonProvider(new JsonOrgJsonProvider()).build(); + + LOG.info("Reading sectionPath at {} into section document {}", _materializationsJSONPath, + sectionJSONDocument); + JsonPath theSectionPolycJsonPath = JsonPath.compile(_materializationsJSONPath); + Object _materializations = theSectionPolycJsonPath.read(sectionJSONDocument, configurationJSONSmart) + .toString(); + + if (_materializations instanceof String) { + String toStringMater = (String) _materializations; + LOG.trace("The _materializations is a String {}", toStringMater); + JSONArray jsonArray = new JSONArray(toStringMater); + for (int i = 0; i < jsonArray.length(); i++) { + // TODO THIS PART SHOULD BE REVISITED/OPTIMIZED + + GCubeSDIViewerLayerDV gsdiLayer = new GCubeSDIViewerLayerDV(); + JSONObject thJsonObject = jsonArray.getJSONObject(i); + try { + gsdiLayer.setType(thJsonObject.getString(_TYPE)); + } catch (Exception e) { + LOG.warn("No " + _TYPE + " found", e); + } + String toSerializeJSONOBJ = jsonArray.getJSONObject(i).toString(); + String jsonPath = null; + try { + jsonPath = String.format("%s.%s", JSON_$_POINTER, _BBOX); + BBOXDV bbox = JsonPath.using(configurationJSONSmart).parse(toSerializeJSONOBJ).read(jsonPath, + BBOXDV.class); + gsdiLayer.setBbox(bbox); + LOG.trace("bbox " + i + " is: " + bbox); + } catch (Exception e) { + LOG.warn(jsonPath + " error: ", e); + } + try { + jsonPath = String.format("%s.%s", JSON_$_POINTER, _OGC_LINKS_WMS); + String jsonString = JsonPath.using(configurationJSONSmart).parse(toSerializeJSONOBJ) + .read(jsonPath).toString(); + Gson gson = new Gson(); + HashMap map = gson.fromJson(jsonString, HashMap.class); + gsdiLayer.setOgcLinks(map); + LOG.trace("ogcLinks " + i + " is: " + map); + } catch (Exception e) { + LOG.warn(jsonPath + " error: ", e); + } + + GCubeSDIViewerLayerDV sdiLayerDV = Serialization.read(toSerializeJSONOBJ, + GCubeSDIViewerLayerDV.class); + LOG.trace("sdiLayerDV " + i + " is: " + sdiLayerDV); + LOG.trace("gsdiLayer " + i + " is: " + gsdiLayer); + listSDILayers.add(gsdiLayer); + } + } + + LOG.info("returning list of payloads {}", listSDILayers); + } catch (Exception e) { + LOG.warn("Error on reading the JSON Path " + _materializationsJSONPath + " in the doc " + + sectionJSONDocument, e); + } + + return listSDILayers; + + } + + /** + * Checks if is accessible section according to policy. + * + * @param section the section + * @param sectionJSONPath the section JSON path + * @param myLogin the my login + * @return true, if is accessible section according to policy + */ + private static boolean isAccessibleSectionAccordingToPolicy(Document section, String sectionJSONPath, + String myLogin) { + LOG.debug("isAccessibleSectionAccordingToPolicy called"); + boolean isAccessible = true; + + // Skipping the root, going to check the access_policy of subsections + if (sectionJSONPath.compareTo(JSON_$_POINTER) != 0) { + isAccessible = checkAccessPolicy(section.toJson(), myLogin); + } + + return isAccessible; + } + + /** + * Check access policy. + * + * @param sectionDocumentJSON the section document JSON + * @param myLogin the my login + * @return true, if successful + */ + private static boolean checkAccessPolicy(String sectionDocumentJSON, String myLogin) { + LOG.debug("checkAccessPolicy called"); + // CHECKING THE POLICY + String accessPolicyPath = JSON_$_POINTER + "._access._policy"; + boolean isAccessible = true; + try { + com.jayway.jsonpath.Configuration configuration = com.jayway.jsonpath.Configuration.builder() + .jsonProvider(new JsonOrgJsonProvider()).build(); + + LOG.info("Reading access policy at {} into section document {}", accessPolicyPath, sectionDocumentJSON); + JsonPath theSectionPolycJsonPath = JsonPath.compile(accessPolicyPath); + String _policy = theSectionPolycJsonPath.read(sectionDocumentJSON, configuration).toString(); + LOG.info("The section {} has policy {}", accessPolicyPath, _policy); + isAccessible = GeportalCheckAccessPolicy.isAccessible(_policy, myLogin); + } catch (Exception e) { + LOG.error(accessPolicyPath + " not found. Check OK"); + } + LOG.info("Is the section {} accessible? {}", sectionDocumentJSON, isAccessible); + return isAccessible; + } + + /** + * Sanitize document value. + * + * @param toDoc the to doc + * @param fieldLabel the field label + * @param theObjectFieldValue the the object field value + * @return the document + */ + private static Document sanitizeDocumentValue(Document toDoc, String fieldLabel, Object theObjectFieldValue) { + + if (theObjectFieldValue != null) { + if (theObjectFieldValue instanceof String) { + String toString = (String) theObjectFieldValue; + if (toString != null && !toString.isEmpty()) { + toDoc.append(fieldLabel, theObjectFieldValue); + } else { + LOG.debug("Skipping String field " + fieldLabel + " its value is null or empty"); + } + + } else if (theObjectFieldValue instanceof ArrayList) { + ArrayList toArrayList = (ArrayList) theObjectFieldValue; + if (toArrayList != null && !toArrayList.isEmpty()) { + toDoc.append(fieldLabel, theObjectFieldValue); + } else { + LOG.debug("Skipping ArrayList field " + fieldLabel + " its value is null or empty"); + } + } else { + toDoc.append(fieldLabel, theObjectFieldValue); + } + } else { + LOG.debug("Skipping field " + fieldLabel + " its value is null or empty"); + } + + return toDoc; + } + + /** + * Pretty print JSON. + * + * @param jsonString the json string + * @return the string + */ + private static String prettyPrintJSON(String jsonString) { + + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + JsonObject jsonObject = new JsonParser().parse(jsonString).getAsJsonObject(); + return gson.toJson(jsonObject); + } + + public static void prettyPrintProjectView(ProjectView projectView) { + + for (SectionView section : projectView.getListSections()) { + System.out.println("\n\n###### Section Title: " + section.getSectionTitle() + " ######"); + int i = 1; + for (SubDocumentView subDocument : section.getListSubDocuments()) { + System.out.println("## " + SubDocumentView.class.getSimpleName() + " n." + i); + System.out.println("***** Metadata"); + System.out.println(prettyPrintJSON(subDocument.getMetadataAsJSON())); + System.out.println("***** Files"); + if (subDocument.getListFiles() != null) { + for (FilesetDV filesetDV : subDocument.getListFiles()) { + System.out.println("******* File Fileset name: " + filesetDV.getName()); + for (PayloadDV payload : filesetDV.getListPayload()) { + System.out.println("********* Payload: " + payload); + } + } + } + System.out.println("***** Images"); + if (subDocument.getListImages() != null) { + for (FilesetDV filesetDV : subDocument.getListImages()) { + System.out.println("******* Image Fileset name: " + filesetDV.getName()); + for (PayloadDV payload : filesetDV.getListPayload()) { + System.out.println("********* Payload: " + payload); + } + } + } + i++; + } + + } + } + +} diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/util/DVConversion.java b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/util/DVConversion.java deleted file mode 100644 index e9af34e..0000000 --- a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/util/DVConversion.java +++ /dev/null @@ -1,68 +0,0 @@ -//package org.gcube.portlets.user.geoportaldataviewer.server.util; -// -//import org.gcube.application.geoportal.client.utils.Serialization; -//import org.gcube.application.geoportal.common.model.configuration.Index; -//import org.gcube.portlets.user.geoportaldataviewer.server.faults.InvalidObjectException; -//import org.gcube.portlets.user.geoportaldataviewer.shared.gis.IndexLayer; -// -//public class DVConversion { -// -// -// -// /** -// * -// * { -// "_type": "GIS-CENTROIDS", -// "layer": { -// "_type": "gcube-sdi-layer", -// "_platformInfo": [ -// { -// "_type": "Geoserver", -// "workspace": "profiledconcessioni_devvre", -// "storeName": "profiledconcessioni_devvre_centroids", -// "_host": "geoserver-218.dev.d4science.org" -// } -// ], -// "_bbox": { -// "_maxX": 180.0, -// "_minX": -180.0, -// "_maxY": 90.0, -// "_minY": -90.0 -// }, -// "_ogcLinks": { -// "wms": { -// "wms": "https://geoserver-218.dev.d4science.org/geoserver/profiledconcessioni_devvre/wms?service=WMS&version=1.1.0&request=GetMap&layers=profiledconcessioni_devvre:null&styles=&bbox=-180.000000,-90.000000,180.000000,90.000000&srs=EPSG:4326&format=application/openlayers&width=400&height=400" -// } -// } -// }, -// "indexName": "profiledconcessioni_devvre_centroids", -// "records": 4, -// "crossReferencedLayers": {}, -// "flag": "public" -// } -// * @throws InvalidObjectException -// * -// */ -// -// public static IndexLayer convert(Index toConvert) throws InvalidObjectException { -// IndexLayer toReturn = null; -// -// -// switch(toConvert.getType()) { -// case "GIS-CENTROIDS" : { -// toReturn = Serialization.convert(toConvert,IndexLayer.class); -// // toReturn.setLayer(Serialization.read(toConvert.get("layer"), GCubeSDILayer.class)); -// // toReturn.setFlag(toConvert.getString("flag")); -// // toReturn.setIndexName(toConvert.getString()); -// break; -// -// } -// default:{ -// throw new InvalidObjectException("Unable to convert Index. Unknown type : "+toConvert.getType()); -// } -// } -// -// return toReturn; -// } -// -//} diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/shared/GCubeCollection.java b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/shared/GCubeCollection.java index ba65891..cb8014f 100644 --- a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/shared/GCubeCollection.java +++ b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/shared/GCubeCollection.java @@ -4,7 +4,7 @@ package org.gcube.portlets.user.geoportaldataviewer.shared; import java.io.Serializable; import java.util.List; -import org.gcube.application.geoportalcommon.shared.geoportal.materialization.IndexLayer; +import org.gcube.application.geoportalcommon.shared.geoportal.materialization.IndexLayerDV; import org.gcube.application.geoportalcommon.shared.geoportal.ucd.UseCaseDescriptorDV; @@ -16,7 +16,7 @@ public class GCubeCollection implements Serializable { private static final long serialVersionUID = -3915546050671671465L; private UseCaseDescriptorDV ucd; - private List indexes; + private List indexes; public GCubeCollection() { } @@ -25,7 +25,7 @@ public class GCubeCollection implements Serializable { return ucd; } - public List getIndexes() { + public List getIndexes() { return indexes; } @@ -34,7 +34,7 @@ public class GCubeCollection implements Serializable { this.ucd = ucd; } - public void setIndexes(List indexes) { + public void setIndexes(List indexes) { this.indexes = indexes; } diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/shared/gis/LayerItem.java b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/shared/gis/LayerItem.java index 2b05b81..243060d 100644 --- a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/shared/gis/LayerItem.java +++ b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/shared/gis/LayerItem.java @@ -993,7 +993,7 @@ public class LayerItem implements Serializable, Cloneable { */ - // TODO Constructor from IndexLayer, GCUBESDILAeryDV or direct info + // TODO Constructor from IndexLayerDV, GCUBESDILAeryDV or direct info private LayerItem toLayerItem(LayerType layerType, String layerTitle, String layerName, String layerURL, String mapServerHost, boolean isExternal, boolean isBase, boolean displayInLayerSwitcher, ArrayList styles, String wmsLink, boolean onTop, HashMap wmsNotStandardParams, diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/shared/gis/LayerObject.java b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/shared/gis/LayerObject.java index 0b009d5..e270cf3 100644 --- a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/shared/gis/LayerObject.java +++ b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/shared/gis/LayerObject.java @@ -2,8 +2,8 @@ package org.gcube.portlets.user.geoportaldataviewer.shared.gis; import java.io.Serializable; -import org.gcube.application.geoportalcommon.shared.geoportal.materialization.GCubeSDILayerDV; -import org.gcube.application.geoportalcommon.shared.geoportal.materialization.IndexLayer; +import org.gcube.application.geoportalcommon.shared.geoportal.materialization.GCubeSDILayer; +import org.gcube.application.geoportalcommon.shared.geoportal.materialization.IndexLayerDV; /** @@ -22,9 +22,9 @@ public class LayerObject implements Serializable { private LayerObjectType type; - private IndexLayer indexLayer; // expected for INDEX_LAYER type + private IndexLayerDV indexLayer; // expected for INDEX_LAYER type private String ucid; // expected for collection layers - private GCubeSDILayerDV projectLayer; // expected for PROJECT_LAYER + private GCubeSDILayer projectLayer; // expected for PROJECT_LAYER private String projectId; // expected for PROJECT_LAYER private LayerItem layerItem; @@ -39,13 +39,13 @@ public class LayerObject implements Serializable { } - public LayerObject(String ucid, IndexLayer indexLayer, LayerItem item) { + public LayerObject(String ucid, IndexLayerDV indexLayer, LayerItem item) { this(LayerObjectType.INDEX_LAYER,item); this.ucid=ucid; this.indexLayer=indexLayer; } - public LayerObject(String ucid, String projectId, GCubeSDILayerDV projectLayer, LayerItem item) { + public LayerObject(String ucid, String projectId, GCubeSDILayer projectLayer, LayerItem item) { this(LayerObjectType.PROJECT_LAYER,item); this.ucid=ucid; this.projectId=projectId; @@ -63,12 +63,12 @@ public class LayerObject implements Serializable { } - public IndexLayer getIndexLayer() { + public IndexLayerDV getIndexLayer() { return indexLayer; } - public void setIndexLayer(IndexLayer indexLayer) { + public void setIndexLayer(IndexLayerDV indexLayer) { this.indexLayer = indexLayer; } @@ -83,12 +83,12 @@ public class LayerObject implements Serializable { } - public GCubeSDILayerDV getProjectLayer() { + public GCubeSDILayer getProjectLayer() { return projectLayer; } - public void setProjectLayer(GCubeSDILayerDV projectLayer) { + public void setProjectLayer(GCubeSDILayer projectLayer) { this.projectLayer = projectLayer; } diff --git a/src/test/java/org/gcube/portlets/user/geoportaldataviewer/GeoportalViewer_Tests.java b/src/test/java/org/gcube/portlets/user/geoportaldataviewer/GeoportalViewer_Tests.java index 5543bb5..b083fe0 100644 --- a/src/test/java/org/gcube/portlets/user/geoportaldataviewer/GeoportalViewer_Tests.java +++ b/src/test/java/org/gcube/portlets/user/geoportaldataviewer/GeoportalViewer_Tests.java @@ -11,6 +11,7 @@ import org.gcube.application.geoportalcommon.shared.geoportal.view.ProjectView; import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.scope.api.ScopeProvider; import org.gcube.portlets.user.geoportaldataviewer.server.GeoportalDataViewerServiceImpl; +import org.gcube.portlets.user.geoportaldataviewer.server.Geoportal_JSON_Mapper; import org.junit.Before; import org.junit.Test; @@ -31,7 +32,7 @@ public class GeoportalViewer_Tests { // private static String TOKEN = ""; //preVRE private static String CONTEXT = "/gcube/devsec/devVRE"; - private static String TOKEN = ""; // devVRE + private static String TOKEN = "c41a00c0-7897-48d2-a67a-05190d6ce5e6-98187548"; // devVRE private static String PROFILE_ID = "profiledConcessioni"; private static String PROJECT_ID = "6311d408900dde90e44d9265"; @@ -53,7 +54,7 @@ public class GeoportalViewer_Tests { } @Test - public void getPreviewObjectForID() { + public void getProjectViewForID() { try { ScopeProvider.instance.set(CONTEXT); @@ -64,9 +65,7 @@ public class GeoportalViewer_Tests { ScopeProvider.instance.set(CONTEXT); SecurityTokenProvider.instance.set(TOKEN); - GeoportalDataViewerServiceImpl gdvsi = new GeoportalDataViewerServiceImpl(); - - ProjectView projectView = gdvsi.loadProjectView(projectDV, CONTEXT, USERNAME); + ProjectView projectView = Geoportal_JSON_Mapper.loadProjectView(projectDV, CONTEXT, USERNAME); System.out.println(projectView); } catch (Exception e) { // TODO Auto-generated catch block