Mercurial > dive4elements > river
comparison 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 |
comparison
equal
deleted
inserted
replaced
8932:8731c3dabb56 | 8933:05b5588bdd94 |
---|---|
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.exports; | |
11 | |
12 import java.text.NumberFormat; | |
13 | |
14 import org.dive4elements.artifacts.CallContext; | |
15 import org.dive4elements.river.artifacts.D4EArtifact; | |
16 import org.dive4elements.river.artifacts.WINFOArtifact; | |
17 import org.dive4elements.river.artifacts.model.WQKms; | |
18 import org.dive4elements.river.artifacts.resources.Resources; | |
19 import org.dive4elements.river.utils.Formatter; | |
20 import org.dive4elements.river.utils.RiverUtils; | |
21 import org.dive4elements.river.utils.RiverUtils.WQ_MODE; | |
22 | |
23 /** | |
24 * Helper that encapsulates the logic how the 'description' column in waterlevel exporting is generated. | |
25 * TODO: this class should also be used in WaterlevelExport (all code is copied from there), but this would involve | |
26 * heavy testing and we leave this to the one who is responsible to clean up this mess. | |
27 * | |
28 * @author Gernot Belger | |
29 */ | |
30 public final class WaterlevelDescriptionBuilder { | |
31 | |
32 private static final String CSV_Q_DESC_HEADER = "export.waterlevel.csv.header.q.desc"; | |
33 | |
34 // FIXME: missing in resource-files! hence always the default is used... | |
35 private static final String CSV_W_DESC_HEADER = "export.waterlevel.csv.header.w.desc"; | |
36 | |
37 private static final String DEFAULT_CSV_Q_DESC_HEADER = "Bezeichnung"; | |
38 | |
39 private static final String DEFAULT_CSV_W_DESC_HEADER = "W/Pegel [cm]"; | |
40 | |
41 private final boolean isQ; | |
42 | |
43 private final boolean atGauge; | |
44 | |
45 private final CallContext context; | |
46 | |
47 private final WQ_MODE mode; | |
48 | |
49 private final D4EArtifact artifact; | |
50 | |
51 public WaterlevelDescriptionBuilder(final D4EArtifact artifact, final CallContext context) { | |
52 this.context = context; | |
53 | |
54 // REMARK: taken from WaterlevelExporter, should be moved into WInfoArtifact | |
55 this.mode = RiverUtils.getWQMode(artifact); | |
56 this.atGauge = this.mode == WQ_MODE.QGAUGE || this.mode == WQ_MODE.WGAUGE; | |
57 this.isQ = this.mode == WQ_MODE.QGAUGE || this.mode == WQ_MODE.QFREE; | |
58 | |
59 this.artifact = artifact; | |
60 } | |
61 | |
62 public boolean isAtGauge() { | |
63 return this.atGauge; | |
64 } | |
65 | |
66 public String getColumnHeader() { | |
67 | |
68 if (!this.atGauge) | |
69 return null; | |
70 | |
71 // REMARK: bad inter-dependency to WaterlevelExporter, but we want to really copy the logic from WInfo | |
72 if (this.isQ) | |
73 return Resources.getMsg(this.context.getMeta(), CSV_Q_DESC_HEADER, DEFAULT_CSV_Q_DESC_HEADER); | |
74 | |
75 return Resources.getMsg(this.context.getMeta(), CSV_W_DESC_HEADER, DEFAULT_CSV_W_DESC_HEADER); | |
76 } | |
77 | |
78 public String getDesc(final WQKms wqkms) { | |
79 return getDesc(wqkms, this.isQ); | |
80 } | |
81 | |
82 public String getDesc(final WQKms wqkms, final boolean isQoverride) { | |
83 String colDesc = ""; | |
84 | |
85 if (this.artifact instanceof WINFOArtifact && isQoverride) { | |
86 colDesc = getCSVRowTitle((WINFOArtifact) this.artifact, wqkms); | |
87 } else if (!isQoverride) { | |
88 final Double value = RiverUtils.getValueFromWQ(wqkms); | |
89 colDesc = (value != null) ? Formatter.getWaterlevelW(this.context).format(value) : null; | |
90 } | |
91 | |
92 if (this.artifact instanceof WINFOArtifact) { | |
93 if (wqkms != null && wqkms.getRawValue() != null) { | |
94 final WINFOArtifact winfo = (WINFOArtifact) this.artifact; | |
95 colDesc = RiverUtils.getNamedMainValue(winfo, wqkms.getRawValue()); | |
96 // For 'W am Pegel' s | |
97 if (colDesc == null) { | |
98 final Double value = RiverUtils.getValueFromWQ(wqkms); | |
99 colDesc = (value != null) ? Formatter.getWaterlevelW(this.context).format(value) : null; | |
100 } | |
101 } | |
102 } | |
103 | |
104 if (colDesc == null) | |
105 return ""; | |
106 | |
107 /* | |
108 * Quick hack. Can be removed when database strings are | |
109 * adapted or left in here as it should never be harmful. | |
110 */ | |
111 return colDesc.replace("Amtl.Festlegung_", "Amtl. "); | |
112 } | |
113 | |
114 private String getCSVRowTitle(final WINFOArtifact winfo, final WQKms wqkms) { | |
115 | |
116 if (this.mode == WQ_MODE.WFREE || this.mode == WQ_MODE.QGAUGE) | |
117 return localizeWQKms(wqkms); | |
118 | |
119 final Double v = wqkms.getRawValue(); | |
120 | |
121 final String nmv = RiverUtils.getNamedMainValue(winfo, v); | |
122 | |
123 if (nmv != null && nmv.length() > 0) { | |
124 return RiverUtils.stripNamedMainValue(nmv); | |
125 } | |
126 | |
127 return localizeWQKms(wqkms); | |
128 } | |
129 | |
130 /** | |
131 * Get a string like 'W=' or 'Q=' with a number following in localized | |
132 * format. | |
133 */ | |
134 private String localizeWQKms(final WQKms wqkms) { | |
135 final Double rawValue = wqkms.getRawValue(); | |
136 | |
137 if (rawValue == null) { | |
138 return wqkms.getName(); | |
139 } | |
140 | |
141 final NumberFormat nf = Formatter.getRawFormatter(this.context); | |
142 | |
143 if (this.mode == WQ_MODE.WFREE || this.mode == WQ_MODE.WGAUGE) | |
144 return "W=" + nf.format(rawValue); | |
145 | |
146 return "Q=" + nf.format(rawValue); | |
147 } | |
148 } |