Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/common/ExportContextCSV.java @ 9188:3931e8741f53
Minor cleanup and bugfix concerning number formatters
author | gernotbelger |
---|---|
date | Thu, 28 Jun 2018 13:14:44 +0200 |
parents | f1be005f0c46 |
children | a4121ec450d6 |
rev | line source |
---|---|
9150 | 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.common; | |
11 | |
12 import java.text.DateFormat; | |
13 import java.text.NumberFormat; | |
9171 | 14 import java.util.ArrayList; |
15 import java.util.Collection; | |
9150 | 16 import java.util.Date; |
17 import java.util.Locale; | |
18 | |
19 import org.apache.commons.lang.math.DoubleRange; | |
20 import org.dive4elements.artifacts.CallContext; | |
21 import org.dive4elements.artifacts.CallMeta; | |
22 import org.dive4elements.river.FLYS; | |
23 import org.dive4elements.river.artifacts.resources.Resources; | |
24 import org.dive4elements.river.artifacts.sinfo.common.SInfoI18NStrings; | |
25 import org.dive4elements.river.artifacts.sinfo.common.SInfoResultType; | |
26 import org.dive4elements.river.artifacts.sinfo.util.BedHeightInfo; | |
27 import org.dive4elements.river.artifacts.sinfo.util.RiverInfo; | |
28 import org.dive4elements.river.artifacts.sinfo.util.WstInfo; | |
29 import org.dive4elements.river.utils.Formatter; | |
30 | |
31 import au.com.bytecode.opencsv.CSVWriter; | |
32 | |
33 /** | |
34 * @author Domenico Nardi Tironi | |
35 * | |
36 */ | |
37 public final class ExportContextCSV { | |
38 | |
39 private static final String CSV_META_HEADER_EVALUATOR = "sinfo.export.flow_depth.csv.meta.header.sounding.evaluator"; | |
40 | |
41 private static final String CSV_META_HEADER_SOUNDING = "sinfo.export.flow_depth.csv.meta.header.sounding"; | |
42 | |
43 private static final String CSV_META_HEADER_SOUNDING_YEAR = "sinfo.export.flow_depth.csv.meta.header.sounding.year"; | |
44 | |
45 private static final String CSV_META_HEADER_SOUNDING_TYPE = "sinfo.export.flow_depth.csv.meta.header.sounding.type"; | |
46 | |
47 private static final String CSV_META_HEADER_SOUNDING_PRJ = "sinfo.export.flow_depth.csv.meta.header.sounding.prj"; | |
48 | |
49 private static final String CSV_META_HEADER_SOUNDING_ELEVATIOIN_MODEL = "sinfo.export.flow_depth.csv.meta.header.sounding.elevationmodel"; | |
50 | |
51 private static final String CSV_META_HEADER_SOUNDING_ELEVATIOIN_MODEL_ORIGINAL = "sinfo.export.flow_depth.csv.meta.header.sounding.elevationmodel.original"; | |
52 | |
9188
3931e8741f53
Minor cleanup and bugfix concerning number formatters
gernotbelger
parents:
9171
diff
changeset
|
53 private NumberFormat qFormat = null; |
9150 | 54 |
9188
3931e8741f53
Minor cleanup and bugfix concerning number formatters
gernotbelger
parents:
9171
diff
changeset
|
55 private NumberFormat flowDepthFormat = null; |
9150 | 56 |
9188
3931e8741f53
Minor cleanup and bugfix concerning number formatters
gernotbelger
parents:
9171
diff
changeset
|
57 private NumberFormat kmFormat = null; |
9150 | 58 |
59 /** The CallContext object. */ | |
60 private final CallContext context; | |
61 | |
62 private final CSVWriter writer; | |
63 | |
64 public ExportContextCSV(final CallContext context, final CSVWriter writer) { | |
65 this.context = context; | |
66 this.writer = writer; | |
67 } | |
68 | |
69 private String msg(final String key) { | |
70 return Resources.getMsg(this.context.getMeta(), key, key); | |
71 } | |
72 | |
73 public String msg(final String key, final Object... args) { | |
74 return Resources.getMsg(this.context.getMeta(), key, key, args); | |
75 } | |
76 | |
77 public final void writeCSVMetaEntry(final String message, final Object... messageArgs) { | |
78 | |
79 final CallMeta meta = this.context.getMeta(); | |
80 | |
81 this.writer.writeNext(new String[] { Resources.getMsg(meta, message, message, messageArgs) }); | |
82 } | |
83 | |
84 public void writeBlankLine() { | |
85 writeCSVLine(new String[] { "" }); | |
86 } | |
87 | |
88 // *** CUSTOM STUFF that is used multiple times ***/// | |
89 | |
90 public final void writeCSVSoundingMetadata(final BedHeightInfo sounding) { | |
91 // "##METADATEN PEILUNG" | |
9154 | 92 writeCSVSoundingMetadata(sounding, CSV_META_HEADER_SOUNDING); |
9150 | 93 |
94 } | |
95 | |
96 public void writeCSVLine(final String[] line) { | |
97 this.writer.writeNext(line); | |
98 } | |
99 | |
100 public String formatCsvHeader(final IResultType type) { | |
101 return msg(type.getCsvHeader()); | |
102 } | |
103 | |
104 public String formatRowValue(final ResultRow row, final IResultType type) { | |
105 return row.exportValue(this.context, type); | |
106 } | |
107 | |
108 public void addJRMetadata(final MetaAndTableJRDataSource source, final String key, final IResultType type) { | |
109 source.addMetaData(key, type.getPdfHeader(this.context.getMeta())); | |
110 } | |
111 | |
9188
3931e8741f53
Minor cleanup and bugfix concerning number formatters
gernotbelger
parents:
9171
diff
changeset
|
112 public final void writeCSVGlobalMetadataDefaults(final AbstractCalculationResults<?> results) { |
9150 | 113 // TODO: results as member |
114 final String calcModeLabel = results.getCalcModeLabel(); | |
115 final RiverInfo river = results.getRiver(); | |
116 final DoubleRange calcRange = results.getCalcRange(); | |
117 | |
118 writeCSVMetaEntry(I18NStrings.CSV_META_HEADER_RESULT, msg(I18NStrings.CSV_META_HEADER_RESULT_LABEL), river.getName(), calcModeLabel); | |
119 | |
120 // "# FLYS-Version: " | |
121 writeCSVMetaEntry(I18NStrings.CSV_META_VERSION, msg(I18NStrings.CSV_META_VERSION_LABEL), FLYS.VERSION); | |
122 | |
123 // "# Bearbeiter: " | |
124 writeCSVMetaEntry(I18NStrings.CSV_META_USER, msg(I18NStrings.CSV_META_USER_LABEL), results.getUser()); | |
125 | |
126 // "# Datum der Erstellung: " | |
127 final Locale locale = Resources.getLocale(this.context.getMeta()); | |
128 final DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale); | |
129 writeCSVMetaEntry(I18NStrings.CSV_META_CREATION, msg(I18NStrings.CSV_META_CREATION_LABEL), df.format(new Date())); | |
130 | |
131 // "# Gewässer: " | |
132 writeCSVMetaEntry(I18NStrings.CSV_META_RIVER, msg(I18NStrings.CSV_META_RIVER_LABEL), river.getName()); | |
133 | |
134 // "# Höhensystem des Flusses: " | |
135 writeCSVMetaEntry(I18NStrings.CSV_META_HEIGHT_UNIT_RIVER, river.getWstUnit()); | |
136 | |
137 if (calcRange != null) { | |
138 // "# Ort/Bereich (km): " | |
139 writeCSVMetaEntry(I18NStrings.CSV_META_RANGE, msg(I18NStrings.CSV_META_RANGE_LABEL), getKmFormatter().format(calcRange.getMinimumDouble()), | |
140 getKmFormatter().format(calcRange.getMaximumDouble())); | |
141 } | |
142 } | |
143 | |
144 public final void writeCSVWaterlevelMetadata(final WstInfo wst) { | |
145 writeCSVWaterlevelMetadata(wst, SInfoI18NStrings.CSV_META_HEADER_WATERLEVEL); // default Label; used in 99% | |
146 } | |
147 | |
148 public final void writeCSVWaterlevelMetadata(final WstInfo wst, final String mainLabel) { | |
149 // "##METADATEN WASSERSPIEGELLAGE" | |
150 writeCSVMetaEntry(mainLabel); | |
151 | |
152 // "# Bezeichnung der Wasserspiegellage: " | |
153 writeCSVMetaEntry(SInfoI18NStrings.CSV_META_HEADER_WATERLEVEL_NAME, wst.getLabel()); | |
154 | |
155 // "# Bezugspegel: " | |
156 writeCSVMetaEntry(SInfoI18NStrings.CSV_META_HEADER_WATERLEVEL_GAUGE, wst.getGauge()); | |
157 | |
158 // "# Jahr/Zeitraum der Wasserspiegellage: " | |
159 final int year = wst.getYear(); | |
160 if (year > 0) | |
161 writeCSVMetaEntry(SInfoI18NStrings.CSV_META_HEADER_WATERLEVEL_YEAR, Integer.toString(year)); | |
162 } | |
163 | |
164 // copy from AbstractExporter TODO merge with ExportContextPDF | |
165 protected NumberFormat getKmFormatter() { | |
166 | |
9188
3931e8741f53
Minor cleanup and bugfix concerning number formatters
gernotbelger
parents:
9171
diff
changeset
|
167 if (this.kmFormat == null) |
9150 | 168 this.kmFormat = Formatter.getWaterlevelKM(this.context); |
9188
3931e8741f53
Minor cleanup and bugfix concerning number formatters
gernotbelger
parents:
9171
diff
changeset
|
169 |
9150 | 170 return this.kmFormat; |
171 } | |
172 | |
173 public void addJRMetadata(final MetaAndTableJRDataSource source, final String key, final String msg) { | |
174 source.addMetaData(key, msg); | |
175 } | |
176 | |
177 public NumberFormat getQFormatter() { | |
9188
3931e8741f53
Minor cleanup and bugfix concerning number formatters
gernotbelger
parents:
9171
diff
changeset
|
178 if (this.qFormat == null) |
9150 | 179 this.qFormat = Formatter.getWaterlevelQ(this.context); |
9188
3931e8741f53
Minor cleanup and bugfix concerning number formatters
gernotbelger
parents:
9171
diff
changeset
|
180 |
9150 | 181 return this.qFormat; |
182 } | |
183 | |
184 public final NumberFormat getFlowDepthFormatter() { | |
185 if (this.flowDepthFormat == null) | |
186 this.flowDepthFormat = Formatter.getFlowDepth(this.context); | |
9188
3931e8741f53
Minor cleanup and bugfix concerning number formatters
gernotbelger
parents:
9171
diff
changeset
|
187 |
9150 | 188 return this.flowDepthFormat; |
189 } | |
190 | |
191 /** | |
192 * Formats header with unit: msg [unit] | |
193 */ | |
194 | |
195 public String msgUnitCSV(final IResultType typeWithUnit) { // TODO: use generic Type! | |
196 final String unit = msg(typeWithUnit.getUnit()); | |
197 return msgUnitCSV(typeWithUnit, unit); | |
198 } | |
199 | |
200 public String msgUnitCSV(final IResultType type, final String unit) { | |
201 final String msg = msg(type.getCsvHeader()); | |
202 return String.format("%s [%s]", msg, unit); | |
203 } | |
204 | |
9154 | 205 public void writeCSVSoundingMetadata(final BedHeightInfo sounding, final String label) { |
206 | |
207 // "##METADATEN PEILUNG" | |
208 writeCSVMetaEntry(label); | |
209 | |
210 // "# Jahr der Peilung: " | |
211 writeCSVMetaEntry(CSV_META_HEADER_SOUNDING_YEAR, Integer.toString(sounding.getYear())); | |
212 // "# Aufnahmeart: " | |
213 writeCSVMetaEntry(CSV_META_HEADER_SOUNDING_TYPE, sounding.getType()); | |
214 // "# Auswerter: " | |
215 writeCSVMetaEntry(CSV_META_HEADER_EVALUATOR, sounding.getEvaluationBy()); | |
216 // "# Lagesystem: " | |
217 writeCSVMetaEntry(CSV_META_HEADER_SOUNDING_PRJ, sounding.getLocationSystem()); | |
218 // "# Höhensystem: " | |
219 writeCSVMetaEntry(CSV_META_HEADER_SOUNDING_ELEVATIOIN_MODEL, sounding.getCurElevationModelUnit()); | |
220 // "# ursprüngliches Höhensystem: " | |
221 writeCSVMetaEntry(CSV_META_HEADER_SOUNDING_ELEVATIOIN_MODEL_ORIGINAL, sounding.getOldElevationModelUnit()); | |
9150 | 222 |
223 } | |
224 | |
225 public final String msgUnitLabel(final IResultType typeWithUnit, final String label) { | |
226 final String msg = msg(typeWithUnit.getCsvHeader()); | |
227 final String unit = msg(typeWithUnit.getUnit()); | |
228 return String.format("%s [%s] (%s)", msg, unit, label); | |
229 } | |
230 | |
231 public String msgPdf(final SInfoResultType type) { | |
232 return type.getPdfHeader(this.context.getMeta()); | |
233 } | |
9171 | 234 |
235 public void writeTitleForTabs(final String tabTitleMsg, final int colSize) { | |
236 | |
237 final Collection<String> title = new ArrayList<>(colSize); | |
238 title.add(msg("export_csv_title") + msg(tabTitleMsg)); | |
239 writeCSVLine(title.toArray(new String[colSize])); | |
240 } | |
9150 | 241 } |