changeset 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 8d1df8639563
children 9f7a285b0ee3
files artifacts/src/main/java/org/dive4elements/river/artifacts/StaticWQKmsArtifact.java artifacts/src/main/java/org/dive4elements/river/artifacts/model/StaticWQKmsCacheKey.java artifacts/src/main/java/org/dive4elements/river/artifacts/model/WQKmsFactory.java artifacts/src/main/java/org/dive4elements/river/artifacts/states/WDifferencesState.java
diffstat 4 files changed, 107 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/StaticWQKmsArtifact.java	Thu Feb 08 18:46:34 2018 +0100
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/StaticWQKmsArtifact.java	Thu Feb 08 18:47:36 2018 +0100
@@ -181,6 +181,24 @@
         return res;
     }
 
+    /**
+     * Get WQKms from factory.
+     * @return WQKms according to parameterization (can be null);
+     */
+    public WQKms getWQKms(final double from, final double to) {
+        log.debug("StaticWQKmsArtifact.getWQKms");
+        
+        int col = Integer.parseInt(getDataAsString("col_pos"));
+        int wst = Integer.parseInt(getDataAsString("wst_id"));
+        
+        /** TODO do not run twice against db to do this. */
+        String wkmsName = WKmsFactory.getWKmsName(col, wst);
+        
+        WQKms res = WQKmsFactory.getWQKms(col, wst, from, to);
+        res.setName(wkmsName);
+        return res;
+    }
+
     /** Return specific name. */
     @Override
     public String getName() {
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/StaticWQKmsCacheKey.java	Thu Feb 08 18:46:34 2018 +0100
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/StaticWQKmsCacheKey.java	Thu Feb 08 18:47:36 2018 +0100
@@ -10,6 +10,9 @@
 
 import java.io.Serializable;
 
+import org.apache.commons.lang.builder.EqualsBuilder;
+import org.apache.commons.lang.builder.HashCodeBuilder;
+
 /**
  * Caching-Key object for 'static' wst- data.
  */
@@ -18,24 +21,48 @@
 {
     public static final String CACHE_NAME = "wst-wq-value-table-static";
 
-    private int column;
-    private int wst_id;
+    private final int column;
+    
+    private final int wst_id;
 
-    public StaticWQKmsCacheKey(int column, int wst_id) {
+    private double from;
+
+    private double to;
+
+    private int hash;
+
+    public StaticWQKmsCacheKey(final int column, final int wst_id, final double from, final double to) {
         this.wst_id  = wst_id;
         this.column  = column;
+        this.from = from;
+        this.to = to;
+        this.hash = new HashCodeBuilder().append(column).append(wst_id).append(false).append(to).toHashCode();
     }
 
+    @Override
     public int hashCode() {
-        return (wst_id << 8) | column;
+        return this.hash;
     }
 
-    public boolean equals(Object other) {
-        if (!(other instanceof StaticWQKmsCacheKey)) {
+    @Override
+    public boolean equals(final Object other) {
+        
+        if (other == null) 
             return false;
+        if( other == this) 
+            return true;
+        if (other.getClass() != getClass()) {
+          return false;
         }
-        StaticWQKmsCacheKey o = (StaticWQKmsCacheKey) other;
-        return this.wst_id == o.wst_id && this.column == o.column;
+        
+        final StaticWQKmsCacheKey rhs = (StaticWQKmsCacheKey) other;
+        
+        return new EqualsBuilder(). //
+                append(column, rhs.column). //
+                append(wst_id, rhs.wst_id). //
+                append(from, rhs.from). //
+                append(to, rhs.to). //
+                isEquals();
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- 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]};
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/states/WDifferencesState.java	Thu Feb 08 18:46:34 2018 +0100
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/states/WDifferencesState.java	Thu Feb 08 18:47:36 2018 +0100
@@ -25,6 +25,7 @@
 import org.dive4elements.river.artifacts.FixationArtifact;
 import org.dive4elements.river.artifacts.MINFOArtifact;
 import org.dive4elements.river.artifacts.StaticWKmsArtifact;
+import org.dive4elements.river.artifacts.StaticWQKmsArtifact;
 import org.dive4elements.river.artifacts.WINFOArtifact;
 
 import org.dive4elements.river.artifacts.math.WKmsOperation;
@@ -138,6 +139,16 @@
             WKms wkms = staticWKms.getWKms(idx, from, to);
             if (wkms == null) {
                 log.error("No WKms from Static artifact for this range.");
+                // FIXME: why does in error case we return Q's?
+                return new WQKms();
+            }
+            return wkms; /* No need for additional km filtering */
+        } else if (d4eArtifact instanceof StaticWQKmsArtifact) {
+            final StaticWQKmsArtifact staticWKms = (StaticWQKmsArtifact) d4eArtifact;
+            log.debug("WDifferencesState obtain data from StaticWQKms");
+            WQKms wkms = staticWKms.getWQKms(from, to);
+            if (wkms == null) {
+                log.error("No WKms from Static artifact for this range.");
                 return new WQKms();
             }
             return wkms; /* No need for additional km filtering */

http://dive4elements.wald.intevation.org