changeset 311:ea3a438a7021

Backend access: Static methods are using the SessionHolder, too. flys-artifacts/trunk@1694 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 14 Apr 2011 13:15:16 +0000
parents 6d2c8915e22c
children 0c6328c227f1
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/AnnotationsFactory.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/DischargeTables.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/GaugesFactory.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/RiverFactory.java
diffstat 5 files changed, 59 insertions(+), 81 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Thu Apr 14 13:11:12 2011 +0000
+++ b/flys-artifacts/ChangeLog	Thu Apr 14 13:15:16 2011 +0000
@@ -1,3 +1,11 @@
+2011-04-14	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/model/RiverFactory.java,
+	  src/main/java/de/intevation/flys/artifacts/model/GaugesFactory.java,
+	  src/main/java/de/intevation/flys/artifacts/model/DischargeTables.java,
+	  src/main/java/de/intevation/flys/artifacts/model/AnnotationsFactory.java:
+	  Static methods are using the SessionHolder, too.
+
 2011-04-14  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/context/SessionCallContextListener.java:
@@ -17,6 +25,7 @@
 
 	* src/main/java/de/intevation/flys/artifacts/model/SessionHolder.java:
 	  New. Contains thread local session holder for hibernate sessions.
+
 	* src/main/java/de/intevation/flys/artifacts/model/RiverFactory.java:
 	  Uses session from SessionHolder.
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/AnnotationsFactory.java	Thu Apr 14 13:11:12 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/AnnotationsFactory.java	Thu Apr 14 13:15:16 2011 +0000
@@ -2,17 +2,13 @@
 
 import java.util.List;
 
-import de.intevation.flys.backend.SessionFactoryProvider;
-
 import de.intevation.flys.model.Annotation;
 import de.intevation.flys.model.Range;
 import de.intevation.flys.model.River;
 
-import org.hibernate.SessionFactory;
 import org.hibernate.Session;
 import org.hibernate.Query;
 
-
 /**
  * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
  */
