diff artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedHeightFactory.java @ 8544:76113b975829

(Issue1754) Make BedDifference calculation range dependent.
author Andre Heinecke <andre.heinecke@intevation.de>
date Fri, 13 Feb 2015 16:17:46 +0100
parents e4606eae8ea5
children 3a0522f1a532
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedHeightFactory.java	Fri Feb 13 14:59:16 2015 +0100
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedHeightFactory.java	Fri Feb 13 16:17:46 2015 +0100
@@ -33,10 +33,16 @@
         "SELECT bhsv.height, bhsv.station, bhsv.data_gap, bhsv.sounding_width," +
         "       bhs.year, bhsv.width" +
         "   FROM bed_height_single bhs" +
-        "       JOIN bed_height_single_values bhsv on bhsv.bed_height_single_id = bhs.id" +
+        "       JOIN bed_height_single_values bhsv on bhsv.bed_height_single_id = bhs.id";
+
+    public static final String ID_CLAUSE =
         "   WHERE bhs.id = :height_id" +
         "       ORDER BY bhsv.station";
 
+    public static final String ID_STATION_CLAUSE =
+        "   WHERE bhs.id = :height_id AND" +
+        "         bhsv.station BETWEEN :fromkm AND :tokm" +
+        "       ORDER BY bhsv.station";
 
     /** Query to get name (description) for wst_id. */
     public static final String SQL_SELECT_DESCR_SINGLE =
@@ -46,17 +52,23 @@
     private BedHeightFactory() {
     }
 
-
     /**
      * Get BedHeightData for given type and height_id, caring about the cache.
+     * If from or to are NaN all values are returned. Otherwise only get
+     * values with stations between from and to.
      */
-    public static BedHeightData getHeight(String type, int height_id) {
+    public static BedHeightData getHeight(String type, int height_id, double from, double to) {
         log.debug("BedHeightFactory.getHeight");
         Cache cache = CacheFactory.getCache("bedheight-value-table-static");
 
-        String cacheKey = Integer.toString(height_id);
+        String cacheKey = Integer.toString(height_id) + ":" +
+            Double.toString(from) + ":" + Double.toString(to);
 
         if (cache != null) {
+            /* We could be more intelligent here and reuse cached values for
+             * a complete river and filter the other stations out afterwards.
+             * It might even be better to cache all values first and filter
+             * later. */
             Element element = cache.get(cacheKey);
             if (element != null) {
                 log.debug("Got static bedheight values from cache");
@@ -67,7 +79,7 @@
             cacheKey = null;
         }
 
-        BedHeightData values = getBedHeightUncached(type, height_id);
+        BedHeightData values = getBedHeightUncached(type, height_id, from, to);
 
         if (values != null && cacheKey != null) {
             log.debug("Store static bed height values in cache.");
@@ -77,6 +89,13 @@
         return values;
     }
 
+    /**
+     * Get BedHeightData for given type and height_id, caring about the cache.
+     */
+    public static BedHeightData getHeight(String type, int height_id) {
+        return getHeight(type, height_id, Double.NaN, Double.NaN);
+    }
+
     /** Get name for a BedHeight. */
     public static String getHeightName(String type, int height_id) {
         log.debug("BedHeightFactory.getHeightName height_id/" + height_id);
@@ -104,12 +123,19 @@
 
     /**
      * Get BedHeightData from db.
+     *
+     * If from or to are negative all stations are returned. Otherwise
+     * only the values with a station betweend from and to.
      * @param height_id database id of the bed_height
+     * @param from minimum station value or NaN
+     * @param to maximum station value or NaN
      * @return according BedHeight.
      */
     public static BedHeightData getBedHeightUncached(
         String type,
-        int height_id)
+        int height_id,
+        double from,
+        double to)
     {
         if (log.isDebugEnabled()) {
             log.debug("BedHeightFactory.getBedHeightUncached");
@@ -120,7 +146,13 @@
         if (type.equals("single")) {
             BedHeightSingleData height =
                 new BedHeightSingleData(getHeightName(type, height_id));
-            sqlQuery = session.createSQLQuery(SQL_SELECT_SINGLE)
+            String queryString = SQL_SELECT_SINGLE;
+            if (Double.isNaN(from) || Double.isNaN(to)) {
+                queryString += ID_CLAUSE;
+            } else {
+                queryString += ID_STATION_CLAUSE;
+            }
+            sqlQuery = session.createSQLQuery(queryString)
                 .addScalar("height", StandardBasicTypes.DOUBLE)
                 .addScalar("station", StandardBasicTypes.DOUBLE)
                 .addScalar("data_gap", StandardBasicTypes.DOUBLE)
@@ -128,6 +160,10 @@
                 .addScalar("width", StandardBasicTypes.DOUBLE)
                 .addScalar("year", StandardBasicTypes.INTEGER);
             sqlQuery.setInteger("height_id", height_id);
+            if (!Double.isNaN(from) && !Double.isNaN(to)) {
+                sqlQuery.setDouble("fromkm", from);
+                sqlQuery.setDouble("tokm", to);
+            }
             List<Object []> results = sqlQuery.list();
 
             for (Object [] row: results) {

http://dive4elements.wald.intevation.org