changeset 5500:700ac898ab0b

Added Factory and model for static sq relations.
author Raimund Renkert <rrenkert@intevation.de>
date Thu, 28 Mar 2013 15:15:33 +0100
parents 43bf4976dd24
children 9a14eac637ec
files flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/StaticSQCacheKey.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/StaticSQContainer.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/StaticSQFactory.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/StaticSQRelation.java
diffstat 4 files changed, 341 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/StaticSQCacheKey.java	Thu Mar 28 15:15:33 2013 +0100
@@ -0,0 +1,30 @@
+package de.intevation.flys.artifacts.model.sq;
+
+import java.io.Serializable;
+
+
+public class StaticSQCacheKey
+implements Serializable
+{
+    public static final String CACHE_NAME = "static-sq-relation";
+
+    private String river;
+    private int measurementId;
+
+    public StaticSQCacheKey(String river, int measurementId) {
+        this.river = river;
+        this.measurementId = measurementId;
+    }
+
+    public int hashCode() {
+        return this.river.hashCode() | measurementId;
+    }
+
+    public boolean equals(Object other) {
+        if (!(other instanceof StaticSQCacheKey)) {
+            return false;
+        }
+        StaticSQCacheKey o = (StaticSQCacheKey) other;
+        return this.river == o.river && this.measurementId == o.measurementId;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/StaticSQContainer.java	Thu Mar 28 15:15:33 2013 +0100
@@ -0,0 +1,85 @@
+package de.intevation.flys.artifacts.model.sq;
+
+import java.util.ArrayList;
+import java.util.List;
+
+
+public class StaticSQContainer
+{
+    private String description;
+    private String stationName;
+    private double km;
+
+    private List<StaticSQRelation> relations;
+
+
+    public StaticSQContainer() {
+        relations = new ArrayList<StaticSQRelation>();
+    }
+
+    public StaticSQContainer(
+        String stationName,
+        String description,
+        double km
+    ) {
+        this.stationName = stationName;
+        this.description = description;
+        this.km = km;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public void setDescription(String description) {
+        this.description = description;
+    }
+
+    public String getStationName() {
+        return stationName;
+    }
+
+    public void setStationName(String stationName) {
+        this.stationName = stationName;
+    }
+
+    public double getKm() {
+        return km;
+    }
+
+    public void setKm(double km) {
+        this.km = km;
+    }
+
+    public List<StaticSQRelation> getSQRelations() {
+        return relations;
+    }
+
+    public void setSQRelations(List<StaticSQRelation> relations) {
+        this.relations = relations;
+    }
+
+    public void addSQRelation(StaticSQRelation relation) {
+        this.relations.add(relation);
+    }
+
+    public StaticSQRelation getSQRelation(int ndx) {
+        return this.relations.get(ndx);
+    }
+
+    public int size() {
+        return this.relations.size();
+    }
+
+    public List<StaticSQRelation> getRelationsByParameter(
+        StaticSQRelation.Parameter parameter
+    ) {
+        List<StaticSQRelation> result = new ArrayList<StaticSQRelation>();
+        for (StaticSQRelation relation : relations) {
+            if (relation.getParameter() == parameter) {
+                result.add(relation);
+            }
+        }
+        return result;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/StaticSQFactory.java	Thu Mar 28 15:15:33 2013 +0100
@@ -0,0 +1,123 @@
+package de.intevation.flys.artifacts.model.sq;
+
+import java.math.BigDecimal;
+import java.util.Date;
+import java.util.List;
+
+import net.sf.ehcache.Cache;
+import net.sf.ehcache.Element;
+
+import org.apache.log4j.Logger;
+import org.hibernate.Query;
+import org.hibernate.Session;
+
+import de.intevation.flys.artifacts.cache.CacheFactory;
+import de.intevation.flys.backend.SessionHolder;
+
+
+public class StaticSQFactory
+{
+    private static final Logger log =
+        Logger.getLogger(StaticSQFactory.class);
+
+    public static final String SQL_SQ =
+        "SELECT " +
+            "sq.description AS description,"+
+            "ti.start_time  AS start_time," +
+            "ti.stop_time    AS stop_time, " +
+            "ms.name AS station_name, " +
+            "ms.station AS station_km, " +
+            "ms.measurement_type AS measurement_type, " +
+            "sqv.parameter AS parameter, " +
+            "sqv.a AS a, " +
+            "sqv.b AS b, " +
+            "sqv.qmax AS qmax " +
+        "FROM sq_relation sq " +
+            "JOIN time_intervals ti ON ti.id   = sq.time_interval_id " +
+            "JOIN rivers r ON r.id = sq.river_id " +
+            "JOIN sq_relation_value sqv ON sqv.sq_relation_id = sq.id " +
+            "JOIN measurement_station ms ON sqv.measurement_station_id = ms.id " +
+        "WHERE " +
+            "r.name = :river " +
+            "AND ms.id = :ms_id ";
+
+
+    private StaticSQFactory() {
+    }
+
+    public static StaticSQContainer getSQRelations(
+        String river,
+        int measurementStation
+    ) {
+        Cache cache = CacheFactory.getCache(StaticSQCacheKey.CACHE_NAME);
+
+        StaticSQCacheKey cacheKey;
+
+        if (cache != null) {
+            cacheKey = new StaticSQCacheKey(river, measurementStation);
+            Element element = cache.get(cacheKey);
+            if (element != null) {
+                log.debug("Got static bedheight values from cache");
+                return (StaticSQContainer)element.getValue();
+            }
+        }
+        else {
+            cacheKey = null;
+        }
+
+        StaticSQContainer values = getUncached(river, measurementStation);
+
+        if (values != null && cacheKey != null) {
+            log.debug("Store static sq relations in cache.");
+            Element element = new Element(cacheKey, values);
+            cache.put(element);
+        }
+        return values;
+    }
+
+    private static StaticSQContainer getUncached(
+        String river,
+        int measurementStation
+    ) {
+        Session session = SessionHolder.HOLDER.get();
+
+        Query query = session.createSQLQuery(SQL_SQ)
+            .addScalar("description")
+            .addScalar("start_time")
+            .addScalar("stop_time")
+            .addScalar("station_name")
+            .addScalar("station_km")
+            .addScalar("measurement_type")
+            .addScalar("parameter")
+            .addScalar("a")
+            .addScalar("b")
+            .addScalar("qmax");
+
+        query.setParameter("river", river);
+        query.setParameter("ms_id", measurementStation);
+
+        List<Object []> list = query.list();
+
+        if (list.isEmpty()) {
+            return new StaticSQContainer();
+        }
+
+        StaticSQContainer sq = new StaticSQContainer();
+        sq.setDescription((String)list.get(0)[0]);
+        sq.setStationName((String)list.get(0)[3]);
+        sq.setKm(((BigDecimal)list.get(0)[4]).doubleValue());
+
+        for (Object[] row : list) {
+            StaticSQRelation relation = new StaticSQRelation();
+            relation.setStartTime((Date)row[1]);
+            relation.setStopTime((Date)row[2]);
+            relation.setType((String)row[5]);
+            relation.setParameter((String)row[6]);
+            relation.setA(((BigDecimal)row[7]).doubleValue());
+            relation.setB(((BigDecimal)row[8]).doubleValue());
+            relation.setQmax(((BigDecimal)row[9]).doubleValue());
+            sq.addSQRelation(relation);
+        }
+        return sq;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/StaticSQRelation.java	Thu Mar 28 15:15:33 2013 +0100
@@ -0,0 +1,103 @@
+package de.intevation.flys.artifacts.model.sq;
+
+import java.io.Serializable;
+import java.util.Date;
+
+
+public class StaticSQRelation implements Serializable{
+
+    private Date startTime;
+    private Date stopTime;
+    private String type;
+    private Parameter parameter;
+    private double a;
+    private double b;
+    private double qmax;
+
+    public static enum Parameter {
+        A, B, C, D, E, F
+    }
+
+
+    public StaticSQRelation() {
+    }
+
+    public StaticSQRelation(
+        Date startTime,
+        Date stopTime,
+        String type,
+        Parameter parameter,
+        double a,
+        double b
+    ) {
+        this.startTime = startTime;
+        this.stopTime = stopTime;
+        this.type = type;
+        this.parameter = parameter;
+        this.a = a;
+        this.b = b;
+    }
+
+    public Date getStartTime() {
+        return startTime;
+    }
+
+    public void setStartTime(Date startTime) {
+        this.startTime = startTime;
+    }
+
+    public Date getStopTime() {
+        return stopTime;
+    }
+
+    public void setStopTime(Date stopTime) {
+        this.stopTime = stopTime;
+    }
+
+    public String getType() {
+        return type;
+    }
+
+    public void setType(String type) {
+        this.type = type;
+    }
+
+    public Parameter getParameter() {
+        return parameter;
+    }
+
+    public void setParameter(Parameter parameter) {
+        this.parameter = parameter;
+    }
+
+    public void setParameter(String parameter) {
+        if (parameter == null) {
+            return;
+        }
+        this.parameter = Parameter.valueOf(parameter);
+    }
+
+    public double getA() {
+        return a;
+    }
+
+    public void setA(double a) {
+        this.a = a;
+    }
+
+    public double getB() {
+        return b;
+    }
+
+    public void setB(double b) {
+        this.b = b;
+    }
+
+    public double getQmax() {
+        return qmax;
+    }
+
+    public void setQmax(double qmax) {
+        this.qmax = qmax;
+    }
+}

http://dive4elements.wald.intevation.org