diff flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java @ 709:3b7e9ddf6bb1

New model to transport data and error reports of calculations. flys-artifacts/trunk@2165 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 20 Jun 2011 12:32:32 +0000
parents 757ff56b43b3
children cded0924193d
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java	Sat Jun 18 20:20:34 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java	Mon Jun 20 12:32:32 2011 +0000
@@ -37,10 +37,12 @@
 import de.intevation.flys.artifacts.model.WQKms;
 import de.intevation.flys.artifacts.model.WstValueTable;
 import de.intevation.flys.artifacts.model.WstValueTableFactory;
+import de.intevation.flys.artifacts.model.Calculation;
 import de.intevation.flys.artifacts.model.Calculation1;
 import de.intevation.flys.artifacts.model.Calculation2;
 import de.intevation.flys.artifacts.model.Calculation3;
 import de.intevation.flys.artifacts.model.Calculation4;
+import de.intevation.flys.artifacts.model.CalculationResult;
 import de.intevation.flys.artifacts.model.Segment;
 
 
@@ -297,19 +299,18 @@
      *
      * @return an array of data triples that consist of W, Q and Kms.
      */
-    public WQKms[] getWaterlevelData()
-    throws NullPointerException
+    public CalculationResult getWaterlevelData()
     {
         logger.debug("WINFOArtifact.getWaterlevelData");
 
         River river = getRiver();
         if (river == null) {
-            throw new NullPointerException("No river selected.");
+            return error(new WQKms[0], "No river selected.");
         }
 
         double[] kms = getKms();
         if (kms == null) {
-            throw new NullPointerException("No Kms selected.");
+            return error(new WQKms[0], "No Kms selected.");
         }
 
         double[] qs   = getQs();
@@ -325,7 +326,7 @@
 
         WstValueTable wst = WstValueTableFactory.getTable(river);
         if (wst == null) {
-            throw new NullPointerException("No Wst found for selected river.");
+            return error(new WQKms[0], "No Wst found for selected river.");
         }
 
         double refKm = Double.NaN;
@@ -341,10 +342,8 @@
             }
         }
 
-        WQKms[] results = computeWaterlevelData(
+        return computeWaterlevelData(
             kms, qs, ws, wst, refKm, river.getKmUp());
-
-        return results;
     }
 
     /**
@@ -357,7 +356,7 @@
      *
      * @return an array of data triples that consist of W, Q and Kms.
      */