@@ -24,23 +20,17 @@
 
 
     public static List<Annotation> getAnnotations(String river) {
-        SessionFactory sf = SessionFactoryProvider.getSessionFactory();
-        Session session   = sf.openSession();
+        Session session = SessionHolder.HOLDER.get();
 
-        try {
-            Query rangesQuery = session.createQuery(
-                "from Range where river.name=:name");
-            rangesQuery.setParameter("name", river);
-            List<Range> ranges = rangesQuery.list();
+        Query rangesQuery = session.createQuery(
+            "from Range where river.name=:name");
+        rangesQuery.setParameter("name", river);
+        List<Range> ranges = rangesQuery.list();
 
-            Query query = session.createQuery(
-                "from Annotation where range in (:ranges)");
-            query.setParameterList("ranges", ranges);
-            return query.list();
-        }
-        finally {
-            session.close();
-        }
+        Query query = session.createQuery(
+            "from Annotation where range in (:ranges)");
+        query.setParameterList("ranges", ranges);
+        return query.list();
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/DischargeTables.java	Thu Apr 14 13:11:12 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/DischargeTables.java	Thu Apr 14 13:15:16 2011 +0000
@@ -7,14 +7,11 @@
 
 import java.io.Serializable;
 
-import org.hibernate.SessionFactory;
 import org.hibernate.Session;
 import org.hibernate.Query;
 
 import org.apache.log4j.Logger;
 
-import de.intevation.flys.backend.SessionFactoryProvider;
-
 import de.intevation.flys.model.Gauge;
 import de.intevation.flys.model.DischargeTable;
 import de.intevation.flys.model.DischargeTableValue;
@@ -56,55 +53,49 @@
     protected Map<String, double [][]> loadValues(double scale) {
         Map<String, double [][]> values = new HashMap<String, double [][]>();
 
-        SessionFactory sf = SessionFactoryProvider.getSessionFactory();
-        Session session = sf.openSession();
-
-        try {
-            Query gaugeQuery = session.createQuery(
-                "from Gauge where name=:gauge and river.name=:river");
-            gaugeQuery.setParameter("river", riverName);
-
-            for (String gaugeName: gaugeNames) {
-                gaugeQuery.setParameter("gauge", gaugeName);
-                List<Gauge> gauges = gaugeQuery.list();
-                if (gauges.isEmpty()) {
-                    log.warn(
-                        "no gauge '"+gaugeName+"' at river '"+riverName+"'");
-                    continue;
-                }
-                Gauge gauge = gauges.get(0);
-
-                List<DischargeTable> tables = gauge.getDischargeTables();
+        Session session = SessionHolder.HOLDER.get();
 
-                if (tables.isEmpty()) {
-                    log.warn(
-                        "no discharge table for gauge '" + gaugeName + "'");
-                    continue;
-                }
-
-                // TODO: Filter by time interval
-                DischargeTable table = tables.get(0);
+        Query gaugeQuery = session.createQuery(
+            "from Gauge where name=:gauge and river.name=:river");
+        gaugeQuery.setParameter("river", riverName);
 
-                List<DischargeTableValue> dtvs =
-                    table.getDischargeTableValues();
-
-                double [][] vs = new double[2][dtvs.size()];
+        for (String gaugeName: gaugeNames) {
+            gaugeQuery.setParameter("gauge", gaugeName);
+            List<Gauge> gauges = gaugeQuery.list();
+            if (gauges.isEmpty()) {
+                log.warn(
+                    "no gauge '"+gaugeName+"' at river '"+riverName+"'");
+                continue;
+            }
+            Gauge gauge = gauges.get(0);
 
-                int idx = 0;
-                for (DischargeTableValue dtv: dtvs) {
-                    vs[0][idx] = dtv.getQ().doubleValue() * scale;
-                    vs[1][idx] = dtv.getW().doubleValue() * scale;
-                    ++idx;
-                }
+            List<DischargeTable> tables = gauge.getDischargeTables();
 
-                values.put(gaugeName, vs);
+            if (tables.isEmpty()) {
+                log.warn(
+                    "no discharge table for gauge '" + gaugeName + "'");
+                continue;
             }
 
-            return values;
+            // TODO: Filter by time interval
+            DischargeTable table = tables.get(0);
+
+            List<DischargeTableValue> dtvs =
+                table.getDischargeTableValues();
+
+            double [][] vs = new double[2][dtvs.size()];
+
+            int idx = 0;
+            for (DischargeTableValue dtv: dtvs) {
+                vs[0][idx] = dtv.getQ().doubleValue() * scale;
+                vs[1][idx] = dtv.getW().doubleValue() * scale;
+                ++idx;
+            }
+
+            values.put(gaugeName, vs);
         }
-        finally {
-            session.close();
-        }
+
+        return values;
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/GaugesFactory.java	Thu Apr 14 13:11:12 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/GaugesFactory.java	Thu Apr 14 13:15:16 2011 +0000
@@ -3,13 +3,10 @@
 import java.util.List;
 import java.util.ArrayList;
 
-import de.intevation.flys.backend.SessionFactoryProvider;
-
 import de.intevation.flys.model.River;
 import de.intevation.flys.model.Gauge;
 import de.intevation.flys.model.Range;
 
-import org.hibernate.SessionFactory;
 import org.hibernate.Session;
 import org.hibernate.Query;
 
@@ -20,17 +17,11 @@
     }
 
     public static List<Gauge> getGauges(String river) {
-        SessionFactory sf = SessionFactoryProvider.getSessionFactory();
-        Session session = sf.openSession();
-        try {
-            Query query = session.createQuery(
-                "from Gauge where river.name=:name");
-            query.setParameter("name", river);
-            return query.list();
-        }
-        finally {
-            session.close();
-        }
+        Session session = SessionHolder.HOLDER.get();
+        Query query = session.createQuery(
+            "from Gauge where river.name=:name");
+        query.setParameter("name", river);
+        return query.list();
     }
 
     public static List<Gauge> filterRanges(
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/RiverFactory.java	Thu Apr 14 13:11:12 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/RiverFactory.java	Thu Apr 14 13:15:16 2011 +0000
@@ -5,9 +5,6 @@
 
 import de.intevation.flys.model.River;
 
-import de.intevation.flys.backend.SessionFactoryProvider;
-
-import org.hibernate.SessionFactory;
 import org.hibernate.Session;
 
 /**

http://dive4elements.wald.intevation.org