Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/LocationDistanceSelect.java @ 627:833290f16f09
ISSUE-85 (part I/III) Added further fields for the location/range state.
flys-artifacts/trunk@1992 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Tue, 24 May 2011 11:27:37 +0000 |
parents | 929137ee8154 |
children | 51b69bca4560 |
comparison
equal
deleted
inserted
replaced
626:e3ee131d5dd3 | 627:833290f16f09 |
---|---|
27 private static Logger logger = Logger.getLogger(LocationDistanceSelect.class); | 27 private static Logger logger = Logger.getLogger(LocationDistanceSelect.class); |
28 | 28 |
29 | 29 |
30 /** The default step width.*/ | 30 /** The default step width.*/ |
31 public static final String DEFAULT_STEP = "100"; | 31 public static final String DEFAULT_STEP = "100"; |
32 | |
33 /** The name of the 'mode' field. */ | |
34 public static final String MODE = "ld_mode"; | |
35 | |
36 /** The name of the 'locations' field.*/ | |
37 public static final String LOCATIONS = "ld_locations"; | |
32 | 38 |
33 /** The name of the 'from' field. */ | 39 /** The name of the 'from' field. */ |
34 public static final String FROM = "ld_from"; | 40 public static final String FROM = "ld_from"; |
35 | 41 |
36 /** The name of the 'to' field. */ | 42 /** The name of the 'to' field. */ |
156 { | 162 { |
157 logger.debug("LocationDistanceSelect.validate"); | 163 logger.debug("LocationDistanceSelect.validate"); |
158 | 164 |
159 FLYSArtifact flys = (FLYSArtifact) artifact; | 165 FLYSArtifact flys = (FLYSArtifact) artifact; |
160 | 166 |
167 if (flys.isRange()) { | |
168 return validateRange(flys, context); | |
169 } | |
170 else { | |
171 return validateLocations(flys, context); | |
172 } | |
173 } | |
174 | |
175 | |
176 protected boolean validateLocations(FLYSArtifact flys, CallContext context) | |
177 throws IllegalArgumentException | |
178 { | |
179 StateData dValues = getData(flys, LOCATIONS); | |
180 String values = dValues != null ? (String)dValues.getValue() : null; | |
181 | |
182 if (values == null || values.length() == 0) { | |
183 throw new IllegalArgumentException("error_empty_state"); | |
184 } | |
185 | |
186 double[] absMinMax = getMinMaxDistance(flys); | |
187 double[] relMinMax = getMinMaxFromString(values); | |
188 | |
189 if (relMinMax[0] < absMinMax[0] || relMinMax[0] > absMinMax[1]) { | |
190 throw new IllegalArgumentException("error_feed_from_out_of_range"); | |
191 } | |
192 | |
193 if (relMinMax[1] > absMinMax[1] || relMinMax[1] < absMinMax[0]) { | |
194 throw new IllegalArgumentException("error_feed_to_out_of_range"); | |
195 } | |
196 | |
197 return true; | |
198 } | |
199 | |
200 | |
201 protected boolean validateRange(FLYSArtifact flys, CallContext context) | |
202 throws IllegalArgumentException | |
203 { | |
161 StateData dFrom = getData(flys, FROM); | 204 StateData dFrom = getData(flys, FROM); |
162 StateData dTo = getData(flys, TO); | 205 StateData dTo = getData(flys, TO); |
163 StateData dStep = getData(flys, STEP); | 206 StateData dStep = getData(flys, STEP); |
164 | 207 |
165 String fromStr = dFrom != null ? (String) dFrom.getValue() : null; | 208 String fromStr = dFrom != null ? (String) dFrom.getValue() : null; |
173 try { | 216 try { |
174 double from = Double.parseDouble(fromStr); | 217 double from = Double.parseDouble(fromStr); |
175 double to = Double.parseDouble(toStr); | 218 double to = Double.parseDouble(toStr); |
176 double step = Double.parseDouble(stepStr); | 219 double step = Double.parseDouble(stepStr); |
177 | 220 |
178 double[] minmaxDist = getMinMaxDistance(artifact); | 221 double[] minmaxDist = getMinMaxDistance(flys); |
179 | 222 |
180 return validateBounds(minmaxDist[0], minmaxDist[1], from, to, step); | 223 return validateBounds(minmaxDist[0], minmaxDist[1], from, to, step); |
181 } | 224 } |
182 catch (NumberFormatException nfe) { | 225 catch (NumberFormatException nfe) { |
183 throw new IllegalArgumentException("error_feed_number_format"); | 226 throw new IllegalArgumentException("error_invalid_double_value"); |
184 } | 227 } |
228 } | |
229 | |
230 | |
231 /** | |
232 * Extracts the min/max values from String <i>s</i>. An | |
233 * IllegalArgumentException is thrown if there is a value that throws a | |
234 * NumberFormatException. | |
235 * | |
236 * @param s String that contains whitespace separated double values. | |
237 * | |
238 * @return a 2dmin array [min,max]. | |
239 */ | |
240 public static double[] getMinMaxFromString(String s) | |
241 throws IllegalArgumentException | |
242 { | |
243 String[] values = s.split(" "); | |
244 | |
245 double[] minmax = new double[] { | |
246 Double.MAX_VALUE, | |
247 -Double.MAX_VALUE }; | |
248 | |
249 for (String v: values) { | |
250 try { | |
251 double value = Double.valueOf(v); | |
252 | |
253 minmax[0] = minmax[0] < value ? minmax[0] : value; | |
254 minmax[1] = minmax[1] > value ? minmax[1] : value; | |
255 } | |
256 catch (NumberFormatException nfe) { | |
257 throw new IllegalArgumentException( | |
258 "error_invalid_double_value"); | |
259 } | |
260 } | |
261 | |
262 return minmax; | |
185 } | 263 } |
186 } | 264 } |
187 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : | 265 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |