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; |
9171
|
13 import java.util.ArrayList; |
|
14 import java.util.Collection; |
9150
|
15 import java.util.Date; |
|
16 import java.util.Locale; |
|
17 |
|
18 import org.apache.commons.lang.math.DoubleRange; |
|
19 import org.dive4elements.artifacts.CallContext; |
|
20 import org.dive4elements.artifacts.CallMeta; |
|
21 import org.dive4elements.river.FLYS; |
|
22 import org.dive4elements.river.artifacts.resources.Resources; |
|
23 import org.dive4elements.river.artifacts.sinfo.common.SInfoI18NStrings; |
|
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 |
|
28 import au.com.bytecode.opencsv.CSVWriter; |
|
29 |
|
30 /** |
|
31 * @author Domenico Nardi Tironi |
|
32 * |
|
33 */ |
9195
|
34 public final class ExportContextCSV extends AbstractExportContext { |
9150
|
35 |
|
36 private static final String CSV_META_HEADER_EVALUATOR = "sinfo.export.flow_depth.csv.meta.header.sounding.evaluator"; |
|
37 |
|
38 private static final String CSV_META_HEADER_SOUNDING = "sinfo.export.flow_depth.csv.meta.header.sounding"; |
|
39 |
|
40 private static final String CSV_META_HEADER_SOUNDING_YEAR = "sinfo.export.flow_depth.csv.meta.header.sounding.year"; |
|
41 |
|
42 private static final String CSV_META_HEADER_SOUNDING_TYPE = "sinfo.export.flow_depth.csv.meta.header.sounding.type"; |
|
43 |
|
44 private static final String CSV_META_HEADER_SOUNDING_PRJ = "sinfo.export.flow_depth.csv.meta.header.sounding.prj"; |
|
45 |
|
46 private static final String CSV_META_HEADER_SOUNDING_ELEVATIOIN_MODEL = "sinfo.export.flow_depth.csv.meta.header.sounding.elevationmodel"; |
|
47 |
|
48 private static final String CSV_META_HEADER_SOUNDING_ELEVATIOIN_MODEL_ORIGINAL = "sinfo.export.flow_depth.csv.meta.header.sounding.elevationmodel.original"; |
|
49 |
|
50 private final CSVWriter writer; |
|
51 |
9195
|
52 private final AbstractCalculationResults<?> results; |
9150
|
53 |
9195
|
54 public ExportContextCSV(final CallContext context, final CSVWriter writer, final AbstractCalculationResults<?> results) { |
|
55 super(context, results); |
9150
|
56 |
9195
|
57 this.writer = writer; |
|
58 this.results = results; |
9150
|
59 } |
|
60 |
|
61 public final void writeCSVMetaEntry(final String message, final Object... messageArgs) { |
|
62 |
9195
|
63 final CallMeta meta = getContext().getMeta(); |
9150
|
64 |
|
65 this.writer.writeNext(new String[] { Resources.getMsg(meta, message, message, messageArgs) }); |
|
66 } |
|
67 |
|
68 public void writeBlankLine() { |
|
69 writeCSVLine(new String[] { "" }); |
|
70 } |
|
71 |
|
72 // *** CUSTOM STUFF that is used multiple times ***/// |
|
73 |
|
74 public final void writeCSVSoundingMetadata(final BedHeightInfo sounding) { |
|
75 // "##METADATEN PEILUNG" |
9154
|
76 writeCSVSoundingMetadata(sounding, CSV_META_HEADER_SOUNDING); |
9150
|
77 |
|
78 } |
|
79 |
|
80 public void writeCSVLine(final String[] line) { |
|
81 this.writer.writeNext(line); |
|
82 } |
|
83 |
|
84 public String formatCsvHeader(final IResultType type) { |
|
85 return msg(type.getCsvHeader()); |
|
86 } |
|
87 |
9195
|
88 public final void writeCSVGlobalMetadataDefaults() { |
9150
|
89 // TODO: results as member |
9195
|
90 final String calcModeLabel = this.results.getCalcModeLabel(); |
|
91 final RiverInfo river = this.results.getRiver(); |
|
92 final DoubleRange calcRange = this.results.getCalcRange(); |
9150
|
93 |
|
94 writeCSVMetaEntry(I18NStrings.CSV_META_HEADER_RESULT, msg(I18NStrings.CSV_META_HEADER_RESULT_LABEL), river.getName(), calcModeLabel); |
|
95 |
|
96 // "# FLYS-Version: " |
|
97 writeCSVMetaEntry(I18NStrings.CSV_META_VERSION, msg(I18NStrings.CSV_META_VERSION_LABEL), FLYS.VERSION); |
|
98 |
|
99 // "# Bearbeiter: " |
9195
|
100 writeCSVMetaEntry(I18NStrings.CSV_META_USER, msg(I18NStrings.CSV_META_USER_LABEL), this.results.getUser()); |
9150
|
101 |
|
102 // "# Datum der Erstellung: " |
9195
|
103 final Locale locale = Resources.getLocale(getContext().getMeta()); |
9150
|
104 final DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale); |
|
105 writeCSVMetaEntry(I18NStrings.CSV_META_CREATION, msg(I18NStrings.CSV_META_CREATION_LABEL), df.format(new Date())); |
|
106 |
|
107 // "# Gewässer: " |
|
108 writeCSVMetaEntry(I18NStrings.CSV_META_RIVER, msg(I18NStrings.CSV_META_RIVER_LABEL), river.getName()); |
|
109 |
|
110 // "# Höhensystem des Flusses: " |
|
111 writeCSVMetaEntry(I18NStrings.CSV_META_HEIGHT_UNIT_RIVER, river.getWstUnit()); |
|
112 |
|
113 if (calcRange != null) { |
|
114 // "# Ort/Bereich (km): " |
|
115 writeCSVMetaEntry(I18NStrings.CSV_META_RANGE, msg(I18NStrings.CSV_META_RANGE_LABEL), getKmFormatter().format(calcRange.getMinimumDouble()), |
|
116 getKmFormatter().format(calcRange.getMaximumDouble())); |
|
117 } |
|
118 } |
|
119 |
|
120 public final void writeCSVWaterlevelMetadata(final WstInfo wst) { |
|
121 writeCSVWaterlevelMetadata(wst, SInfoI18NStrings.CSV_META_HEADER_WATERLEVEL); // default Label; used in 99% |
|
122 } |
|
123 |
|
124 public final void writeCSVWaterlevelMetadata(final WstInfo wst, final String mainLabel) { |
|
125 // "##METADATEN WASSERSPIEGELLAGE" |
|
126 writeCSVMetaEntry(mainLabel); |
|
127 |
|
128 // "# Bezeichnung der Wasserspiegellage: " |
|
129 writeCSVMetaEntry(SInfoI18NStrings.CSV_META_HEADER_WATERLEVEL_NAME, wst.getLabel()); |
|
130 |
|
131 // "# Bezugspegel: " |
|
132 writeCSVMetaEntry(SInfoI18NStrings.CSV_META_HEADER_WATERLEVEL_GAUGE, wst.getGauge()); |
|
133 |
|
134 // "# Jahr/Zeitraum der Wasserspiegellage: " |
|
135 final int year = wst.getYear(); |
|
136 if (year > 0) |
|
137 writeCSVMetaEntry(SInfoI18NStrings.CSV_META_HEADER_WATERLEVEL_YEAR, Integer.toString(year)); |
|
138 } |
|
139 |
|
140 /** |
|
141 * Formats header with unit: msg [unit] |
|
142 */ |
|
143 |
|
144 public String msgUnitCSV(final IResultType typeWithUnit) { // TODO: use generic Type! |
|
145 final String unit = msg(typeWithUnit.getUnit()); |
|
146 return msgUnitCSV(typeWithUnit, unit); |
|
147 } |
|
148 |
|
149 public String msgUnitCSV(final IResultType type, final String unit) { |
|
150 final String msg = msg(type.getCsvHeader()); |
|
151 return String.format("%s [%s]", msg, unit); |
|
152 } |
|
153 |
9154
|
154 public void writeCSVSoundingMetadata(final BedHeightInfo sounding, final String label) { |
|
155 |
|
156 // "##METADATEN PEILUNG" |
|
157 writeCSVMetaEntry(label); |
|
158 |
|
159 // "# Jahr der Peilung: " |
|
160 writeCSVMetaEntry(CSV_META_HEADER_SOUNDING_YEAR, Integer.toString(sounding.getYear())); |
|
161 // "# Aufnahmeart: " |
|
162 writeCSVMetaEntry(CSV_META_HEADER_SOUNDING_TYPE, sounding.getType()); |
|
163 // "# Auswerter: " |
|
164 writeCSVMetaEntry(CSV_META_HEADER_EVALUATOR, sounding.getEvaluationBy()); |
|
165 // "# Lagesystem: " |
|
166 writeCSVMetaEntry(CSV_META_HEADER_SOUNDING_PRJ, sounding.getLocationSystem()); |
|
167 // "# Höhensystem: " |
|
168 writeCSVMetaEntry(CSV_META_HEADER_SOUNDING_ELEVATIOIN_MODEL, sounding.getCurElevationModelUnit()); |
|
169 // "# ursprüngliches Höhensystem: " |
|
170 writeCSVMetaEntry(CSV_META_HEADER_SOUNDING_ELEVATIOIN_MODEL_ORIGINAL, sounding.getOldElevationModelUnit()); |
9150
|
171 |
|
172 } |
|
173 |
|
174 public final String msgUnitLabel(final IResultType typeWithUnit, final String label) { |
|
175 final String msg = msg(typeWithUnit.getCsvHeader()); |
|
176 final String unit = msg(typeWithUnit.getUnit()); |
|
177 return String.format("%s [%s] (%s)", msg, unit, label); |
|
178 } |
|
179 |
9171
|
180 public void writeTitleForTabs(final String tabTitleMsg, final int colSize) { |
|
181 |
|
182 final Collection<String> title = new ArrayList<>(colSize); |
|
183 title.add(msg("export_csv_title") + msg(tabTitleMsg)); |
|
184 writeCSVLine(title.toArray(new String[colSize])); |
|
185 } |
9150
|
186 } |