Projekt

Obecné

Profil

Stáhnout (3.35 KB) Statistiky
| Větev: | Revize:
1
package vldc.aswi.service;
2

    
3
import lombok.extern.slf4j.Slf4j;
4
import org.apache.commons.compress.utils.IOUtils;
5
import org.springframework.beans.factory.annotation.Autowired;
6
import org.springframework.stereotype.Service;
7
import vldc.aswi.domain.Configuration;
8
import vldc.aswi.model.ExportType;
9
import vldc.aswi.model.table.contingencyTable.ContingencyTableRow;
10
import vldc.aswi.utils.ExporterPDF;
11
import vldc.aswi.utils.ExporterXLSX;
12

    
13
import javax.servlet.ServletOutputStream;
14
import javax.servlet.http.HttpServletResponse;
15
import java.io.BufferedInputStream;
16
import java.io.File;
17
import java.io.FileInputStream;
18
import java.io.InputStream;
19
import java.util.List;
20

    
21
/**
22
 * Manager for Export.
23
 */
24
@Service
25
@Slf4j
26
public class ExportManagerImpl implements ExportManager {
27

    
28
    /** Autowired sql query manager. */
29
    @Autowired
30
    private SqlQueryManager sqlQueryManager;
31

    
32
    /**
33
     * Export contingency table to specific export type.
34
     * @param newConfiguration - Exported configuration.
35
     * @param isNotRemoveEmpty - If blank rows / columns must be inherit or not.
36
     * @param response - Http response.
37
     * @param type - Export type.
38
     */
39
    @Override
40
    public void exportContingencyTable(Configuration newConfiguration, boolean isNotRemoveEmpty, HttpServletResponse response, ExportType type) {
41

    
42
        // Get contingency table rows.
43
        List<ContingencyTableRow> contingencyTableRows = this.sqlQueryManager.getContingencyTableRow(newConfiguration.getAssembly(),
44
                newConfiguration.getParametersInConfiguration(), isNotRemoveEmpty);
45

    
46
        try {
47
            File tmpFile = null;
48

    
49
            switch(type) {
50

    
51
                case PDF:
52

    
53
                    // Create temporary file.
54
                    tmpFile = File.createTempFile("outPDF", ".pdf");
55

    
56
                    // Export contingency table rows to pdf format.
57
                    ExporterPDF.export(tmpFile, contingencyTableRows);
58

    
59
                    // Set content type to pdf.
60
                    response.setContentType("application/pdf");
61
                    break;
62
                case XLSX:
63

    
64
                    // Create temporary file.
65
                    tmpFile = File.createTempFile("outXLSX", ".xlsx");
66

    
67
                    // Export contingency table rows to xlsx format.
68
                    ExporterXLSX.export(tmpFile, newConfiguration.getTableName(), contingencyTableRows);
69

    
70
                    // Set content type to xlsx.
71
                    response.setContentType("application/xlsx");
72
                    break;
73
            }
74

    
75
            // Force "Save As..." dialog.
76
            response.setHeader("Content-Disposition", "attachment; filename=" + tmpFile + "");
77
            response.setContentLength((int)tmpFile.length());
78

    
79
            // Copy temporary file content to response.
80
            InputStream in = new BufferedInputStream(new FileInputStream(tmpFile));
81
            ServletOutputStream out = response.getOutputStream();
82

    
83
            IOUtils.copy(in, out);
84
            in.close();
85

    
86
            out.flush();
87
            out.close();
88

    
89
            // Delete temporary file.
90
            tmpFile.delete();
91

    
92
            System.out.println("pdf successfully exported");
93
            // TODO: print success message - file created.
94

    
95
        } catch (Exception e) {
96
            System.out.println("pdf export FAILED 1");
97
            // TODO: print error message - file not created.
98
        }
99
    }
100
}
(6-6/18)