View Javadoc
1   package org.woehlke.bloodmoney.measurements;
2   
3   import com.opencsv.CSVWriter;
4   import com.opencsv.bean.StatefulBeanToCsv;
5   import com.opencsv.bean.StatefulBeanToCsvBuilder;
6   import lombok.extern.slf4j.Slf4j;
7   import org.springframework.beans.factory.annotation.Autowired;
8   import org.springframework.http.HttpHeaders;
9   import org.springframework.web.bind.annotation.*;
10  import org.woehlke.bloodmoney.config.BloodMoneyProperties;
11  
12  import javax.servlet.http.HttpServletResponse;
13  
14  @Slf4j
15  @RestController
16  @RequestMapping("/measurement/export")
17  @SessionAttributes("userSession")
18  public class BloodPressureMeasurementControllerExport {
19  
20      @GetMapping("/all")
21      @ResponseBody
22      public void exportAll(HttpServletResponse response) throws Exception  {
23          this.exportCSV(response);
24      }
25  
26      @GetMapping("/all/csv")
27      @ResponseBody
28      public void exportCSV(HttpServletResponse response) throws Exception  {
29          //set file name and content type
30          response.setContentType("text/csv");
31          response.setHeader(HttpHeaders.CONTENT_DISPOSITION,
32              "attachment; filename=\"" + this.bloodMoneyProperties.getWebConfig().getExportFilename() + "\"");
33          //create a csv writer
34          StatefulBeanToCsv<BloodPressureMeasurementEntity> writer = new StatefulBeanToCsvBuilder<BloodPressureMeasurementEntity>(response.getWriter())
35              .withQuotechar(CSVWriter.NO_QUOTE_CHARACTER)
36              .withSeparator(this.bloodMoneyProperties.getWebConfig().getExportFilenameSeparator().charAt(0))
37              .withOrderedResults(false)
38              .build();
39          //write all Measurements to csv file
40          writer.write(bloodPressureMeasurementService.getAll());
41      }
42  
43      @GetMapping("/all/xml")
44      @ResponseBody
45      public void exportXML(HttpServletResponse response) throws Exception  {
46          //set file name and content type
47          response.setContentType("text/csv");
48          response.setHeader(HttpHeaders.CONTENT_DISPOSITION,
49              "attachment; filename=\"" + this.bloodMoneyProperties.getWebConfig().getExportFilename() + "\"");
50          //create a csv writer
51          StatefulBeanToCsv<BloodPressureMeasurementEntity> writer = new StatefulBeanToCsvBuilder<BloodPressureMeasurementEntity>(response.getWriter())
52              .withQuotechar(CSVWriter.NO_QUOTE_CHARACTER)
53              .withSeparator(this.bloodMoneyProperties.getWebConfig().getExportFilenameSeparator().charAt(0))
54              .withOrderedResults(false)
55              .build();
56          //write all Measurements to csv file
57          writer.write(bloodPressureMeasurementService.getAll());
58      }
59  
60      @GetMapping("/all/json")
61      @ResponseBody
62      public void exportJSON(HttpServletResponse response) throws Exception  {
63          //set file name and content type
64          response.setContentType("text/csv");
65          response.setHeader(HttpHeaders.CONTENT_DISPOSITION,
66              "attachment; filename=\"" + this.bloodMoneyProperties.getWebConfig().getExportFilename() + "\"");
67          //create a csv writer
68          StatefulBeanToCsv<BloodPressureMeasurementEntity> writer = new StatefulBeanToCsvBuilder<BloodPressureMeasurementEntity>(response.getWriter())
69              .withQuotechar(CSVWriter.NO_QUOTE_CHARACTER)
70              .withSeparator(this.bloodMoneyProperties.getWebConfig().getExportFilenameSeparator().charAt(0))
71              .withOrderedResults(false)
72              .build();
73          //write all Measurements to csv file
74          writer.write(bloodPressureMeasurementService.getAll());
75      }
76  
77      private final BloodPressureMeasurementService bloodPressureMeasurementService;
78      private final BloodMoneyProperties bloodMoneyProperties;
79  
80      @Autowired
81      public BloodPressureMeasurementControllerExport(
82          BloodPressureMeasurementService bloodPressureMeasurementService,
83          BloodMoneyProperties bloodMoneyProperties
84      ) {
85          this.bloodPressureMeasurementService = bloodPressureMeasurementService;
86          this.bloodMoneyProperties = bloodMoneyProperties;
87      }
88  }