diff flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java @ 687:06689035024c facets-slt

Added a compute() method to WINFOArtifact which acts as a dispatcher for different computations. It triggers a calculation based on ComputeCallbacks that are generated by the current states. flys-artifacts/branches/facets-slt@2127 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 16 Jun 2011 07:50:56 +0000
parents 3dc61e00385e
children f8ac04b2dbd0
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java	Wed Jun 15 15:28:54 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java	Thu Jun 16 07:50:56 2011 +0000
@@ -1,6 +1,7 @@
 package de.intevation.flys.artifacts;
 
 import java.util.List;
+import java.util.Map;
 
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
@@ -27,6 +28,7 @@
 import de.intevation.flys.artifacts.context.FLYSContext;
 
 import de.intevation.flys.artifacts.model.ComputeCallback;
+import de.intevation.flys.artifacts.model.DischargeTables;
 import de.intevation.flys.artifacts.model.MainValuesFactory;
 import de.intevation.flys.artifacts.model.WQDay;
 import de.intevation.flys.artifacts.model.WQKms;
@@ -243,32 +245,38 @@
     //
 
 
-    public Object compute() {
-        return compute(hash());
+    /**
+     * Dispatches the computation request to compute(CallContext context, String
+     * hash) with the current hash value of the artifact which is provided by
+     * hash().
+     *
+     * @param context The CallContext.
+     */
+    public Object compute(CallContext context) {
+        return compute(context, hash());
     }
 
 
-    public Object compute(String hash) {
+    /**
+     * Dispatches computation requests to the current state which needs to
+     * implement a createComputeCallback(String hash, FLYSArtifact artifact)
+     * method.
+     *
+     * @param context The CallContext.
+     * @param hash The hash value which is used to fetch computed data from
+     * cache.
+     *
+     * @return the computed data.
+     */
+    public Object compute(CallContext context, String hash) {
         String calc = (String) getData(CalculationSelect.FIELD_MODE).getValue();
 
-        ComputeCallback callback = null;
+        DefaultState current = (DefaultState) getCurrentState(context);
 
-        if (calc.equals(CalculationSelect.CALCULATION_SURFACE_CURVE)) {
-            callback = createSurfaceCurveCallback();
-        }
-        else if (calc.equals(CalculationSelect.CALCULATION_DURATION_CURVE)) {
-            callback = createDurationCurveCallback();
-        }
-        else if (
-            calc.equals(
-                CalculationSelect.CALCULATION_DISCHARGE_LONGITUDINAL_CURVE))
-        {
-            callback = createDischargeLongitudinalCurveCallback();
-        }
-        else if (calc.equals(CalculationSelect.CALCULATION_DISCHARGE_CURVE)) {
-            callback = createDischargeCurveCallback();
-        }
-        else {
+        ComputeCallback callback = current.createComputeCallback(hash(), this);
+
+        if (callback == null) {
+            logger.info("There is no data which needs to be computed.");
             return null;
         }
 
@@ -276,42 +284,6 @@
     }
 
 
-    protected ComputeCallback createSurfaceCurveCallback() {
-        return new ComputeCallback() {
-            public Object compute() {
-                return getWaterlevelData();
-            }
-        };
-    }
-
-
-    protected ComputeCallback createDurationCurveCallback() {
-        return new ComputeCallback() {
-            public Object compute() {
-                return getDurationCurveData();
-            }
-        };
-    }
-
-
-    protected ComputeCallback createDischargeLongitudinalCurveCallback() {
-        return new ComputeCallback() {
-            public Object compute() {
-                return getDischargeLongitudinalSectionData();
-            }
-        };
-    }
-
-
-    protected ComputeCallback createDischargeCurveCallback() {
-        return new ComputeCallback() {
-            public Object compute() {
-                return getComputedDischargeCurveData();
-            }
-        };
-    }
-
-
     /**
      * Returns the data that is computed by a waterlevel computation.
      *
@@ -446,6 +418,37 @@
 
 
     /**
+     * Returns the data that is used to create discharge curves.
+     *
+     * @return a map where the key is the name of the gauge and a double[][]
+     * which are the values.
+     */
+    public Map<String, double[][]> getDischargeCurveData() {
+        River       river  = getRiver();
+        List<Gauge> gauges = getGauges();
+
+        if (gauges == null) {
+            logger.warn("No gauges found for the current kilometer range.");
+            return null;
+        }
+
+        int num = gauges.size();
+
+        logger.debug("Found " + num + " gauges.");
+
+        String[] gaugeNames = new String[num];
+
+        for (int i = 0; i < num; i++) {
+            gaugeNames[i] = gauges.get(i).getName();
+        }
+
+        DischargeTables dt = new DischargeTables(river.getName(), gaugeNames);
+
+        return dt.getValues(100d);
+    }
+
+
+    /**
      * Returns the data that is computed by a discharge curve computation.
      *
      * @return the data computed by a discharge curve computation.

http://dive4elements.wald.intevation.org