changeset 1095:f465785ed1ae

Refactored the code to fetch the km range/locations into a utility class. flys-artifacts/trunk@2598 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 26 Aug 2011 14:58:35 +0000
parents b1c6d945848d
children 9f88cc54570c
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/MainValuesArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/WQAdapted.java flys-artifacts/src/main/java/de/intevation/flys/exports/ChartGenerator.java flys-artifacts/src/main/java/de/intevation/flys/utils/FLYSUtils.java
diffstat 6 files changed, 141 insertions(+), 93 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Fri Aug 26 14:55:18 2011 +0000
+++ b/flys-artifacts/ChangeLog	Fri Aug 26 14:58:35 2011 +0000
@@ -1,3 +1,19 @@
+2011-08-26  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/utils/FLYSUtils.java: New. This helper
+	  class should provide some basic FLYS stuff. Currently, there are functions
+	  that return the km range/location.
+
+	* src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java: Removed
+	  the methods that return the km range/locations. This is implemented in
+	  FLYSUtils now.
+
+	* src/main/java/de/intevation/flys/artifacts/MainValuesArtifact.java,
+	  src/main/java/de/intevation/flys/artifacts/states/WQAdapted.java,
+	  src/main/java/de/intevation/flys/exports/ChartGenerator.java: Adapted
+	  the WINFO method calls to retrieve the km range/locations - call
+	  FLYSUtils now.
+
 2011-08-26  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java: New method
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/MainValuesArtifact.java	Fri Aug 26 14:55:18 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/MainValuesArtifact.java	Fri Aug 26 14:58:35 2011 +0000
@@ -7,25 +7,26 @@
 
 import org.w3c.dom.Document;
 
+import de.intevation.artifactdatabase.data.DefaultStateData;
+import de.intevation.artifactdatabase.data.StateData;
 import de.intevation.artifactdatabase.state.Facet;
 import de.intevation.artifactdatabase.state.DefaultOutput;
 import de.intevation.artifactdatabase.state.State;
-import de.intevation.artifactdatabase.data.StateData;
 
 import de.intevation.artifacts.Artifact;
 import de.intevation.artifacts.ArtifactFactory;
 import de.intevation.artifacts.CallMeta;
-import de.intevation.flys.artifacts.model.RiverFactory;
+
+import de.intevation.flys.model.Gauge;
+import de.intevation.flys.model.MainValue;
+import de.intevation.flys.model.River;
 
 import de.intevation.flys.artifacts.model.MainValuesQFacet;
 import de.intevation.flys.artifacts.model.MainValuesWFacet;
 import de.intevation.flys.artifacts.model.NamedDouble;
-import de.intevation.artifactdatabase.data.DefaultStateData;
+import de.intevation.flys.artifacts.model.RiverFactory;
 import de.intevation.flys.artifacts.states.StaticState;
