# HG changeset patch # User Ingo Weinzierl # Date 1306236457 0 # Node ID 833290f16f097bde407014e2b339559b5bc1caed # Parent e3ee131d5dd324836f55c1d6e765bba663dd99ce ISSUE-85 (part I/III) Added further fields for the location/range state. flys-artifacts/trunk@1992 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r e3ee131d5dd3 -r 833290f16f09 flys-artifacts/ChangeLog --- a/flys-artifacts/ChangeLog Tue May 24 11:23:57 2011 +0000 +++ b/flys-artifacts/ChangeLog Tue May 24 11:27:37 2011 +0000 @@ -15,6 +15,20 @@ 2011-05-23 Ingo Weinzierl + ISSUE-85 (part I/III) + + * doc/conf/artifacts/winfo.xml: Added two further field 'ld_mode' and + 'ld_locations' to the range/locations state to track the selected mode + and locations. + + * src/main/java/de/intevation/flys/artifacts/states/LocationDistanceSelect.java: + Added methods to validate the user inserted locations. + + * src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java: Added a + method to determine of a range or locations have been inserted. + +2011-05-23 Ingo Weinzierl + ISSUE-62 (part II/II) * src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java: Dump the diff -r e3ee131d5dd3 -r 833290f16f09 flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java --- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java Tue May 24 11:23:57 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java Tue May 24 11:27:37 2011 +0000 @@ -461,6 +461,25 @@ /** + * Determines the selected mode of distance/range input. + * + * @return true, if the range mode is selected otherwise false. + */ + public boolean isRange() { + StateData mode = getData("ld_mode"); + + if (mode == null) { + logger.warn("No mode location/range chosen. Defaults to range."); + return true; + } + + String value = (String) mode.getValue(); + + return value.equals("range"); + } + + + /** * This method returns the given distance * * @return an array with lower and upper kilometer range for each diff -r e3ee131d5dd3 -r 833290f16f09 flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/LocationDistanceSelect.java --- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/LocationDistanceSelect.java Tue May 24 11:23:57 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/LocationDistanceSelect.java Tue May 24 11:27:37 2011 +0000 @@ -30,6 +30,12 @@ /** The default step width.*/ public static final String DEFAULT_STEP = "100"; + /** The name of the 'mode' field. */ + public static final String MODE = "ld_mode"; + + /** The name of the 'locations' field.*/ + public static final String LOCATIONS = "ld_locations"; + /** The name of the 'from' field. */ public static final String FROM = "ld_from"; @@ -158,6 +164,43 @@ FLYSArtifact flys = (FLYSArtifact) artifact; + if (flys.isRange()) { + return validateRange(flys, context); + } + else { + return validateLocations(flys, context); + } + } + + + protected boolean validateLocations(FLYSArtifact flys, CallContext context) + throws IllegalArgumentException + { + StateData dValues = getData(flys, LOCATIONS); + String values = dValues != null ? (String)dValues.getValue() : null; + + if (values == null || values.length() == 0) { + throw new IllegalArgumentException("error_empty_state"); + } + + double[] absMinMax = getMinMaxDistance(flys); + double[] relMinMax = getMinMaxFromString(values); + + if (relMinMax[0] < absMinMax[0] || relMinMax[0] > absMinMax[1]) { + throw new IllegalArgumentException("error_feed_from_out_of_range"); + } + + if (relMinMax[1] > absMinMax[1] || relMinMax[1] < absMinMax[0]) { + throw new IllegalArgumentException("error_feed_to_out_of_range"); + } + + return true; + } + + + protected boolean validateRange(FLYSArtifact flys, CallContext context) + throws IllegalArgumentException + { StateData dFrom = getData(flys, FROM); StateData dTo = getData(flys, TO); StateData dStep = getData(flys, STEP); @@ -175,13 +218,48 @@ double to = Double.parseDouble(toStr); double step = Double.parseDouble(stepStr); - double[] minmaxDist = getMinMaxDistance(artifact); + double[] minmaxDist = getMinMaxDistance(flys); return validateBounds(minmaxDist[0], minmaxDist[1], from, to, step); } catch (NumberFormatException nfe) { - throw new IllegalArgumentException("error_feed_number_format"); + throw new IllegalArgumentException("error_invalid_double_value"); } } + + + /** + * Extracts the min/max values from String s. An + * IllegalArgumentException is thrown if there is a value that throws a + * NumberFormatException. + * + * @param s String that contains whitespace separated double values. + * + * @return a 2dmin array [min,max]. + */ + public static double[] getMinMaxFromString(String s) + throws IllegalArgumentException + { + String[] values = s.split(" "); + + double[] minmax = new double[] { + Double.MAX_VALUE, + -Double.MAX_VALUE }; + + for (String v: values) { + try { + double value = Double.valueOf(v); + + minmax[0] = minmax[0] < value ? minmax[0] : value; + minmax[1] = minmax[1] > value ? minmax[1] : value; + } + catch (NumberFormatException nfe) { + throw new IllegalArgumentException( + "error_invalid_double_value"); + } + } + + return minmax; + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :