diff artifacts/src/main/java/org/dive4elements/river/artifacts/model/WQKmsFactory.java @ 8876:23264d1a528f

Allow access to staticwqkms data; also allow to acces by km range
author gernotbelger
date Thu, 08 Feb 2018 18:47:36 +0100
parents 5e38e2924c07
children
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/WQKmsFactory.java	Thu Feb 08 18:46:34 2018 +0100
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/WQKmsFactory.java	Thu Feb 08 18:47:36 2018 +0100
@@ -33,16 +33,22 @@
     private static Logger log = Logger.getLogger(WQKmsFactory.class);
 
     /** Query to get km and wqs for wst_id and column_pos. */
-    public static final String SQL_SELECT_WQS =
+    private static final String SQL_SELECT_WQS =
         "SELECT position, w, q FROM wst_value_table " +
         "WHERE wst_id = :wst_id AND column_pos = :column_pos";
+    /** Query to get km and wqs for wst_id and column_pos. */
 
+    private static final String SQL_SELECT_WQS_RANGE =
+            "SELECT position, w, q FROM wst_value_table " +
+            "WHERE wst_id = :wst_id AND column_pos = :column_pos " +
+            "AND position BETWEEN :kmfrom AND :kmto";
+    
     /** Get wst_id and position from wst_columns. */
-    public static final String SQL_SELECT_COLUMN =
+    private static final String SQL_SELECT_COLUMN =
         "SELECT wst_id, position FROM wst_columns WHERE id = :column_id";
 
     /** Query to get name for wst_id and column_pos. */
-    public static final String SQL_SELECT_NAME =
+    private static final String SQL_SELECT_NAME =
         "SELECT name " +
         "FROM wst_columns "+
         "WHERE id = :column_id";
@@ -56,14 +62,21 @@
     /**
      * Get WKms for given column (pos) and wst_id, caring about the cache.
      */
-    public static WQKms getWQKms(int columnPos, int wst_id) {
+    public static WQKms getWQKms(final int columnPos, final int wst_id) {
+        return getWQKms(columnPos, wst_id, Double.NaN, Double.NaN);
+    }
+
+    /**
+     * Get WKms for given column (pos) and wst_id, caring about the cache.
+     */
+    public static WQKms getWQKms(final int columnPos, final int wst_id, final double from, final double to) {
         log.debug("WQKmsFactory.getWQKms");
-        Cache cache = CacheFactory.getCache(StaticWQKmsCacheKey.CACHE_NAME);
+        final Cache cache = CacheFactory.getCache(StaticWQKmsCacheKey.CACHE_NAME);
 
-        StaticWQKmsCacheKey cacheKey;
+        final StaticWQKmsCacheKey cacheKey;
 
         if (cache != null) {
-            cacheKey = new StaticWQKmsCacheKey(wst_id, columnPos);
+            cacheKey = new StaticWQKmsCacheKey(wst_id, columnPos, from, to);
             Element element = cache.get(cacheKey);
             if (element != null) {
                 log.debug("Got static wst values from cache");
@@ -74,16 +87,16 @@
             cacheKey = null;
         }
 
-        WQKms values = getWQKmsUncached(columnPos, wst_id);
+        final WQKms values = getWQKmsUncached(columnPos, wst_id, from, to);
 
-        if (values != null && cacheKey != null) {
+        if (values != null && cache != null && cacheKey != null) {
             log.debug("Store static wst values in cache.");
-            Element element = new Element(cacheKey, values);
+            final Element element = new Element(cacheKey, values);
             cache.put(element);
         }
         return values;
     }
-
+    
     /**
      * Get WKms for given column (id), caring about the cache.
      */
@@ -94,7 +107,7 @@
         StaticWQKmsCacheKey cacheKey;
 
         if (cache != null) {
-            cacheKey = new StaticWQKmsCacheKey(-columnID, -columnID);
+            cacheKey = new StaticWQKmsCacheKey(-columnID, -columnID, Double.NaN, Double.NaN);
             Element element = cache.get(cacheKey);
             if (element != null) {
                 log.debug("Got static wst values from cache");
@@ -107,7 +120,7 @@
 
         int[] cInfo = getColumn(columnID);
         if (cInfo == null) return null;
-        WQKms values = getWQKmsUncached(cInfo[1], cInfo[0]);
+        WQKms values = getWQKmsUncached(cInfo[1], cInfo[0], Double.NaN, Double.NaN);
 
 
         if (values != null && cacheKey != null) {
@@ -125,27 +138,37 @@
      * @param wst_id database id of the wst
      * @return respective WQKms.
      */
-    public static WQKms getWQKmsUncached(int column, int wst_id) {
+    private static WQKms getWQKmsUncached(final int column, final int wst_id, final double from, final double to) {
 
         if (log.isDebugEnabled()) {
             log.debug("WQKmsFactory.getWQKmsUncached, column "
                 + column + ", wst_id " + wst_id);
         }
 
-        WQKms wqkms = new WQKms(WKmsFactory.getWKmsName(column, wst_id));
+        final WQKms wqkms = new WQKms(WKmsFactory.getWKmsName(column, wst_id));
 
-        Session session = SessionHolder.HOLDER.get();
-        SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_WQS)
+        boolean hasRange = !Double.isNaN(from) && !Double.isNaN(to);
+
+        final String query = hasRange ? SQL_SELECT_WQS_RANGE : SQL_SELECT_WQS;
+        
+        final Session session = SessionHolder.HOLDER.get();
+        final SQLQuery sqlQuery = session.createSQLQuery(query)
             .addScalar("position", StandardBasicTypes.DOUBLE)
             .addScalar("w",  StandardBasicTypes.DOUBLE)
             .addScalar("q",  StandardBasicTypes.DOUBLE);
+
         sqlQuery.setInteger("wst_id",     wst_id);
         sqlQuery.setInteger("column_pos", column);
+        if( hasRange )
+        {
+            sqlQuery.setDouble("kmfrom", from);
+            sqlQuery.setDouble("kmto", to);
+        }
 
-        List<Object []> results = sqlQuery.list();
+        final List<Object []> results = sqlQuery.list();
 
         for (int i = 0, N = results.size(); i < N; i++) {
-            Object[] row = results.get(i);
+            final Object[] row = results.get(i);
             // add(w, q, km)
             if (row == null
                 || row[0] == null
@@ -184,6 +207,7 @@
 
         List<Object []> results = sqlQuery.list();
 
+        // FIXME: right? this will always return row[0]!
         for (int i = 0, N = results.size(); i < N; i++) {
             Object[] row = results.get(i);
             return new int[] {(Integer)row[0], (Integer)row[1]};

http://dive4elements.wald.intevation.org