Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhstate/TkhExporter.java @ 9006:7134a4c7d1b6
U-Info S-Info general work
author | gernotbelger |
---|---|
date | Tue, 17 Apr 2018 13:20:25 +0200 |
parents | fb9430250899 |
children | ae3565385e6a |
rev | line source |
---|---|
8928 | 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.tkhstate; | |
10 | |
9006 | 11 import java.io.OutputStream; |
8928 | 12 import java.util.ArrayList; |
13 import java.util.Collection; | |
14 | |
15 import org.apache.log4j.Logger; | |
8996 | 16 import org.dive4elements.river.artifacts.common.GeneralResultType; |
9006 | 17 import org.dive4elements.river.artifacts.common.JasperReporter; |
18 import org.dive4elements.river.artifacts.common.MetaAndTableJRDataSource; | |
8996 | 19 import org.dive4elements.river.artifacts.common.ResultRow; |
8928 | 20 import org.dive4elements.river.artifacts.sinfo.common.AbstractSInfoExporter; |
8948 | 21 import org.dive4elements.river.artifacts.sinfo.common.SInfoResultType; |
8928 | 22 import org.dive4elements.river.artifacts.sinfo.util.RiverInfo; |
23 import org.dive4elements.river.artifacts.sinfo.util.WstInfo; | |
24 | |
25 import au.com.bytecode.opencsv.CSVWriter; | |
9006 | 26 import net.sf.jasperreports.engine.JRException; |
8928 | 27 |
28 /** | |
29 * Generates different output formats (csv, pdf) of data that resulted from a tkh computation. | |
30 * | |
31 * @author Gernot Belger | |
32 */ | |
33 // REMARK: must be public because its registered in generators.xml | |
8948 | 34 public class TkhExporter extends AbstractSInfoExporter<TkhCalculationResult, TkhCalculationResults> { |
8928 | 35 |
36 /** The log used in this exporter. */ | |
37 private static Logger log = Logger.getLogger(TkhExporter.class); | |
38 | |
39 private static final String CSV_META_CALCULATION_FORMULA = "sinfo.export.tkh.calculation.formula"; | |
40 | |
9006 | 41 private static final String JASPER_FILE = "/jasper/templates/sinfo.tkh.jrxml"; |
8928 | 42 |
43 @Override | |
44 protected Logger getLog() { | |
45 return log; | |
46 } | |
47 | |
48 @Override | |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
49 protected void writeCSVGlobalMetadata(final CSVWriter writer, final TkhCalculationResults results) { |
8928 | 50 log.info("TkhExporter.writeCSVMeta"); |
51 | |
8946
5d5d482da3e9
Implementing SINFO - FlowDepthMinMax calculation
gernotbelger
parents:
8938
diff
changeset
|
52 super.writeCSVGlobalMetadataDefaults(writer, results); |
8928 | 53 |
54 // "# Berechnungsgrundlage: Gleichung nach GILL (1971)" | |
55 writeCSVMetaEntry(writer, CSV_META_CALCULATION_FORMULA); | |
56 | |
57 writer.writeNext(new String[] { "" }); | |
58 } | |
59 | |
60 /** | |
61 * Write the header, with different headings depending on whether at a gauge or at a location. | |
62 */ | |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
63 |
8928 | 64 @Override |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
65 protected void writeCSVHeader(final CSVWriter writer, final TkhCalculationResults results, final RiverInfo river) { |
8928 | 66 log.info("TkhExporter.writeCSVHeader"); |
67 | |
68 final Collection<String> header = new ArrayList<>(11); | |
69 | |
8996 | 70 header.add(msg(GeneralResultType.station.getCsvHeader())); |
8948 | 71 header.add(msgUnit(SInfoResultType.tkh.getCsvHeader(), SInfoResultType.tkh.getUnit())); |
72 header.add(msg(SInfoResultType.soilkind.getCsvHeader())); | |
73 header.add(msgUnit(SInfoResultType.meanBedHeight.getCsvHeader(), river.getWstUnit())); | |
74 header.add(msgUnit(SInfoResultType.waterlevel.getCsvHeader(), river.getWstUnit())); | |
75 header.add(msgUnit(SInfoResultType.discharge.getCsvHeader(), SInfoResultType.discharge.getUnit())); | |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
76 |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
77 final String descriptionHeader = results.getDescriptionHeader(); |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
78 if (descriptionHeader != null) |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
79 header.add(msg(descriptionHeader)); |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
80 |
8948 | 81 header.add(msg(SInfoResultType.gaugeLabel.getCsvHeader())); |
82 header.add(msg(SInfoResultType.location.getCsvHeader())); | |
8928 | 83 |
84 writer.writeNext(header.toArray(new String[header.size()])); | |
85 } | |
86 | |
87 @Override | |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
88 // FIXME: rename |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
89 protected void writeCSVResultMetadata(final CSVWriter writer, final TkhCalculationResults results, final TkhCalculationResult result) { |
8928 | 90 |
91 final WstInfo wst = result.getWst(); | |
8946
5d5d482da3e9
Implementing SINFO - FlowDepthMinMax calculation
gernotbelger
parents:
8938
diff
changeset
|
92 super.writeCSVWaterlevelMetadata(writer, wst); |
9006 | 93 writer.writeNext(new String[] { "" }); // break line |
8948 | 94 // FIXME: |
8946
5d5d482da3e9
Implementing SINFO - FlowDepthMinMax calculation
gernotbelger
parents:
8938
diff
changeset
|
95 // "# W/Pegel [cm]: " (nur bei Eingabe des Wasserstands am Pegel) |
5d5d482da3e9
Implementing SINFO - FlowDepthMinMax calculation
gernotbelger
parents:
8938
diff
changeset
|
96 // "# Q (m³/s): " (nur bei Eingabe des Durchflusses) |
8928 | 97 } |
98 | |
99 /** | |
100 * Format a row of a flow depth result into an array of string, both used by csv and pdf | |
101 * | |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
102 * @param results |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
103 * |
8928 | 104 * @param useTkh |
105 */ | |
8996 | 106 @Override |
107 protected String[] formatRow(final TkhCalculationResults results, final ResultRow row, final ExportMode mode) { | |
8928 | 108 |
109 final Collection<String> lines = new ArrayList<>(11); | |
110 | |
8996 | 111 lines.add(row.exportValue(this.context, GeneralResultType.station)); |
8948 | 112 lines.add(row.exportValue(this.context, SInfoResultType.tkh)); |
113 lines.add(row.exportValue(this.context, SInfoResultType.soilkind)); | |
114 lines.add(row.exportValue(this.context, SInfoResultType.meanBedHeight)); | |
115 lines.add(row.exportValue(this.context, SInfoResultType.waterlevel)); | |
116 lines.add(row.exportValue(this.context, SInfoResultType.discharge)); | |
8928 | 117 |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
118 // REMARK: always export this column in pdf-mode, because WInfo also does it (no need for two jasper-templates). |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
119 if (results.getDescriptionHeader() != null || mode == ExportMode.pdf) |
8948 | 120 lines.add(row.exportValue(this.context, SInfoResultType.waterlevelLabel)); |
8928 | 121 |
8948 | 122 lines.add(row.exportValue(this.context, SInfoResultType.gaugeLabel)); |
123 lines.add(row.exportValue(this.context, SInfoResultType.location)); | |
8928 | 124 |
125 return lines.toArray(new String[lines.size()]); | |
126 } | |
127 | |
128 @Override | |
129 protected final void addJRMetaData(final MetaAndTableJRDataSource source, final TkhCalculationResults results) { | |
130 | |
131 /* general metadata */ | |
8996 | 132 super.addJRMetaData(source, results); |
8928 | 133 |
8996 | 134 source.addMetaData("calculation_label", msg("sinfo.export.flow_depth.pdf.meta.calculation.label")); |
135 source.addMetaData("calculation_name", msg("sinfo.export.flow_depth.pdf.meta.calculation.name")); | |
136 | |
8928 | 137 /* column headings */ |
8996 | 138 source.addMetaData("station_header", GeneralResultType.station.getPdfHeader(this.context.getMeta())); |
8949
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
139 source.addMetaData("tkh_header", SInfoResultType.tkh.getPdfHeader(this.context.getMeta())); |
8984 | 140 source.addMetaData("tkhkind_header", SInfoResultType.soilkind.getPdfHeader(this.context.getMeta())); |
8949
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
141 source.addMetaData("bedheight_header", SInfoResultType.meanBedHeight.getPdfHeader(this.context.getMeta())); |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
142 source.addMetaData("waterlevel_header", SInfoResultType.waterlevel.getPdfHeader(this.context.getMeta())); |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
143 source.addMetaData("discharge_header", SInfoResultType.discharge.getPdfHeader(this.context.getMeta())); |
8996 | 144 |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
145 // REMARK: actually the column makes no sense if description header is null. But (software symmetry...) WINFO also |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
146 // writes an empty column into the pdf in that case (most probably to avoid the need for two jasper templates). |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
147 final String descriptionHeader = results.getDescriptionHeader(); |
8949
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
148 final String waterlevelNameHeader = descriptionHeader == null ? SInfoResultType.waterlevelLabel.getPdfHeader(this.context.getMeta()) |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
149 : descriptionHeader; |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
150 source.addMetaData("waterlevel_name_header", waterlevelNameHeader); |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
151 |
8949
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
152 source.addMetaData("gauge_header", SInfoResultType.gaugeLabel.getPdfHeader(this.context.getMeta())); |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
153 source.addMetaData("location_header", SInfoResultType.location.getPdfHeader(this.context.getMeta())); |
8928 | 154 } |
155 | |
9006 | 156 @Override |
157 protected void writePDF(final OutputStream out) { | |
158 try { | |
159 final MetaAndTableJRDataSource source = createJRData(this.data); | |
160 | |
161 final JasperReporter reporter = new JasperReporter(); | |
162 reporter.addReport(JASPER_FILE, source); | |
163 reporter.exportPDF(out); | |
164 } | |
165 catch (final JRException je) { | |
166 getLog().warn("Error generating PDF Report!", je); | |
167 } | |
168 } | |
169 | |
8928 | 170 } |