diff backend/src/main/java/org/dive4elements/river/model/CrossSection.java @ 6538:de62db0f2035

issue1157: Let CrossSection be able to find out whether it should be active & master.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Thu, 04 Jul 2013 11:52:06 +0200
parents c155f8f57b7e
children 7e697c6eb379
line wrap: on
line diff
--- a/backend/src/main/java/org/dive4elements/river/model/CrossSection.java	Thu Jul 04 11:48:33 2013 +0200
+++ b/backend/src/main/java/org/dive4elements/river/model/CrossSection.java	Thu Jul 04 11:52:06 2013 +0200
@@ -38,11 +38,16 @@
 
 import org.dive4elements.river.backend.SessionHolder;
 
+import org.apache.log4j.Logger;
+
 @Entity
 @Table(name = "cross_sections")
 public class CrossSection
 implements   Serializable
 {
+    private static Logger logger =
+        Logger.getLogger(CrossSection.class);
+
     public static final MathContext PRECISION = new MathContext(6);
 
     public static final String SQL_FAST_CROSS_SECTION_LINES =
@@ -60,7 +65,11 @@
             "WHERE cross_section_id IN " +
             " (SELECT id FROM cross_sections WHERE river_id = :river_id) " +
             "  GROUP BY cross_section_id" +
-        ") AS cs_ranges WHERE :km BETWEEN minkm AND maxkm ORDER BY :km - minkm "; 
+        ") AS cs_ranges " +
+        "JOIN cross_sections as CS on cs_ranges.cross_section_id = cs.id " +
+        "LEFT OUTER JOIN time_intervals on cs.time_interval_id = time_intervals.id " +
+        "WHERE :km BETWEEN minkm AND maxkm " +
+        "ORDER BY stop_time desc, start_time asc, :km - minkm";
     // Order by time interval missing.
 
     private Integer                id;
@@ -213,5 +222,31 @@
 
         return lines;
     }
+
+    /**
+     * True if the given section is the "newest" for that river and has values at km.
+     * @param km Given station.
+     * @return true if the section has the most advanced end of its validity interval
+     *         or the most advanced start of its validity interval.
+     */
+    public boolean shouldBeMaster(double km) {
+        Session session = SessionHolder.HOLDER.get();
+
+        SQLQuery sqlQuery = session.createSQLQuery(SQL_MIN_MAX)
+            .addScalar("cross_section_id", StandardBasicTypes.INTEGER);
+
+        sqlQuery
+            .setInteger("river_id", getRiver().getId())
+            .setDouble("km", km);
+
+        List<Integer> results = sqlQuery.list();
+
+        for (Integer result: results) {
+            if (result == getId()) {
+                return true;
+            }
+        }
+        return false;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org