From 2f7d93cd8e160438735e692e566410949c7687bc Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Tue, 2 May 2023 15:16:59 +0200 Subject: [PATCH] Fixed the Update facility, #24166 --- .../client/ui/edit/UpdateRecord.java | 2 - .../server/GeoportalDataEntryServiceImpl.java | 155 ++++++++++++++++-- .../org/gcube/application/Service_Tests.java | 67 ++++---- 3 files changed, 179 insertions(+), 45 deletions(-) diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataentry/client/ui/edit/UpdateRecord.java b/src/main/java/org/gcube/portlets/user/geoportaldataentry/client/ui/edit/UpdateRecord.java index 2ad2cb4..8afd138 100644 --- a/src/main/java/org/gcube/portlets/user/geoportaldataentry/client/ui/edit/UpdateRecord.java +++ b/src/main/java/org/gcube/portlets/user/geoportaldataentry/client/ui/edit/UpdateRecord.java @@ -241,7 +241,6 @@ public class UpdateRecord extends Composite { String htmlMsg = "Updating the section "+listBoxSections.getSelectedItemText()+" of the project with:"; htmlMsg += ""; @@ -281,7 +280,6 @@ public class UpdateRecord extends Composite { String htmlMsg = "The project with:"; htmlMsg += ""; 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 d230b14..d779069 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 @@ -232,6 +232,17 @@ public class GeoportalDataEntryServiceImpl extends RemoteServiceServlet implemen } } + /** + * Update geportal data form. + * + * @param profileID the profile ID + * @param projectID the project ID + * @param section the section + * @param sectionPath the section path + * @param listFilePaths the list file paths + * @return the commit report + * @throws Exception the exception + */ @Override public CommitReport updateGeportalDataForm(String profileID, String projectID, GeoNaFormDataObject section, String sectionPath, List listFilePaths) throws Exception { @@ -327,6 +338,14 @@ public class GeoportalDataEntryServiceImpl extends RemoteServiceServlet implemen if (listFilePaths != null) { + // Collecting new files + List filesUploaded = sectionBean.getFilesUploaded(); + Map mapFilesToRegistrer = null; + if (filesUploaded != null && !filesUploaded.isEmpty()) { + mapFilesToRegistrer = collectFiles(currentProject, sectionPath, section.getGcubeProfileDV(), + filesUploaded); + } + // Cleaning all the fileset path of the section (defined in the UCD) for (FilePathDV filePath : listFilePaths) { @@ -342,11 +361,27 @@ public class GeoportalDataEntryServiceImpl extends RemoteServiceServlet implemen } } - // Registering new files - List filesUploaded = sectionBean.getFilesUploaded(); - if (filesUploaded != null && !filesUploaded.isEmpty()) { - replaceFiles(currentProject, sectionPath, section.getGcubeProfileDV(), filesUploaded); + // Registering fileset in the section according to mapFilesToRegistrer + if (mapFilesToRegistrer != null && mapFilesToRegistrer.size()>0) { + LOG.info("Cluster of fileset per fieldDefinition is: " + mapFilesToRegistrer); + String theJSONDocument = currentProject.getTheDocument().toJson(); + MongoServiceUtil mongoService = new MongoServiceUtil(); + + for (String fieldDefinition : mapFilesToRegistrer.keySet()) { + FileSetDataObject uploadedFileset = mapFilesToRegistrer.get(fieldDefinition); + LOG.info("Uploading fileset: " + uploadedFileset); + File[] fileset = uploadedFileset.getFileset(); + FilePathDV filePath = uploadedFileset.getFilePathDV(); + Access access = ConvertToDataServiceModel.getAccessFromDocumentSection(theJSONDocument, + sectionPath); + + LOG.info("Going to register fileset: " + Arrays.asList(fileset).toString()); + mongoService.registerFileSet(currentProject.getProfileID(), currentProject, sectionPath, + filePath.getFieldName(), filePath.getFieldDefinition(), access, fileset); + + } } + } LOG.info("Project with id " + currentProject.getId() + " updated correclty"); @@ -530,6 +565,84 @@ public class GeoportalDataEntryServiceImpl extends RemoteServiceServlet implemen } + /** + * Collect files. + * + * @param theProject the the project + * @param sectionJSONPath the section JSON path + * @param gcubeProfile the gcube profile + * @param files the files + * @return the map of files that must be registered + * @throws Exception the exception + */ + protected Map collectFiles(Project theProject, String sectionJSONPath, + GcubeProfileDV gcubeProfile, List files) throws Exception { + LOG.debug("collectFiles called [projectID: " + theProject.getId() + "], [sectionJSONPath: " + sectionJSONPath + + "], [files: " + files + "]"); + + Map collectFilesetPerFieldDef = new HashMap(); + if (files.size() > 0) { + // 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(); + LOG.debug("Uploading file: " + file.getFileName() + ", from formFieldName: " + formFieldName); + FilePathDV filePath = retrieveFilePathForGcubeProfileFieldName(formFieldName, gcubeProfile); + 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: " + gcubeProfile; + throw new Exception(error); + } + + // Collecting Fileset per Field Definition + FileSetDataObject collFieldDef = collectFilesetPerFieldDef.get(filePath.getFieldDefinition()); + if (collFieldDef == null) { + collFieldDef = new FileSetDataObject(); + collFieldDef.setFilePathDV(filePath); + } + + try { + + File tempDir = Files.createTempDirectory("GEOPORTAL_REPLACE_FILES_").toFile(); + String tmpDirPath = tempDir.getAbsolutePath(); + File input; + File output; + if (file instanceof FileUploadedRemote) { + FileUploadedRemote remote = (FileUploadedRemote) file; + LOG.info("Uploaded file is remote: " + remote.getUrl()); + InputStream in = new URL(remote.getUrl()).openStream(); + String fileName = (remote.getFileName() == null || remote.getFileName().isEmpty()) + ? "file_" + Random.nextInt() + : remote.getFileName(); + output = new File(tmpDirPath, fileName); + Path outputAbsolutePath = Paths.get(output.getAbsolutePath()); + Files.copy(in, outputAbsolutePath, StandardCopyOption.REPLACE_EXISTING); + LOG.info("Remote file: " + remote.getUrl() + ", copied to new file: " + output.getName()); + } else { + LOG.info("Uploaded file is local: " + file.getTempSystemPath()); + input = new File(file.getTempSystemPath()); + output = new File(tmpDirPath, file.getFileName()); + copyContent(input, output); + LOG.info( + "Temp file: " + file.getTempSystemPath() + ", copied to new file: " + output.getName()); + } + + collFieldDef.addFile(output); + tempDir.deleteOnExit(); + } catch (Exception e) { + LOG.warn("Skipping file: " + file.getFileName() + ". Error: " + e.getMessage()); + } + + collectFilesetPerFieldDef.put(filePath.getFieldDefinition(), collFieldDef); + + } + + } + + return collectFilesetPerFieldDef; + } + /** * Replace files. * @@ -576,7 +689,9 @@ public class GeoportalDataEntryServiceImpl extends RemoteServiceServlet implemen FileUploadedRemote remote = (FileUploadedRemote) file; LOG.info("Uploaded file is remote: " + remote.getUrl()); InputStream in = new URL(remote.getUrl()).openStream(); - String fileName = (remote.getFileName()==null || remote.getFileName().isEmpty())?"file_"+Random.nextInt():remote.getFileName(); + String fileName = (remote.getFileName() == null || remote.getFileName().isEmpty()) + ? "file_" + Random.nextInt() + : remote.getFileName(); output = new File(tmpDirPath, fileName); Path outputAbsolutePath = Paths.get(output.getAbsolutePath()); Files.copy(in, outputAbsolutePath, StandardCopyOption.REPLACE_EXISTING); @@ -613,7 +728,7 @@ public class GeoportalDataEntryServiceImpl extends RemoteServiceServlet implemen FilePathDV filePath = uploadedFileset.getFilePathDV(); Access access = ConvertToDataServiceModel.getAccessFromDocumentSection(theJSONDocument, sectionJSONPath); - LOG.info("Going to registrer files: " + Arrays.asList(fileset).toString()); + LOG.info("Going to register files: " + Arrays.asList(fileset).toString()); mongoService.registerFileSet(theProject.getProfileID(), theProject, sectionJSONPath, filePath.getFieldName(), filePath.getFieldDefinition(), access, fileset); @@ -1397,6 +1512,14 @@ public class GeoportalDataEntryServiceImpl extends RemoteServiceServlet implemen return null; } + /** + * Gets the project edit. + * + * @param profileID the profile ID + * @param projectID the project ID + * @return the project edit + * @throws Exception the exception + */ @Override public ProjectEdit getProjectEdit(String profileID, String projectID) throws Exception { LOG.info("getProjectEdit called for profileID: {}, and projectID: {}", profileID, projectID); @@ -1417,16 +1540,25 @@ public class GeoportalDataEntryServiceImpl extends RemoteServiceServlet implemen Geoportal_JSON_Mapper.prettyPrintProjectEdit(projectEdit); } - LOG.info(ProjectEdit.class.getSimpleName()+" returing not null: " + (projectEdit != null)); + LOG.info(ProjectEdit.class.getSimpleName() + " returing not null: " + (projectEdit != null)); return projectEdit; } catch (Exception e) { - String erroMsg = "Error occurred on reading "+ProjectEdit.class.getSimpleName()+" DTO for id: " + projectID; + String erroMsg = "Error occurred on reading " + ProjectEdit.class.getSimpleName() + " DTO for id: " + + projectID; LOG.warn(erroMsg, e); throw new Exception( erroMsg + ". Error: " + e.getMessage() + ". Refresh and try again or contact the support"); } } - + + /** + * Gets the project view. + * + * @param profileID the profile ID + * @param projectID the project ID + * @return the project view + * @throws Exception the exception + */ @Override public ProjectView getProjectView(String profileID, String projectID) throws Exception { LOG.info("getProjectEdit called for profileID: {}, and projectID: {}", profileID, projectID); @@ -1447,10 +1579,11 @@ public class GeoportalDataEntryServiceImpl extends RemoteServiceServlet implemen Geoportal_JSON_Mapper.prettyPrintProjectView(projectView); } - LOG.info(ProjectView.class.getSimpleName()+" returing not null: " + (projectView != null)); + LOG.info(ProjectView.class.getSimpleName() + " returing not null: " + (projectView != null)); return projectView; } catch (Exception e) { - String erroMsg = "Error occurred on reading "+ProjectView.class.getSimpleName()+" DTO for id: " + projectID; + String erroMsg = "Error occurred on reading " + ProjectView.class.getSimpleName() + " DTO for id: " + + projectID; LOG.warn(erroMsg, e); throw new Exception( erroMsg + ". Error: " + e.getMessage() + ". Refresh and try again or contact the support"); diff --git a/src/test/java/org/gcube/application/Service_Tests.java b/src/test/java/org/gcube/application/Service_Tests.java index cb66470..1817a2a 100644 --- a/src/test/java/org/gcube/application/Service_Tests.java +++ b/src/test/java/org/gcube/application/Service_Tests.java @@ -97,37 +97,40 @@ public class Service_Tests { Project doc = clientPrj.getProjectByID(PROFILE_ID, PROJECT_ID); - JSONPathWrapper wrapper = new JSONPathWrapper(doc.getTheDocument().toJson()); - List matchingPaths = wrapper.getMatchingPaths(path); - - LOG.info("matchingPaths is: " + matchingPaths); - - String error = null; - if (matchingPaths.isEmpty()) { - error = "No Registered FileSet found at " + path; - if (!ignore_errors) { - throw new WebApplicationException(error, Response.Status.BAD_REQUEST); - } - } - if (matchingPaths.size() > 1 && !ignore_errors) { - error = "Multiple Fileset (" + matchingPaths.size() + ") matching " + path; - if (!ignore_errors) - throw new WebApplicationException(error, Response.Status.BAD_REQUEST); - } - - if (error != null && ignore_errors) { - LOG.info("Error detected {}. Ignoring it and returning input doc", error); - - } - - List listPath = wrapper.getByPath(path); - LOG.info("List path: " + listPath); +// JSONPathWrapper wrapper = new JSONPathWrapper(doc.getTheDocument().toJson()); +// List matchingPaths = wrapper.getMatchingPaths(path); +// +// LOG.info("matchingPaths is: " + matchingPaths); +// +// String error = null; +// if (matchingPaths.isEmpty()) { +// error = "No Registered FileSet found at " + path; +// if (!ignore_errors) { +// throw new WebApplicationException(error, Response.Status.BAD_REQUEST); +// } +// } +// if (matchingPaths.size() > 1 && !ignore_errors) { +// error = "Multiple Fileset (" + matchingPaths.size() + ") matching " + path; +// if (!ignore_errors) +// throw new WebApplicationException(error, Response.Status.BAD_REQUEST); +// } +// +// if (error != null && ignore_errors) { +// LOG.info("Error detected {}. Ignoring it and returning input doc", error); +// +// } +// +// List listPath = wrapper.getByPath(path); +// LOG.info("List path: " + listPath); // RegisteredFileSet fs = Serialization.convert(listPath.get(0), RegisteredFileSet.class); // LOG.info("Going to delete {}", fs); - + + LOG.info("Going to delete {}", path); + Project newDoc = clientPrj.deleteFileset(PROFILE_ID, PROJECT_ID, path, true, true); + LOG.info("newDoc {}", newDoc); } - // @Test + //@Test public void registerFileSet() throws Exception { ScopeProvider.instance.set(CONTEXT); SecurityTokenProvider.instance.set(TOKEN); @@ -175,16 +178,16 @@ public class Service_Tests { copyContent(input, output); LOG.info("Temp file: " + file.getTempSystemPath() + ", copied to new file: " + output.getName()); } - tempDir.deleteOnExit(); + //tempDir.deleteOnExit(); } catch (Exception e) { LOG.warn("Skipping file: " + file.getFileName() + ". Error: " + e.getMessage(), e); } File fileset = output; - LOG.info("the fileName is: " + fileset.getName()); - - //mongoService.registerFileSet(theProject.getProfileID(), theProject, sectionJSONPath, fieldName, fieldDefinition, access, fileset); - + LOG.info("final fileName is: " + fileset.getName()); + mongoService.registerFileSet(theProject.getProfileID(), theProject, sectionJSONPath, fieldName, fieldDefinition, access, fileset); + + LOG.info("registerFileSet: finished!"); } /**