Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhstate/TkhExporter.java @ 8948:a4f1ac81f26d
Work on SINFO-FlowDepthMinMax.
Also rework of result row stuff, in order to reduce abstraction, using result type concept
author | gernotbelger |
---|---|
date | Wed, 14 Mar 2018 14:10:32 +0100 |
parents | 5d5d482da3e9 |
children | 09e4a4909814 |
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 | |
11 import java.util.ArrayList; | |
12 import java.util.Collection; | |
13 | |
14 import org.apache.log4j.Logger; | |
15 import org.dive4elements.river.artifacts.sinfo.common.AbstractSInfoExporter; | |
8948 | 16 import org.dive4elements.river.artifacts.sinfo.common.SInfoResultRow; |
17 import org.dive4elements.river.artifacts.sinfo.common.SInfoResultType; | |
8928 | 18 import org.dive4elements.river.artifacts.sinfo.util.MetaAndTableJRDataSource; |
19 import org.dive4elements.river.artifacts.sinfo.util.RiverInfo; | |
20 import org.dive4elements.river.artifacts.sinfo.util.WstInfo; | |
21 | |
22 import au.com.bytecode.opencsv.CSVWriter; | |
23 | |
24 /** | |
25 * Generates different output formats (csv, pdf) of data that resulted from a tkh computation. | |
26 * | |
27 * @author Gernot Belger | |
28 */ | |
29 // REMARK: must be public because its registered in generators.xml | |
8948 | 30 public class TkhExporter extends AbstractSInfoExporter<TkhCalculationResult, TkhCalculationResults> { |
8928 | 31 |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
32 private static enum ExportMode { |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
33 pdf, csv |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
34 } |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
35 |
8928 | 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 | |
8948 | 41 // FIXME: use pgetPdfHeader instead of getCsvHeader |
8928 | 42 private static final String CSV_MEAN_BED_HEIGHT_HEADER_SHORT = "sinfo.export.flow_depth.csv.header.mean_bed_height.short"; |
43 | |
44 private static final String JASPER_FILE = "/jasper/sinfo.flowdepth.jasper"; | |
45 | |
46 @Override | |
47 protected Logger getLog() { | |
48 return log; | |
49 } | |
50 | |
51 @Override | |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
52 protected void writeCSVGlobalMetadata(final CSVWriter writer, final TkhCalculationResults results) { |
8928 | 53 log.info("TkhExporter.writeCSVMeta"); |
54 | |
8946
5d5d482da3e9
Implementing SINFO - FlowDepthMinMax calculation
gernotbelger
parents:
8938
diff
changeset
|
55 super.writeCSVGlobalMetadataDefaults(writer, results); |
8928 | 56 |
57 // "# Berechnungsgrundlage: Gleichung nach GILL (1971)" | |
58 writeCSVMetaEntry(writer, CSV_META_CALCULATION_FORMULA); | |
59 | |
60 writer.writeNext(new String[] { "" }); | |
61 } | |
62 | |
63 /** | |
64 * Write the header, with different headings depending on whether at a gauge or at a location. | |
65 */ | |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
66 |
8928 | 67 @Override |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
68 protected void writeCSVHeader(final CSVWriter writer, final TkhCalculationResults results, final RiverInfo river) { |
8928 | 69 log.info("TkhExporter.writeCSVHeader"); |
70 | |
71 final Collection<String> header = new ArrayList<>(11); | |
72 | |
8948 | 73 header.add(msg(SInfoResultType.station.getCsvHeader())); |
74 header.add(msgUnit(SInfoResultType.tkh.getCsvHeader(), SInfoResultType.tkh.getUnit())); | |
75 header.add(msg(SInfoResultType.soilkind.getCsvHeader())); | |
76 header.add(msgUnit(SInfoResultType.meanBedHeight.getCsvHeader(), river.getWstUnit())); | |
77 header.add(msgUnit(SInfoResultType.waterlevel.getCsvHeader(), river.getWstUnit())); | |
78 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
|
79 |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
80 final String descriptionHeader = results.getDescriptionHeader(); |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
81 if (descriptionHeader != null) |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
82 header.add(msg(descriptionHeader)); |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
83 |
8948 | 84 header.add(msg(SInfoResultType.gaugeLabel.getCsvHeader())); |
85 header.add(msg(SInfoResultType.location.getCsvHeader())); | |
8928 | 86 |
87 writer.writeNext(header.toArray(new String[header.size()])); | |
88 } | |
89 | |
90 @Override | |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
91 // FIXME: rename |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
92 protected void writeCSVResultMetadata(final CSVWriter writer, final TkhCalculationResults results, final TkhCalculationResult result) { |
8928 | 93 |
94 final WstInfo wst = result.getWst(); | |
8946
5d5d482da3e9
Implementing SINFO - FlowDepthMinMax calculation
gernotbelger
parents:
8938
diff
changeset
|
95 super.writeCSVWaterlevelMetadata(writer, wst); |
8928 | 96 |
8948 | 97 // FIXME: |
8946
5d5d482da3e9
Implementing SINFO - FlowDepthMinMax calculation
gernotbelger
parents:
8938
diff
changeset
|
98 // "# W/Pegel [cm]: " (nur bei Eingabe des Wasserstands am Pegel) |
5d5d482da3e9
Implementing SINFO - FlowDepthMinMax calculation
gernotbelger
parents:
8938
diff
changeset
|
99 // "# Q (m³/s): " (nur bei Eingabe des Durchflusses) |
8928 | 100 } |
101 | |
102 @Override | |
8948 | 103 protected String[] formatCSVRow(final TkhCalculationResults results, final TkhCalculationResult result, final SInfoResultRow row) { |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
104 return formatRow(results, row, ExportMode.csv); |
8928 | 105 } |
106 | |
107 /** | |
108 * Format a row of a flow depth result into an array of string, both used by csv and pdf | |
109 * | |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
110 * @param results |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
111 * |
8928 | 112 * @param useTkh |
113 */ | |
8948 | 114 private String[] formatRow(final TkhCalculationResults results, final SInfoResultRow row, final ExportMode mode) { |
8928 | 115 |
116 final Collection<String> lines = new ArrayList<>(11); | |
117 | |
8948 | 118 lines.add(row.exportValue(this.context, SInfoResultType.station)); |
119 lines.add(row.exportValue(this.context, SInfoResultType.tkh)); | |
120 lines.add(row.exportValue(this.context, SInfoResultType.soilkind)); | |
121 lines.add(row.exportValue(this.context, SInfoResultType.meanBedHeight)); | |
122 lines.add(row.exportValue(this.context, SInfoResultType.waterlevel)); | |
123 lines.add(row.exportValue(this.context, SInfoResultType.discharge)); | |
8928 | 124 |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
125 // 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
|
126 if (results.getDescriptionHeader() != null || mode == ExportMode.pdf) |
8948 | 127 lines.add(row.exportValue(this.context, SInfoResultType.waterlevelLabel)); |
8928 | 128 |
8948 | 129 lines.add(row.exportValue(this.context, SInfoResultType.gaugeLabel)); |
130 lines.add(row.exportValue(this.context, SInfoResultType.location)); | |
8928 | 131 |
132 return lines.toArray(new String[lines.size()]); | |
133 } | |
134 | |
135 @Override | |
136 protected final String getJasperFile() { | |
137 return JASPER_FILE; | |
138 } | |
139 | |
140 @Override | |
141 protected final void addJRMetaData(final MetaAndTableJRDataSource source, final TkhCalculationResults results) { | |
142 | |
143 /* general metadata */ | |
8946
5d5d482da3e9
Implementing SINFO - FlowDepthMinMax calculation
gernotbelger
parents:
8938
diff
changeset
|
144 super.addJRMetaDataDefaults(source, results); |
8928 | 145 |
146 /* column headings */ | |
8948 | 147 source.addMetaData("station_header", msg( SInfoResultType.station.getCsvHeader())); |
148 source.addMetaData("tkh_header", msg(SInfoResultType.tkh.getCsvHeader())); | |
149 source.addMetaData("bedheight_header", msg(SInfoResultType.meanBedHeight.getCsvHeader())); | |
150 source.addMetaData("waterlevel_header", msg(SInfoResultType.waterlevel.getCsvHeader())); | |
151 source.addMetaData("discharge_header", msg(SInfoResultType.discharge.getCsvHeader())); | |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
152 |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
153 // 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
|
154 // 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
|
155 final String descriptionHeader = results.getDescriptionHeader(); |
8948 | 156 final String waterlevelNameHeader = descriptionHeader == null ? msg(SInfoResultType.waterlevelLabel.getCsvHeader()) : descriptionHeader; |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
157 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
|
158 |
8948 | 159 source.addMetaData("gauge_header", msg(SInfoResultType.gaugeLabel.getCsvHeader())); |
160 source.addMetaData("location_header", msg(SInfoResultType.location.getCsvHeader())); | |
8928 | 161 } |
162 | |
163 @Override | |
8948 | 164 protected String[] formatPDFRow(final TkhCalculationResults results, final SInfoResultRow row) { |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
165 return formatRow(results, row, ExportMode.pdf); |
8928 | 166 } |
167 } |