From a0f3bc915cc2d17c4a597ae9e3d23fc6aa2dd345 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Fri, 5 Aug 2022 12:44:33 +0200 Subject: [PATCH] Project publication with fileset implemented --- .../server/FileSetDataObject.java | 49 ++++++ .../server/FormDataObjectToJSON.java | 25 ++- .../server/GeoportalDataEntryServiceImpl.java | 163 +++++++++++------- .../server/MongoServiceUtil.java | 6 +- 4 files changed, 165 insertions(+), 78 deletions(-) create mode 100644 src/main/java/org/gcube/portlets/user/geoportaldataentry/server/FileSetDataObject.java diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataentry/server/FileSetDataObject.java b/src/main/java/org/gcube/portlets/user/geoportaldataentry/server/FileSetDataObject.java new file mode 100644 index 0000000..45dcfad --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/geoportaldataentry/server/FileSetDataObject.java @@ -0,0 +1,49 @@ +package org.gcube.portlets.user.geoportaldataentry.server; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +import org.gcube.application.geoportalcommon.shared.geoportal.config.FilePathDV; + +public class FileSetDataObject { + + private FilePathDV filePathDV; + private List files = new ArrayList(); + + public FileSetDataObject() { + } + + public void setFilePathDV(FilePathDV filePath) { + this.filePathDV = filePath; + + } + + public void addFile(File file) { + files.add(file); + } + + public File[] getFileset() { + File[] arr = new File[files.size()]; + arr = files.toArray(arr); + return arr; + } + + public FilePathDV getFilePathDV() { + return filePathDV; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("FileSetDataObject [filePathDV.getFieldName()="); + builder.append(filePathDV.getFieldName()); + builder.append(", files size="); + builder.append(files.size()); + builder.append("]"); + return builder.toString(); + } + + + +} diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataentry/server/FormDataObjectToJSON.java b/src/main/java/org/gcube/portlets/user/geoportaldataentry/server/FormDataObjectToJSON.java index ab160a1..54accc5 100644 --- a/src/main/java/org/gcube/portlets/user/geoportaldataentry/server/FormDataObjectToJSON.java +++ b/src/main/java/org/gcube/portlets/user/geoportaldataentry/server/FormDataObjectToJSON.java @@ -2,7 +2,6 @@ package org.gcube.portlets.user.geoportaldataentry.server; import java.lang.reflect.Field; import java.util.ArrayList; -import java.util.Arrays; import java.util.LinkedHashMap; import java.util.List; @@ -29,7 +28,7 @@ import com.jayway.jsonpath.spi.json.JsonOrgJsonProvider; */ public class FormDataObjectToJSON { - private static final String JSON_$_POINTER = "$"; + public static final String JSON_$_POINTER = "$"; private static final Logger LOG = LoggerFactory.getLogger(FormDataObjectToJSON.class); /** @@ -63,23 +62,23 @@ public class FormDataObjectToJSON { LOG.debug("The profile is: " + profile); // Building JSON/section full PATH and section name - String fullJSONPath = ""; - String parentFullName = profile.getParentName() == null ? "" : profile.getParentName(); + String sectionJSONPath = ""; + String parentPathFromProfile = profile.getParentName() == null ? "" : profile.getParentName(); String theSectionName = profile.getSectionName(); if (theSectionName.compareTo(JSON_$_POINTER) == 0 || theSectionName.compareTo(JSON_$_POINTER + ".") == 0) { - fullJSONPath = JSON_$_POINTER; + sectionJSONPath = JSON_$_POINTER; theSectionName = ""; } else { - fullJSONPath = String.format("%s%s", - parentFullName.endsWith(".") ? parentFullName : parentFullName + ".", theSectionName); + sectionJSONPath = String.format("%s%s", + parentPathFromProfile.endsWith(".") ? parentPathFromProfile : parentPathFromProfile + ".", theSectionName); } - LOG.debug("The json path to build: " + fullJSONPath); + LOG.debug("The sectionJSONPath is: " + sectionJSONPath); LOG.info("Current document is: " + theRootDocument); // Building Parent PATH - String parentPath = fullJSONPath.compareTo(JSON_$_POINTER) == 0 ? JSON_$_POINTER - : fullJSONPath.substring(0, fullJSONPath.lastIndexOf(".")); + String parentPath = sectionJSONPath.compareTo(JSON_$_POINTER) == 0 ? JSON_$_POINTER + : sectionJSONPath.substring(0, sectionJSONPath.lastIndexOf(".")); JsonPath parentJSONPath = JsonPath.compile(parentPath); LOG.info("Putting into parentJSONPath: " + parentJSONPath); @@ -90,10 +89,10 @@ public class FormDataObjectToJSON { if (profile.getMaxOccurs() == 0 || profile.getMaxOccurs() > 1) { LOG.debug("maxOccurs is NOT 1"); // Must be an array - boolean pathExists = pathExists(theRootDocument, fullJSONPath + "[*]"); - LOG.debug(fullJSONPath+ "exists? "+pathExists); + boolean pathExists = pathExists(theRootDocument, sectionJSONPath + "[*]"); + LOG.debug(sectionJSONPath+ "exists? "+pathExists); if (pathExists) { - theRootDocument = JsonPath.parse(theRootDocument, configuration).add(fullJSONPath, jsonObject) + theRootDocument = JsonPath.parse(theRootDocument, configuration).add(sectionJSONPath, jsonObject) .json(); } else { // Adding as array of object diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataentry/server/GeoportalDataEntryServiceImpl.java b/src/main/java/org/gcube/portlets/user/geoportaldataentry/server/GeoportalDataEntryServiceImpl.java index 64fb447..4edf74f 100644 --- a/src/main/java/org/gcube/portlets/user/geoportaldataentry/server/GeoportalDataEntryServiceImpl.java +++ b/src/main/java/org/gcube/portlets/user/geoportaldataentry/server/GeoportalDataEntryServiceImpl.java @@ -42,7 +42,6 @@ import org.gcube.application.geoportalcommon.shared.products.content.WorkspaceCo import org.gcube.application.geoportalcommon.shared.products.paths.FileSetPathsDV; import org.gcube.common.portal.PortalContext; import org.gcube.portlets.user.geoportaldataentry.client.ConstantsGeoPortalDataEntryApp.RECORD_TYPE; -import org.gcube.portlets.user.geoportaldataentry.server.FormDataObjectToJSON.JSONObjecOrdered; import org.gcube.portlets.user.geoportaldataentry.client.GeoportalDataEntryService; import org.gcube.portlets.user.geoportaldataentry.client.ProjectFormCard; import org.gcube.portlets.user.geoportaldataentry.shared.CommitReport; @@ -59,8 +58,6 @@ import org.gcube.vomanagement.usermanagement.exception.UserRetrievalFault; import org.gcube.vomanagement.usermanagement.impl.LiferayRoleManager; import org.gcube.vomanagement.usermanagement.model.GCubeRole; import org.gcube.vomanagement.usermanagement.model.GCubeUser; -import org.json.JSONArray; -import org.json.JSONException; import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -148,81 +145,87 @@ public class GeoportalDataEntryServiceImpl extends RemoteServiceServlet implemen // Uploading files LOG.debug("Going to upload the files"); - uploadFileset(tree_Node); - /*for (Tree_Node geoNaFormDataObject : tree_Node.getChildren()) { - List listGDB = geoNaFormDataObject.getListGDB(); - GcubeProfileDV profile = geoNaFormDataObject.getGcubeProfileDV(); - - String parentPath = String.format("%s%s", profile.getParentName(), profile.getSectionName()); - - LOG.info("The profile is: " + profile); - for (GenericDatasetBean gdb : listGDB) { - List files = gdb.getFilesUploaded(); - - if (files.size() > 0) { - LOG.info("Uploading file/s in the parentPath: " + parentPath); - for (int i = 0; i < files.size(); i++) { - FileUploaded file = files.get(i); - String formFieldName = file.getFilePath().getFormFieldLabel(); - LOG.debug( - "Uploading file: " + file.getFileName() + ", from formFieldName: " + formFieldName); - FilePathDV filePath = retrieveFilePathForGcubeProfileFieldName(formFieldName, profile); - LOG.info("Found {} for the form fieldName {}", filePath, formFieldName); - if (filePath == null) { - String error = "It is not possible to register the file " + formFieldName - + ", missing configuration in the filePaths config of: " + profile; - throw new Exception(error); - } - - mongoService.registerFileSet(profileID, theProject, new File(file.getTempSystemPath()), - parentPath, filePath.getFieldName(), filePath.getFieldDefinition()); - - } - - } - - } - } - - LifecycleInformation lifecycleInfo = theProject.getLifecycleInformation(); - LifecycleInformationDV liDV = ConvertToDataValueObjectModel.toLifecycleInformationDV(lifecycleInfo); - return new CommitReport(theProject.getId(), liDV); - */ - - throw new Exception("Must be implemented!!!!"); + recursiveUploadFileset(mongoService, profileID, theProject, tree_Node, null); } catch (Exception e) { LOG.error("Error on uploading files: ", e); throw new Exception( "Error occurred on uploading files, try again or contact the support. Error: " + e.getMessage()); } + + try { + LifecycleInformation lifecycleInfo = theProject.getLifecycleInformation(); + LifecycleInformationDV liDV = ConvertToDataValueObjectModel.toLifecycleInformationDV(lifecycleInfo); + return new CommitReport(theProject.getId(), liDV); + } catch (Exception e) { + throw new Exception("Error occurred on loading LifecycleInformation for the project: " + theProject.getId() + + ". Error: " + e.getMessage()); + } } /** - * Upload fileset. + * Recursive upload fileset. * - * @param tree_Node the tree node + * @param mongoService the mongo service + * @param profileID the profile ID + * @param theProject the the project + * @param tree_Node the tree node + * @param sectionJSONPathIndexer the json path indexer * @throws Exception the exception */ - public void uploadFileset(Tree_Node tree_Node) throws Exception { + public void recursiveUploadFileset(MongoServiceUtil mongoService, String profileID, Project theProject, + Tree_Node tree_Node, Map sectionJSONPathIndexer) throws Exception { + LOG.debug("recursiveUploadFileset called [tree_Node: " + tree_Node + "], [jsonPathIndexer: " + + sectionJSONPathIndexer + "]"); - // Uploading files - LOG.debug("Going to upload the files"); - for (Tree_Node treeNodeChild_GNAFormDataObject : tree_Node.getChildren()) { - List listGDB = treeNodeChild_GNAFormDataObject.getData().getListGDB(); - GcubeProfileDV profile = treeNodeChild_GNAFormDataObject.getData().getGcubeProfileDV(); + if (tree_Node == null) + return; - String parentPath = profile.getParentName().endsWith(".") ? profile.getParentName() - : profile.getParentName() + "."; - String jsonPathExp = String.format("%s%s", parentPath, profile.getSectionName()); - LOG.debug("The json path to build: " + jsonPathExp); + if (sectionJSONPathIndexer == null) { + sectionJSONPathIndexer = new HashMap(); + } + + for (Tree_Node treeNodeChild_GNA_DO : tree_Node.getChildren()) { + LOG.debug("Going to upload the files of tree node: " + treeNodeChild_GNA_DO); + + // list GDB has always one element in the current implementation. One GDB per + // web-form/profile + List listGDB = treeNodeChild_GNA_DO.getData().getListGDB(); + GcubeProfileDV profile = treeNodeChild_GNA_DO.getData().getGcubeProfileDV(); + + // Building JSON/section full PATH and section name + String sectionJSONPath = ""; + String parentPathIntoProfile = profile.getParentName() == null ? "" : profile.getParentName(); + String theSectionName = profile.getSectionName(); + + if (theSectionName.compareTo(FormDataObjectToJSON.JSON_$_POINTER) == 0 + || theSectionName.compareTo(FormDataObjectToJSON.JSON_$_POINTER + ".") == 0) { + sectionJSONPath = FormDataObjectToJSON.JSON_$_POINTER; + theSectionName = ""; + } else { + sectionJSONPath = String.format("%s%s", + parentPathIntoProfile.endsWith(".") ? parentPathIntoProfile : parentPathIntoProfile + ".", + theSectionName); + } + + LOG.debug("The sectionJSONPath is: " + sectionJSONPath); + + Integer jpcV = sectionJSONPathIndexer.get(sectionJSONPath); + if (jpcV == null) { + jpcV = 0; + sectionJSONPathIndexer.put(sectionJSONPath, jpcV); + } else { + jpcV = jpcV + 1; + sectionJSONPathIndexer.put(sectionJSONPath, jpcV); + } + LOG.debug("sectionJSONPathIndexer is: " + sectionJSONPathIndexer); LOG.info("The profile is: " + profile); for (GenericDatasetBean gdb : listGDB) { + Map collectFilesetPerFieldDef = new HashMap(); List files = gdb.getFilesUploaded(); - if (files.size() > 0) { - LOG.info("Uploading file/s in the parentPath: " + jsonPathExp); + // Iterating on the files upload for the section for (int i = 0; i < files.size(); i++) { FileUploaded file = files.get(i); String formFieldName = file.getFilePath().getFormFieldLabel(); @@ -235,13 +238,49 @@ public class GeoportalDataEntryServiceImpl extends RemoteServiceServlet implemen throw new Exception(error); } -// mongoService.registerFileSet(profileID, theProject, new File(file.getTempSystemPath()), -// parentPath, filePath.getFieldName(), filePath.getFieldDefinition()); + // Collecting Fileset per Field Definition + FileSetDataObject collFieldDef = collectFilesetPerFieldDef.get(filePath.getFieldDefinition()); + if (collFieldDef == null) { + collFieldDef = new FileSetDataObject(); + collFieldDef.setFilePathDV(filePath); + } + + collFieldDef.addFile(new File(file.getTempSystemPath())); + collectFilesetPerFieldDef.put(filePath.getFieldDefinition(), collFieldDef); } } - uploadFileset(treeNodeChild_GNAFormDataObject); + + LOG.info("Cluster of fileset per fieldDefinition is: " + collectFilesetPerFieldDef); + + for (String fieldDefinition : collectFilesetPerFieldDef.keySet()) { + FileSetDataObject uploadedFileset = collectFilesetPerFieldDef.get(fieldDefinition); + LOG.info("Uploading fileset: " + uploadedFileset); + + File[] fileset = uploadedFileset.getFileset(); + FilePathDV filePath = uploadedFileset.getFilePathDV(); + + // If the maxOccurs is not 1 + if (profile.getMaxOccurs() == 0 || profile.getMaxOccurs() > 1) { + LOG.info("The gCube Profile with the section " + sectionJSONPath + + " has maxOccurs > 1 need to manage it as array, going to add the array index"); + String arraySectionJSONPAth = String.format("%s[%d]", sectionJSONPath, jpcV); + LOG.debug("registering the fileset in the array section: " + sectionJSONPath); + mongoService.registerFileSet(profileID, theProject, arraySectionJSONPAth, + filePath.getFieldName(), filePath.getFieldDefinition(), fileset); + } else { + LOG.info("The gCube Profile with the section " + sectionJSONPath + " has maxOccurs = 1"); + LOG.debug("registering the fileset in the section: " + sectionJSONPath); + + mongoService.registerFileSet(profileID, theProject, sectionJSONPath, filePath.getFieldName(), + filePath.getFieldDefinition(), fileset); + } + + } + + recursiveUploadFileset(mongoService, profileID, theProject, treeNodeChild_GNA_DO, + sectionJSONPathIndexer); } } diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataentry/server/MongoServiceUtil.java b/src/main/java/org/gcube/portlets/user/geoportaldataentry/server/MongoServiceUtil.java index bf69687..4a7943e 100644 --- a/src/main/java/org/gcube/portlets/user/geoportaldataentry/server/MongoServiceUtil.java +++ b/src/main/java/org/gcube/portlets/user/geoportaldataentry/server/MongoServiceUtil.java @@ -68,14 +68,14 @@ public class MongoServiceUtil { return project; } - public void registerFileSet(String profileID, Project project, File theFile, String parentPath, String fieldName, - String fieldDefinition) throws RemoteException, FileNotFoundException, JsonProcessingException, InvalidRequestException { + public void registerFileSet(String profileID, Project project, String parentPath, String fieldName, + String fieldDefinition, File... files) throws RemoteException, FileNotFoundException, JsonProcessingException, InvalidRequestException { LOG.debug("registerFileSet called for profileID: " + profileID); Projects client = getProjectsClient(profileID); // Prepare request RegisterFileSetRequest fsRequest = FileSets.prepareRequest(new StorageUtils(), parentPath, fieldName, - fieldDefinition, theFile); + fieldDefinition, files); project = client.registerFileSet(project.getId(), fsRequest); LOG.trace("Resulting Project : " + project); LOG.debug("Resulting Project as JSON: " + Serialization.write(project));