changeset 2619:6ed24efc80de

Loaded columns of fixings from database and store them into the cache. flys-artifacts/trunk@4204 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 05 Apr 2012 15:29:52 +0000 (2012-04-05)
parents 3a93bbbe2ec7
children cc0fa1798a3c
files flys-artifacts/ChangeLog flys-artifacts/doc/conf/cache.xml flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FixingsColumn.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FixingsColumnFactory.java
diffstat 4 files changed, 99 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Thu Apr 05 15:29:25 2012 +0000
+++ b/flys-artifacts/ChangeLog	Thu Apr 05 15:29:52 2012 +0000
@@ -1,3 +1,13 @@
+2012-04-05	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/model/FixingsColumnFactory.java:
+	  Load ws and qs from database. Cache the fixings columns.
+
+	* src/main/java/de/intevation/flys/artifacts/model/FixingsColumn.java:
+	  Removed column id and added q values.
+
+	* doc/conf/cache.xml: Added cache for the columns of the fixings.
+
 2012-04-05  Raimund Renkert <raimund.renkert@intevation.de>
 
 	Fix for 'W free' validation and theme names.
@@ -35,7 +45,6 @@
 	  src/main/java/de/intevation/flys/utils/MapfileGenerator.java: Methods
 	  for creating the WSPLGEN layer now take a style document.
 
-
 2012-04-04	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/services/FixingsKMChartService.java:
--- a/flys-artifacts/doc/conf/cache.xml	Thu Apr 05 15:29:25 2012 +0000
+++ b/flys-artifacts/doc/conf/cache.xml	Thu Apr 05 15:29:52 2012 +0000
@@ -104,6 +104,16 @@
            diskPersistent="true"
            />
 
+    <!-- This one is used to hold the data columns of fixings. -->
+    <cache name="fixings-columns"
+           maxElementsInMemory="50"
+           eternal="false"
+           timeToLiveSeconds="7200"
+           memoryStoreEvictionPolicy="LFU"
+           overflowToDisk="true"
+           diskPersistent="true"
+           />
+
     <!-- This one is used to load single HYK Zones  -->
     <cache name="hykache"
            maxElementsInMemory="500"
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FixingsColumn.java	Thu Apr 05 15:29:25 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FixingsColumn.java	Thu Apr 05 15:29:52 2012 +0000
@@ -9,8 +9,6 @@
 public class FixingsColumn
 implements   Serializable
 {
-    protected int columnId;
-
     protected double [] kms;
     protected double [] ws;
 
@@ -20,22 +18,18 @@
     }
 
     public FixingsColumn(
-        int       columnId, 
-        double [] kms,
-        double [] ws
+        double []  kms,
+        double []  ws,
+        QRangeTree qs
     ) {
-        this.columnId = columnId;
-        this.kms      = kms;
-        this.ws       = ws;
-    }
-
-    public int getColumnId() {
-        return columnId;
+        this.kms = kms;
+        this.ws  = ws;
+        this.qs  = qs;
     }
 
     public boolean getW(double km, double [] w) {
 
-        if (km < kms[0] || km > kms[kms.length-1]) {
+        if (kms.length == 0 || km < kms[0] || km > kms[kms.length-1]) {
             w[0] = Double.NaN;
             return true;
         }
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FixingsColumnFactory.java	Thu Apr 05 15:29:25 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FixingsColumnFactory.java	Thu Apr 05 15:29:52 2012 +0000
@@ -2,17 +2,31 @@
 
 import de.intevation.flys.artifacts.model.FixingsOverview.Fixing;
 
+import de.intevation.flys.artifacts.cache.CacheFactory;
+
+import de.intevation.flys.backend.SessionHolder;
+
+import java.util.List;
+
+import net.sf.ehcache.Cache;
+import net.sf.ehcache.Element;
+
+import org.hibernate.Session;
+import org.hibernate.SQLQuery;
+
+import org.hibernate.type.StandardBasicTypes;
+
 public class FixingsColumnFactory
 {
     public static final String CACHE_NAME = "fixing-columns";
 
-    public static final String SQL_COLUMN_W =
+    public static final String SQL_COLUMN_WS =
         "SELECT wcv.position AS km, wcv.w AS w " +
         "FROM wst_column_values wcv " +
         "WHERE wst_column_id = :column_id " +
         "ORDER by wcv.position";
 
-    public static final String SQL_COLUMN_Q =
+    public static final String SQL_COLUMN_QS =
         "SELECT wqr.q AS q, r.a AS a, r.b AS b " +
         "FROM wst_column_q_ranges wcqr " +
         "JOIN wst_q_ranges wqr ON wcqr.wst_q_range_id = wqr.id " +
@@ -26,8 +40,62 @@
     }
 
     public FixingsColumn getColumnData(Fixing.Column column) {
-        // TODO: Implement me!
-        return null;
+
+        FixingsColumn result;
+
+        Cache cache = CacheFactory.getCache(CACHE_NAME);
+
+        if (cache != null) {
+            Integer cacheKey = Integer.valueOf(column.getId());
+            Element element = cache.get(cacheKey);
+
+            if (element != null) {
+                return (FixingsColumn)element.getValue();
+            }
+
+            result = getUncached(column);
+            cache.put(new Element(cacheKey, result));
+        }
+        else {
+            result = getUncached(column);
+        }
+
+        return result;
+    }
+
+    protected FixingsColumn getUncached(Fixing.Column column) {
+        Session session = SessionHolder.HOLDER.get();
+
+        SQLQuery sqlQuery = session.createSQLQuery(SQL_COLUMN_WS)
+            .addScalar("km", StandardBasicTypes.DOUBLE)
+            .addScalar("w",  StandardBasicTypes.DOUBLE);
+
+        sqlQuery.setInteger("column_id", column.getId());
+
+        List<Object []> results = sqlQuery.list();
+
+        double [] kms = new double[results.size()];
+        double [] ws  = new double[kms.length];
+
+        for (int i = 0; i < kms.length; ++i) {
+            Object [] row = results.get(i);
+            kms[i] = ((Double)row[0]).doubleValue();
+            ws [i] = ((Double)row[1]).doubleValue();
+        }
+
+        sqlQuery = session.createSQLQuery(SQL_COLUMN_QS)
+            .addScalar("q", StandardBasicTypes.DOUBLE)
+            .addScalar("a", StandardBasicTypes.DOUBLE)
+            .addScalar("b", StandardBasicTypes.DOUBLE);
+
+        sqlQuery.setInteger("column_id", column.getId());
+
+        results = sqlQuery.list();
+
+        QRangeTree qs = new QRangeTree(
+            results, QRangeTree.WITHOUT_COLUMN, 0, results.size());
+
+        return new FixingsColumn(kms, ws, qs);
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org