Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhstate/TkhExporter.java @ 8949:09e4a4909814
Distinguish between csv and pdf column headers
author | gernotbelger |
---|---|
date | Wed, 14 Mar 2018 18:34:34 +0100 |
parents | a4f1ac81f26d |
children | 7c1611b5a59e |
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 | |
41 private static final String JASPER_FILE = "/jasper/sinfo.flowdepth.jasper"; | |
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 | |
8948 | 70 header.add(msg(SInfoResultType.station.getCsvHeader())); |
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); |
8928 | 93 |
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 @Override | |
8948 | 100 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
|
101 return formatRow(results, row, ExportMode.csv); |
8928 | 102 } |
103 | |
104 /** | |
105 * Format a row of a flow depth result into an array of string, both used by csv and pdf | |
106 * | |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
107 * @param results |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
108 * |
8928 | 109 * @param useTkh |
110 */ | |
8948 | 111 private String[] formatRow(final TkhCalculationResults results, final SInfoResultRow row, final ExportMode mode) { |
8928 | 112 |
113 final Collection<String> lines = new ArrayList<>(11); | |
114 | |
8948 | 115 lines.add(row.exportValue(this.context, SInfoResultType.station)); |
116 lines.add(row.exportValue(this.context, SInfoResultType.tkh)); | |
117 lines.add(row.exportValue(this.context, SInfoResultType.soilkind)); | |
118 lines.add(row.exportValue(this.context, SInfoResultType.meanBedHeight)); | |
119 lines.add(row.exportValue(this.context, SInfoResultType.waterlevel)); | |
120 lines.add(row.exportValue(this.context, SInfoResultType.discharge)); | |
8928 | 121 |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
122 // 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
|
123 if (results.getDescriptionHeader() != null || mode == ExportMode.pdf) |
8948 | 124 lines.add(row.exportValue(this.context, SInfoResultType.waterlevelLabel)); |
8928 | 125 |
8948 | 126 lines.add(row.exportValue(this.context, SInfoResultType.gaugeLabel)); |
127 lines.add(row.exportValue(this.context, SInfoResultType.location)); | |
8928 | 128 |
129 return lines.toArray(new String[lines.size()]); | |
130 } | |
131 | |
132 @Override | |
133 protected final String getJasperFile() { | |
134 return JASPER_FILE; | |
135 } | |
136 | |
137 @Override | |
138 protected final void addJRMetaData(final MetaAndTableJRDataSource source, final TkhCalculationResults results) { | |
139 | |
140 /* general metadata */ | |
8946
5d5d482da3e9
Implementing SINFO - FlowDepthMinMax calculation
gernotbelger
parents:
8938
diff
changeset
|
141 super.addJRMetaDataDefaults(source, results); |
8928 | 142 |
143 /* column headings */ | |
8949
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
144 source.addMetaData("station_header", SInfoResultType.station.getPdfHeader(this.context.getMeta())); |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
145 source.addMetaData("tkh_header", SInfoResultType.tkh.getPdfHeader(this.context.getMeta())); |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
146 source.addMetaData("bedheight_header", SInfoResultType.meanBedHeight.getPdfHeader(this.context.getMeta())); |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
147 source.addMetaData("waterlevel_header", SInfoResultType.waterlevel.getPdfHeader(this.context.getMeta())); |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
148 source.addMetaData("discharge_header", SInfoResultType.discharge.getPdfHeader(this.context.getMeta())); |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
149 |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
150 // 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
|
151 // 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
|
152 final String descriptionHeader = results.getDescriptionHeader(); |
8949
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
153 final String waterlevelNameHeader = descriptionHeader == null ? SInfoResultType.waterlevelLabel.getPdfHeader(this.context.getMeta()) |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
154 : descriptionHeader; |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
155 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
|
156 |
8949
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
157 source.addMetaData("gauge_header", SInfoResultType.gaugeLabel.getPdfHeader(this.context.getMeta())); |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
158 source.addMetaData("location_header", SInfoResultType.location.getPdfHeader(this.context.getMeta())); |
8928 | 159 } |
160 | |
161 @Override | |
8948 | 162 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
|
163 return formatRow(results, row, ExportMode.pdf); |
8928 | 164 } |
165 } |