Revize d90fc9de
Přidáno uživatelem Vojtěch Danišík před více než 4 roky(ů)
pom.xml | ||
---|---|---|
113 | 113 |
<artifactId>openpdf</artifactId> |
114 | 114 |
<version>1.3.14</version> |
115 | 115 |
</dependency> |
116 |
|
|
117 |
<dependency> |
|
118 |
<groupId>com.github.dhorions</groupId> |
|
119 |
<artifactId>boxable</artifactId> |
|
120 |
<version>1.5</version> |
|
121 |
</dependency> |
|
116 | 122 |
</dependencies> |
117 | 123 |
|
118 | 124 |
<build> |
src/main/java/vldc/aswi/model/ExportType.java | ||
---|---|---|
1 |
package vldc.aswi.model; |
|
2 |
|
|
3 |
/** |
|
4 |
* Export types. |
|
5 |
*/ |
|
6 |
public enum ExportType { |
|
7 |
PDF, |
|
8 |
XLSX |
|
9 |
} |
src/main/java/vldc/aswi/service/ExportManager.java | ||
---|---|---|
1 |
package vldc.aswi.service; |
|
2 |
|
|
3 |
import vldc.aswi.domain.Configuration; |
|
4 |
import vldc.aswi.model.ExportType; |
|
5 |
|
|
6 |
import javax.servlet.http.HttpServletResponse; |
|
7 |
|
|
8 |
/** |
|
9 |
* Interface for Export manager. |
|
10 |
*/ |
|
11 |
public interface ExportManager { |
|
12 |
|
|
13 |
/** |
|
14 |
* Export contingency table to specific export type. |
|
15 |
* @param newConfiguration - Exported configuration. |
|
16 |
* @param isNotRemoveEmpty - If blank rows / columns must be inherit or not. |
|
17 |
* @param response - Http response. |
|
18 |
* @param type - Export type. |
|
19 |
*/ |
|
20 |
void exportContingencyTable(Configuration newConfiguration, boolean isNotRemoveEmpty, HttpServletResponse response, ExportType type); |
|
21 |
} |
src/main/java/vldc/aswi/service/ExportManagerImpl.java | ||
---|---|---|
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 |
} |
src/main/java/vldc/aswi/utils/ExporterPDF.java | ||
---|---|---|
1 | 1 |
package vldc.aswi.utils; |
2 | 2 |
|
3 |
import com.lowagie.text.Cell;
|
|
4 |
import com.lowagie.text.Document;
|
|
3 |
import be.quodlibet.boxable.BaseTable;
|
|
4 |
import com.lowagie.text.*;
|
|
5 | 5 |
import com.lowagie.text.Font; |
6 |
import com.lowagie.text.Phrase; |
|
6 |
import com.lowagie.text.alignment.HorizontalAlignment; |
|
7 |
import com.lowagie.text.alignment.VerticalAlignment; |
|
7 | 8 |
import com.lowagie.text.pdf.PdfWriter; |
9 |
import com.sun.media.jfxmedia.logging.Logger; |
|
10 |
import org.apache.pdfbox.pdmodel.PDPage; |
|
11 |
import org.apache.pdfbox.pdmodel.font.PDType1Font; |
|
12 |
import vldc.aswi.model.table.contingencyTable.ContingencyTableRow; |
|
13 |
import vldc.aswi.model.table.contingencyTable.ContingencyTableRowCell; |
|
8 | 14 |
|
9 |
import java.io.FileNotFoundException; |
|
15 |
import java.awt.*; |
|
16 |
import java.io.File; |
|
10 | 17 |
import java.io.FileOutputStream; |
18 |
import java.util.List; |
|
11 | 19 |
|
12 | 20 |
/** |
13 |
* Class used for exporting data to PDF files |
|
21 |
* Class used for exporting data to PDF files.
|
|
14 | 22 |
*/ |
15 | 23 |
public class ExporterPDF { |
16 | 24 |
|
17 | 25 |
/** |
18 |
* !!!DUMMY!!! |
|
19 | 26 |
* Exports data to .pdf file |
20 |
* @param tableName name of the table |
|
27 |
* @param tmpFile - Temporary file. |
|
28 |
* @param contingencyTableRows - Contingency table rows. |
|
21 | 29 |
*/ |
22 |
public static void export(String tableName) throws FileNotFoundException { |
|
23 |
Document doc = new Document(); |
|
30 |
public static void export(File tmpFile, List<ContingencyTableRow> contingencyTableRows) { |
|
24 | 31 |
|
25 |
PdfWriter.getInstance(doc, new FileOutputStream("out.pdf")); |
|
32 |
try { |
|
33 |
if (contingencyTableRows.size() == 0) { |
|
26 | 34 |
|
27 |
Font font = new Font(com.lowagie.text.Font.COURIER, 14);
|
|
28 |
com.lowagie.text.Table table = new com.lowagie.text.Table(3);
|
|
35 |
return;
|
|
36 |
}
|
|
29 | 37 |
|
30 |
Cell cell = new Cell(new Phrase(tableName, font)); |
|
31 |
cell.setColspan(2); |
|
32 |
cell.setHeader(true); |
|
38 |
// Create new document. |
|
39 |
Document doc = new Document(); |
|
33 | 40 |
|
34 |
Cell cell2 = new Cell(new Phrase("SUM", font)); |
|
35 |
cell2.setColspan(1); |
|
36 |
cell2.setHeader(true); |
|
41 |
// Get instance of PdfWriter. |
|
42 |
PdfWriter.getInstance(doc, new FileOutputStream(tmpFile)); |
|
37 | 43 |
|
38 |
table.addCell(cell); |
|
39 |
table.addCell(cell2); |
|
44 |
// Create fonts. |
|
45 |
Font colRowFont = new Font(Font.HELVETICA, 14, Font.BOLD); |
|
46 |
Font dataFont = new Font(Font.HELVETICA, 12); |
|
40 | 47 |
|
41 |
table.addCell(new Phrase("Val1", font));
|
|
42 |
table.addCell(new Phrase("Val2", font));
|
|
43 |
table.addCell(new Phrase("", font));
|
|
48 |
// Calculate column and row count.
|
|
49 |
int columnsCount = getColumnCount(contingencyTableRows.get(0));
|
|
50 |
int rowsCount = contingencyTableRows.size();
|
|
44 | 51 |
|
45 |
table.addCell("1"); |
|
46 |
table.addCell("2"); |
|
47 |
table.addCell("3"); |
|
52 |
// Create table. |
|
53 |
Table table = new Table(columnsCount, rowsCount); |
|
48 | 54 |
|
49 |
table.addCell("4"); |
|
50 |
table.addCell("5"); |
|
51 |
table.addCell("9"); |
|
55 |
for (ContingencyTableRow row : contingencyTableRows) { |
|
52 | 56 |
|
53 |
doc.open(); |
|
54 |
doc.add(table); |
|
57 |
if (row.isHeader()) { |
|
58 |
// Row is header. |
|
59 |
setColRow(table, row.getCells(), colRowFont); |
|
60 |
} else { |
|
61 |
// Row contains data. |
|
62 |
setDataRow(table, row.getCells(), colRowFont, dataFont); |
|
63 |
} |
|
64 |
} |
|
55 | 65 |
|
56 |
doc.close(); |
|
66 |
// Open document and write created table into it. |
|
67 |
doc.open(); |
|
68 |
doc.add(table); |
|
69 |
|
|
70 |
doc.close(); |
|
71 |
} catch (Exception e) { |
|
72 |
|
|
73 |
Logger.logMsg(Logger.WARNING, "Error while exporting contingency table to PDF."); |
|
74 |
Logger.logMsg(Logger.WARNING, e.getStackTrace().toString()); |
|
75 |
} |
|
76 |
} |
|
77 |
|
|
78 |
/** |
|
79 |
* Set column header row. |
|
80 |
* @param table - Table. |
|
81 |
* @param cells - List of contingency table cells. |
|
82 |
* @param colRowFont - Column row styles. |
|
83 |
* @throws BadElementException If cell is not created successfully. |
|
84 |
*/ |
|
85 |
private static void setColRow(Table table, List<ContingencyTableRowCell> cells, Font colRowFont) throws BadElementException { |
|
86 |
|
|
87 |
for (ContingencyTableRowCell cell : cells) { |
|
88 |
|
|
89 |
// Create and prepare pdf cell. |
|
90 |
Cell pdfCell = prepareCell(cell.getValue(), colRowFont, cell.getColSpan()); |
|
91 |
pdfCell.setHeader(true); |
|
92 |
table.addCell(pdfCell); |
|
93 |
} |
|
94 |
} |
|
95 |
|
|
96 |
/** |
|
97 |
* Set data row. |
|
98 |
* @param table - Table. |
|
99 |
* @param cells - List of contingency table cells. |
|
100 |
* @param colRowFont - Column row styles. |
|
101 |
* @param dataFont - Data row styles. |
|
102 |
* @throws BadElementException If cell is not created successfully. |
|
103 |
*/ |
|
104 |
private static void setDataRow(Table table, List<ContingencyTableRowCell> cells, Font colRowFont, Font dataFont) throws BadElementException { |
|
105 |
|
|
106 |
boolean headerWrited = false; |
|
107 |
|
|
108 |
for (ContingencyTableRowCell cell : cells) { |
|
109 |
|
|
110 |
// Create pdf cell. |
|
111 |
Cell pdfCell; |
|
112 |
|
|
113 |
// First cell is always header, so apply different style. |
|
114 |
if (!headerWrited) { |
|
115 |
|
|
116 |
headerWrited = true; |
|
117 |
|
|
118 |
// Prepare pdf cell. |
|
119 |
pdfCell = prepareCell(cell.getValue(), colRowFont, cell.getColSpan()); |
|
120 |
pdfCell.setHeader(true); |
|
121 |
|
|
122 |
} else { |
|
123 |
|
|
124 |
// Prepare pdf cell. |
|
125 |
pdfCell = prepareCell(cell.getValue(), dataFont, cell.getColSpan()); |
|
126 |
} |
|
127 |
|
|
128 |
table.addCell(pdfCell); |
|
129 |
} |
|
130 |
} |
|
131 |
|
|
132 |
/** |
|
133 |
* Prepare pdf cell. |
|
134 |
* @param value - Cell value. |
|
135 |
* @param font - Cell font. |
|
136 |
* @param colSpan - Cell colspan. |
|
137 |
* @return Prepared pdf cell. |
|
138 |
*/ |
|
139 |
private static Cell prepareCell(String value, Font font, int colSpan) { |
|
140 |
|
|
141 |
Cell cell = new Cell(new Phrase(value, font)); |
|
142 |
cell.setColspan(colSpan); |
|
143 |
cell.setHorizontalAlignment(HorizontalAlignment.CENTER); |
|
144 |
cell.setVerticalAlignment(VerticalAlignment.CENTER); |
|
145 |
|
|
146 |
return cell; |
|
147 |
} |
|
148 |
|
|
149 |
/** |
|
150 |
* Calculate column count in contingency table row. |
|
151 |
* @param firstRow - First row of contingency table. |
|
152 |
* @return Column count in contingency table. |
|
153 |
*/ |
|
154 |
private static int getColumnCount(ContingencyTableRow firstRow) { |
|
155 |
|
|
156 |
int columnCount = 0; |
|
157 |
|
|
158 |
for (ContingencyTableRowCell cell : firstRow.getCells()) { |
|
159 |
|
|
160 |
columnCount += cell.getColSpan(); |
|
161 |
} |
|
162 |
|
|
163 |
return columnCount; |
|
164 |
} |
|
165 |
|
|
166 |
|
|
167 |
public static void exportt(File tmpFile, List<ContingencyTableRow> contingencyTableRows) { |
|
168 |
/* |
|
169 |
//Dummy Table |
|
170 |
float margin = 50; |
|
171 |
|
|
172 |
// starting y position is whole page height subtracted by top and bottom margin |
|
173 |
float yStartNewPage = myPage.getMediaBox().getHeight() - (2 * margin); |
|
174 |
|
|
175 |
// we want table across whole page width (subtracted by left and right margin ofcourse) |
|
176 |
float tableWidth = myPage.getMediaBox().getWidth() - (2 * margin); |
|
177 |
|
|
178 |
boolean drawContent = true; |
|
179 |
float yStart = yStartNewPage; |
|
180 |
float bottomMargin = 70; |
|
181 |
// y position is your coordinate of top left corner of the table |
|
182 |
float yPosition = 550; |
|
183 |
|
|
184 |
BaseTable table = new BaseTable(yPosition, yStartNewPage, bottomMargin, tableWidth, margin, mainDocument, myPage, true, drawContent); |
|
185 |
|
|
186 |
|
|
187 |
Row<PDPage> headerRow = table.createRow(15f); |
|
188 |
Cell<PDPage> cell = headerRow.createCell(100, "Header"); |
|
189 |
table.addHeaderRow(headerRow); |
|
190 |
|
|
191 |
|
|
192 |
Row<PDPage> row = table.createRow(12); |
|
193 |
cell = row.createCell(30, "Data 1"); |
|
194 |
cell = row.createCell(70, "Some value"); |
|
195 |
|
|
196 |
table.draw(); |
|
197 |
|
|
198 |
|
|
199 |
contentStream.close(); |
|
200 |
mainDocument.addPage(myPage); |
|
201 |
mainDocument.save("testfile.pdf"); |
|
202 |
mainDocument.close(); |
|
203 |
*/ |
|
57 | 204 |
} |
58 | 205 |
} |
src/main/java/vldc/aswi/utils/ExporterXLSX.java | ||
---|---|---|
1 | 1 |
package vldc.aswi.utils; |
2 | 2 |
|
3 | 3 |
import org.apache.poi.ss.usermodel.*; |
4 |
import org.apache.poi.ss.util.CellRangeAddress; |
|
4 | 5 |
import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
6 |
import vldc.aswi.model.table.contingencyTable.ContingencyTableRow; |
|
7 |
import vldc.aswi.model.table.contingencyTable.ContingencyTableRowCell; |
|
5 | 8 |
|
6 |
import java.io.FileNotFoundException;
|
|
9 |
import java.io.File; |
|
7 | 10 |
import java.io.FileOutputStream; |
8 |
import java.io.IOException;
|
|
11 |
import java.util.List;
|
|
9 | 12 |
|
10 | 13 |
/** |
11 |
* Class used for exporting data to XLSX |
|
14 |
* Class used for exporting data to XLSX.
|
|
12 | 15 |
*/ |
13 | 16 |
public class ExporterXLSX { |
14 | 17 |
|
15 | 18 |
/** |
16 |
* !!!DUMMY!!! |
|
17 | 19 |
* Exports data to .xlsx file |
18 |
* @param tableName name of the table |
|
20 |
* @param tableName - name of the table. |
|
21 |
* @param contingencyTableRows - List of contingency table rows. |
|
19 | 22 |
*/ |
20 |
public static void export(String tableName) { |
|
21 |
|
|
22 |
//temporary dummy implementation, used for testing |
|
23 |
String[] columns = new String[] {"studuje", "nestuduje"}; |
|
24 |
String[] rows = new String[] {"fav", "fst"}; |
|
25 |
String[] data = new String[] {"1", "2", "3", "4"}; |
|
23 |
public static void export(File tmpFile, String tableName, List<ContingencyTableRow> contingencyTableRows) { |
|
26 | 24 |
|
25 |
// Create workbook. |
|
27 | 26 |
Workbook workbook = new XSSFWorkbook(); |
28 | 27 |
|
28 |
// Create sheet. |
|
29 | 29 |
Sheet sheet = workbook.createSheet(tableName); |
30 | 30 |
|
31 |
// Create font for row names. |
|
31 | 32 |
Font colRowNameFont = workbook.createFont(); |
32 | 33 |
colRowNameFont.setBold(true); |
33 | 34 |
colRowNameFont.setFontHeightInPoints((short) 16); |
35 |
colRowNameFont.setFontName("HELVETICA"); |
|
34 | 36 |
colRowNameFont.setColor(IndexedColors.BLACK.getIndex()); |
35 | 37 |
|
38 |
// Create font for data. |
|
36 | 39 |
Font dataFont = workbook.createFont(); |
40 |
dataFont.setFontName("HELVETICA"); |
|
37 | 41 |
dataFont.setBold(false); |
38 | 42 |
dataFont.setFontHeightInPoints((short) 12); |
39 | 43 |
dataFont.setColor(IndexedColors.BLACK.getIndex()); |
40 | 44 |
|
45 |
// Set row cell style. |
|
41 | 46 |
CellStyle colRowStyle = workbook.createCellStyle(); |
42 | 47 |
colRowStyle.setFont(colRowNameFont); |
48 |
colRowStyle.setAlignment(HorizontalAlignment.CENTER); |
|
43 | 49 |
|
50 |
// Set data cell style. |
|
44 | 51 |
CellStyle dataStyle = workbook.createCellStyle(); |
45 | 52 |
dataStyle.setFont(dataFont); |
53 |
dataStyle.setAlignment(HorizontalAlignment.CENTER); |
|
46 | 54 |
|
47 |
Row descRow = sheet.createRow(0);
|
|
55 |
int rowCounter = 0;
|
|
48 | 56 |
|
49 |
for (int i = 0; i < 2; i++) { |
|
50 |
Cell cell = descRow.createCell(i + 1); |
|
51 |
cell.setCellValue(columns[i]); |
|
52 |
cell.setCellStyle(colRowStyle); |
|
53 |
} |
|
57 |
for (ContingencyTableRow row : contingencyTableRows) { |
|
54 | 58 |
|
55 |
for (int i = 0; i < 2; i++) { |
|
56 |
Row row = sheet.createRow(i + 1); |
|
57 |
Cell nameCell = row.createCell(0); |
|
58 |
nameCell.setCellValue(rows[i]); |
|
59 |
nameCell.setCellStyle(colRowStyle); |
|
59 |
Row xlsxRow = sheet.createRow(rowCounter); |
|
60 | 60 |
|
61 |
Cell cell1 = row.createCell(1); |
|
62 |
cell1.setCellValue(data[i + i + 1]); |
|
63 |
cell1.setCellStyle(dataStyle); |
|
61 |
if (row.isHeader()) { |
|
62 |
// Row is header. |
|
63 |
setColRow(sheet, xlsxRow, row.getCells(), colRowStyle); |
|
64 |
} |
|
65 |
else { |
|
66 |
// Row contains data. |
|
67 |
setDataRow(sheet, xlsxRow, row.getCells(), colRowStyle, dataStyle); |
|
68 |
} |
|
64 | 69 |
|
65 |
Cell cell2 = row.createCell(2); |
|
66 |
cell2.setCellValue(data[i + i + 1]); |
|
67 |
cell2.setCellStyle(dataStyle); |
|
70 |
rowCounter++; |
|
68 | 71 |
} |
69 | 72 |
|
70 |
for (int i = 0; i < 3; i++) { |
|
71 |
sheet.autoSizeColumn(i); |
|
72 |
} |
|
73 |
try { |
|
73 | 74 |
|
74 |
//merge of columns, will be used for column headers |
|
75 |
//sheet.addMergedRegion(new CellRangeAddress(0,0,1,2)); |
|
75 |
FileOutputStream out = new FileOutputStream(tmpFile); |
|
76 |
workbook.write(out); |
|
77 |
out.close(); |
|
76 | 78 |
|
77 |
FileOutputStream streamOut = null;
|
|
79 |
} catch (Exception e) {
|
|
78 | 80 |
|
79 |
try { |
|
80 |
streamOut = new FileOutputStream("out.xlsx"); |
|
81 |
} catch (FileNotFoundException e) { |
|
82 |
e.printStackTrace(); |
|
83 | 81 |
} |
82 |
} |
|
84 | 83 |
|
85 |
try { |
|
86 |
workbook.write(streamOut); |
|
87 |
} catch (IOException e) { |
|
88 |
e.printStackTrace(); |
|
89 |
} |
|
84 |
/** |
|
85 |
* Set column row. |
|
86 |
* @param sheet - Sheet. |
|
87 |
* @param row - Row. |
|
88 |
* @param cells - List of contingency table cells. |
|
89 |
* @param colRowStyle - Column row styles. |
|
90 |
*/ |
|
91 |
private static void setColRow(Sheet sheet, Row row, List<ContingencyTableRowCell> cells, CellStyle colRowStyle) { |
|
90 | 92 |
|
91 |
try { |
|
92 |
if (streamOut != null) { |
|
93 |
streamOut.close(); |
|
93 |
int cellCounter = 0; |
|
94 |
|
|
95 |
for (ContingencyTableRowCell cell : cells) { |
|
96 |
|
|
97 |
// Create new xlsx cell. |
|
98 |
Cell xlsxCell = row.createCell(cellCounter); |
|
99 |
xlsxCell.setCellValue(cell.getValue()); |
|
100 |
xlsxCell.setCellStyle(colRowStyle); |
|
101 |
|
|
102 |
// Calculate last cell to be merged with current. |
|
103 |
int colSpan = cell.getColSpan(); |
|
104 |
int newCellPosition = cellCounter + (colSpan - 1); |
|
105 |
|
|
106 |
// If colSpan is higher than 1, then merge cells. |
|
107 |
if (colSpan > 1) { |
|
108 |
sheet.addMergedRegion(new CellRangeAddress(row.getRowNum(), row.getRowNum(), cellCounter, newCellPosition)); |
|
109 |
} |
|
110 |
|
|
111 |
// If cell has some value, then autosize current column. |
|
112 |
if (!cell.getValue().equals("")) { |
|
113 |
|
|
114 |
for (int i = cellCounter; i < (newCellPosition + 1); i++) { |
|
115 |
sheet.autoSizeColumn(cellCounter); |
|
116 |
} |
|
94 | 117 |
} |
95 |
} catch (IOException e) { |
|
96 |
e.printStackTrace();
|
|
118 |
|
|
119 |
cellCounter = newCellPosition + 1;
|
|
97 | 120 |
} |
121 |
} |
|
98 | 122 |
|
99 |
try { |
|
100 |
workbook.close(); |
|
101 |
} catch (IOException e) { |
|
102 |
e.printStackTrace(); |
|
123 |
/** |
|
124 |
* Set data row. |
|
125 |
* @param sheet - Sheet. |
|
126 |
* @param row - Row. |
|
127 |
* @param cells - List of contingency table cells. |
|
128 |
* @param colRowStyle - Column row styles. |
|
129 |
* @param dataStyle - Data row styles. |
|
130 |
*/ |
|
131 |
private static void setDataRow(Sheet sheet, Row row, List<ContingencyTableRowCell> cells, CellStyle colRowStyle, CellStyle dataStyle) { |
|
132 |
|
|
133 |
int cellCounter = 0; |
|
134 |
|
|
135 |
for (ContingencyTableRowCell cell : cells) { |
|
136 |
|
|
137 |
// Create new xlsx cell. |
|
138 |
Cell xlsxCell = row.createCell(cellCounter); |
|
139 |
xlsxCell.setCellValue(cell.getValue()); |
|
140 |
|
|
141 |
// First cell is always header, so apply different style. |
|
142 |
if (cellCounter == 0) { |
|
143 |
xlsxCell.setCellStyle(colRowStyle); |
|
144 |
} else { |
|
145 |
xlsxCell.setCellStyle(dataStyle); |
|
146 |
} |
|
147 |
|
|
148 |
sheet.autoSizeColumn(cellCounter); |
|
149 |
cellCounter++; |
|
103 | 150 |
} |
104 | 151 |
} |
105 | 152 |
} |
src/main/java/vldc/aswi/web/controller/AssemblyController.java | ||
---|---|---|
12 | 12 |
import vldc.aswi.domain.*; |
13 | 13 |
import vldc.aswi.domain.parameter.Parameter; |
14 | 14 |
import vldc.aswi.domain.parameter.ParameterInConfiguration; |
15 |
import vldc.aswi.model.table.BooleanWrapper;
|
|
15 |
import vldc.aswi.model.ExportType;
|
|
16 | 16 |
import vldc.aswi.model.table.contingencyTable.ContingencyTableRow; |
17 | 17 |
import vldc.aswi.service.*; |
18 | 18 |
import vldc.aswi.service.parameter.ParameterManager; |
... | ... | |
21 | 21 |
import vldc.aswi.validators.AssemblyValidator; |
22 | 22 |
import vldc.aswi.utils.Utils; |
23 | 23 |
import vldc.aswi.validators.ConfigurationValidator; |
24 |
|
|
24 |
import javax.servlet.http.HttpServletResponse; |
|
25 | 25 |
import javax.validation.Valid; |
26 |
import java.util.*; |
|
27 |
import java.util.stream.Collectors; |
|
26 |
import java.util.ArrayList; |
|
27 |
import java.util.Comparator; |
|
28 |
import java.util.List; |
|
28 | 29 |
|
29 | 30 |
/** |
30 | 31 |
* Controller for assemblies and configurations |
... | ... | |
44 | 45 |
@Autowired |
45 | 46 |
private ParameterManager parameterManager; |
46 | 47 |
|
48 |
/** Autowired export manager. */ |
|
49 |
@Autowired |
|
50 |
private ExportManager exportManager; |
|
51 |
|
|
47 | 52 |
/** Autowired configuration manager */ |
48 | 53 |
@Autowired |
49 | 54 |
private ConfigurationManager configurationManager; |
... | ... | |
151 | 156 |
return modelAndView; |
152 | 157 |
} |
153 | 158 |
|
154 |
|
|
155 | 159 |
/** |
156 | 160 |
* Post method for form, where configuration is created from assembly. |
157 | 161 |
* @param newConfiguration - Configuration values. |
158 | 162 |
* @param bindingResult - Error results from assembly validators. |
159 |
* @param atts - Redirect attributes. |
|
160 | 163 |
* @return ModelAndView for assembly. |
161 | 164 |
*/ |
162 | 165 |
@PostMapping("/assembly") |
... | ... | |
166 | 169 |
@RequestParam("isNotRemoveEmpty") String isNotRemoveEmptyString, |
167 | 170 |
@RequestParam("assemblyID") String id, |
168 | 171 |
@RequestParam(required=false, value="generateTable") String generateTable, |
169 |
@RequestParam(required=false, value="exportXls") String exportXls,
|
|
172 |
@RequestParam(required=false, value="exportXlsx") String exportXlsx,
|
|
170 | 173 |
@RequestParam(required=false, value="exportPdf") String exportPdf, |
171 | 174 |
@RequestParam(required=false, value="saveConfiguration") String saveConfiguration, |
172 |
RedirectAttributes redirectAttributes) |
|
175 |
RedirectAttributes redirectAttributes, |
|
176 |
HttpServletResponse response |
|
177 |
) |
|
173 | 178 |
{ |
174 | 179 |
|
175 | 180 |
ModelAndView modelAndView = new ModelAndView(); |
176 | 181 |
ModelMap modelMap = modelAndView.getModelMap(); |
182 |
boolean isNotRemoveEmpty = isNotRemoveEmptyString == null || !isNotRemoveEmptyString.equals(""); |
|
177 | 183 |
|
178 | 184 |
if (bindingResult.hasErrors()) { |
179 | 185 |
// TODO: 04.05.2020 Error message |
... | ... | |
193 | 199 |
newConfiguration.setName(assembly.getName()); |
194 | 200 |
} |
195 | 201 |
|
196 |
System.out.println("Generuj tabulku"); |
|
197 | 202 |
prepareForTable(newConfiguration); |
198 | 203 |
BindingResult errors = new BeanPropertyBindingResult(newConfiguration, newConfiguration.getClass().getName()); |
199 | 204 |
configurationValidator.validate(newConfiguration, errors); |
... | ... | |
204 | 209 |
modelMap.addAttribute(assemblyTitleName, generateTableTitleText + " " + newConfiguration.getName()); |
205 | 210 |
} |
206 | 211 |
else { |
207 |
boolean isNotRemoveEmpty = isNotRemoveEmptyString == null || !isNotRemoveEmptyString.equals(""); |
|
208 | 212 |
List<ContingencyTableRow> rows = this.sqlQueryManager.getContingencyTableRow(newConfiguration.getAssembly(), |
209 | 213 |
newConfiguration.getParametersInConfiguration(), isNotRemoveEmpty); |
210 | 214 |
|
211 |
System.out.println("Generuj tabulku"); |
|
212 | 215 |
modelMap.addAttribute("configurationID", id); |
213 | 216 |
modelMap.addAttribute("contingencyTableRows", rows); |
214 | 217 |
modelMap.addAttribute("isNotRemoveEmpty", isNotRemoveEmpty); |
215 | 218 |
addConfigurationDataIntoModelAndView(newConfiguration, modelAndView, modelMap); |
216 | 219 |
} |
217 | 220 |
} |
218 |
else if (exportXls != null) |
|
221 |
else if (exportXlsx != null)
|
|
219 | 222 |
{ |
220 |
System.out.println("Generuj XLS"); |
|
223 |
prepareForTable(newConfiguration); |
|
224 |
this.exportManager.exportContingencyTable(newConfiguration, isNotRemoveEmpty, response, ExportType.XLSX); |
|
225 |
|
|
226 |
// Return null, because we want to continue on our page with selected parameters / attributes. |
|
227 |
return null; |
|
221 | 228 |
} |
222 | 229 |
else if (exportPdf != null) |
223 | 230 |
{ |
224 |
System.out.println("Generuj PDF"); |
|
231 |
prepareForTable(newConfiguration); |
|
232 |
this.exportManager.exportContingencyTable(newConfiguration, isNotRemoveEmpty, response, ExportType.PDF); |
|
233 |
|
|
234 |
// Return null, because we want to continue on our page with selected parameters / attributes. |
|
235 |
return null; |
|
236 |
|
|
225 | 237 |
} |
226 | 238 |
else if (saveConfiguration != null) |
227 | 239 |
{ |
228 |
System.out.println("ulož konfiguraci"); |
|
229 |
|
|
230 | 240 |
Configuration configuration = configurationManager.saveConfiguration(newConfiguration, ""); |
231 | 241 |
|
232 | 242 |
initializeFields(configuration); |
... | ... | |
237 | 247 |
} |
238 | 248 |
|
239 | 249 |
return modelAndView; |
240 |
/* |
|
241 |
ModelMap modelMap = modelAndView.getModelMap(); |
|
242 |
|
|
243 |
long assemblyID = assembly.getId(); |
|
244 |
System.out.println(assemblyID); |
|
245 |
|
|
246 |
Assembly assembly2 = this.assemblyManager.getAssemblyById(assemblyID); |
|
247 |
|
|
248 |
modelMap.addAttribute("assemblies", this.assemblyManager.getAssemblies()); |
|
249 |
modelMap.addAttribute("assembly", assembly2); |
|
250 |
modelMap.addAttribute("contingencyTableRows", this.sqlQueryManager.getContingencyTableRow(assembly2.getSQLQuery())); |
|
251 |
|
|
252 |
return modelAndView; |
|
253 |
|
|
254 |
*/ |
|
255 | 250 |
} |
256 | 251 |
|
257 | 252 |
/** |
src/main/java/vldc/aswi/web/controller/ConfigurationController.java | ||
---|---|---|
5 | 5 |
import org.springframework.ui.ModelMap; |
6 | 6 |
import org.springframework.validation.BeanPropertyBindingResult; |
7 | 7 |
import org.springframework.validation.BindingResult; |
8 |
import org.springframework.web.bind.WebDataBinder; |
|
9 | 8 |
import org.springframework.web.bind.annotation.*; |
10 | 9 |
import org.springframework.web.servlet.ModelAndView; |
11 | 10 |
import org.springframework.web.servlet.mvc.support.RedirectAttributes; |
... | ... | |
14 | 13 |
import vldc.aswi.domain.Location; |
15 | 14 |
import vldc.aswi.domain.Operator; |
16 | 15 |
import vldc.aswi.domain.parameter.ParameterInConfiguration; |
16 |
import vldc.aswi.model.ExportType; |
|
17 | 17 |
import vldc.aswi.model.table.contingencyTable.ContingencyTableRow; |
18 |
import vldc.aswi.service.AssemblyManager; |
|
19 |
import vldc.aswi.service.ConfigurationManager; |
|
20 |
import vldc.aswi.service.LocationManager; |
|
21 |
import vldc.aswi.service.SqlQueryManager; |
|
18 |
import vldc.aswi.service.*; |
|
22 | 19 |
import vldc.aswi.service.parameter.ParameterManager; |
23 | 20 |
import vldc.aswi.utils.AuthControl; |
24 | 21 |
import vldc.aswi.utils.Utils; |
25 |
import vldc.aswi.validators.AssemblyValidator; |
|
22 |
|
|
23 |
import javax.servlet.http.HttpServletRequest; |
|
24 |
import javax.servlet.http.HttpServletResponse; |
|
26 | 25 |
import vldc.aswi.validators.ConfigurationValidator; |
27 | 26 |
|
28 | 27 |
import javax.validation.Valid; |
... | ... | |
48 | 47 |
@Autowired |
49 | 48 |
private SqlQueryManager sqlQueryManager; |
50 | 49 |
|
50 |
/** Autowired export manager. */ |
|
51 |
@Autowired |
|
52 |
private ExportManager exportManager; |
|
53 |
|
|
51 | 54 |
/** Autowired parameter manager. */ |
52 | 55 |
@Autowired |
53 | 56 |
private ParameterManager parameterManager; |
... | ... | |
122 | 125 |
@RequestParam("isNotRemoveEmpty") String isNotRemoveEmptyString, |
123 | 126 |
@RequestParam("configurationID") String id, |
124 | 127 |
@RequestParam(required=false, value="generateTable") String generateTable, |
125 |
@RequestParam(required=false, value="exportXls") String exportXls,
|
|
128 |
@RequestParam(required=false, value="exportXlsx") String exportXlsx,
|
|
126 | 129 |
@RequestParam(required=false, value="exportPdf") String exportPdf, |
127 | 130 |
@RequestParam(required=false, value="saveConfiguration") String saveConfiguration, |
128 |
RedirectAttributes redirectAttributes |
|
131 |
RedirectAttributes redirectAttributes, |
|
132 |
HttpServletRequest request, |
|
133 |
HttpServletResponse response |
|
129 | 134 |
) |
130 | 135 |
{ |
131 | 136 |
ModelAndView modelAndView = new ModelAndView(); |
132 | 137 |
ModelMap modelMap = modelAndView.getModelMap(); |
138 |
boolean isNotRemoveEmpty = isNotRemoveEmptyString == null || !isNotRemoveEmptyString.equals(""); |
|
133 | 139 |
|
134 | 140 |
if (bindingResult.hasErrors()) { |
135 | 141 |
// TODO: 04.05.2020 Error message |
... | ... | |
150 | 156 |
modelMap.addAttribute(assemblyTitleName, generateTableTitleText + " " + newConfiguration.getName()); |
151 | 157 |
} |
152 | 158 |
else { |
153 |
boolean isNotRemoveEmpty = isNotRemoveEmptyString == null || !isNotRemoveEmptyString.equals(""); |
|
154 | 159 |
List<ContingencyTableRow> rows = this.sqlQueryManager.getContingencyTableRow(newConfiguration.getAssembly(), |
155 | 160 |
newConfiguration.getParametersInConfiguration(), isNotRemoveEmpty); |
156 |
System.out.println("Generuj tabulku"); |
|
161 |
|
|
157 | 162 |
modelMap.addAttribute("configurationID", id); |
158 | 163 |
modelMap.addAttribute("contingencyTableRows", rows); |
159 | 164 |
addConfigurationDataIntoModelAndView(newConfiguration, modelAndView, modelMap); |
160 | 165 |
} |
161 | 166 |
} |
162 |
else if (exportXls != null) |
|
167 |
else if (exportXlsx != null)
|
|
163 | 168 |
{ |
164 |
System.out.println("Generuj XLS"); |
|
169 |
prepareForTable(newConfiguration); |
|
170 |
this.exportManager.exportContingencyTable(newConfiguration, isNotRemoveEmpty, response, ExportType.XLSX); |
|
171 |
|
|
172 |
// Return null, because we want to continue on our page with selected parameters / attributes. |
|
173 |
return null; |
|
165 | 174 |
} |
166 | 175 |
else if (exportPdf != null) |
167 | 176 |
{ |
168 |
System.out.println("Generuj PDF"); |
|
177 |
prepareForTable(newConfiguration); |
|
178 |
this.exportManager.exportContingencyTable(newConfiguration, isNotRemoveEmpty, response, ExportType.PDF); |
|
179 |
|
|
180 |
// Return null, because we want to continue on our page with selected parameters / attributes. |
|
181 |
return null; |
|
169 | 182 |
} |
170 | 183 |
else if (saveConfiguration != null) |
171 | 184 |
{ |
172 |
System.out.println("ulož konfiguraci"); |
|
173 | 185 |
Configuration configuration = configurationManager.saveConfiguration(newConfiguration, id); |
174 | 186 |
|
175 | 187 |
initializeFields(configuration); |
... | ... | |
265 | 277 |
initializeFields(newConfiguration); |
266 | 278 |
modelMap.addAttribute("configuration", newConfiguration); |
267 | 279 |
modelMap.addAttribute("comparator", comparator); |
268 |
modelMap.addAttribute("formAction", "/assembly?assemblyID=" + newConfiguration.getAssembly().getId());
|
|
280 |
modelMap.addAttribute("formAction", "/configuration?configurationID=" + newConfiguration.getAssembly().getId());
|
|
269 | 281 |
modelAndView.setViewName("assembly"); |
270 | 282 |
} |
271 | 283 |
} |
src/main/webapp/WEB-INF/templates/assembly.html | ||
---|---|---|
208 | 208 |
|
209 | 209 |
<div class="buttons-wrap"> |
210 | 210 |
<button type="submit" class="btn btn-primary mb-2 show-spinner" name="generateTable">Vygenerovat tabulku</button> |
211 |
<button type="submit" class="btn btn-primary mb-2 show-spinner" name="exportXls">Export do XLS</button>
|
|
211 |
<button type="submit" class="btn btn-primary mb-2 show-spinner" name="exportXlsx">Export do XLSX</button>
|
|
212 | 212 |
<button type="submit" class="btn btn-primary mb-2 show-spinner" name="exportPdf">Export do PDF</button> |
213 | 213 |
</div> |
214 | 214 |
</div> |
Také k dispozici: Unified diff
re #8133 #8132 Export contingency table to XLSX + PDF (not fully complete).