annotate artifacts/src/main/java/org/dive4elements/river/exports/WaterlevelDescriptionBuilder.java @ 8933:05b5588bdd94

Extracting some winfo logic without breaking old code
author gernotbelger
date Tue, 06 Mar 2018 17:05:13 +0100
parents
children 5294114b1df4
rev   line source
8933
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
1 /** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
2 * Software engineering by
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
3 * Björnsen Beratende Ingenieure GmbH
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
4 * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
5 *
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
6 * This file is Free Software under the GNU AGPL (>=v3)
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
7 * and comes with ABSOLUTELY NO WARRANTY! Check out the
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
8 * documentation coming with Dive4Elements River for details.
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
9 */
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
10 package org.dive4elements.river.exports;
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
11
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
12 import java.text.NumberFormat;
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
13
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
14 import org.dive4elements.artifacts.CallContext;
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
15 import org.dive4elements.river.artifacts.D4EArtifact;
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
16 import org.dive4elements.river.artifacts.WINFOArtifact;
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
17 import org.dive4elements.river.artifacts.model.WQKms;
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
18 import org.dive4elements.river.artifacts.resources.Resources;
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
19 import org.dive4elements.river.utils.Formatter;
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
20 import org.dive4elements.river.utils.RiverUtils;
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
21 import org.dive4elements.river.utils.RiverUtils.WQ_MODE;
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
22
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
23 /**
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
24 * Helper that encapsulates the logic how the 'description' column in waterlevel exporting is generated.
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
25 * TODO: this class should also be used in WaterlevelExport (all code is copied from there), but this would involve
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
26 * heavy testing and we leave this to the one who is responsible to clean up this mess.
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
27 *
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
28 * @author Gernot Belger
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
29 */
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
30 public final class WaterlevelDescriptionBuilder {
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
31
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
32 private static final String CSV_Q_DESC_HEADER = "export.waterlevel.csv.header.q.desc";
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
33
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
34 // FIXME: missing in resource-files! hence always the default is used...
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
35 private static final String CSV_W_DESC_HEADER = "export.waterlevel.csv.header.w.desc";
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
36
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
37 private static final String DEFAULT_CSV_Q_DESC_HEADER = "Bezeichnung";
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
38
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
39 private static final String DEFAULT_CSV_W_DESC_HEADER = "W/Pegel [cm]";
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
40
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
41 private final boolean isQ;
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
42
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
43 private final boolean atGauge;
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
44
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
45 private final CallContext context;
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
46
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
47 private final WQ_MODE mode;
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
48
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
49 private final D4EArtifact artifact;
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
50
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
51 public WaterlevelDescriptionBuilder(final D4EArtifact artifact, final CallContext context) {
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
52 this.context = context;
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
53
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
54 // REMARK: taken from WaterlevelExporter, should be moved into WInfoArtifact
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
55 this.mode = RiverUtils.getWQMode(artifact);
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
56 this.atGauge = this.mode == WQ_MODE.QGAUGE || this.mode == WQ_MODE.WGAUGE;
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
57 this.isQ = this.mode == WQ_MODE.QGAUGE || this.mode == WQ_MODE.QFREE;
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
58
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
59 this.artifact = artifact;
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
60 }
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
61
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
62 public boolean isAtGauge() {
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
63 return this.atGauge;
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
64 }
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
65
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
66 public String getColumnHeader() {
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
67
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
68 if (!this.atGauge)
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
69 return null;
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
70
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
71 // REMARK: bad inter-dependency to WaterlevelExporter, but we want to really copy the logic from WInfo
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
72 if (this.isQ)
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
73 return Resources.getMsg(this.context.getMeta(), CSV_Q_DESC_HEADER, DEFAULT_CSV_Q_DESC_HEADER);
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
74
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
75 return Resources.getMsg(this.context.getMeta(), CSV_W_DESC_HEADER, DEFAULT_CSV_W_DESC_HEADER);
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
76 }
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
77
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
78 public String getDesc(final WQKms wqkms) {
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
79 return getDesc(wqkms, this.isQ);
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
80 }
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
81
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
82 public String getDesc(final WQKms wqkms, final boolean isQoverride) {
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
83 String colDesc = "";
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
84
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
85 if (this.artifact instanceof WINFOArtifact && isQoverride) {
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
86 colDesc = getCSVRowTitle((WINFOArtifact) this.artifact, wqkms);
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
87 } else if (!isQoverride) {
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
88 final Double value = RiverUtils.getValueFromWQ(wqkms);
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
89 colDesc = (value != null) ? Formatter.getWaterlevelW(this.context).format(value) : null;
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
90 }
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
91
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
92 if (this.artifact instanceof WINFOArtifact) {
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
93 if (wqkms != null && wqkms.getRawValue() != null) {
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
94 final WINFOArtifact winfo = (WINFOArtifact) this.artifact;
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
95 colDesc = RiverUtils.getNamedMainValue(winfo, wqkms.getRawValue());
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
96 // For 'W am Pegel' s
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
97 if (colDesc == null) {
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
98 final Double value = RiverUtils.getValueFromWQ(wqkms);
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
99 colDesc = (value != null) ? Formatter.getWaterlevelW(this.context).format(value) : null;
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
100 }
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
101 }
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
102 }
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
103
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
104 if (colDesc == null)
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
105 return "";
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
106
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
107 /*
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
108 * Quick hack. Can be removed when database strings are
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
109 * adapted or left in here as it should never be harmful.
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
110 */
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
111 return colDesc.replace("Amtl.Festlegung_", "Amtl. ");
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
112 }
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
113
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
114 private String getCSVRowTitle(final WINFOArtifact winfo, final WQKms wqkms) {
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
115
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
116 if (this.mode == WQ_MODE.WFREE || this.mode == WQ_MODE.QGAUGE)
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
117 return localizeWQKms(wqkms);
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
118
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
119 final Double v = wqkms.getRawValue();
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
120
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
121 final String nmv = RiverUtils.getNamedMainValue(winfo, v);
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
122
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
123 if (nmv != null && nmv.length() > 0) {
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
124 return RiverUtils.stripNamedMainValue(nmv);
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
125 }
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
126
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
127 return localizeWQKms(wqkms);
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
128 }
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
129
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
130 /**
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
131 * Get a string like 'W=' or 'Q=' with a number following in localized
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
132 * format.
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
133 */
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
134 private String localizeWQKms(final WQKms wqkms) {
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
135 final Double rawValue = wqkms.getRawValue();
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
136
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
137 if (rawValue == null) {
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
138 return wqkms.getName();
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
139 }
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
140
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
141 final NumberFormat nf = Formatter.getRawFormatter(this.context);
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
142
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
143 if (this.mode == WQ_MODE.WFREE || this.mode == WQ_MODE.WGAUGE)
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
144 return "W=" + nf.format(rawValue);
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
145
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
146 return "Q=" + nf.format(rawValue);
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
147 }
05b5588bdd94 Extracting some winfo logic without breaking old code
gernotbelger
parents:
diff changeset
148 }

http://dive4elements.wald.intevation.org