-    public static WQKms[] computeWaterlevelData(
+    public static CalculationResult computeWaterlevelData(
         double []     kms,
         double []     qs,
         double []     ws,
@@ -367,13 +366,9 @@
     ) {
         logger.info("WINFOArtifact.computeWaterlevelData");
 
-        Calculation1 calculation = new Calculation1(kms, qs, ws, refKm, up);
+        Calculation1 calc1 = new Calculation1(kms, qs, ws, refKm, up);
 
-        WQKms[] wqkms = calculation.calculate(wst);
-
-        // TODO: report problems to user
-
-        return wqkms;
+        return calc1.calculate(wst);
     }
 
 
@@ -382,32 +377,30 @@
      *
      * @return the data computed by a duration curve computation.
      */
-    public WQDay getDurationCurveData()
-    throws NullPointerException
-    {
+    public CalculationResult getDurationCurveData() {
         logger.debug("WINFOArtifact.getDurationCurveData");
 
         River r = getRiver();
 
         if (r == null) {
-            throw new NullPointerException("Cannot determine river.");
+            return error(null, "Cannot determine river.");
         }
 
         Gauge g = getGauge();
 
         if (g == null) {
-            throw new NullPointerException("Cannot determine gauge.");
+           return error(null, "Cannot determine gauge.");
         }
 
         double[] locations = getLocations();
 
         if (locations == null) {
-            throw new NullPointerException("Cannot determine location.");
+            return error(null, "Cannot determine location.");
         }
 
         WstValueTable wst = WstValueTableFactory.getTable(r);
         if (wst == null) {
-            throw new NullPointerException("No Wst found for selected river.");
+            return error(null, "No Wst found for selected river.");
         }
 
         return computeDurationCurveData(g, wst, locations[0]);
@@ -422,7 +415,7 @@
      *
      * @return the computed data.
      */
-    public static WQDay computeDurationCurveData(
+    public static CalculationResult computeDurationCurveData(
         Gauge           gauge,
         WstValueTable   wst,
         double          location)
@@ -436,7 +429,6 @@
 
         Calculation3 calculation = new Calculation3(location, days, qs);
 
-        // TODO: report the errors to the user.
         return calculation.calculate(wst);
     }
 
@@ -477,7 +469,7 @@
      *
      * @return the data computed by a discharge curve computation.
      */
-    public WQKms getComputedDischargeCurveData()
+    public CalculationResult getComputedDischargeCurveData()
     throws NullPointerException
     {
         logger.debug("WINFOArtifact.getComputedDischargeCurveData");
@@ -485,23 +477,21 @@
         River r = getRiver();
 
         if (r == null) {
-            throw new NullPointerException("Cannot determine river.");
+            return error(new WQKms[0], "Cannot determine river.");
         }
 
         double[] locations = getLocations();
 
         if (locations == null) {
-            throw new NullPointerException("Cannot determine location.");
+            return error(new WQKms[0], "Cannot determine location.");
         }
 
         WstValueTable wst = WstValueTableFactory.getTable(r);
         if (wst == null) {
-            throw new NullPointerException("No Wst found for selected river.");
+            return error(new WQKms[0], "No Wst found for selected river.");
         }
 
-        WQKms wqkms = computeDischargeCurveData(wst, locations[0]);
-
-        return wqkms;
+        return computeDischargeCurveData(wst, locations[0]);
     }
 
 
@@ -514,7 +504,7 @@
      * @return an object that contains tuples of W/Q values at the specified
      * location.
      */
-    public static WQKms computeDischargeCurveData(
+    public static CalculationResult computeDischargeCurveData(
         WstValueTable wst,
         double location)
     {
@@ -522,11 +512,11 @@
 
         Calculation2 calculation = new Calculation2(location);
 
-        WQKms wqkms = calculation.calculate(wst);
+        return calculation.calculate(wst);
+    }
 
-        // TODO: Report errors to the user
-
-        return wqkms;
+    protected static final CalculationResult error(Object data, String msg) {
+        return new CalculationResult(data, new Calculation(msg));
     }
 
     /**
@@ -535,41 +525,39 @@
      *
      * @return an array of WQKms object - one object for each given Q value.
      */
-    public WQKms [] getDischargeLongitudinalSectionData() {
+    public CalculationResult getDischargeLongitudinalSectionData() {
 
         logger.debug("WINFOArtifact.getDischargeLongitudinalSectionData");
 
         River river = getRiver();
         if (river == null) {
-            logger.error("No river selected.");
-            return new WQKms[0];
+            logger.debug("No river selected.");
+            return error(new WQKms[0], "No river selected.");
         }
 
         WstValueTable table = WstValueTableFactory.getTable(river);
         if (table == null) {
-            logger.error("No wst found for selected river.");
-            return new WQKms[0];
+            logger.debug("No wst found for selected river.");
+            return error(new WQKms[0], "No wst found for selected river.");
         }
 
         List<Segment> segments = getSegments();
 
         if (segments == null) {
-            logger.error("Cannot create segments.");
-            return new WQKms[0];
+            logger.debug("Cannot create segments.");
+            return error(new WQKms[0], "Cannot create segments.");
         }
 
         double [] range = getFromToStep();
 
         if (range == null) {
-            logger.error("Cannot figure out range.");
-            return new WQKms[0];
+            logger.debug("Cannot figure out range.");
+            return error(new WQKms[0], "Cannot figure out range.");
         }
 
         Calculation4 calc4 = new Calculation4(segments, river, isQ());
 
-        WQKms [] results = calc4.calculate(table, range[0], range[1], range[2]);
-
-        return results;
+        return calc4.calculate(table, range[0], range[1], range[2]);
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org