Mercurial > dive4elements > river
changeset 146:e8dfe6ba2000
Fetch w/q values of discharge tables for given gauges.
flys-artifacts/trunk@1562 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 24 Mar 2011 17:29:08 +0000 |
parents | d9e4b7e8905a |
children | 76cc1a66a05f |
files | flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/DischargeTables.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/RiverFactory.java |
diffstat | 3 files changed, 114 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog Thu Mar 24 17:06:08 2011 +0000 +++ b/flys-artifacts/ChangeLog Thu Mar 24 17:29:08 2011 +0000 @@ -1,3 +1,12 @@ +2011-03-24 Sascha L. Teichmann <sascha.teichmann@intevation.de> + + * src/main/java/de/intevation/flys/artifacts/model/DischargeTables.java: + New. Fetches values of discharge tables in form of packed + w/q double arrays for given gauges. + + * src/main/java/de/intevation/flys/artifacts/model/RiverFactory.java: + Removed needless import. + 2011-03-24 Ingo Weinzierl <ingo@intevation.de> * src/main/resources/messages_de_DE.properties: Added a german resource
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/DischargeTables.java Thu Mar 24 17:29:08 2011 +0000 @@ -0,0 +1,105 @@ +package de.intevation.flys.artifacts.model; + +import java.util.List; +import java.util.Map; +import java.util.HashMap; +import java.util.Arrays; + +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; + +public class DischargeTables +implements Serializable +{ + private static Logger log = Logger.getLogger(DischargeTables.class); + + protected List<String> gaugeNames; + + protected String riverName; + + protected Map<String, double []> values; + + public DischargeTables() { + } + + public DischargeTables(String riverName, String [] gaugeNames) { + this(riverName, Arrays.asList(gaugeNames)); + } + + public DischargeTables(String riverName, List<String> gaugeNames) { + this.riverName = riverName; + this.gaugeNames = gaugeNames; + } + + public Map<String, double []> getValues() { + if (values == null) { + values = loadValues(); + } + return values; + } + + protected Map<String, double []> loadValues() { + 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(); + + if (tables.isEmpty()) { + log.warn( + "no discharge table for gauge '" + gaugeName + "'"); + continue; + } + + // TODO: Filter by time interval + DischargeTable table = tables.get(0); + + List<DischargeTableValue> dtvs = + table.getDischargeTableValues(); + + double [] vs = new double[dtvs.size() << 1]; + + int idx = 0; + for (DischargeTableValue dtv: dtvs) { + vs[idx++] = dtv.getW().doubleValue(); + vs[idx++] = dtv.getQ().doubleValue(); + } + + values.put(gaugeName, vs); + } + + return values; + } + finally { + session.close(); + } + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/RiverFactory.java Thu Mar 24 17:06:08 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/RiverFactory.java Thu Mar 24 17:29:08 2011 +0000 @@ -9,7 +9,6 @@ import org.hibernate.SessionFactory; import org.hibernate.Session; -import org.hibernate.Query; /** * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>