changeset 443:5d65fe4c08d5

Separated the WST table loading logic from the calculations. flys-artifacts/trunk@1931 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 17 May 2011 08:55:38 +0000
parents c1ef5f63278e
children 932a5e3c7fa1
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WstValueTable.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WstValueTableFactory.java
diffstat 4 files changed, 209 insertions(+), 176 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Tue May 17 07:39:33 2011 +0000
+++ b/flys-artifacts/ChangeLog	Tue May 17 08:55:38 2011 +0000
@@ -1,3 +1,14 @@
+2011-05-17	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/model/WstValueTable.java:
+	  Removed the Hibernate loading stuff.
+
+	* src/main/java/de/intevation/flys/artifacts/model/WstValueTableFactory.java:
+	  New. The Hibernate loading.
+
+	* src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java:
+	  Use the WstValueTableFactory for loading now.
+
 2011-05-17  Ingo Weinzierl <ingo@intevation.de>
 
 	Tagged RELEASE 0.1
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java	Tue May 17 07:39:33 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java	Tue May 17 08:55:38 2011 +0000
@@ -31,6 +31,7 @@
 import de.intevation.flys.artifacts.model.WQDay;
 import de.intevation.flys.artifacts.model.WQKms;
 import de.intevation.flys.artifacts.model.WstValueTable;
