changeset 2155:8c9c40459d8f

Respect rivers flow direction when looking for hyk-formations. flys-artifacts/trunk@3738 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Fri, 20 Jan 2012 09:38:43 +0000
parents ce9b15673f3f
children 4e0878695c5f
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/HYKFactory.java
diffstat 2 files changed, 29 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Fri Jan 20 09:18:56 2012 +0000
+++ b/flys-artifacts/ChangeLog	Fri Jan 20 09:38:43 2012 +0000
@@ -1,3 +1,9 @@
+2012-01-20  Felix Wolfsteller	<felix.wolfsteller@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/model/HYKFactory.java:
+	  Respect flowing direction of river when finding valid
+	  hykformations.
+
 2012-01-20  Raimund Renkert <raimund.renkert@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/states/WaterlevelState.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/HYKFactory.java	Fri Jan 20 09:18:56 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/HYKFactory.java	Fri Jan 20 09:38:43 2012 +0000
@@ -15,6 +15,7 @@
 import org.hibernate.Query;
 import org.hibernate.Session;
 
+import de.intevation.flys.model.HYK;
 import de.intevation.flys.model.HYKFormation;
 import de.intevation.flys.model.HYKFlowZone;
 
@@ -72,13 +73,12 @@
     }
 
 
+    /** Return name for hyk with given id. */
     public static String getHykName(int hykid) {
         logger.debug("HYKFactory.getHykName " + hykid);
 
         Session session = SessionHolder.HOLDER.get();
 
-        // TODO respect interval better. respect multiples (sort, limit),
-        // TODO respect flow direction of river.
         Query query = session.createQuery(
             "select description from HYK where id = :hykid ");
         query.setParameter("hykid", hykid);
@@ -88,13 +88,11 @@
 
 
     /**
-     * 
-     * @param column the position columns value
-     * @param wst_id database id of the wst
-     * @return according WKms.
+     * Ask DB for hyk zones.
+     * @param hykid ID of the 'main' HYK to query.
+     * @param km for which to get the hyk-zones.
+     * @return according zones.
      */
-    // TODO we also need to know the HYK-id to specify which set we are
-    //      inspecting.
     public static List<Zone> getZonesUncached(int hykid, double km) {
 
         if (logger.isDebugEnabled()) {
@@ -103,20 +101,30 @@
 
         Session session = SessionHolder.HOLDER.get();
 
-        // TODO respect interval better. respect multiples (sort, limit),
-        // TODO respect flow direction of river.
+        // Find out flow-direction of river.
+        // OPTIMIZE: 1) query kmUp directly 2) merge queries.
+        Query rQuery = session.createQuery("from HYK where id = :hykid");
+        rQuery.setParameter("hykid", hykid);
+        rQuery.setMaxResults(1);
+        HYK hyk = (HYK) rQuery.uniqueResult();
+        BigDecimal flowDir = (hyk.getRiver().getKmUp()) ? new BigDecimal(1) : new BigDecimal(-1);
+
+        // Query HYKFormations in range.
         Query query = session.createQuery(
             "from HYKFormation where entry.HYK.id = :hykid " +
-            " and :km between entry.km - cast(distance_vl/1000.0 + 0.001 as big_decimal) and " +
-            "                 entry.km + cast(distance_vl/1000.0 + 0.001 as big_decimal)" +
+            " and :km between entry.km and " +
+            "                 entry.km + :flowDir * "+
+            "                     cast(distance_vl/1000.0 + 0.001 as big_decimal)" +
             " order by entry.km asc");
-        query.setParameter("hykid", hykid);
-        query.setParameter("km", new BigDecimal(km));
+        query.setParameter("hykid",   hykid);
+        query.setParameter("km",      new BigDecimal(km));
+        query.setParameter("flowDir", flowDir);
+        query.setMaxResults(1);
 
         List<HYKFormation> forms = query.list();
 
         List<Zone> zones = new ArrayList<Zone>();
-        // TODO limit query by one; sensible sorting.
+
         // Take the first one.
         if (forms.size() >= 1) {
             HYKFormation form = forms.get(0);

http://dive4elements.wald.intevation.org