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