From 1eaed1d7a2119a86d237684c716cb085a1699bdd Mon Sep 17 00:00:00 2001 From: Ruslan Forostianov Date: Tue, 17 Dec 2024 15:54:02 +0100 Subject: [PATCH] Improve code how we manage output stream flushing --- .../service/impl/ExportService.java | 78 ++++++++++--------- 1 file changed, 42 insertions(+), 36 deletions(-) diff --git a/src/main/java/org/cbioportal/service/impl/ExportService.java b/src/main/java/org/cbioportal/service/impl/ExportService.java index be01b76c580..a28d2bab613 100644 --- a/src/main/java/org/cbioportal/service/impl/ExportService.java +++ b/src/main/java/org/cbioportal/service/impl/ExportService.java @@ -17,9 +17,7 @@ import org.springframework.stereotype.Service; import java.io.*; -import java.nio.charset.StandardCharsets; import java.util.*; -import java.util.AbstractMap.SimpleEntry; import java.util.stream.Collectors; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -48,17 +46,29 @@ public ExportService(StudyService studyService, this.clinicalAttributeDataFetcher = clinicalAttributeDataFetcher; } + class ZipEntryOutputStreamWriter extends OutputStreamWriter { + private final ZipOutputStream zipOutputStream; + + public ZipEntryOutputStreamWriter(String name, ZipOutputStream zipOutputStream) throws IOException { + super(zipOutputStream); + zipOutputStream.putNextEntry(new ZipEntry(name)); + this.zipOutputStream = zipOutputStream; + } + + @Override + public void close() throws IOException { + flush(); + zipOutputStream.closeEntry(); + } + } + public void exportStudyDataToZip(OutputStream outputStream, String studyId) throws IOException { - CancerStudyInfo cancerStudyInfo = getCancerStudyInfo(studyId); + CancerStudyInfo cancerStudyInfo = getCancerStudyInfo(studyId); try (ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream)) { // Add files to the ZIP - OutputStreamWriter writer = new OutputStreamWriter(zipOutputStream, StandardCharsets.UTF_8); - - zipOutputStream.putNextEntry(new ZipEntry("meta_study.txt")); - new KeyValueMetadataWriter(writer).write(cancerStudyInfo.metadata); - //FIXME for some reasons I have to flush to make sure the content is written to the write file - writer.flush(); - zipOutputStream.closeEntry(); + try (ZipEntryOutputStreamWriter studyMetadataWriter = new ZipEntryOutputStreamWriter("meta_study.txt", zipOutputStream)) { + new KeyValueMetadataWriter(studyMetadataWriter).write(cancerStudyInfo.metadata); + } // TODO detect what data types are available for a study and export them // by iterating over the available data types and calling the appropriate fetchers and writers @@ -66,26 +76,22 @@ public void exportStudyDataToZip(OutputStream outputStream, String studyId) thro ClinicalAttributeData clinicalAttributeData = clinicalAttributeDataFetcher.fetch(cancerStudyInfo.studyToSampleMap); if (clinicalAttributeData.rows().hasNext()) { - zipOutputStream.putNextEntry(new ZipEntry("meta_clinical_samples.txt")); ClinicalSampleAttributesMetadata clinicalSampleAttributesMetadata = new ClinicalSampleAttributesMetadata( studyId, "data_clinical_samples.txt" ); - new KeyValueMetadataWriter(writer).write(clinicalSampleAttributesMetadata); - //FIXME for some reasons I have to flush to make sure the content is written to the write file - writer.flush(); - zipOutputStream.closeEntry(); - - zipOutputStream.putNextEntry(new ZipEntry("data_clinical_samples.txt")); - ClinicalAttributeDataWriter clinicalAttributeDataWriter = new ClinicalAttributeDataWriter(writer); - clinicalAttributeDataWriter.write(clinicalAttributeData); - //FIXME for some reasons I have to flush to make sure the content is written to the write file - writer.flush(); - zipOutputStream.closeEntry(); + try (ZipEntryOutputStreamWriter clinicalSampleMetadataWriter = new ZipEntryOutputStreamWriter("meta_clinical_samples.txt", zipOutputStream)) { + new KeyValueMetadataWriter(clinicalSampleMetadataWriter).write(clinicalSampleAttributesMetadata); + } + + try (ZipEntryOutputStreamWriter clinicalSampleDataWriter = new ZipEntryOutputStreamWriter("data_clinical_samples.txt", zipOutputStream)) { + ClinicalAttributeDataWriter clinicalAttributeDataWriter = new ClinicalAttributeDataWriter(clinicalSampleDataWriter); + clinicalAttributeDataWriter.write(clinicalAttributeData); + } } Map> molecularProfilesByStableId = this.molecularProfileService.getMolecularProfilesInStudies(cancerStudyInfo.studyToSampleMap.keySet().stream().toList(), "SUMMARY").stream().collect(Collectors.groupingBy(MolecularProfile::getStableId)); - for (Map.Entry> molecularProfiles: molecularProfilesByStableId.entrySet()) { + for (Map.Entry> molecularProfiles : molecularProfilesByStableId.entrySet()) { String stableId = molecularProfiles.getKey(); List molecularProfileList = molecularProfiles.getValue(); Map molecularAlterationTypeToDatatype = molecularProfileList.stream() @@ -97,7 +103,6 @@ public void exportStudyDataToZip(OutputStream outputStream, String studyId) thro if ("MAF".equals(molecularAlterationTypeToDatatype.get(MolecularProfile.MolecularAlterationType.MUTATION_EXTENDED))) { Iterator mafRecordIterator = mafRecordFetcher.fetch(cancerStudyInfo.studyToSampleMap, stableId); if (mafRecordIterator.hasNext()) { - zipOutputStream.putNextEntry(new ZipEntry("meta_mutations.txt")); GenericProfileDatatypeMetadata genericProfileDatatypeMetadata = new GenericProfileDatatypeMetadata( stableId, //TODO Use mol. alteration type and datatype from the map above instead @@ -112,17 +117,14 @@ public void exportStudyDataToZip(OutputStream outputStream, String studyId) thro //Is it true for all data types? true ); - new KeyValueMetadataWriter(writer).write(genericProfileDatatypeMetadata); - //FIXME for some reasons I have to flush to make sure the content is written to the write file - writer.flush(); - zipOutputStream.closeEntry(); - - zipOutputStream.putNextEntry(new ZipEntry("data_mutations.txt")); - MafRecordWriter mafRecordWriter = new MafRecordWriter(writer); - mafRecordWriter.write(mafRecordIterator); - //FIXME for some reasons I have to flush to make sure the content is written to the write file - writer.flush(); - zipOutputStream.closeEntry(); + try (ZipEntryOutputStreamWriter mafMetaWriter = new ZipEntryOutputStreamWriter("meta_mutations.txt", zipOutputStream)) { + new KeyValueMetadataWriter(mafMetaWriter).write(genericProfileDatatypeMetadata); + } + + try (ZipEntryOutputStreamWriter mafDataWriter = new ZipEntryOutputStreamWriter("data_mutations.txt", zipOutputStream)) { + MafRecordWriter mafRecordWriter = new MafRecordWriter(mafDataWriter); + mafRecordWriter.write(mafRecordIterator); + } } } } @@ -132,7 +134,11 @@ public void exportStudyDataToZip(OutputStream outputStream, String studyId) thro record CancerStudyInfo( CancerStudyMetadata metadata, Map> studyToSampleMap - ) {}; + ) { + } + + ; + private CancerStudyInfo getCancerStudyInfo(String studyId) { List studies = studyService.fetchStudies(List.of(studyId), "DETAILED"); Map> studyToSampleMap = new HashMap<>();