diff artifacts/src/main/java/org/dive4elements/river/artifacts/model/WKmsFactory.java @ 8547:04367906f158

(issue1754) Add distantce handling to WINFO differences state
author Andre Heinecke <andre.heinecke@intevation.de>
date Mon, 16 Feb 2015 12:55:38 +0100
parents e4606eae8ea5
children 532da345d4a7
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/WKmsFactory.java	Mon Feb 16 11:30:27 2015 +0100
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/WKmsFactory.java	Mon Feb 16 12:55:38 2015 +0100
@@ -39,6 +39,11 @@
         "SELECT km, w FROM wst_w_values " +
         "WHERE wst_id = :wst_id AND column_pos = :column_pos";
 
+    public static final String SQL_SELECT_WS_FOR_RANGE =
+        "SELECT km, w FROM wst_w_values " +
+        "WHERE wst_id = :wst_id AND column_pos = :column_pos " +
+        "AND km BETWEEN :kmfrom AND :kmto";
+
     /** Query to get name for wst_id and column_pos. */
     public static final String SQL_SELECT_NAME =
         "SELECT name " +
@@ -82,31 +87,26 @@
     private WKmsFactory() {
     }
 
+    public static WKms getWKms(int column, int wst_id, double from, double to) {
+        log.debug("WKmsFactory.getWKms");
+        Cache cache = CacheFactory.getCache("waterlevels-static");
 
-    /**
-     * Get WKms for given column and wst_id, caring about the cache.
-     */
-    public static WKms getWKms(int column, int wst_id) {
-        log.debug("WKmsFactory.getWKms");
-        Cache cache = CacheFactory.getCache(StaticWKmsCacheKey.CACHE_NAME);
-
-        StaticWKmsCacheKey cacheKey;
+        String cacheKey = Integer.toString(column) + ":" + Integer.toString(wst_id);
 
         if (cache != null) {
-            cacheKey = new StaticWKmsCacheKey(wst_id, column);
+            if (!Double.isNaN(from) && ! Double.isNaN(to)) {
+                cacheKey += ":" + Double.toString(from) + ":" + Double.toString(to);
+            }
             Element element = cache.get(cacheKey);
             if (element != null) {
                 log.debug("Got static wst values from cache");
                 return (WKms)element.getValue();
             }
         }
-        else {
-            cacheKey = null;
-        }
 
-        WKms values = getWKmsUncached(column, wst_id);
+        WKms values = getWKmsUncached(column, wst_id, from, to);
 
-        if (values != null && cacheKey != null) {
+        if (values != null && cache != null) {
             log.debug("Store static wst values in cache.");
             Element element = new Element(cacheKey, values);
             cache.put(element);
@@ -114,6 +114,13 @@
         return values;
     }
 
+    /**
+     * Get WKms for given column and wst_id, caring about the cache.
+     */
+    public static WKms getWKms(int column, int wst_id) {
+        return getWKms(column, wst_id, Double.NaN, Double.NaN);
+    }
+
     /** Get name for a WKms wrapped in W, if suitable. */
     public static String getWKmsNameWWrapped(int wst_id) {
         return getWKmsNameWWrapped(-1, wst_id);
@@ -210,7 +217,7 @@
      * @param wst_id database id of the wst
      * @return according WKms.
      */
-    public static WKms getWKmsUncached(int column, int wst_id) {
+    public static WKms getWKmsUncached(int column, int wst_id, double from, double to) {
 
         if (log.isDebugEnabled()) {
             log.debug("WKmsFactory.getWKmsUncached c/" + column + ", wst_id/" + wst_id);
@@ -219,9 +226,17 @@
         WKmsImpl wkms = new WKmsImpl(getWKmsName(column, wst_id));
 
         Session session = SessionHolder.HOLDER.get();
-        SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_WS)
-            .addScalar("km", StandardBasicTypes.DOUBLE)
-            .addScalar("w",  StandardBasicTypes.DOUBLE);
+        SQLQuery sqlQuery;
+        if (Double.isNaN(from) || Double.isNaN(to)) {
+            sqlQuery = session.createSQLQuery(SQL_SELECT_WS);
+        } else {
+            sqlQuery = session.createSQLQuery(SQL_SELECT_WS_FOR_RANGE);
+            sqlQuery.setDouble("kmfrom", from);
+            sqlQuery.setDouble("kmto", to);
+        }
+
+        sqlQuery.addScalar("km", StandardBasicTypes.DOUBLE)
+                .addScalar("w",  StandardBasicTypes.DOUBLE);
         sqlQuery.setInteger("wst_id",     wst_id);
         sqlQuery.setInteger("column_pos", column);
 

http://dive4elements.wald.intevation.org