changeset 151:4eddbb219866

Add factory to fetch gauges from database and filter them for their ranges. XXX: Coded inefficent. flys-artifacts/trunk@1581 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 28 Mar 2011 09:17:25 +0000
parents c904d52cdfd0
children 956445dd601d
files flys-artifacts/ChangeLog flys-artifacts/pom.xml flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/GaugesFactory.java
diffstat 3 files changed, 76 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Fri Mar 25 13:51:09 2011 +0000
+++ b/flys-artifacts/ChangeLog	Mon Mar 28 09:17:25 2011 +0000
@@ -1,3 +1,9 @@
+2011-03-28	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/model/GaugesFactory.java:
+	  New. Load gauges for a river and filter them for given
+	  ranges.
+
 2011-03-25	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/model/DischargeTables.java:
--- a/flys-artifacts/pom.xml	Fri Mar 25 13:51:09 2011 +0000
+++ b/flys-artifacts/pom.xml	Mon Mar 28 09:17:25 2011 +0000
@@ -30,6 +30,11 @@
 
   <dependencies>
     <dependency>
+      <groupId>jfree</groupId>
+      <artifactId>jfreechart</artifactId>
+      <version>1.0.13</version>
+    </dependency>
+    <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>3.8.1</version>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/GaugesFactory.java	Mon Mar 28 09:17:25 2011 +0000
@@ -0,0 +1,65 @@
+package de.intevation.flys.artifacts.model;
+
+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;
+
+public class GaugesFactory
+{
+    public static List<Gauge> getGauges(River river) {
+        return getGauges(river.getName());
+    }
+
+    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();
+        }
+    }
+
+    public static List<Gauge> filterRanges(
+        List<Gauge>     gauges,
+        List<double []> ranges
+    ) {
+        // XXX: Inefficent!
+        ArrayList<Range> rs = new ArrayList<Range>();
+        for (double [] range: ranges) {
+            double a = range[0];
+            double b = range[1];
+            rs.add(new Range(Math.min(a, b), Math.max(a, b), null));
+        }
+        return filter(gauges, rs);
+    }
+
+    public static List<Gauge> filter(List<Gauge> gauges, List<Range> ranges) {
+        // TODO: Make it an HQL filter!
+        ArrayList<Gauge> out = new ArrayList<Gauge>();
+        for (Gauge gauge: gauges) {
+            Range range = gauge.getRange();
+            for (Range cmp: ranges) {
+                if (range.intersects(cmp)) {
+                    out.add(gauge);
+                    break;
+                }
+            }
+        }
+        return out;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org