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 |
|
53 private static NumberFormat qFormat = null; |
|
54 |
|
55 private static NumberFormat flowDepthFormat = null; |
|
56 |
|
57 private NumberFormat kmFormat; |
|
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 |
|
89 // *** CUSTOM STUFF that is used multiple times ***/// |
|
90 |
|
91 public final void writeCSVSoundingMetadata(final BedHeightInfo sounding) { |
|
92 // "##METADATEN PEILUNG" |
9154
|
93 writeCSVSoundingMetadata(sounding, CSV_META_HEADER_SOUNDING); |
9150
|
94 |
|
95 } |
|
96 |
|
97 public void writeCSVLine(final String[] line) { |
|
98 this.writer.writeNext(line); |
|
99 } |
|
100 |
|
101 public String formatCsvHeader(final IResultType type) { |
|
102 return msg(type.getCsvHeader()); |
|
103 } |
|
104 |
|
105 public String formatRowValue(final ResultRow row, final IResultType type) { |
|
106 return row.exportValue(this.context, type); |
|
107 } |
|
108 |
|
109 public void addJRMetadata(final MetaAndTableJRDataSource source, final String key, final IResultType type) { |
|
110 source.addMetaData(key, type.getPdfHeader(this.context.getMeta())); |
|
111 } |
|
112 |
|
113 public final void writeCSVGlobalMetadataDefaults(final AbstractCalculationResults results) { |
|
114 // TODO: results as member |
|
115 final String calcModeLabel = results.getCalcModeLabel(); |
|
116 final RiverInfo river = results.getRiver(); |
|
117 final DoubleRange calcRange = results.getCalcRange(); |
|
118 |
|
119 writeCSVMetaEntry(I18NStrings.CSV_META_HEADER_RESULT, msg(I18NStrings.CSV_META_HEADER_RESULT_LABEL), river.getName(), calcModeLabel); |
|
120 |
|
121 // "# FLYS-Version: " |
|
122 writeCSVMetaEntry(I18NStrings.CSV_META_VERSION, msg(I18NStrings.CSV_META_VERSION_LABEL), FLYS.VERSION); |
|
123 |
|
124 // "# Bearbeiter: " |
|
125 writeCSVMetaEntry(I18NStrings.CSV_META_USER, msg(I18NStrings.CSV_META_USER_LABEL), results.getUser()); |
|
126 |
|
127 // "# Datum der Erstellung: " |
|
128 final Locale locale = Resources.getLocale(this.context.getMeta()); |
|
129 final DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale); |
|
130 writeCSVMetaEntry(I18NStrings.CSV_META_CREATION, msg(I18NStrings.CSV_META_CREATION_LABEL), df.format(new Date())); |
|
131 |
|
132 // "# Gewässer: " |
|
133 writeCSVMetaEntry(I18NStrings.CSV_META_RIVER, msg(I18NStrings.CSV_META_RIVER_LABEL), river.getName()); |
|
134 |
|
135 // "# Höhensystem des Flusses: " |
|
136 writeCSVMetaEntry(I18NStrings.CSV_META_HEIGHT_UNIT_RIVER, river.getWstUnit()); |
|
137 |
|
138 if (calcRange != null) { |
|
139 // "# Ort/Bereich (km): " |
|
140 writeCSVMetaEntry(I18NStrings.CSV_META_RANGE, msg(I18NStrings.CSV_META_RANGE_LABEL), getKmFormatter().format(calcRange.getMinimumDouble()), |
|
141 getKmFormatter().format(calcRange.getMaximumDouble())); |
|
142 } |
|
143 } |
|
144 |
|
145 public final void writeCSVWaterlevelMetadata(final WstInfo wst) { |
|
146 writeCSVWaterlevelMetadata(wst, SInfoI18NStrings.CSV_META_HEADER_WATERLEVEL); // default Label; used in 99% |
|
147 } |
|
148 |
|
149 public final void writeCSVWaterlevelMetadata(final WstInfo wst, final String mainLabel) { |
|
150 // "##METADATEN WASSERSPIEGELLAGE" |
|
151 writeCSVMetaEntry(mainLabel); |
|
152 |
|
153 // "# Bezeichnung der Wasserspiegellage: " |
|
154 writeCSVMetaEntry(SInfoI18NStrings.CSV_META_HEADER_WATERLEVEL_NAME, wst.getLabel()); |
|
155 |
|
156 // "# Bezugspegel: " |
|
157 writeCSVMetaEntry(SInfoI18NStrings.CSV_META_HEADER_WATERLEVEL_GAUGE, wst.getGauge()); |
|
158 |
|
159 // "# Jahr/Zeitraum der Wasserspiegellage: " |
|
160 final int year = wst.getYear(); |
|
161 if (year > 0) |
|
162 writeCSVMetaEntry(SInfoI18NStrings.CSV_META_HEADER_WATERLEVEL_YEAR, Integer.toString(year)); |
|
163 } |
|
164 |
|
165 // copy from AbstractExporter TODO merge with ExportContextPDF |
|
166 protected NumberFormat getKmFormatter() { |
|
167 |
|
168 if (this.kmFormat == null) { |
|
169 this.kmFormat = Formatter.getWaterlevelKM(this.context); |
|
170 } |
|
171 return this.kmFormat; |
|
172 } |
|
173 |
|
174 public void addJRMetadata(final MetaAndTableJRDataSource source, final String key, final String msg) { |
|
175 source.addMetaData(key, msg); |
|
176 |
|
177 } |
|
178 |
|
179 public NumberFormat getQFormatter() { |
|
180 if (this.qFormat == null) { |
|
181 this.qFormat = Formatter.getWaterlevelQ(this.context); |
|
182 } |
|
183 return this.qFormat; |
|
184 } |
|
185 |
|
186 public final NumberFormat getFlowDepthFormatter() { |
|
187 if (this.flowDepthFormat == null) |
|
188 this.flowDepthFormat = Formatter.getFlowDepth(this.context); |
|
189 return this.flowDepthFormat; |
|
190 } |
|
191 |
|
192 /** |
|
193 * Formats header with unit: msg [unit] |
|
194 */ |
|
195 |
|
196 public String msgUnitCSV(final IResultType typeWithUnit) { // TODO: use generic Type! |
|
197 final String unit = msg(typeWithUnit.getUnit()); |
|
198 return msgUnitCSV(typeWithUnit, unit); |
|
199 } |
|
200 |
|
201 public String msgUnitCSV(final IResultType type, final String unit) { |
|
202 final String msg = msg(type.getCsvHeader()); |
|
203 return String.format("%s [%s]", msg, unit); |
|
204 } |
|
205 |
9154
|
206 public void writeCSVSoundingMetadata(final BedHeightInfo sounding, final String label) { |
|
207 |
|
208 // "##METADATEN PEILUNG" |
|
209 writeCSVMetaEntry(label); |
|
210 |
|
211 // "# Jahr der Peilung: " |
|
212 writeCSVMetaEntry(CSV_META_HEADER_SOUNDING_YEAR, Integer.toString(sounding.getYear())); |
|
213 // "# Aufnahmeart: " |
|
214 writeCSVMetaEntry(CSV_META_HEADER_SOUNDING_TYPE, sounding.getType()); |
|
215 // "# Auswerter: " |
|
216 writeCSVMetaEntry(CSV_META_HEADER_EVALUATOR, sounding.getEvaluationBy()); |
|
217 // "# Lagesystem: " |
|
218 writeCSVMetaEntry(CSV_META_HEADER_SOUNDING_PRJ, sounding.getLocationSystem()); |
|
219 // "# Höhensystem: " |
|
220 writeCSVMetaEntry(CSV_META_HEADER_SOUNDING_ELEVATIOIN_MODEL, sounding.getCurElevationModelUnit()); |
|
221 // "# ursprüngliches Höhensystem: " |
|
222 writeCSVMetaEntry(CSV_META_HEADER_SOUNDING_ELEVATIOIN_MODEL_ORIGINAL, sounding.getOldElevationModelUnit()); |
9150
|
223 |
|
224 } |
|
225 |
|
226 public final String msgUnitLabel(final IResultType typeWithUnit, final String label) { |
|
227 final String msg = msg(typeWithUnit.getCsvHeader()); |
|
228 final String unit = msg(typeWithUnit.getUnit()); |
|
229 return String.format("%s [%s] (%s)", msg, unit, label); |
|
230 } |
|
231 |
|
232 public String msgPdf(final SInfoResultType type) { |
|
233 return type.getPdfHeader(this.context.getMeta()); |
|
234 |
|
235 } |
9171
|
236 |
|
237 public void writeTitleForTabs(final String tabTitleMsg, final int colSize) { |
|
238 |
|
239 final Collection<String> title = new ArrayList<>(colSize); |
|
240 title.add(msg("export_csv_title") + msg(tabTitleMsg)); |
|
241 writeCSVLine(title.toArray(new String[colSize])); |
|
242 } |
9150
|
243 } |