comparison flys-artifacts/src/main/java/org/dive4elements/river/artifacts/states/SoundingsSelect.java @ 5831:bd047b71ab37

Repaired internal references
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 12:06:39 +0200
parents flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/SoundingsSelect.java@c79b98085096
children
comparison
equal deleted inserted replaced
5830:160f53ee0870 5831:bd047b71ab37
1 package org.dive4elements.river.artifacts.states;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.apache.log4j.Logger;
7
8 import org.dive4elements.artifacts.Artifact;
9 import org.dive4elements.artifacts.CallContext;
10
11 import org.dive4elements.artifacts.common.model.KVP;
12
13 import org.dive4elements.river.model.BedHeightEpoch;
14 import org.dive4elements.river.model.BedHeightSingle;
15 import org.dive4elements.river.model.River;
16
17 import org.dive4elements.river.artifacts.FLYSArtifact;
18 import org.dive4elements.river.utils.FLYSUtils;
19
20
21 public class SoundingsSelect extends MultiStringArrayState {
22
23 public static final String SOUNDINGS = "soundings";
24
25 public static final String PREFIX_SINGLE = "single-";
26
27 public static final String PREFIX_EPOCH = "epoch-";
28
29 /** Private logger. */
30 private static final Logger logger = Logger.getLogger(SoundingsSelect.class);
31
32
33 @Override
34 public String getUIProvider() {
35 return "parameter-matrix";
36 }
37
38
39 @Override
40 protected KVP<String, String>[] getOptions(
41 Artifact artifact,
42 String parameterName,
43 CallContext context
44 )
45 throws IllegalArgumentException
46 {
47 logger.debug("Get options for parameter: '" + parameterName + "'");
48
49 if (!testParameterName(parameterName)) {
50 throw new IllegalArgumentException(
51 "Invalid parameter for state: '" + parameterName + "'");
52 }
53
54 River river = FLYSUtils.getRiver((FLYSArtifact) artifact);
55 double lo = ((FLYSArtifact) artifact).getDataAsDouble("ld_from");
56 double hi = ((FLYSArtifact) artifact).getDataAsDouble("ld_to");
57
58 double kmLo = Math.min(lo, hi);
59 double kmHi = Math.max(lo, hi);
60
61 List<KVP<String, String>> kvp = new ArrayList<KVP<String, String>>();
62
63 appendSingles(river, kmLo, kmHi, kvp);
64 appendEpochs(river, kmLo, kmHi, kvp);
65
66 return kvp.toArray(new KVP[kvp.size()]);
67 }
68
69
70 protected void appendSingles(
71 River river,
72 double kmLo,
73 double kmHi,
74 List<KVP<String, String>> kvp
75 ) {
76 List<BedHeightSingle> singles =
77 BedHeightSingle.getBedHeightSingles(river, kmLo, kmHi);
78
79 if (singles != null) {
80 int size = singles.size();
81
82 logger.debug("Found " + size + " singles.");
83
84 for (int i = 0; i < size; i++) {
85 BedHeightSingle s = singles.get(i);
86
87 String id = PREFIX_SINGLE + s.getId();
88 String value = s.getDescription();
89
90 kvp.add(new KVP(id, value));
91 }
92 }
93 }
94
95
96 protected void appendEpochs(
97 River river,
98 double kmLo,
99 double kmHi,
100 List<KVP<String, String>> kvp
101 ) {
102 List<BedHeightEpoch> epochs =
103 BedHeightEpoch.getBedHeightEpochs(river, kmLo, kmHi);
104
105 if (epochs != null) {
106 int size = epochs.size();
107
108 logger.debug("Found " + size + " epochs.");
109
110 for (int i = 0; i < size; i++) {
111 BedHeightEpoch e = epochs.get(i);
112
113 String id = PREFIX_EPOCH + e.getId();
114 String value = e.getDescription();
115
116 kvp.add(new KVP(id, value));
117 }
118 }
119 }
120
121
122 @Override
123 protected String getLabelFor(
124 CallContext cc,
125 String parameterName,
126 String value
127 ) throws IllegalArgumentException
128 {
129 if (!testParameterName(parameterName)) {
130 throw new IllegalArgumentException(
131 "Invalid parameter for state: '" + parameterName + "'");
132 }
133
134 if (value.indexOf(PREFIX_SINGLE) >= 0) {
135 return getLabelForSingle(cc, value);
136 }
137 else if (value.indexOf(PREFIX_EPOCH) >= 0) {
138 return getLabelForEpoch(cc, value);
139 }
140
141 return value;
142 }
143
144
145 protected String getLabelForSingle(CallContext cc, String value) {
146 String id = value.replace(PREFIX_SINGLE, "");
147 try {
148 BedHeightSingle s = BedHeightSingle.getBedHeightSingleById(
149 Integer.parseInt(id));
150
151 if (s != null) {
152 return s.getDescription();
153 }
154 else {
155 return "no value for '" + id + "'";
156 }
157 }
158 catch (NumberFormatException nfe) {
159 logger.warn("Could not parse id from string '" + id + "'", nfe);
160 }
161
162 return "n.A.";
163 }
164
165
166 protected String getLabelForEpoch(CallContext cc, String value) {
167 String id = value.replace(PREFIX_EPOCH, "");
168 try {
169 BedHeightEpoch e = BedHeightEpoch.getBedHeightEpochById(
170 Integer.parseInt(id));
171
172 if (e != null) {
173 return e.getDescription();
174 }
175 else {
176 return "no value for '" + id + "'";
177 }
178 }
179 catch (NumberFormatException nfe) {
180 logger.warn("Could not parse id from string '" + id + "'", nfe);
181 }
182
183 return "n.A.";
184 }
185
186
187 /**
188 * This method might be used to test, if a parameter name is handled by this
189 * state.
190 *
191 * @param parameterName The name of a parameter.
192 *
193 * @return true, if parameterName is one of <i>MAIN_CHANNEL</i> or
194 * <i>TOTAL_CHANNEL</i>. Otherwise false.
195 */
196 protected boolean testParameterName(String parameterName) {
197 if (parameterName == null || parameterName.length() == 0) {
198 return false;
199 }
200 else if (parameterName.equals(SOUNDINGS)) {
201 return true;
202 }
203 else {
204 return false;
205 }
206 }
207 }
208 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org