changeset 468:8d76556c9616

Added methods to retrieve the min and max W and Q values of a Wst and Gauge. flys-backend/trunk@1705 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 15 Apr 2011 13:45:06 +0000
parents c8c09e31cdb8
children b5ca22aae092
files flys-backend/ChangeLog flys-backend/src/main/java/de/intevation/flys/model/Gauge.java flys-backend/src/main/java/de/intevation/flys/model/Wst.java
diffstat 3 files changed, 112 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/ChangeLog	Fri Apr 15 10:31:15 2011 +0000
+++ b/flys-backend/ChangeLog	Fri Apr 15 13:45:06 2011 +0000
@@ -1,3 +1,11 @@
+2011-04-15  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/model/Wst.java: A Wst is now able to
+	  return its min and max Q values.
+
+	* src/main/java/de/intevation/flys/model/Gauge.java: A Gauge is now able
+	  to return its min and max W values.
+
 2011-04-15	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* doc/schema/postgresql.sql: Added new column 'kind' in
--- a/flys-backend/src/main/java/de/intevation/flys/model/Gauge.java	Fri Apr 15 10:31:15 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/model/Gauge.java	Fri Apr 15 13:45:06 2011 +0000
@@ -17,6 +17,12 @@
 import javax.persistence.OneToOne;
 import javax.persistence.OneToMany;
 
+import org.hibernate.Session;
+import org.hibernate.Query;
+
+import de.intevation.flys.backend.SessionHolder;
+
+
 @Entity
 @Table(name = "gauges")
 public class Gauge
@@ -133,5 +139,43 @@
     public void setDischargeTables(List<DischargeTable> dischargeTables) {
         this.dischargeTables = dischargeTables;
     }
+
+
+    /**
+     * Returns min and max W values of this gauge.
+     *
+     *  @return the min and max W value of this gauge [min,max].
+     */
+    public double[] determineMinMaxW() {
+        Session session = SessionHolder.HOLDER.get();
+
+        List<DischargeTable> tables   = getDischargeTables();
+        DischargeTable dischargeTable = null;
+
+        for (DischargeTable tmp: tables) {
+            if (tmp.getKind() == 0) {
+                dischargeTable = tmp;
+                break;
+            }
+        }
+
+        if (dischargeTable == null) {
+            return null;
+        }
+
+        Query query  = session.createQuery(
+            "select min(w) as min, max(w) as max from DischargeTableValue " +
+            "where table_id =:table");
+        query.setParameter("table", dischargeTable.getId());
+
+        List     results = query.list();
+        Object[] result  = (Object[]) results.get(0);
+
+        return result != null
+            ? new double[] {
+                ((BigDecimal) result[0]).doubleValue(),
+                ((BigDecimal) result[1]).doubleValue() }
+            : null;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-backend/src/main/java/de/intevation/flys/model/Wst.java	Fri Apr 15 10:31:15 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/model/Wst.java	Fri Apr 15 13:45:06 2011 +0000
@@ -1,6 +1,8 @@
 package de.intevation.flys.model;
 
 import java.io.Serializable;
+import java.math.BigDecimal;
+import java.util.List;
 
 import javax.persistence.Entity;
 import javax.persistence.Id;
@@ -12,11 +14,21 @@
 import javax.persistence.JoinColumn;
 import javax.persistence.OneToOne;
 
+import org.apache.log4j.Logger;
+
+import org.hibernate.Session;
+import org.hibernate.Query;
+
+import de.intevation.flys.backend.SessionHolder;
+
+
 @Entity
 @Table(name = "wsts")
 public class Wst
 implements   Serializable
 {
+    private static Logger logger = Logger.getLogger(Wst.class);
+
     private Integer id;
     private River   river;
     private String  description;
@@ -79,5 +91,53 @@
     public void setKind(Integer kind) {
         this.kind = kind;
     }
+
+
+    /**
+     * Determines the min and max Q values of this WST. The min value is placed
+     * in the first field of the resulting array - the max value is placed in
+     * the second field.
+     *
+     * @return the min and max Q values of this WST.
+     */
+    public double[] determineMinMaxQ() {
+        double[] ab = river.determineMinMaxDistance();
+        return determineMinMaxQ(new Range(ab[0], ab[1], river));
+    }
+
+
+    /**
+     * Determines the min and max Q values of this WST in the given range. The
+     * min value is placed in the first field of the resulting array - the max
+     * value is placed in the second field.
+     *
+     * @param range The range used for querying the Q values.
+     *
+     * @return the min and max Q values of this WST.
+     */
+    public double[] determineMinMaxQ(Range range) {
+        Session session = SessionHolder.HOLDER.get();
+
+        Query query = session.createQuery(
+          "select min(q), max(q) from WstQRange where " +
+          " id in " +
+          "  (select wstQRange.id from WstColumnQRange where " +
+          "    wstColumn.id in (select id from WstColumn where wst.id = :wst)) " +
+          " and range.id in " +
+          "  (select id from Range where not (a > :end or b < :start))");
+
+        query.setParameter("wst", getId());
+        query.setParameter("start", range.getA());
+        query.setParameter("end", range.getB());
+
+        List     results = query.list();
+        Object[] result  = (Object[]) results.get(0);
+
+        return result != null
+            ? new double[] {
+                ((BigDecimal) result[0]).doubleValue(),
+                ((BigDecimal) result[1]).doubleValue() }
+            : null;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org