+import de.intevation.flys.artifacts.model.WstValueTableFactory;
 
 
 /**
@@ -259,7 +260,7 @@
             qs = getQsForWs(ws);
         }
 
-        WstValueTable wst = WstValueTable.getTable(river);
+        WstValueTable wst = WstValueTableFactory.getTable(river);
         if (wst == null) {
             throw new NullPointerException("No Wst found for selected river.");
         }
@@ -336,7 +337,7 @@
             throw new NullPointerException("Cannot determine location.");
         }
 
-        WstValueTable wst = WstValueTable.getTable(r);
+        WstValueTable wst = WstValueTableFactory.getTable(r);
         if (wst == null) {
             throw new NullPointerException("No Wst found for selected river.");
         }
@@ -402,7 +403,7 @@
             throw new NullPointerException("Cannot determine location.");
         }
 
-        WstValueTable wst = WstValueTable.getTable(r);
+        WstValueTable wst = WstValueTableFactory.getTable(r);
         if (wst == null) {
             throw new NullPointerException("No Wst found for selected river.");
         }
@@ -462,7 +463,7 @@
             logger.error("No river selected.");
         }
 
-        WstValueTable wst = WstValueTable.getTable(river);
+        WstValueTable wst = WstValueTableFactory.getTable(river);
         if (wst == null) {
             logger.error("No Wst found for selected river.");
         }
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WstValueTable.java	Tue May 17 07:39:33 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WstValueTable.java	Tue May 17 08:55:38 2011 +0000
@@ -2,19 +2,11 @@
 
 import java.io.Serializable;
 
-import de.intevation.flys.model.River;
-import de.intevation.flys.model.Wst;
-import de.intevation.flys.model.WstColumn;
 
 import de.intevation.flys.artifacts.math.LinearRemap;
 
-import de.intevation.flys.artifacts.cache.CacheFactory;
-
-import de.intevation.flys.backend.SessionHolder;
-
 import java.util.Arrays;
 import java.util.ArrayList;
-import java.util.Comparator;
 import java.util.List;
 import java.util.Collections;
 
@@ -26,28 +18,11 @@
 
 import org.apache.commons.math.ArgumentOutsideDomainException;
 
-import org.hibernate.Session;
-import org.hibernate.Query;
-import org.hibernate.SQLQuery;
-
-import org.hibernate.type.StandardBasicTypes;
-
-import net.sf.ehcache.Cache;
-import net.sf.ehcache.Element;
-
 public class WstValueTable
 implements   Serializable
 {
     private static Logger log = Logger.getLogger(WstValueTable.class);
 
-    public static final String CACHE_NAME = "wst-value-table";
-
-    // TODO: put this into a property file
-    public static final String SQL_POS_WQ = 
-        "SELECT position, w, q, column_pos" +
-        "    FROM wst_value_table"          +
-        "    WHERE wst_id = :wst_id";
-
     public static final int DEFAULT_Q_STEPS = 500;
 
     public static class Column
@@ -666,152 +641,5 @@
 
         return qPosition;
     }
-
-    public static WstValueTable getTable(River river) {
-        return getTable(river, 0);
-    }
-
-    public static final class CacheKey
-    implements                Serializable
-    {
-        private int riverId;
-        private int kind;
-
-        public CacheKey(int riverId, int kind) {
-            this.riverId = riverId;
-            this.kind    = kind;
-        }
-
-        public int hashCode() {
-            return (riverId << 8) | kind;
-        }
-
-        public boolean equals(Object other) {
-            if (!(other instanceof CacheKey)) {
-                return false;
-            }
-            CacheKey o = (CacheKey)other;
-            return riverId == o.riverId && kind == o.kind;
-        }
-    } // class CacheKey
-
-    public static WstValueTable getTable(River river, int kind) {
-
-        Cache cache = CacheFactory.getCache(CACHE_NAME);
-
-        CacheKey cacheKey;
-
-        if (cache != null) {
-            cacheKey = new CacheKey(river.getId(), kind);
-            Element element = cache.get(cacheKey);
-            if (element != null) {
-                log.debug("got wst value table from cache");
-                return (WstValueTable)element.getValue();
-            }
-        }
-        else {
-            cacheKey = null;
-        }
-
-        WstValueTable valueTable = getTableUncached(river, kind);
-
-        if (cacheKey != null) {
-            log.debug("store wst value in cache");
-            Element element = new Element(cacheKey, valueTable);
-            cache.put(element);
-        }
-
-        return valueTable;
-    }
-
-    public static WstValueTable getTableUncached(River river, int kind) {
-
-        Session session = SessionHolder.HOLDER.get();
-
-        Query query = session.createQuery(
-            "from Wst where river=:river and kind=:kind");
-        query.setParameter("river", river);
-        query.setInteger("kind",    kind);
-
-        List<Wst> wsts = query.list();
-
-        if (wsts.isEmpty()) {
-            return null;
-        }
-
-        Wst wst = wsts.get(0);
-
-        // TODO: Do this sorting at database level
-        List<WstColumn> wstColumns = new ArrayList(wst.getColumns());
-        Collections.sort(wstColumns, new Comparator<WstColumn>() {
-            public int compare(WstColumn a, WstColumn b) {
-                int pa = a.getPosition();
-                int pb = b.getPosition();
-                if (pa < pb) return -1;
-                if (pa > pb) return +1;
-                return 0;
-            }
-        });
-
-        Column [] columns = new Column[wstColumns.size()];
-        for (int i = 0; i < columns.length; ++i) {
-            columns[i] = new Column(wstColumns.get(i).getName());
-        }
-
-        // using native SQL here to avoid myriad of small objects.
-        SQLQuery sqlQuery = session.createSQLQuery(SQL_POS_WQ)
-            .addScalar("position",   StandardBasicTypes.DOUBLE)
-            .addScalar("w",          StandardBasicTypes.DOUBLE)
-            .addScalar("q",          StandardBasicTypes.DOUBLE)
-            .addScalar("column_pos", StandardBasicTypes.INTEGER);
-
-        sqlQuery.setInteger("wst_id", wst.getId());
-
-        WstValueTable valueTable = new WstValueTable(columns);
-
-        int lastColumnNo = -1;
-        Row row          = null;
-
-        Double lastQ     = -Double.MAX_VALUE;
-        boolean qSorted  = true;
-
-        for (Object  r: sqlQuery.list()) {
-            Object[] result = (Object[]) r;
-
-            double km    = (Double) result[0];
-            Double w     = (Double) result[1];
-            Double q     = (Double) result[2];
-            int columnNo = (Integer)result[3];
-
-            if (columnNo > lastColumnNo) { // new row
-                if (row != null) {
-                    row.qSorted = qSorted;
-                    valueTable.rows.add(row);
-                }
-                row = new Row(
-                    km,
-                    new double[columnNo+1],
-                    new double[columnNo+1]);
-                lastQ = -Double.MAX_VALUE;
-                qSorted = true;
-            }
-
-            row.ws[columnNo] = w != null ? w : Double.NaN;
-            row.qs[columnNo] = q != null ? q : Double.NaN;
-
-            if (qSorted && (q == null || lastQ > q)) {
-                qSorted = false;
-            }
-            lastQ = q;
-
-            lastColumnNo = columnNo;
-        }
-
-        if (row != null) {
-            valueTable.rows.add(row);
-        }
-
-        return valueTable;
-    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WstValueTableFactory.java	Tue May 17 08:55:38 2011 +0000
@@ -0,0 +1,193 @@
+package de.intevation.flys.artifacts.model;
+
+import java.io.Serializable;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Collections;
+
+import org.hibernate.Session;
+import org.hibernate.Query;
+import org.hibernate.SQLQuery;
+
+import org.hibernate.type.StandardBasicTypes;
+
+import net.sf.ehcache.Cache;
+import net.sf.ehcache.Element;
+
+import org.apache.log4j.Logger;
+
+import de.intevation.flys.model.River;
+import de.intevation.flys.model.Wst;
+import de.intevation.flys.model.WstColumn;
+
+import de.intevation.flys.artifacts.cache.CacheFactory;
+
+import de.intevation.flys.backend.SessionHolder;
+
+public class WstValueTableFactory
+{
+    private static Logger log = Logger.getLogger(WstValueTableFactory.class);
+
+    public static final String CACHE_NAME = "wst-value-table";
+
+    // TODO: put this into a property file
+    public static final String SQL_POS_WQ = 
+        "SELECT position, w, q, column_pos" +
+        "    FROM wst_value_table"          +
+        "    WHERE wst_id = :wst_id";
+
+    public static final class CacheKey
+    implements                Serializable
+    {
+        private int riverId;
+        private int kind;
+
+        public CacheKey(int riverId, int kind) {
+            this.riverId = riverId;
+            this.kind    = kind;
+        }
+
+        public int hashCode() {
+            return (riverId << 8) | kind;
+        }
+
+        public boolean equals(Object other) {
+            if (!(other instanceof CacheKey)) {
+                return false;
+            }
+            CacheKey o = (CacheKey)other;
+            return riverId == o.riverId && kind == o.kind;
+        }
+    } // class CacheKey
+
+    private WstValueTableFactory() {
+    }
+
+    public static WstValueTable getTable(River river) {
+        return getTable(river, 0);
+    }
+
+    public static WstValueTable getTable(River river, int kind) {
+
+        Cache cache = CacheFactory.getCache(CACHE_NAME);
+
+        CacheKey cacheKey;
+
+        if (cache != null) {
+            cacheKey = new CacheKey(river.getId(), kind);
+            Element element = cache.get(cacheKey);
+            if (element != null) {
+                log.debug("got wst value table from cache");
+                return (WstValueTable)element.getValue();
+            }
+        }
+        else {
+            cacheKey = null;
+        }
+
+        WstValueTable valueTable = getTableUncached(river, kind);
+
+        if (cacheKey != null) {
+            log.debug("store wst value table in cache");
+            Element element = new Element(cacheKey, valueTable);
+            cache.put(element);
+        }
+
+        return valueTable;
+    }
+
+    public static WstValueTable getTableUncached(River river, int kind) {
+
+        Session session = SessionHolder.HOLDER.get();
+
+        Query query = session.createQuery(
+            "from Wst where river=:river and kind=:kind");
+        query.setParameter("river", river);
+        query.setInteger("kind",    kind);
+
+        List<Wst> wsts = query.list();
+
+        if (wsts.isEmpty()) {
+            return null;
+        }
+
+        Wst wst = wsts.get(0);
+
+        // TODO: Do this sorting at database level
+        List<WstColumn> wstColumns = new ArrayList(wst.getColumns());
+        Collections.sort(wstColumns, new Comparator<WstColumn>() {
+            public int compare(WstColumn a, WstColumn b) {
+                int pa = a.getPosition();
+                int pb = b.getPosition();
+                if (pa < pb) return -1;
+                if (pa > pb) return +1;
+                return 0;
+            }
+        });
+
+        WstValueTable.Column [] columns =
+            new WstValueTable.Column[wstColumns.size()];
+
+        for (int i = 0; i < columns.length; ++i) {
+            columns[i] = new WstValueTable.Column(wstColumns.get(i).getName());
+        }
+
+        // using native SQL here to avoid myriad of small objects.
+        SQLQuery sqlQuery = session.createSQLQuery(SQL_POS_WQ)
+            .addScalar("position",   StandardBasicTypes.DOUBLE)
+            .addScalar("w",          StandardBasicTypes.DOUBLE)
+            .addScalar("q",          StandardBasicTypes.DOUBLE)
+            .addScalar("column_pos", StandardBasicTypes.INTEGER);
+
+        sqlQuery.setInteger("wst_id", wst.getId());
+
+        WstValueTable valueTable = new WstValueTable(columns);
+
+        int lastColumnNo      = -1;
+        WstValueTable.Row row = null;
+
+        Double lastQ     = -Double.MAX_VALUE;
+        boolean qSorted  = true;
+
+        for (Object  r: sqlQuery.list()) {
+            Object[] result = (Object[]) r;
+
+            double km    = (Double) result[0];
+            Double w     = (Double) result[1];
+            Double q     = (Double) result[2];
+            int columnNo = (Integer)result[3];
+
+            if (columnNo > lastColumnNo) { // new row
+                if (row != null) {
+                    row.qSorted = qSorted;
+                    valueTable.rows.add(row);
+                }
+                row = new WstValueTable.Row(
+                    km,
+                    new double[columnNo+1],
+                    new double[columnNo+1]);
+                lastQ = -Double.MAX_VALUE;
+                qSorted = true;
+            }
+
+            row.ws[columnNo] = w != null ? w : Double.NaN;
+            row.qs[columnNo] = q != null ? q : Double.NaN;
+
+            if (qSorted && (q == null || lastQ > q)) {
+                qSorted = false;
+            }
+            lastQ = q;
+
+            lastColumnNo = columnNo;
+        }
+
+        if (row != null) {
+            valueTable.rows.add(row);
+        }
+
+        return valueTable;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org