-
-import de.intevation.flys.model.Gauge;
-import de.intevation.flys.model.MainValue;
-import de.intevation.flys.model.River;
+import de.intevation.flys.utils.FLYSUtils;
 
 
 /**
@@ -74,7 +75,7 @@
     protected void initialize(Artifact artifact, Object context, CallMeta meta) {
         logger.debug("MainValuesArtifact.initialize");
         WINFOArtifact winfo = (WINFOArtifact) artifact;
-        double location = winfo.getLocations()[0];
+        double location = FLYSUtils.getLocations(winfo)[0];
         addData("location", new DefaultStateData("location", null, null,
                     String.valueOf(location)));
         addData("river", winfo.getData("river"));
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java	Fri Aug 26 14:55:18 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java	Fri Aug 26 14:58:35 2011 +0000
@@ -40,6 +40,7 @@
 import de.intevation.flys.model.River;
 
 import de.intevation.flys.utils.DoubleUtil;
+import de.intevation.flys.utils.FLYSUtils;
 
 import gnu.trove.TDoubleArrayList;
 
@@ -321,7 +322,7 @@
         }
 
 
-        double [] range = getDistance();
+        double [] range = FLYSUtils.getKmRange(this);
         if (range == null) {
             return error(new WQKms[0], "No range found");
         }
@@ -393,7 +394,7 @@
            return error(null, "Cannot determine gauge.");
         }
 
-        double[] locations = getLocations();
+        double[] locations = FLYSUtils.getLocations(this);
 
         if (locations == null) {
             return error(null, "Cannot determine location.");
@@ -445,7 +446,7 @@
             return error(new WQKms[0], "no river found");
         }
 
-        double [] distance = getDistance();
+        double [] distance = FLYSUtils.getKmRange(this);
 
         if (distance == null) {
             return error(new WQKms[0], "no range found");
@@ -502,7 +503,7 @@
             return error(new WQKms[0], "Cannot determine river.");
         }
 
-        double[] locations = getLocations();
+        double[] locations = FLYSUtils.getLocations(this);
 
         if (locations == null) {
             return error(new WQKms[0], "Cannot determine location.");
@@ -619,7 +620,7 @@
             return null;
         }
 
-        double [] range = getDistance();
+        double [] range = FLYSUtils.getKmRange(this);
         if (range == null) {
             logger.warn("no ranges found");
             return null;
@@ -669,44 +670,6 @@
             : null;
     }
 
-
-    /**
-     * Returns the selected distance of points.
-     *
-     * @return the selected distance or points.
-     */
-    public double[] getDistance() {
-        StateData dMode      = getData("ld_mode");
-        StateData dFrom      = getData("ld_from");
-        StateData dTo        = getData("ld_to");
-        StateData dLocations = getData("ld_locations");
-
-        if (dMode != null) {
-            String mode = (String)dMode.getValue();
-            if ("location".equals(mode)) {
-                double[] locations = getLocations();
-                return new double[] { locations[0], locations[locations.length-1] };
-            }
-            if (dFrom != null && dTo != null) {
-                return getDistanceByRange(dFrom, dTo);
-            }
-        }
-
-        if (dLocations != null) {
-            double[] locations = getLocations();
-            return new double[] { locations[0], locations[locations.length-1] };
-        }
-
-        if (dFrom != null && dTo != null) {
-            return getDistanceByRange(dFrom, dTo);
-        }
-
-        logger.warn("No data found for distance determination!");
-
-        return null;
-    }
-
-
     /**
      * Determines the selected mode of distance/range input.
      *
@@ -727,42 +690,6 @@
 
 
     /**
-     * Returns the selected locations based on a given array of locations.
-     *
-     * @param locations The StateData that contains the locations.
-     *
-     * @return the selected locations.
-     */
-    public double[] getLocations() {
-        StateData dLocations  = getData("ld_locations");
-        String    locationStr = dLocations != null
-            ? (String) dLocations.getValue()
-            : "";
-
-        if (locationStr == null || locationStr.length() == 0) {
-            logger.warn("No valid location string found!");
-            return null;
-        }
-
-        String[] tmp               = locationStr.split(" ");
-        TDoubleArrayList locations = new TDoubleArrayList();
-
-        for (String l: tmp) {
-            try {
-                locations.add(Double.parseDouble(l));
-            }
-            catch (NumberFormatException nfe) {
-                logger.warn(nfe, nfe);
-            }
-        }
-
-        locations.sort();
-
-        return locations.toNativeArray();
-    }
-
-
-    /**
      * Returns the selected distance based on a given range (from, to).
      *
      * @param dFrom The StateData that contains the lower value.
@@ -813,7 +740,7 @@
      */
     public double[] getKms() {
         if (isRange()) {
-            double[] distance = getDistance();
+            double[] distance = FLYSUtils.getKmRange(this);
             return getKms(distance);
 
         }
@@ -826,7 +753,7 @@
         if (!isRange()) {
             return null;
         }
-        double [] fromTo = getDistance();
+        double [] fromTo = FLYSUtils.getKmRange(this);
 
         if (fromTo == null) {
             return null;
@@ -866,7 +793,7 @@
             return null;
         }
 
-        double[] dist  = getDistance();
+        double[] dist  = FLYSUtils.getKmRange(this);
 
         if (dist == null) {
             logger.debug("no range found");
@@ -900,7 +827,7 @@
             return null;
         }
 
-        double [] dist  = getDistance();
+        double [] dist  = FLYSUtils.getKmRange(this);
         if (dist == null) {
             return null;
         }
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/WQAdapted.java	Fri Aug 26 14:55:18 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/WQAdapted.java	Fri Aug 26 14:58:35 2011 +0000
@@ -26,6 +26,7 @@
 
 import de.intevation.flys.artifacts.model.RangeWithValues;
 import de.intevation.flys.artifacts.model.WstFactory;
+import de.intevation.flys.utils.FLYSUtils;
 
 
 /**
@@ -121,7 +122,7 @@
 
         WINFOArtifact flysArtifact = (WINFOArtifact) artifact;
 
-        double[]    dist   = flysArtifact.getDistance();
+        double[]    dist   = FLYSUtils.getKmRange(flysArtifact);
         River       river  = flysArtifact.getRiver();
         Wst         wst    = WstFactory.getWst(river);
         List<Gauge> gauges = flysArtifact.getGauges();
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/ChartGenerator.java	Fri Aug 26 14:55:18 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/ChartGenerator.java	Fri Aug 26 14:58:35 2011 +0000
@@ -25,6 +25,7 @@
 import de.intevation.flys.artifacts.WINFOArtifact;
 
 import de.intevation.flys.artifacts.resources.Resources;
+import de.intevation.flys.utils.FLYSUtils;
 
 
 /**
@@ -104,7 +105,7 @@
     protected double[] getRange() {
         WINFOArtifact flys = (WINFOArtifact) master;
 
-        return flys.getDistance();
+        return FLYSUtils.getKmRange(flys);
     }
 
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/utils/FLYSUtils.java	Fri Aug 26 14:58:35 2011 +0000
@@ -0,0 +1,102 @@
+package de.intevation.flys.utils;
+
+import gnu.trove.TDoubleArrayList;
+
+import de.intevation.flys.artifacts.FLYSArtifact;
+
+
+public class FLYSUtils {
+
+    public static enum KM_MODE { RANGE, LOCATIONS, NONE };
+
+
+    private FLYSUtils() {
+    }
+
+
+    public static KM_MODE getKmRangeMode(FLYSArtifact flys) {
+        String mode  = flys.getDataAsString("ld_mode");
+
+        if (mode == null || mode.length() == 0) {
+            return KM_MODE.NONE;
+        }
+        else if (mode.equals("distance"))  {
+            return KM_MODE.RANGE;
+        }
+        else if (mode.equals("locations")) {
+            return KM_MODE.LOCATIONS;
+        }
+        else {
+            return KM_MODE.NONE;
+        }
+    }
+
+
+    public static double[] getKmRange(FLYSArtifact flys) {
+        switch (getKmRangeMode(flys)) {
+            case RANGE: {
+                return getKmFromTo(flys);
+            }
+
+            case LOCATIONS: {
+                double[] locs = getLocations(flys);
+                return new double[] { locs[0], locs[locs.length-1] };
+            }
+
+            case NONE: {
+                double[] locs = getLocations(flys);
+                if (locs != null) {
+                    return new double[] { locs[0], locs[locs.length-1] };
+                }
+                else {
+                    return getKmFromTo(flys);
+                }
+            }
+        }
+
+        return new double[] { Double.NaN, Double.NaN };
+    }
+
+
+    public static double[] getKmFromTo(FLYSArtifact flys) {
+        String strFrom = flys.getDataAsString("ld_from");
+        String strTo   = flys.getDataAsString("ld_to");
+
+        if (strFrom == null || strTo == null) {
+            return null;
+        }
+
+        try {
+            return new double[] {
+                Double.parseDouble(strFrom),
+                Double.parseDouble(strTo) };
+        }
+        catch (NumberFormatException nfe) {
+            return null;
+        }
+    }
+
+
+    public static double[] getLocations(FLYSArtifact flys) {
+        String locationStr = flys.getDataAsString("ld_locations");
+
+        if (locationStr == null || locationStr.length() == 0) {
+            return null;
+        }
+
+        String[] tmp               = locationStr.split(" ");
+        TDoubleArrayList locations = new TDoubleArrayList();
+
+        for (String l: tmp) {
+            try {
+                locations.add(Double.parseDouble(l));
+            }
+            catch (NumberFormatException nfe) {
+            }
+        }
+
+        locations.sort();
+
+        return locations.toNativeArray();
+    }
+}

http://dive4elements.wald.intevation.org