-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
EVA-3227 Match metadata files info with uploaded files #15
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package uk.ac.ebi.eva.submission.exception; | ||
|
||
public class MetadataFileInfoMismatchException extends RuntimeException { | ||
public MetadataFileInfoMismatchException(String message) { | ||
super(message); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,16 @@ | ||
package uk.ac.ebi.eva.submission.service; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import uk.ac.ebi.eva.submission.entity.Submission; | ||
import uk.ac.ebi.eva.submission.entity.SubmissionAccount; | ||
import uk.ac.ebi.eva.submission.entity.SubmissionDetails; | ||
import uk.ac.ebi.eva.submission.entity.SubmissionProcessing; | ||
import uk.ac.ebi.eva.submission.exception.MetadataFileInfoMismatchException; | ||
import uk.ac.ebi.eva.submission.exception.RequiredFieldsMissingException; | ||
import uk.ac.ebi.eva.submission.exception.SubmissionDoesNotExistException; | ||
import uk.ac.ebi.eva.submission.model.SubmissionProcessingStatus; | ||
|
@@ -20,15 +24,26 @@ | |
import uk.ac.ebi.eva.submission.util.MailSender; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.StreamSupport; | ||
|
||
@Service | ||
public class SubmissionService { | ||
private static final String PROJECT = "project"; | ||
private static final String TITLE = "title"; | ||
private static final String DESCRIPTION = "description"; | ||
private static final String METADATA_FILES_TAG = "files"; | ||
private static final String METADATA_FILE_NAME = "fileName"; | ||
private static final String METADATA_FILE_SIZE = "fileSize"; | ||
private static final String GLOBUS_FILES_TAG = "DATA"; | ||
private static final String GLOBUS_FILE_NAME = "name"; | ||
private static final String GLOBUS_FILE_SIZE = "size"; | ||
|
||
private final SubmissionRepository submissionRepository; | ||
|
||
|
@@ -82,6 +97,63 @@ public Submission initiateSubmission(SubmissionAccount submissionAccount) { | |
return submissionRepository.save(submission); | ||
} | ||
|
||
public void checkMetadataFileInfoMatchesWithUploadedFiles(SubmissionAccount submissionAccount, String submissionId, JsonNode metadataJson) { | ||
Map<String, Long> metadataFileInfo = new HashMap<>(); | ||
if (metadataJson.get(METADATA_FILES_TAG) != null) { | ||
metadataFileInfo = StreamSupport.stream(metadataJson.get(METADATA_FILES_TAG).spliterator(), false) | ||
.collect(Collectors.toMap( | ||
dataNode -> dataNode.get(METADATA_FILE_NAME).asText(), | ||
dataNode -> dataNode.get(METADATA_FILE_SIZE).asLong() | ||
)); | ||
} | ||
if (metadataFileInfo.isEmpty()) { | ||
throw new MetadataFileInfoMismatchException("Metadata json file does not have any file info"); | ||
} | ||
|
||
String directoryToList = String.format("%s/%s", submissionAccount.getId(), submissionId); | ||
String uploadedFilesInfo = globusDirectoryProvisioner.listSubmittedFiles(directoryToList); | ||
if (uploadedFilesInfo.isEmpty()) { | ||
throw new MetadataFileInfoMismatchException("Failed to retrieve any file info from submission directory."); | ||
} else { | ||
Map<String, Long> globusFileInfo = new HashMap<>(); | ||
try { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
ObjectNode globusFileInfoJson = (ObjectNode) mapper.readTree(uploadedFilesInfo); | ||
if (globusFileInfoJson.get(GLOBUS_FILES_TAG) != null) { | ||
globusFileInfo = StreamSupport.stream(globusFileInfoJson.get(GLOBUS_FILES_TAG).spliterator(), false) | ||
.collect(Collectors.toMap( | ||
dataNode -> dataNode.get(GLOBUS_FILE_NAME).asText(), | ||
dataNode -> dataNode.get(GLOBUS_FILE_SIZE).asLong() | ||
)); | ||
} | ||
} catch (JsonProcessingException ex) { | ||
throw new MetadataFileInfoMismatchException("Error parsing fileInfo from Submission Directory"); | ||
} | ||
Comment on lines
+114
to
+131
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Opinion: I feel like this code should be in GlobusDirectoryProvisioner. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO this is not the core responsibility of GlobusDirectoryProvisioner. It should just be responsible for retrieving the dir/file details. The checking of missing files or file size mismatches should be done outside. |
||
|
||
List<String> missingFileList = new ArrayList<>(); | ||
String fileSizeMismatchInfo = ""; | ||
|
||
for (Map.Entry<String, Long> fileEntry : metadataFileInfo.entrySet()) { | ||
String fileName = fileEntry.getKey(); | ||
Long metadataFileSize = fileEntry.getValue(); | ||
if (globusFileInfo.containsKey(fileName)) { | ||
Long fileSizeInGlobus = globusFileInfo.get(fileName); | ||
if (!metadataFileSize.equals(fileSizeInGlobus)) { | ||
fileSizeMismatchInfo += fileName + ": metadata json file size (" + metadataFileSize + ") is not equal to uploaded file size (" + fileSizeInGlobus + ")\n"; | ||
} | ||
} else { | ||
missingFileList.add(fileName); | ||
} | ||
} | ||
|
||
if (!missingFileList.isEmpty() || !fileSizeMismatchInfo.isEmpty()) { | ||
String missingFileMsg = missingFileList.isEmpty() ? "" : "There are some files mentioned in metadata json but not uploaded. Files : " + String.join(", ", missingFileList) + "\n"; | ||
String fileSizeMismatchMsg = fileSizeMismatchInfo.isEmpty() ? "" : "There are some files mentioned in metadata json whose size does not match with the files uploaded.\n" + fileSizeMismatchInfo; | ||
throw new MetadataFileInfoMismatchException(missingFileMsg + fileSizeMismatchMsg); | ||
} | ||
} | ||
} | ||
|
||
public Submission uploadMetadataJsonAndMarkUploaded(String submissionId, JsonNode metadataJson) { | ||
SubmissionDetails submissionDetails = new SubmissionDetails(submissionId); | ||
try { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we not log rather than print out ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
apologies copy paste mistake.. updated