Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/common/AbstractCommonExporter.java @ 8999:e3b3ce32c825
Work on uinfo
author | gernotbelger |
---|---|
date | Thu, 12 Apr 2018 19:15:42 +0200 |
parents | |
children | 460fcc128794 |
comparison
equal
deleted
inserted
replaced
8998:c38098b15427 | 8999:e3b3ce32c825 |
---|---|
1 /** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde | |
2 * Software engineering by | |
3 * Björnsen Beratende Ingenieure GmbH | |
4 * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt | |
5 * | |
6 * This file is Free Software under the GNU AGPL (>=v3) | |
7 * and comes with ABSOLUTELY NO WARRANTY! Check out the | |
8 * documentation coming with Dive4Elements River for details. | |
9 */ | |
10 package org.dive4elements.river.artifacts.common; | |
11 | |
12 import java.io.OutputStream; | |
13 import java.util.Collection; | |
14 import java.util.HashMap; | |
15 import java.util.List; | |
16 import java.util.Map; | |
17 | |
18 import org.apache.log4j.Logger; | |
19 import org.dive4elements.artifacts.CallMeta; | |
20 import org.dive4elements.artifacts.common.utils.Config; | |
21 import org.dive4elements.river.artifacts.model.CalculationResult; | |
22 import org.dive4elements.river.artifacts.resources.Resources; | |
23 import org.dive4elements.river.artifacts.sinfo.util.MetaAndTableJRDataSource; | |
24 import org.dive4elements.river.artifacts.sinfo.util.RiverInfo; | |
25 import org.dive4elements.river.exports.AbstractExporter; | |
26 | |
27 import au.com.bytecode.opencsv.CSVWriter; | |
28 import net.sf.jasperreports.engine.JRDataSource; | |
29 import net.sf.jasperreports.engine.JRElement; | |
30 import net.sf.jasperreports.engine.JRException; | |
31 import net.sf.jasperreports.engine.JRPrintPage; | |
32 import net.sf.jasperreports.engine.JasperCompileManager; | |
33 import net.sf.jasperreports.engine.JasperExportManager; | |
34 import net.sf.jasperreports.engine.JasperFillManager; | |
35 import net.sf.jasperreports.engine.JasperPrint; | |
36 import net.sf.jasperreports.engine.JasperReport; | |
37 import net.sf.jasperreports.engine.design.JasperDesign; | |
38 import net.sf.jasperreports.engine.util.JRProperties; | |
39 import net.sf.jasperreports.engine.xml.JRSaxParserFactory; | |
40 import net.sf.jasperreports.engine.xml.JRXmlLoader; | |
41 | |
42 /** | |
43 * @author Gernot Belger | |
44 */ | |
45 public abstract class AbstractCommonExporter<RESULT extends AbstractCalculationResult, RESULTS extends AbstractCalculationResults<RESULT>> extends AbstractExporter { | |
46 | |
47 /** The storage that contains the current calculation result. */ | |
48 protected static enum ExportMode { | |
49 pdf, csv | |
50 } | |
51 | |
52 /** The log used in this exporter. */ | |
53 protected abstract Logger getLog(); | |
54 | |
55 protected RESULTS data = null; | |
56 | |
57 public RESULTS getData() { | |
58 return this.data; | |
59 } | |
60 | |
61 @Override | |
62 protected void addData(final Object d) { | |
63 /* reset */ | |
64 this.data = null; | |
65 | |
66 if (d instanceof CalculationResult) { | |
67 | |
68 final Object dat = ((CalculationResult) d).getData(); | |
69 if (dat != null) { | |
70 @SuppressWarnings("unchecked") | |
71 final RESULTS result = (RESULTS) dat; | |
72 this.data = result; | |
73 } | |
74 } | |
75 } | |
76 | |
77 /** | |
78 * Formats header with unit and label: msg [unit] (label) | |
79 */ | |
80 protected String msgUnitLabel(final String key, final String unit, final String label) { | |
81 final String msg = msg(key); | |
82 return String.format("%s [%s] (%s)", msg, unit, label); | |
83 } | |
84 | |
85 @Override | |
86 protected void writeCSVData(final CSVWriter writer) { | |
87 getLog().info("writeCSVData"); | |
88 | |
89 /* fetch calculation results */ | |
90 final RESULTS results = this.data; | |
91 | |
92 /* write as csv */ | |
93 writeCSVGlobalMetadata(writer, results); | |
94 writer.writeNext(new String[] { "" }); // break line HERE to avoid redundance | |
95 final RiverInfo river = results.getRiver(); | |
96 // FIXME :with comment if not first result | |
97 writeCSVHeader(writer, results, river); | |
98 writer.writeNext(new String[] { "" }); // break line HERE to avoid redundance | |
99 | |
100 for (final RESULT result : results.getResults()) { | |
101 writeCSVResult(writer, results, result); | |
102 writer.writeNext(new String[] { "" }); // break line HERE after each resultset | |
103 } | |
104 | |
105 } | |
106 | |
107 protected abstract void writeCSVHeader(final CSVWriter writer, final RESULTS results, final RiverInfo river); | |
108 | |
109 /** | |
110 * Add metadata that is once written to the top of the file. | |
111 */ | |
112 protected abstract void writeCSVGlobalMetadata(final CSVWriter writer, final RESULTS results); | |
113 | |
114 protected void writeCSVMetaEntry(final CSVWriter writer, final String message, final Object... messageArgs) { | |
115 | |
116 final CallMeta meta = this.context.getMeta(); | |
117 | |
118 writer.writeNext(new String[] { Resources.getMsg(meta, message, message, messageArgs) }); | |
119 } | |
120 | |
121 private void writeCSVResult(final CSVWriter writer, final RESULTS results, final RESULT result) { | |
122 | |
123 writeCSVResultMetadata(writer, results, result); | |
124 writer.writeNext(new String[] { "" }); // break line HERE to avoid redundance | |
125 | |
126 // final RiverInfo river = results.getRiver(); | |
127 // | |
128 // writeCSVHeader(writer, results, river); | |
129 /* now the value rows */ | |
130 final Collection<ResultRow> rows = result.getRows(); // war mal SInfoResultRow | |
131 for (final ResultRow row : rows) { | |
132 writeCSVRow(writer, results, result, row); | |
133 } | |
134 } | |
135 | |
136 /** | |
137 * Add metadata that is written once per result set. | |
138 */ | |
139 protected abstract void writeCSVResultMetadata(CSVWriter writer, RESULTS results, RESULT result); | |
140 | |
141 protected void writeCSVRow(final CSVWriter writer, final RESULTS results, final RESULT result, final ResultRow row) { | |
142 getLog().debug("writeCSVFlowDepthRow"); | |
143 | |
144 final String[] formattedRow = formatCSVRow(results, row); | |
145 writer.writeNext(formattedRow); | |
146 } | |
147 | |
148 protected final String[] formatCSVRow(final RESULTS results, final ResultRow row) { | |
149 return formatRow(results, row, ExportMode.csv); | |
150 } | |
151 | |
152 // protected abstract void tweakDesign() | |
153 | |
154 @Override | |
155 protected void writePDF(final OutputStream outStream) { | |
156 getLog().debug("write PDF"); | |
157 | |
158 final JRDataSource source = createJRData(); | |
159 final JRDataSource source2 = createJRData(); | |
160 | |
161 final String confPath = Config.getConfigDirectory().toString(); | |
162 | |
163 // FIXME: distinguish between with and without tkh: we need two jasper reports! | |
164 | |
165 final Map<String, Object> parameters = new HashMap<>(); | |
166 parameters.put("ReportTitle", "Exported Data"); | |
167 | |
168 try { | |
169 | |
170 // JRProperties.setProperty(JRProperties.COMPILER_XML_VALIDATION, false); | |
171 JRProperties.setProperty(JRSaxParserFactory.PROPERTY_REPORT_PARSER_FACTORY, JRReportSaxParserFactory.class.getName()); | |
172 | |
173 final String jasperPath = confPath + getJasperFile(); | |
174 final JasperDesign test = JRXmlLoader.load(jasperPath); | |
175 | |
176 final JRElement element = test.getColumnHeader().getElementByKey("TEST"); | |
177 // element.setWidth(200); | |
178 | |
179 final JasperReport compiled = JasperCompileManager.compileReport(test); | |
180 | |
181 final JasperPrint print = JasperFillManager.fillReport(compiled, parameters, source); | |
182 // JasperExportManager.exportReportToPdfStream(print, outStream); | |
183 | |
184 final JasperPrint print2 = JasperFillManager.fillReport(compiled, parameters, source2); | |
185 | |
186 final List<JRPrintPage> pages = print2.getPages(); | |
187 for (final JRPrintPage page : pages) | |
188 print.addPage(page); | |
189 | |
190 JasperExportManager.exportReportToPdfStream(print, outStream); | |
191 } | |
192 catch (final JRException je) { | |
193 getLog().warn("Error generating PDF Report!", je); | |
194 } | |
195 } | |
196 | |
197 protected abstract String getJasperFile(); | |
198 | |
199 private JRDataSource createJRData() { | |
200 | |
201 /* fetch calculation results */ | |
202 final RESULTS results = this.data; | |
203 | |
204 final MetaAndTableJRDataSource source = new MetaAndTableJRDataSource(); | |
205 | |
206 addJRMetaData(source, results); | |
207 | |
208 for (final RESULT result : results.getResults()) { | |
209 addJRTableData(source, results, result); | |
210 } | |
211 | |
212 return source; | |
213 } | |
214 | |
215 protected abstract void addJRMetaData(final MetaAndTableJRDataSource source, final RESULTS results); | |
216 | |
217 protected void addJRTableData(final MetaAndTableJRDataSource source, final RESULTS results, final RESULT result) { | |
218 | |
219 final Collection<ResultRow> rows = result.getRows(); | |
220 | |
221 for (final ResultRow row : rows) { | |
222 | |
223 final String[] formattedRow = formatPDFRow(results, row); | |
224 source.addData(formattedRow); | |
225 } | |
226 } | |
227 | |
228 protected abstract String[] formatRow(RESULTS results, ResultRow row, ExportMode mode); | |
229 | |
230 protected final String[] formatPDFRow(final RESULTS results, final ResultRow row) { | |
231 // @Override | |
232 // protected String[] formatPDFRow(final FlowDepthDevelopmentCalculationResults results, final ResultRow row) { | |
233 return formatRow(results, row, ExportMode.pdf); | |
234 // } | |
235 } | |
236 | |
237 protected abstract void writeCSVGlobalMetadataDefaults(final CSVWriter writer, final RESULTS results); | |
238 | |
239 protected abstract void addJRMetaDataDefaults(final MetaAndTableJRDataSource source, final RESULTS results); | |
240 } |