@@ -75,9 +75,6 @@ public class EpipulseCsvExportOrchestrator {
7575 */
7676 public void orchestrateExport (String uuid , ExportFunction exportFunction , CsvExportStrategy csvStrategy ) {
7777
78- CSVWriter writer = null ;
79- FileOutputStream fos = null ;
80- OutputStreamWriter osw = null ;
8178 EpipulseExport epipulseExport = null ;
8279 EpipulseExportStatus exportStatus = EpipulseExportStatus .FAILED ;
8380 boolean shouldUpdateStatus = false ;
@@ -119,52 +116,30 @@ public void orchestrateExport(String uuid, ExportFunction exportFunction, CsvExp
119116 EpipulseDiseaseExportResult exportResult = exportFunction .execute (exportDto , serverCountryCode , serverCountryName );
120117 totalRecords = exportResult .getExportEntryList ().size ();
121118
122- // Setup CSV writer with explicit stream management
123- fos = new FileOutputStream (exportFilePath );
124- osw = new OutputStreamWriter (fos , StandardCharsets .UTF_8 );
125- writer = CSVUtils .createCSVWriter (osw , configFacadeEjb .getCsvSeparator ());
119+ // Setup CSV writer with try-with-resources for automatic resource management
120+ try ( FileOutputStream fos = new FileOutputStream (exportFilePath );
121+ OutputStreamWriter osw = new OutputStreamWriter (fos , StandardCharsets .UTF_8 );
122+ CSVWriter writer = CSVUtils .createCSVWriter (osw , configFacadeEjb .getCsvSeparator ())) {
126123
127- // Build column names using strategy
128- List <String > columnNames = csvStrategy .buildColumnNames (exportResult );
124+ // Build column names using strategy
125+ List <String > columnNames = csvStrategy .buildColumnNames (exportResult );
129126
130- // Write headers
131- writer .writeNext (columnNames .toArray (new String [columnNames .size ()]));
127+ // Write headers
128+ writer .writeNext (columnNames .toArray (new String [columnNames .size ()]));
132129
133- // Write entries using strategy
134- for (EpipulseDiseaseExportEntryDto dto : exportResult .getExportEntryList ()) {
135- String [] exportLine = new String [columnNames .size ()];
136- csvStrategy .writeEntryRow (dto , exportLine , exportResult );
137- writer .writeNext (exportLine );
130+ // Write entries using strategy
131+ for (EpipulseDiseaseExportEntryDto dto : exportResult .getExportEntryList ()) {
132+ String [] exportLine = new String [columnNames .size ()];
133+ csvStrategy .writeEntryRow (dto , exportLine , exportResult );
134+ writer .writeNext (exportLine );
135+ }
138136 }
139137
140138 exportStatus = EpipulseExportStatus .COMPLETED ;
141139 } catch (Exception e ) {
142140 exportStatus = EpipulseExportStatus .FAILED ;
143141 logger .error ("Error during export with uuid " + uuid + ": " + e .getMessage (), e );
144142 } finally {
145- // Close resources in reverse order
146- if (writer != null ) {
147- try {
148- writer .close ();
149- } catch (Exception e ) {
150- logger .error ("CRITICAL: Failed to close CSVWriter for uuid " + uuid + ": " + e .getMessage (), e );
151- }
152- }
153- if (osw != null ) {
154- try {
155- osw .close ();
156- } catch (Exception e ) {
157- logger .error ("CRITICAL: Failed to close OutputStreamWriter for uuid " + uuid + ": " + e .getMessage (), e );
158- }
159- }
160- if (fos != null ) {
161- try {
162- fos .close ();
163- } catch (Exception e ) {
164- logger .error ("CRITICAL: Failed to close FileOutputStream for uuid " + uuid + ": " + e .getMessage (), e );
165- }
166- }
167-
168143 // Calculate file size after writer is closed
169144 if (exportFilePath != null && exportStatus == EpipulseExportStatus .COMPLETED ) {
170145 try {
0 commit comments