# HG changeset patch # User Sascha L. Teichmann # Date 1300987748 0 # Node ID e8dfe6ba2000427e40c328abdf4665dc803ff4ab # Parent d9e4b7e8905af66bafbb3a73cc2aa6bb4f3e48d4 Fetch w/q values of discharge tables for given gauges. flys-artifacts/trunk@1562 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r d9e4b7e8905a -r e8dfe6ba2000 flys-artifacts/ChangeLog --- 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 + + * 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 * src/main/resources/messages_de_DE.properties: Added a german resource diff -r d9e4b7e8905a -r e8dfe6ba2000 flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/DischargeTables.java --- /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 gaugeNames; + + protected String riverName; + + protected Map values; + + public DischargeTables() { + } + + public DischargeTables(String riverName, String [] gaugeNames) { + this(riverName, Arrays.asList(gaugeNames)); + } + + public DischargeTables(String riverName, List gaugeNames) { + this.riverName = riverName; + this.gaugeNames = gaugeNames; + } + + public Map getValues() { + if (values == null) { + values = loadValues(); + } + return values; + } + + protected Map loadValues() { + Map values = new HashMap(); + + 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 gauges = gaugeQuery.list(); + if (gauges.isEmpty()) { + log.warn( + "no gauge '"+gaugeName+"' at river '"+riverName+"'"); + continue; + } + Gauge gauge = gauges.get(0); + + List 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 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 : diff -r d9e4b7e8905a -r e8dfe6ba2000 flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/RiverFactory.java --- 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 Ingo Weinzierl