Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/common/AbstractSInfoExporter.java @ 8928:791714b92b5c
Basically implemented SINFO-Tkh Exports
author | gernotbelger |
---|---|
date | Thu, 01 Mar 2018 18:49:34 +0100 |
parents | |
children | 9c02733a1b3c |
comparison
equal
deleted
inserted
replaced
8927:04ad2cfce559 | 8928:791714b92b5c |
---|---|
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.sinfo.common; | |
11 | |
12 import java.io.OutputStream; | |
13 import java.util.Collection; | |
14 import java.util.HashMap; | |
15 import java.util.Map; | |
16 | |
17 import org.apache.log4j.Logger; | |
18 import org.dive4elements.artifacts.CallMeta; | |
19 import org.dive4elements.artifacts.common.utils.Config; | |
20 import org.dive4elements.river.artifacts.model.CalculationResult; | |
21 import org.dive4elements.river.artifacts.resources.Resources; | |
22 import org.dive4elements.river.artifacts.sinfo.util.MetaAndTableJRDataSource; | |
23 import org.dive4elements.river.artifacts.sinfo.util.RiverInfo; | |
24 import org.dive4elements.river.exports.AbstractExporter; | |
25 | |
26 import au.com.bytecode.opencsv.CSVWriter; | |
27 import net.sf.jasperreports.engine.JRDataSource; | |
28 import net.sf.jasperreports.engine.JRException; | |
29 import net.sf.jasperreports.engine.JasperExportManager; | |
30 import net.sf.jasperreports.engine.JasperFillManager; | |
31 import net.sf.jasperreports.engine.JasperPrint; | |
32 | |
33 /** | |
34 * @author Gernot Belger | |
35 */ | |
36 public abstract class AbstractSInfoExporter<ROW extends AbstractSInfoResultRow, RESULT extends AbstractSInfoCalculationResult<ROW>, RESULTS extends AbstractSInfoCalculationResults<ROW, RESULT>> extends AbstractExporter { | |
37 | |
38 /** The storage that contains the current calculation result. */ | |
39 private RESULTS data = null; | |
40 | |
41 protected abstract Logger getLog(); | |
42 | |
43 public RESULTS getData() { | |
44 return this.data; | |
45 } | |
46 | |
47 @Override | |
48 protected final void addData(final Object d) { | |
49 /* reset */ | |
50 this.data = null; | |
51 | |
52 if (d instanceof CalculationResult) { | |
53 | |
54 final Object dat = ((CalculationResult) d).getData(); | |
55 if (dat != null) | |
56 this.data = (RESULTS) dat; | |
57 } | |
58 } | |
59 | |
60 @Override | |
61 protected final void writeCSVData(final CSVWriter writer) { | |
62 getLog().info("writeCSVData"); | |
63 | |
64 /* fetch calculation results */ | |
65 final RESULTS results = this.data; | |
66 | |
67 final RiverInfo river = results.getRiver(); | |
68 | |
69 /* write as csv */ | |
70 writeCSVMeta(writer, results); | |
71 writeCSVHeader(writer, river); | |
72 | |
73 for (final RESULT result : results.getResults()) { | |
74 writeCSVResult(writer, result); | |
75 } | |
76 } | |
77 | |
78 protected abstract void writeCSVHeader(final CSVWriter writer, final RiverInfo river); | |
79 | |
80 protected abstract void writeCSVMeta(final CSVWriter writer, final RESULTS results); | |
81 | |
82 protected final void writeCSVMetaEntry(final CSVWriter writer, final String message, final Object... messageArgs) { | |
83 | |
84 final CallMeta meta = this.context.getMeta(); | |
85 | |
86 writer.writeNext(new String[] { Resources.getMsg(meta, message, message, messageArgs) }); | |
87 } | |
88 | |
89 protected final void writeCSVResult(final CSVWriter writer, final RESULT result) { | |
90 | |
91 writeCSVResultHeader(writer, result); | |
92 | |
93 /* nwo the value rows */ | |
94 final Collection<ROW> rows = result.getRows(); | |
95 for (final ROW row : rows) { | |
96 writeCSVRow(writer, row); | |
97 } | |
98 } | |
99 | |
100 protected abstract void writeCSVResultHeader(CSVWriter writer, RESULT result); | |
101 | |
102 protected final void writeCSVRow(final CSVWriter writer, final ROW row) { | |
103 getLog().debug("writeCSVFlowDepthRow"); | |
104 | |
105 final String[] formattedRow = formatCSVRow(row); | |
106 writer.writeNext(formattedRow); | |
107 } | |
108 | |
109 protected abstract String[] formatCSVRow(final ROW row); | |
110 | |
111 @Override | |
112 protected final void writePDF(final OutputStream outStream) { | |
113 getLog().debug("write PDF"); | |
114 | |
115 final JRDataSource source = createJRData(); | |
116 | |
117 final String confPath = Config.getConfigDirectory().toString(); | |
118 | |
119 // FIXME: distinguish between with and without tkh: we need two jasper reports! | |
120 | |
121 final Map<String, Object> parameters = new HashMap<>(); | |
122 parameters.put("ReportTitle", "Exported Data"); | |
123 | |
124 try { | |
125 final String jasperPath = confPath + getJasperFile(); | |
126 | |
127 final JasperPrint print = JasperFillManager.fillReport(jasperPath, parameters, source); | |
128 JasperExportManager.exportReportToPdfStream(print, outStream); | |
129 } | |
130 catch (final JRException je) { | |
131 getLog().warn("Error generating PDF Report!", je); | |
132 } | |
133 } | |
134 | |
135 protected abstract String getJasperFile(); | |
136 | |
137 private JRDataSource createJRData() { | |
138 | |
139 /* fetch calculation results */ | |
140 final RESULTS results = this.data; | |
141 | |
142 final MetaAndTableJRDataSource source = new MetaAndTableJRDataSource(); | |
143 | |
144 addJRMetaData(source, results); | |
145 | |
146 for (final RESULT result : results.getResults()) { | |
147 addJRTableData(source, result); | |
148 } | |
149 | |
150 return source; | |
151 } | |
152 | |
153 protected abstract void addJRMetaData(final MetaAndTableJRDataSource source, final RESULTS results); | |
154 | |
155 protected final void addJRTableData(final MetaAndTableJRDataSource source, final RESULT result) { | |
156 | |
157 final Collection<ROW> rows = result.getRows(); | |
158 | |
159 for (final ROW row : rows) { | |
160 | |
161 final String[] formattedRow = formatPDFRow(row); | |
162 source.addData(formattedRow); | |
163 } | |
164 } | |
165 | |
166 protected abstract String[] formatPDFRow(final ROW row); | |
167 } |