8854
|
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde |
|
2 * Software engineering by Intevation GmbH |
|
3 * |
|
4 * This file is Free Software under the GNU AGPL (>=v3) |
|
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the |
|
6 * documentation coming with Dive4Elements River for details. |
|
7 */ |
|
8 |
|
9 package org.dive4elements.river.artifacts.sinfo.flowdepth; |
|
10 |
|
11 import java.io.OutputStream; |
|
12 import java.text.DateFormat; |
|
13 import java.util.ArrayList; |
|
14 import java.util.Collection; |
|
15 import java.util.Date; |
|
16 import java.util.HashMap; |
|
17 import java.util.Locale; |
|
18 import java.util.Map; |
|
19 |
|
20 import org.apache.log4j.Logger; |
|
21 import org.dive4elements.artifacts.CallMeta; |
|
22 import org.dive4elements.artifacts.common.utils.Config; |
8863
|
23 import org.dive4elements.river.FLYS; |
8854
|
24 import org.dive4elements.river.artifacts.model.CalculationResult; |
|
25 import org.dive4elements.river.artifacts.resources.Resources; |
|
26 import org.dive4elements.river.artifacts.sinfo.util.MetaAndTableJRDataSource; |
|
27 import org.dive4elements.river.exports.AbstractExporter; |
|
28 import org.dive4elements.river.model.River; |
8863
|
29 import org.dive4elements.river.model.Unit; |
8854
|
30 |
|
31 import au.com.bytecode.opencsv.CSVWriter; |
|
32 import net.sf.jasperreports.engine.JRDataSource; |
|
33 import net.sf.jasperreports.engine.JRException; |
|
34 import net.sf.jasperreports.engine.JasperExportManager; |
|
35 import net.sf.jasperreports.engine.JasperFillManager; |
|
36 import net.sf.jasperreports.engine.JasperPrint; |
|
37 |
|
38 /** |
|
39 * Generates different output formats (csv, pdf) of data that resulted from a flow depths computation. |
|
40 * |
|
41 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> |
|
42 * @author Gernot Belger |
|
43 */ |
|
44 // REMARK: must be public because its registered in generators.xml |
|
45 public class FlowDepthExporter extends AbstractExporter { |
|
46 |
|
47 /** The log used in this exporter.*/ |
|
48 private static Logger log = Logger.getLogger(FlowDepthExporter.class); |
|
49 |
|
50 private static final String CSV_KM_HEADER = "sinfo.export.flow_depth.csv.header.km"; |
|
51 private static final String CSV_FLOWDEPTH_HEADER = "sinfo.export.flow_depth.csv.header.flowdepth"; |
|
52 private static final String CSV_FLOWDEPTHTKH_HEADER = "sinfo.export.flow_depth.csv.header.flowdepthTkh"; |
|
53 private static final String CSV_TKH_HEADER = "sinfo.export.flow_depth.csv.header.tkh"; |
|
54 private static final String CSV_WATERLEVEL_HEADER = "sinfo.export.flow_depth.csv.header.waterlevel"; |
|
55 private static final String CSV_DISCHARGE_HEADER = "sinfo.export.flow_depth.csv.header.discharge"; |
|
56 private static final String CSV_LABEL_HEADER = "sinfo.export.flow_depth.csv.header.label"; |
|
57 private static final String CSV_GAUGE_HEADER = "sinfo.export.flow_depth.csv.header.gauge"; |
|
58 private static final String CSV_MEAN_BED_HEIGHT_HEADER = "sinfo.export.flow_depth.csv.header.mean_bed_height"; |
8863
|
59 private static final String CSV_MEAN_BED_HEIGHT_HEADER_SHORT = "sinfo.export.flow_depth.csv.header.mean_bed_height.short"; |
8854
|
60 private static final String CSV_SOUNDING_HEADER = "sinfo.export.flow_depth.csv.header.sounding"; |
|
61 private static final String CSV_LOCATION_HEADER = "sinfo.export.flow_depth.csv.header.location"; |
|
62 |
|
63 private static final String CSV_META_HEADER_RESULT = |
|
64 "sinfo.export.flow_depth.csv.meta.header.result"; |
|
65 |
8863
|
66 private static final String CSV_META_HEADER_RESULT_LABEL = |
|
67 "sinfo.export.flow_depth.csv.meta.header.result.label"; |
|
68 |
8854
|
69 private static final String CSV_META_VERSION = |
|
70 "sinfo.export.flow_depth.csv.meta.version"; |
8863
|
71 |
|
72 private static final String CSV_META_VERSION_LABEL = |
|
73 "sinfo.export.flow_depth.csv.meta.version.label"; |
8854
|
74 |
|
75 private static final String CSV_META_USER = |
|
76 "sinfo.export.flow_depth.csv.meta.user"; |
|
77 |
8863
|
78 private static final String CSV_META_USER_LABEL = |
|
79 "sinfo.export.flow_depth.csv.meta.user.label"; |
|
80 |
8854
|
81 private static final String CSV_META_CREATION = |
|
82 "sinfo.export.flow_depth.csv.meta.creation"; |
|
83 |
8863
|
84 private static final String CSV_META_CREATION_LABEL = |
|
85 "sinfo.export.flow_depth.csv.meta.creation.label"; |
|
86 |
8854
|
87 private static final String CSV_META_RIVER = |
|
88 "sinfo.export.flow_depth.csv.meta.river"; |
|
89 |
8863
|
90 private static final String CSV_META_RIVER_LABEL = |
|
91 "sinfo.export.flow_depth.csv.meta.river.label"; |
|
92 |
8854
|
93 private static final String CSV_META_HEADER_SOUNDING = |
|
94 "sinfo.export.flow_depth.csv.meta.header.sounding"; |
|
95 |
|
96 private static final String CSV_META_HEADER_WATERLEVEL = |
|
97 "sinfo.export.flow_depth.csv.meta.header.waterlevel"; |
8863
|
98 |
|
99 private static final String CSV_META_RANGE = |
|
100 "sinfo.export.flow_depth.csv.meta.range"; |
|
101 |
|
102 private static final String CSV_META_RANGE_LABEL = |
|
103 "sinfo.export.flow_depth.csv.meta.range.label"; |
8854
|
104 |
8863
|
105 private static final String CSV_META_HEIGHT_UNIT_RIVER = "sinfo.export.flow_depth.csv.meta.height_unit.river"; |
|
106 |
|
107 private static final String JASPER_FILE = "/jasper/sinfo.flowdepth.jasper"; |
|
108 |
|
109 private static final String UNIT_M = "m"; |
|
110 |
|
111 private static final String UNIT_CM = "cm"; |
|
112 |
|
113 private static final String UNIT_CUBIC_M = "m³/s"; |
8854
|
114 |
|
115 /** The storage that contains the current calculation result.*/ |
|
116 private FlowDepthCalculationResults data = null; |
|
117 |
8863
|
118 /** |
|
119 * Formats header with unit |
|
120 */ |
|
121 private String msgUnit(final String key, String unit ) { |
|
122 |
|
123 final String msg = msg(key); |
|
124 return String.format("%s [%s]", msg, unit); |
|
125 } |
8854
|
126 |
|
127 @Override |
|
128 protected void addData(Object d) { |
|
129 /* reset */ |
|
130 data = null; |
|
131 |
|
132 if (d instanceof CalculationResult) { |
|
133 |
|
134 final Object dat = ((CalculationResult)d).getData(); |
|
135 if( dat != null ) |
|
136 data = (FlowDepthCalculationResults)dat; |
|
137 } |
|
138 } |
8863
|
139 |
8854
|
140 @Override |
|
141 protected void writeCSVData(CSVWriter writer) { |
|
142 log.info("FlowDepthExporter.writeCSVData"); |
|
143 |
|
144 /* fetch calculation results */ |
|
145 final FlowDepthCalculationResults results = data; |
|
146 |
8863
|
147 final boolean useTkh = results.isUseTkh(); |
|
148 final River river = results.getRiver(); |
8854
|
149 |
8863
|
150 /* write as csv */ |
8854
|
151 writeCSVMeta(writer, results); |
8863
|
152 writeCSVHeader(writer, river, useTkh); |
8854
|
153 |
|
154 for (final FlowDepthCalculationResult result : results.getResults()) { |
|
155 writeCSVFlowDepthResult(writer, result, useTkh); |
|
156 } |
|
157 } |
|
158 |
|
159 private void writeCSVFlowDepthResult(final CSVWriter writer, final FlowDepthCalculationResult result, final boolean useTkh) { |
|
160 final Collection<FlowDepthRow> rows = result.getRows(); |
|
161 for (final FlowDepthRow flowDepthRow : rows) { |
|
162 writeCSVFlowDepthRow(writer, flowDepthRow, useTkh); |
|
163 } |
|
164 } |
|
165 |
|
166 private void writeCSVMeta(final CSVWriter writer, final FlowDepthCalculationResults results) { |
|
167 log.info("FlowDepthExporter.writeCSVMeta"); |
|
168 |
8863
|
169 final String calcModeLabel = results.getCalcModeLabel(); |
8854
|
170 final River river = results.getRiver(); |
8863
|
171 writeCSVMetaEntry(writer, CSV_META_HEADER_RESULT, msg( CSV_META_HEADER_RESULT_LABEL ), river.getName(), calcModeLabel ); |
8854
|
172 |
|
173 // "# FLYS-Version: " |
8863
|
174 writeCSVMetaEntry(writer, CSV_META_VERSION, msg( CSV_META_VERSION_LABEL ), FLYS.VERSION ); |
8854
|
175 |
|
176 // "# Bearbeiter: " |
8863
|
177 writeCSVMetaEntry(writer, CSV_META_USER, msg( CSV_META_USER_LABEL ), results.getUser() ); |
8854
|
178 |
|
179 // "# Datum der Erstellung: " |
|
180 final Locale locale = Resources.getLocale(context.getMeta()); |
|
181 final DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale); |
8863
|
182 writeCSVMetaEntry(writer, CSV_META_CREATION, msg( CSV_META_CREATION_LABEL ), df.format(new Date()) ); |
8854
|
183 |
|
184 // "# Gewässer: " |
8863
|
185 writeCSVMetaEntry(writer, CSV_META_RIVER, msg( CSV_META_RIVER_LABEL ), river.getName() ); |
8854
|
186 |
|
187 // "# Höhensystem des Flusses: " |
8863
|
188 final Unit wstUnit = river.getWstUnit(); |
|
189 writeCSVMetaEntry(writer, CSV_META_HEIGHT_UNIT_RIVER, wstUnit.getName()); |
8854
|
190 |
|
191 // "# Ort/Bereich (km): " |
8863
|
192 writeCSVMetaEntry(writer, CSV_META_RANGE, msg( CSV_META_RANGE_LABEL ), getKmFormatter().format(results.getFrom() ), getKmFormatter().format( results.getTo())); |
8854
|
193 |
|
194 // "##METADATEN PEILUNG" |
8863
|
195 writeCSVMetaEntry(writer, CSV_META_HEADER_SOUNDING ); |
8854
|
196 |
8863
|
197 // FIXME: check: macht nicht viel sinn da es mehrere geben kann.. oder immer wieder wiederholen? |
8854
|
198 // "# Jahr der Peilung: " |
|
199 // FIXME |
|
200 // "# Aufnahmeart: " |
|
201 // FIXME |
|
202 // "# Lagesystem: " |
|
203 // FIXME |
|
204 // "# Höhensystem: " |
|
205 // FIXME |
|
206 // "# ursprüngliches Höhensystem: " |
|
207 // FIXME |
|
208 // "##METADATEN WASSERSPIEGELLAGE" |
8863
|
209 writeCSVMetaEntry(writer, CSV_META_HEADER_WATERLEVEL ); |
8854
|
210 // "# Bezeichnung der Wasserspiegellage: " |
|
211 // FIXME |
|
212 // "# Höhensystem der Wasserspiegellage: " |
|
213 // FIXME |
|
214 // "# Auswerter: " |
|
215 // FIXME |
|
216 // "# Bezugspegel: " |
|
217 // FIXME |
|
218 // "# Jahr/Zeitraum der Wasserspiegellage: " |
|
219 // FIXME |
|
220 |
|
221 // "# W/Pegel [cm]: " (nur bei Eingabe des Wasserstands am Pegel) |
|
222 // TODO: unklar, es wird kein W eingegeben |
|
223 |
|
224 // "# Q (m³/s): " (nur bei Eingabe des Durchflusses) |
|
225 // TODO: unklar, es wird kein Q eingegeben |
|
226 |
|
227 writer.writeNext(new String[] { "" }); |
|
228 } |
|
229 |
|
230 |
8863
|
231 private void writeCSVMetaEntry(CSVWriter writer, String message, Object... messageArgs) { |
8854
|
232 |
|
233 CallMeta meta = context.getMeta(); |
|
234 |
|
235 writer.writeNext(new String[] { |
|
236 Resources.getMsg( |
|
237 meta, |
|
238 message, |
|
239 message, |
|
240 messageArgs) |
|
241 }); |
|
242 } |
|
243 |
|
244 /** |
|
245 * Write the header, with different headings depending on whether at a |
|
246 * gauge or at a location. |
8863
|
247 * @param river |
8854
|
248 * @param useTkh |
|
249 */ |
|
250 private void writeCSVHeader( |
|
251 final CSVWriter writer, |
8863
|
252 final River river, |
8854
|
253 final boolean useTkh |
|
254 ) { |
|
255 log.info("FlowDepthExporter.writeCSVHeader"); |
|
256 |
|
257 final Collection<String> header = new ArrayList<>(11); |
|
258 |
8863
|
259 header.add(msg(CSV_KM_HEADER)); |
|
260 header.add(msgUnit(CSV_FLOWDEPTH_HEADER, UNIT_M)); |
8854
|
261 if( useTkh ) |
|
262 { |
8863
|
263 header.add(msgUnit(CSV_FLOWDEPTHTKH_HEADER, UNIT_M)); |
|
264 header.add(msgUnit(CSV_TKH_HEADER, UNIT_CM)); |
8854
|
265 } |
8863
|
266 |
|
267 final String wstUnitName = river.getWstUnit().getName(); |
|
268 header.add(msgUnit(CSV_WATERLEVEL_HEADER, wstUnitName)); |
|
269 header.add(msgUnit(CSV_DISCHARGE_HEADER, UNIT_CUBIC_M)); |
8854
|
270 header.add(msg(CSV_LABEL_HEADER)); |
|
271 header.add(msg(CSV_GAUGE_HEADER)); |
8863
|
272 header.add(msgUnit(CSV_MEAN_BED_HEIGHT_HEADER, wstUnitName)); |
8854
|
273 header.add(msg(CSV_SOUNDING_HEADER)); |
|
274 header.add(msg(CSV_LOCATION_HEADER)); |
|
275 |
|
276 writer.writeNext(header.toArray(new String[header.size()])); |
|
277 } |
|
278 |
|
279 /** |
|
280 * Format a row of a flow depth result into an array of string, both used by csv and pdf |
|
281 * @param useTkh |
|
282 */ |
|
283 private String[] formatFlowDepthRow( |
|
284 final FlowDepthRow row, |
|
285 boolean useTkh ) { |
|
286 |
|
287 final Collection<String> lines = new ArrayList<>(11); |
|
288 |
|
289 // Fluss-km |
|
290 lines.add( getKmFormatter().format( row.getStation() ) ); |
|
291 |
|
292 // Fließtiefe [m] |
|
293 lines.add( getFlowDepthFormatter().format( row.getFlowDepth() ) ); |
|
294 |
|
295 if( useTkh ) |
|
296 { |
|
297 // Fließtiefe mit TKH [m] |
|
298 lines.add( getFlowDepthFormatter().format( row.getFlowDepthWithTkh() ) ); |
|
299 |
|
300 // TKH [cm] |
|
301 lines.add( getTkhFormatter().format( row.getTkh() ) ); |
|
302 } |
|
303 |
|
304 // Wasserstand [NN + m] |
8863
|
305 lines.add( getW2Formatter().format( row.getWaterlevel() ) ); |
8854
|
306 |
|
307 // Q [m³/s] |
|
308 lines.add( getQFormatter().format( row.getDischarge() ) ); |
|
309 |
|
310 // Bezeichnung |
|
311 lines.add( row.getWaterlevelLabel() ); |
|
312 |
|
313 // Bezugspegel |
|
314 lines.add( row.getGauge() ); |
|
315 |
|
316 // Mittlere Sohlhöhe [NN + m] |
8863
|
317 lines.add( getMeanBedHeighFormatter().format( row.getMeanBedHeight( ) ) ); |
8854
|
318 |
|
319 // Peilung/Epoche |
|
320 lines.add( row.getSoundageLabel() ); |
|
321 |
|
322 // Lage |
|
323 lines.add( row.getLocation() ); |
|
324 |
|
325 return lines.toArray(new String[lines.size()]); |
|
326 } |
|
327 /** |
|
328 * Write "rows" of csv data from wqkms with writer. |
|
329 * @param useTkh |
|
330 */ |
|
331 private void writeCSVFlowDepthRow( |
|
332 final CSVWriter writer, |
|
333 final FlowDepthRow row, |
|
334 final boolean useTkh |
|
335 ) { |
|
336 log.debug("FlowDepthExporter.writeCSVFlowDepthRow"); |
|
337 |
|
338 final String[] formattedRow = formatFlowDepthRow(row, useTkh); |
|
339 writer.writeNext( formattedRow ); |
|
340 } |
|
341 |
|
342 @Override |
|
343 protected void writePDF(OutputStream outStream) { |
|
344 log.debug("write PDF"); |
|
345 |
|
346 final JRDataSource source = createJRData(); |
|
347 |
|
348 final String confPath = Config.getConfigDirectory().toString(); |
|
349 |
|
350 // FIXME: distinguish between with and without tkh: we need two jasper reports! |
|
351 |
|
352 final Map<String,Object> parameters = new HashMap<>(); |
|
353 parameters.put("ReportTitle", "Exported Data"); |
|
354 try { |
|
355 final JasperPrint print = JasperFillManager.fillReport( |
|
356 confPath + JASPER_FILE, |
|
357 parameters, |
|
358 source); |
|
359 JasperExportManager.exportReportToPdfStream(print, outStream); |
|
360 } |
|
361 catch(JRException je) { |
|
362 log.warn("Error generating PDF Report!", je); |
|
363 } |
|
364 } |
|
365 |
|
366 private JRDataSource createJRData() { |
|
367 |
|
368 /* fetch calculation results */ |
|
369 final FlowDepthCalculationResults results = data; |
|
370 |
|
371 final MetaAndTableJRDataSource source = new MetaAndTableJRDataSource(); |
|
372 |
|
373 addJRMetaData(source, results); |
|
374 |
|
375 final boolean useTkh = results.isUseTkh(); |
|
376 |
|
377 for (final FlowDepthCalculationResult result : results.getResults()) { |
|
378 addJRTableData(source, result, useTkh); |
|
379 } |
|
380 |
|
381 return source; |
|
382 } |
|
383 |
|
384 private void addJRMetaData(final MetaAndTableJRDataSource source, FlowDepthCalculationResults results) { |
|
385 |
8863
|
386 final River river = results.getRiver(); |
|
387 final String wstUnitName = river.getWstUnit().getName(); |
8854
|
388 |
8863
|
389 /* general metadata */ |
|
390 source.addMetaData("header", msg(CSV_META_HEADER_RESULT_LABEL)); |
|
391 source.addMetaData("calcMode", results.getCalcModeLabel()); |
|
392 |
|
393 source.addMetaData("version_label", msg(CSV_META_VERSION_LABEL)); |
|
394 source.addMetaData("version", FLYS.VERSION); |
|
395 |
|
396 source.addMetaData("user_label", msg(CSV_META_USER_LABEL)); |
|
397 source.addMetaData("user", results.getUser()); |
8854
|
398 |
|
399 final Locale locale = Resources.getLocale(context.getMeta()); |
|
400 final DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale); |
8863
|
401 source.addMetaData("date_label", msg(CSV_META_CREATION_LABEL)); |
8854
|
402 source.addMetaData("date", df.format(new Date())); |
|
403 |
8863
|
404 source.addMetaData("river_label", msg(CSV_META_RIVER_LABEL) ); |
|
405 source.addMetaData("river", river.getName()); |
8854
|
406 |
8863
|
407 final String rangeValue = String.format( "%s - %s", getKmFormatter().format(results.getFrom() ), getKmFormatter().format( results.getTo())); |
|
408 source.addMetaData("range_label", msg(CSV_META_RANGE_LABEL)); |
|
409 source.addMetaData("range", rangeValue); |
8854
|
410 |
8863
|
411 /* column headings */ |
|
412 source.addMetaData("station_header", msg(CSV_KM_HEADER)); |
|
413 source.addMetaData("flowdepth_header", msg(CSV_FLOWDEPTH_HEADER)); |
|
414 source.addMetaData("flowdepth_tkh_header", msg(CSV_FLOWDEPTHTKH_HEADER)); |
|
415 source.addMetaData("tkh_header", msg(CSV_TKH_HEADER)); |
|
416 source.addMetaData("waterlevel_header", msg(CSV_WATERLEVEL_HEADER)); |
|
417 source.addMetaData("river_unit", wstUnitName); |
|
418 source.addMetaData("discharge_header", msg(CSV_DISCHARGE_HEADER)); |
|
419 source.addMetaData("waterlevel_name_header", msg(CSV_LABEL_HEADER)); |
|
420 source.addMetaData("gauge_header", msg(CSV_GAUGE_HEADER)); |
|
421 source.addMetaData("bedheight_header", msg(CSV_MEAN_BED_HEIGHT_HEADER_SHORT)); |
|
422 source.addMetaData("sounding_name_header", msg(CSV_SOUNDING_HEADER)); |
|
423 source.addMetaData("location_header", msg(CSV_LOCATION_HEADER)); |
8854
|
424 } |
|
425 |
|
426 private void addJRTableData(final MetaAndTableJRDataSource source, final FlowDepthCalculationResult result, final boolean useTkh) { |
|
427 |
|
428 final Collection<FlowDepthRow> rows = result.getRows(); |
8863
|
429 |
8854
|
430 for (final FlowDepthRow row : rows) { |
|
431 |
|
432 final String[] formattedRow = formatFlowDepthRow(row, useTkh); |
|
433 source.addData(formattedRow); |
|
434 } |
|
435 } |
|
436 } |