changeset 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 (2013-07-04)
parents f2722602936c
children 4c463ca9b36e
files artifacts/src/main/java/org/dive4elements/river/artifacts/model/CrossSectionFactory.java backend/src/main/java/org/dive4elements/river/model/CrossSection.java
diffstat 2 files changed, 36 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/CrossSectionFactory.java	Thu Jul 04 11:48:33 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/CrossSectionFactory.java	Thu Jul 04 11:52:06 2013 +0200
@@ -59,30 +59,6 @@
     }
 
 
-    /**
-     * True if the given section is the "newest" for that river.
-     * @param section Given section
-     * @return true if the section has the most advanced end of its validity interval
-     *         or the most advanced start of its validity interval.
-     */
-    public static boolean isNewest(CrossSection section) {
-        Session session = SessionHolder.HOLDER.get();
-        Query query = session.createQuery(
-            "from CrossSection where river.id = :riverid "
-            + " order by timeInterval.stopTime desc, timeInterval.startTime desc");
-        query.setParameter("riverid", section.getRiver().getId());
-
-        List result = query.list();
-
-        if (result == null || result.isEmpty()) {
-            return true;
-        }
-        else {
-            CrossSection cs = (CrossSection) result.get(0);
-            return section.getId().equals(cs.getId());
-        }
-    }
-
 
     /**
      *  Get a specific CrossSection from db.
--- 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