Skip to content

Commit

Permalink
2488 : DatasetDownloadServiceImpl code division update
Browse files Browse the repository at this point in the history
  • Loading branch information
DuckflipXYZ committed Dec 4, 2024
1 parent cc4dcf2 commit 24a05fc
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,69 +117,21 @@ public void massiveDownload(String format, List<Dataset> datasets, HttpServletRe

try (ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream())) {
for (Dataset dataset : datasets) {
if (!dataset.isDownloadable()) {
downloadResults.put(dataset.getId(), new DatasetDownloadError("Dataset not downloadable", DatasetDownloadError.ERROR));
continue;
}
DatasetDownloadError downloadResult = new DatasetDownloadError();
downloadResults.put(dataset.getId(), downloadResult);

// Create a new folder organized by subject / examination
String subjectName = getSubjectName(dataset);
if (subjectName.contains(File.separator)) {
subjectName = subjectName.replaceAll(File.separator, "_");
}

String studyName = studyRepository.findById(dataset.getStudyId()).orElse(null).getName();


String datasetFilePath = null;
if (datasets.size() != 1) {
datasetFilePath = getDatasetFilepath(dataset, studyName, subjectName);
}
manageDatasetDownload(dataset, downloadResults, zipOutputStream, subjectName, datasetFilePath, format, withManifest, filesByAcquisitionId, converterId);

List<URL> pathURLs = new ArrayList<>();

if (dataset.getDatasetProcessing() != null) {
// DOWNLOAD PROCESSED DATASET
DatasetFileUtils.getDatasetFilePathURLs(dataset, pathURLs, DatasetExpressionFormat.NIFTI_SINGLE_FILE, downloadResult);
DatasetFileUtils.copyNiftiFilesForURLs(pathURLs, zipOutputStream, dataset, subjectName, true, datasetFilePath);
} else if (dataset instanceof EegDataset) {
// DOWNLOAD EEG
DatasetFileUtils.getDatasetFilePathURLs(dataset, pathURLs, DatasetExpressionFormat.EEG, downloadResult);
DatasetFileUtils.copyNiftiFilesForURLs(pathURLs, zipOutputStream, dataset, subjectName, false, datasetFilePath);
} else if (dataset instanceof BidsDataset) {
// DOWNLOAD BIDS
DatasetFileUtils.getDatasetFilePathURLs(dataset, pathURLs, DatasetExpressionFormat.BIDS, downloadResult);
DatasetFileUtils.copyNiftiFilesForURLs(pathURLs, zipOutputStream, dataset, subjectName, true, datasetFilePath);
// Manage errors here
} else if (DCM.equals(format)) {
// DOWNLOAD DICOM
DatasetFileUtils.getDatasetFilePathURLs(dataset, pathURLs, DatasetExpressionFormat.DICOM, downloadResult);
List<String> files = downloader.downloadDicomFilesForURLsAsZip(pathURLs, zipOutputStream, subjectName, dataset, datasetFilePath, downloadResult);
if (withManifest) {
filesByAcquisitionId.putIfAbsent(dataset.getDatasetAcquisition().getId(), new ArrayList<>());
filesByAcquisitionId.get(dataset.getDatasetAcquisition().getId()).addAll(files);
}
} else if (NII.equals(format)) {
// Check if we have a specific converter -> nifti reconversion
if (converterId != null) {
reconvertToNifti(format, converterId, dataset, pathURLs, downloadResult, subjectName, zipOutputStream);
} else {
// Check that we have existing nifti, otherwise reconvert using dcm2niix by default.
DatasetFileUtils.getDatasetFilePathURLs(dataset, pathURLs, DatasetExpressionFormat.NIFTI_SINGLE_FILE, downloadResult);
if (!pathURLs.isEmpty()) {
List<String> files = DatasetFileUtils.copyNiftiFilesForURLs(pathURLs, zipOutputStream, dataset, subjectName, false, datasetFilePath);
} else {
// Reconvert using dcm2niix by default.
reconvertToNifti(format, DEFAULT_NIFTI_CONVERTER_ID, dataset, pathURLs, downloadResult, subjectName, zipOutputStream);
}
}
} else {
downloadResult.update("Dataset format was not adapted to dataset download choosen", DatasetDownloadError.ERROR);
}

if (downloadResult.getStatus() == null) {
downloadResults.remove(dataset.getId());
}
}
if(!filesByAcquisitionId.isEmpty()){
DatasetFileUtils.writeManifestForExport(zipOutputStream, filesByAcquisitionId);
Expand Down Expand Up @@ -208,6 +160,60 @@ public void massiveDownload(String format, List<Dataset> datasets, HttpServletRe
}
}

protected void manageDatasetDownload(Dataset dataset, Map<Long, DatasetDownloadError> downloadResults, ZipOutputStream zipOutputStream, String subjectName, String datasetFilePath, String format, boolean withManifest, Map<Long, List<String>> filesByAcquisitionId, Long converterId) throws IOException, RestServiceException {
if (!dataset.isDownloadable()) {
downloadResults.put(dataset.getId(), new DatasetDownloadError("Dataset not downloadable", DatasetDownloadError.ERROR));
return;
}
DatasetDownloadError downloadResult = new DatasetDownloadError();
downloadResults.put(dataset.getId(), downloadResult);

List<URL> pathURLs = new ArrayList<>();

if (dataset.getDatasetProcessing() != null) {
// DOWNLOAD PROCESSED DATASET
DatasetFileUtils.getDatasetFilePathURLs(dataset, pathURLs, DatasetExpressionFormat.NIFTI_SINGLE_FILE, downloadResult);
DatasetFileUtils.copyNiftiFilesForURLs(pathURLs, zipOutputStream, dataset, subjectName, true, datasetFilePath);
} else if (dataset instanceof EegDataset) {
// DOWNLOAD EEG
DatasetFileUtils.getDatasetFilePathURLs(dataset, pathURLs, DatasetExpressionFormat.EEG, downloadResult);
DatasetFileUtils.copyNiftiFilesForURLs(pathURLs, zipOutputStream, dataset, subjectName, false, datasetFilePath);
} else if (dataset instanceof BidsDataset) {
// DOWNLOAD BIDS
DatasetFileUtils.getDatasetFilePathURLs(dataset, pathURLs, DatasetExpressionFormat.BIDS, downloadResult);
DatasetFileUtils.copyNiftiFilesForURLs(pathURLs, zipOutputStream, dataset, subjectName, true, datasetFilePath);
// Manage errors here
} else if (Objects.equals("dcm", format)) {
// DOWNLOAD DICOM
DatasetFileUtils.getDatasetFilePathURLs(dataset, pathURLs, DatasetExpressionFormat.DICOM, downloadResult);
List<String> files = downloader.downloadDicomFilesForURLsAsZip(pathURLs, zipOutputStream, subjectName, dataset, datasetFilePath, downloadResult);
if (withManifest) {
filesByAcquisitionId.putIfAbsent(dataset.getDatasetAcquisition().getId(), new ArrayList<>());
filesByAcquisitionId.get(dataset.getDatasetAcquisition().getId()).addAll(files);
}
} else if (Objects.equals("nii", format)) {
// Check if we have a specific converter -> nifti reconversion
if (converterId != null) {
reconvertToNifti(format, converterId, dataset, pathURLs, downloadResult, subjectName, zipOutputStream);
} else {
// Check that we have existing nifti, otherwise reconvert using dcm2niix by default.
DatasetFileUtils.getDatasetFilePathURLs(dataset, pathURLs, DatasetExpressionFormat.NIFTI_SINGLE_FILE, downloadResult);
if (!pathURLs.isEmpty()) {
List<String> files = DatasetFileUtils.copyNiftiFilesForURLs(pathURLs, zipOutputStream, dataset, subjectName, false, datasetFilePath);
} else {
// Reconvert using dcm2niix by default.
reconvertToNifti(format, DEFAULT_NIFTI_CONVERTER_ID, dataset, pathURLs, downloadResult, subjectName, zipOutputStream);
}
}
} else {
downloadResult.update("Dataset format was not adapted to dataset download choosen", DatasetDownloadError.ERROR);
}

if (downloadResult.getStatus() == null) {
downloadResults.remove(dataset.getId());
}
}

protected void reconvertToNifti(String format, Long converterId, Dataset dataset, List<URL> pathURLs, DatasetDownloadError downloadResult, String subjectName, ZipOutputStream zipOutputStream) throws RestServiceException, IOException {
File userDir = DatasetFileUtils.getUserImportDir("/tmp");
String tmpFilePath = userDir + File.separator + dataset.getId() + "_" + format;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private void manageProcessingsDownload(List<DatasetProcessing> processingList, M
String processingFilePath = getExecFilepath(processing.getId(), getExaminationDatas(processing.getInputDatasets()));
String subjectName = getProcessingSubject(processing);
for (Dataset dataset : Stream.concat(processing.getInputDatasets().stream(), processing.getOutputDatasets().stream()).toList()) {
manageDatasetDownload(dataset, downloadResults, zipOutputStream, subjectName, processingFilePath, format, withManifest, filesByAcquisitionId, converterId);
manageDatasetDownload(dataset, downloadResults, zipOutputStream, subjectName, processingFilePath + "/" + shapeForPath(dataset.getName()), format, withManifest, filesByAcquisitionId, converterId);

}
}
Expand All @@ -95,60 +95,6 @@ private void manageProcessingsDownload(List<DatasetProcessing> processingList, M
}
}

private void manageDatasetDownload(Dataset dataset, Map<Long, DatasetDownloadError> downloadResults, ZipOutputStream zipOutputStream, String subjectName, String processingFilePath, String format, boolean withManifest, Map<Long, List<String>> filesByAcquisitionId, Long converterId) throws IOException, RestServiceException {
if (!dataset.isDownloadable()) {
downloadResults.put(dataset.getId(), new DatasetDownloadError("Dataset not downloadable", DatasetDownloadError.ERROR));
return;
}
DatasetDownloadError downloadResult = new DatasetDownloadError();
downloadResults.put(dataset.getId(), downloadResult);

List<URL> pathURLs = new ArrayList<>();

if (dataset.getDatasetProcessing() != null) {
// DOWNLOAD PROCESSED DATASET
DatasetFileUtils.getDatasetFilePathURLs(dataset, pathURLs, DatasetExpressionFormat.NIFTI_SINGLE_FILE, downloadResult);
DatasetFileUtils.copyNiftiFilesForURLs(pathURLs, zipOutputStream, dataset, subjectName, true, processingFilePath);
} else if (dataset instanceof EegDataset) {
// DOWNLOAD EEG
DatasetFileUtils.getDatasetFilePathURLs(dataset, pathURLs, DatasetExpressionFormat.EEG, downloadResult);
DatasetFileUtils.copyNiftiFilesForURLs(pathURLs, zipOutputStream, dataset, subjectName, false, processingFilePath);
} else if (dataset instanceof BidsDataset) {
// DOWNLOAD BIDS
DatasetFileUtils.getDatasetFilePathURLs(dataset, pathURLs, DatasetExpressionFormat.BIDS, downloadResult);
DatasetFileUtils.copyNiftiFilesForURLs(pathURLs, zipOutputStream, dataset, subjectName, true, processingFilePath);
// Manage errors here
} else if (Objects.equals("dcm", format)) {
// DOWNLOAD DICOM
DatasetFileUtils.getDatasetFilePathURLs(dataset, pathURLs, DatasetExpressionFormat.DICOM, downloadResult);
List<String> files = downloader.downloadDicomFilesForURLsAsZip(pathURLs, zipOutputStream, subjectName, dataset, processingFilePath + "/" + shapeForPath(dataset.getName()), downloadResult);
if (withManifest) {
filesByAcquisitionId.putIfAbsent(dataset.getDatasetAcquisition().getId(), new ArrayList<>());
filesByAcquisitionId.get(dataset.getDatasetAcquisition().getId()).addAll(files);
}
} else if (Objects.equals("nii", format)) {
// Check if we have a specific converter -> nifti reconversion
if (converterId != null) {
reconvertToNifti(format, converterId, dataset, pathURLs, downloadResult, subjectName, zipOutputStream);
} else {
// Check that we have existing nifti, otherwise reconvert using dcm2niix by default.
DatasetFileUtils.getDatasetFilePathURLs(dataset, pathURLs, DatasetExpressionFormat.NIFTI_SINGLE_FILE, downloadResult);
if (!pathURLs.isEmpty()) {
List<String> files = DatasetFileUtils.copyNiftiFilesForURLs(pathURLs, zipOutputStream, dataset, subjectName, false, processingFilePath + "/" + shapeForPath(dataset.getName()));
} else {
// Reconvert using dcm2niix by default.
reconvertToNifti(format, DEFAULT_NIFTI_CONVERTER_ID, dataset, pathURLs, downloadResult, subjectName, zipOutputStream);
}
}
} else {
downloadResult.update("Dataset format was not adapted to dataset download choosen", DatasetDownloadError.ERROR);
}

if (downloadResult.getStatus() == null) {
downloadResults.remove(dataset.getId());
}
}

public void massiveDownloadByExaminations(List<Examination> examinationList, String processingComment, boolean resultOnly, String format, HttpServletResponse response, boolean withManifest, Long converterId) throws RestServiceException {
List<Long> processingIdsList = datasetProcessingRepository.findAllIdsByExaminationIds(examinationList.stream().map(Examination::getId).toList());
List<DatasetProcessing> processingList = datasetProcessingService.findAllById(processingIdsList).stream().filter(it -> Objects.equals(it.getComment(), processingComment)).toList();
Expand Down

0 comments on commit 24a05fc

Please sign in to comment.