# HG changeset patch # User Sascha L. Teichmann # Date 1301303845 0 # Node ID 4eddbb219866d72469e29d57b5279513b46c66b0 # Parent c904d52cdfd01a833af0e3f2d65a83bab3325a13 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 diff -r c904d52cdfd0 -r 4eddbb219866 flys-artifacts/ChangeLog --- 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 + + * 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 * src/main/java/de/intevation/flys/artifacts/model/DischargeTables.java: diff -r c904d52cdfd0 -r 4eddbb219866 flys-artifacts/pom.xml --- 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 @@ + jfree + jfreechart + 1.0.13 + + junit junit 3.8.1 diff -r c904d52cdfd0 -r 4eddbb219866 flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/GaugesFactory.java --- /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 getGauges(River river) { + return getGauges(river.getName()); + } + + public static List 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 filterRanges( + List gauges, + List ranges + ) { + // XXX: Inefficent! + ArrayList rs = new ArrayList(); + 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 filter(List gauges, List ranges) { + // TODO: Make it an HQL filter! + ArrayList out = new ArrayList(); + 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 :