1818 */
1919package de .rwth .idsg .steve .service ;
2020
21+ import de .rwth .idsg .steve .SteveException ;
2122import de .rwth .idsg .steve .repository .DataImportExportRepository ;
2223import de .rwth .idsg .steve .web .dto .DataExportForm .ExportType ;
2324import lombok .RequiredArgsConstructor ;
2425import lombok .extern .slf4j .Slf4j ;
26+ import org .apache .commons .lang3 .StringUtils ;
2527import org .jooq .Named ;
2628import org .jooq .Table ;
2729import org .springframework .stereotype .Service ;
30+ import org .springframework .web .multipart .MultipartFile ;
2831
32+ import jakarta .servlet .http .HttpServletResponse ;
2933import java .io .FilterInputStream ;
3034import java .io .IOException ;
3135import java .io .InputStream ;
3438import java .nio .charset .StandardCharsets ;
3539import java .util .Collection ;
3640import java .util .List ;
41+ import java .util .concurrent .TimeUnit ;
3742import java .util .stream .Stream ;
3843import java .util .zip .ZipEntry ;
3944import java .util .zip .ZipInputStream ;
@@ -115,7 +120,37 @@ public List<String> getMasterDataTableNames() {
115120 return MASTER_DATA_TABLES .stream ().map (Named ::getName ).toList ();
116121 }
117122
118- public void exportZip (OutputStream out , ExportType exportType ) throws IOException {
123+ public void exportZip (HttpServletResponse response , ExportType exportType ) throws IOException {
124+ String fileName = "data-export_" + System .currentTimeMillis () + ".zip" ;
125+ String headerKey = "Content-Disposition" ;
126+ String headerValue = "attachment; filename=\" %s\" " .formatted (fileName );
127+ response .setHeader (headerKey , headerValue );
128+ response .setContentType ("application/zip" );
129+
130+ exportZip (response .getOutputStream (), exportType );
131+ }
132+
133+ public void importZip (MultipartFile file ) throws IOException {
134+ if (file .isEmpty ()) {
135+ throw new SteveException .BadRequest ("File is empty" );
136+ }
137+
138+ String fileName = file .getOriginalFilename ();
139+
140+ if (StringUtils .isEmpty (fileName )) {
141+ throw new SteveException .BadRequest ("File name is empty" );
142+ }
143+
144+ if (!fileName .endsWith (".zip" )) {
145+ throw new SteveException .BadRequest ("File must be a ZIP archive" );
146+ }
147+
148+ importZip (file .getInputStream ());
149+ }
150+
151+ private void exportZip (OutputStream out , ExportType exportType ) throws IOException {
152+ long start = System .currentTimeMillis ();
153+
119154 try (ZipOutputStream zipOut = new ZipOutputStream (out );
120155 OutputStreamWriter writer = new OutputStreamWriter (zipOut , StandardCharsets .UTF_8 )) {
121156
@@ -136,10 +171,17 @@ public void exportZip(OutputStream out, ExportType exportType) throws IOExceptio
136171 }
137172 }
138173 zipOut .finish ();
174+
175+ } finally {
176+ long stop = System .currentTimeMillis ();
177+ long durationSeconds = TimeUnit .MILLISECONDS .toSeconds (stop - start );
178+ log .info ("Data export finished in {} seconds." , durationSeconds );
139179 }
140180 }
141181
142- public void importZip (InputStream in ) throws IOException {
182+ private void importZip (InputStream in ) throws IOException {
183+ long start = System .currentTimeMillis ();
184+
143185 dataImportExportRepository .beforeImport ();
144186
145187 try (ZipInputStream zipIn = new ZipInputStream (in )) {
@@ -170,6 +212,10 @@ public void close() throws IOException {
170212 }
171213 } finally {
172214 dataImportExportRepository .afterImport ();
215+
216+ long stop = System .currentTimeMillis ();
217+ long durationSeconds = TimeUnit .MILLISECONDS .toSeconds (stop - start );
218+ log .info ("Data import finished in {} seconds." , durationSeconds );
173219 }
174220 }
175221
0 commit comments