Mercurial > dive4elements > river
view flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/DischargeTables.java @ 147:76cc1a66a05f
Implemented a FLYSArtifactCollection that will be used in this application now.
flys-artifacts/trunk@1565 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Thu, 24 Mar 2011 18:47:07 +0000 |
parents | e8dfe6ba2000 |
children | c904d52cdfd0 |
line wrap: on
line source
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 :