changeset 1909:4b64692b2d1e

Added methods to create WstValueTables ('interpolators') for specific columns of wsts. flys-artifacts/trunk@3269 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Wed, 16 Nov 2011 14:47:02 +0000
parents 19c53705a2eb
children 61baa34af1d4
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WstValueTableFactory.java
diffstat 2 files changed, 121 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Wed Nov 16 14:44:24 2011 +0000
+++ b/flys-artifacts/ChangeLog	Wed Nov 16 14:47:02 2011 +0000
@@ -1,3 +1,9 @@
+2011-11-16  Felix Wolfsteller <felix.wolfsteller@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/model/WstValueTableFactory.java:
+	  Added methods to create WstValueTables (interpolators) for specific
+	  columns of wsts.
+
 2011-11-16  Felix Wolfsteller <felix.wolfsteller@intevation.de>
 
 	* doc/conf/themes.xml: Added Point Style for other.wq data.
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WstValueTableFactory.java	Wed Nov 16 14:44:24 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WstValueTableFactory.java	Wed Nov 16 14:47:02 2011 +0000
@@ -41,17 +41,31 @@
         "SELECT position, name FROM wst_columns " +
         "WHERE wst_id = :wst_id ORDER BY position";
 
+    /** Select Qs for wst (view sorted by column). */
     public static final String SQL_SELECT_QS =
         "SELECT column_pos, q, a, b FROM wst_q_values " +
         "WHERE wst_id = :wst_id";
 
+    // (sorted by km)
     public static final String SQL_SELECT_WS =
         "SELECT km, w, column_pos FROM wst_w_values " +
         "WHERE wst_id = :wst_id";
 
+    /** Statement to query qranges of a single column. */
+    public static final String SQL_SELECT_QS_AT_COL =
+        "SELECT column_pos, q, a, b FROM wst_q_values " +
+        "WHERE wst_id = :wst_id AND column_pos = :column_pos";
+
+    // (sorted by km)
+    public static final String SQL_SELECT_WS_AT_COL =
+        "SELECT km, w FROM wst_w_values " +
+        "WHERE wst_id = :wst_id AND column_pos = :column_pos";
+
+
     private WstValueTableFactory() {
     }
 
+
     public static WstValueTable getTable(River river) {
         return getTable(river, DEFAULT_KIND);
     }
@@ -98,7 +112,28 @@
         return valueTable;
     }
 
+    /**
+     * Get Table for a specific column of a wst.
+     */
+    public static WstValueTable getWstColumnTable(int wst_id, int col_pos) {
 
+        /** @TODO cached/uncached */
+
+        Session session = SessionHolder.HOLDER.get();
+
+        // Fetch data for one column only.
+        
+        WstValueTable.Column [] columns = loadColumn(session, wst_id, col_pos);
+        loadQRanges(session, columns, wst_id, col_pos);
+        List<WstValueTable.Row> rows = loadRowsOneColumn(session, wst_id, col_pos);
+
+        return new WstValueTable(columns, rows);
+    }
+
+
+    /**
+     * Get table for first wst of given kind at given river.
+     */
     public static WstValueTable getTable(River river, int kind) {
 
         Cache cache = CacheFactory.getCache(WstValueTableCacheKey.CACHE_NAME);
@@ -165,11 +200,53 @@
         return wsts.isEmpty() ? null : wsts.get(0);
     }
 
+
+    /**
+     * Load rows with a single columns result.
+     * @param session    session to use for querying db.
+     * @param wst_id     id of wst (in db).
+     * @param column_pos the column_pos (within the db) of the wst_value_table
+     *                   of which the values shall be fetched.
+     * @return resultant rows.
+     */
+    protected static List<WstValueTable.Row> loadRowsOneColumn(
+        Session session,
+        int     wst_id,
+        int     column_pos
+    ) {
+        SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_WS_AT_COL)
+            .addScalar("km",         StandardBasicTypes.DOUBLE)
+            .addScalar("w",          StandardBasicTypes.DOUBLE);
+
+        sqlQuery.setInteger("wst_id", wst_id);
+        sqlQuery.setInteger("column_pos", column_pos);
+
+        List<Object []> results = sqlQuery.list();
+
+        double [] ws = null;
+
+        ArrayList<WstValueTable.Row> rows = new ArrayList<WstValueTable.Row>();
+
+        // Walk over rows.
+        for (Object [] result: results) {
+            ws = new double[1];
+            WstValueTable.Row row =
+                new WstValueTable.Row((Double) result[0], ws);
+            rows.add(row);
+
+            Double w = (Double) result[1];
+            ws[0] = w != null ? w : Double.NaN;
+        }
+
+        rows.trimToSize();
+        return rows;
+    }
+
     protected static List<WstValueTable.Row> loadRows(
         Session session,
         int     wst_id,
         int     numColumns
-    ){
+    ) {
         SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_WS)
             .addScalar("km",         StandardBasicTypes.DOUBLE)
             .addScalar("w",          StandardBasicTypes.DOUBLE)
@@ -200,7 +277,6 @@
 
         rows.trimToSize();
         return rows;
-    
     }
 
     protected static List<WstValueTable.Row> loadRows(
@@ -212,6 +288,16 @@
     }
 
 
+    protected static WstValueTable.Column [] loadColumn(
+        Session session,
+        int wst_id,
+        int col_pos
+    ) {
+        return new WstValueTable.Column [] {
+            new WstValueTable.Column(WKmsFactory.getWKmsName(col_pos, wst_id))};
+    }
+
+
     /**
      * Get columns from wst-id.
      */
@@ -247,6 +333,33 @@
         return loadColumns(session, wst.getId());
     }
 
+
+    /**
+     * Build a QRange-Tree.
+     */
+    protected static void loadQRanges(
+        Session                 session,
+        WstValueTable.Column [] columns,
+        int                     wst_id,
+        int                     column_pos
+    ) {
+        SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_QS_AT_COL)
+            .addScalar("column_pos", StandardBasicTypes.INTEGER) // keep to maintain order.
+            .addScalar("q",          StandardBasicTypes.DOUBLE)
+            .addScalar("a",          StandardBasicTypes.DOUBLE)
+            .addScalar("b",          StandardBasicTypes.DOUBLE);
+
+        sqlQuery.setInteger("wst_id",     wst_id);
+        sqlQuery.setInteger("column_pos", column_pos);
+
+        List<Object []> qRanges = sqlQuery.list();
+
+        int qSize = qRanges.size();
+
+        QRangeTree qRangeTree = new QRangeTree(qRanges, 0, qSize);
+        columns[0].setQRangeTree(qRangeTree);
+    }
+
     protected static void loadQRanges(
         Session                 session,
         WstValueTable.Column [] columns,

http://dive4elements.wald.intevation.org