changeset 6741:35f2d32b921d

issue1393: Survive when off-epochs no total data, but other.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Thu, 01 Aug 2013 12:08:46 +0200
parents 68c7b5811c4f
children 0db5f3642335
files artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadFactory.java
diffstat 1 files changed, 45 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadFactory.java	Thu Aug 01 11:58:25 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadFactory.java	Thu Aug 01 12:08:46 2013 +0200
@@ -411,10 +411,13 @@
             return load;
         }
         else if (type.equals("off_epoch")) {
+            // Set calendars to fetch the epochs by their start and end
+            // dates.
             Calendar toStart = Calendar.getInstance();
             toStart.set(eyear - 1, 11, 31);
             Calendar toEnd = Calendar.getInstance();
-            toEnd.set(eyear+1, 11, 31);
+            toEnd.set(eyear+1, 00, 01);
+            // Set query parameters.
             sqlQuery = session.createSQLQuery(SQL_SELECT_EPOCHS_DATA)
                 .addScalar("description", StandardBasicTypes.STRING)
                 .addScalar("startYear", StandardBasicTypes.DATE)
@@ -427,37 +430,38 @@
             sqlQuery.setDouble("endKm", endKm);
             sqlQuery.setDate("sbegin", start.getTime());
             sqlQuery.setDate("send", end.getTime());
-            sqlQuery.setDate("ebegin",toStart.getTime());
+            sqlQuery.setDate("ebegin", toStart.getTime());
             sqlQuery.setDate("eend", toEnd.getTime());
             sqlQuery.setString("grain", "total");
 
             List<Object[]> results = null;
             results = sqlQuery.list();
 
-            SedimentLoad load = new SedimentLoad();
+            SedimentLoad load = null;
             if (results.isEmpty()) {
-                log.warn("No results for off-epoch");
-                return new SedimentLoad();
+                log.warn("No total results for off-epoch");
             }
-            Object[] row = results.get(0);
-            load = new SedimentLoad(
-                    (String) row[0],
-                    (Date) row[1],
-                    (Date) row[4],
-                    true,
-                    (String)row[5]);
-            TDoubleArrayList kms = new TDoubleArrayList();
-            for (int i = 0; i < results.size(); i++) {
-                row = results.get(i);
-                kms.add((Double)row[3]);
-                load.setLoadTotal((Double)row[3], (Double)row[2]);
+            else {
+                Object[] row = results.get(0);
+                load = new SedimentLoad(
+                        (String) row[0],
+                        (Date) row[1],
+                        (Date) row[4],
+                        true,
+                        (String)row[5]);
+                TDoubleArrayList kms = new TDoubleArrayList();
+                for (int i = 0; i < results.size(); i++) {
+                    row = results.get(i);
+                    kms.add((Double)row[3]);
+                    load.setLoadTotal((Double)row[3], (Double)row[2]);
+                }
             }
-            getValues("coarse", sqlQuery, load, floatStations);
-            getValues("fine_middle", sqlQuery, load, floatStations);
-            getValues("sand", sqlQuery, load, floatStations);
-            getValues("suspended_sediment", sqlQuery, load, suspStations);
-            getValues("susp_sand_bed", sqlQuery, load, floatStations);
-            getValues("susp_sand", sqlQuery, load, floatStations);
+            load = getValues("coarse", sqlQuery, load, floatStations);
+            load = getValues("fine_middle", sqlQuery, load, floatStations);
+            load = getValues("sand", sqlQuery, load, floatStations);
+            load = getValues("suspended_sediment", sqlQuery, load, suspStations);
+            load = getValues("susp_sand_bed", sqlQuery, load, floatStations);
+            load = getValues("susp_sand", sqlQuery, load, floatStations);
             return load;
         }
         return new SedimentLoad();
@@ -466,13 +470,15 @@
 
     /**
      * Run query with grain parameter set to fraction, feed result into
-     * load.
+     * load. Create load if null.
+     *
      * @param fraction value to set 'grain' parameter in query to.
      * @param query query in which to set 'grain' parameter and run.
      * @param load[out] SedimentLoad which to populate with values. if null
      *                  and values are found, return a new load.
+     * @return param load or new load if null.
      */
-    protected static void getValues (
+    protected static SedimentLoad getValues (
         String fraction,
         SQLQuery query,
         SedimentLoad load,
@@ -480,6 +486,18 @@
     ) {
         query.setString("grain", fraction);
         List<Object[]> results = query.list();
+
+        // We have the first results for this query, create new load.
+        if (!results.isEmpty() && load == null) {
+           Object[] row = results.get(0);
+           load = new SedimentLoad(
+                   (String) row[0],
+                   (Date) row[1],
+                   (Date) row[4],
+                   true,
+                   (String)row[5]);
+        }
+
         for (int i = 0; i < results.size(); i++) {
             Object[] row = results.get(i);
             double km = (Double)row[3];
@@ -526,6 +544,8 @@
                 load.setSuspSandBed(km, v, range);
             }
         }
+
+        return load;
     }
 
     public static SedimentLoad getLoadUnknown(

http://dive4elements.wald.intevation.org