changeset 8693:851ea37d35f3

(issue1750) New methods for finding the actual location and companion station of a measurement station.
author Tom Gottfried <tom@intevation.de>
date Mon, 20 Apr 2015 10:21:45 +0200
parents 67e53dbe05d2
children 9bb7f19cbb6f
files backend/src/main/java/org/dive4elements/river/model/MeasurementStation.java
diffstat 1 files changed, 36 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/backend/src/main/java/org/dive4elements/river/model/MeasurementStation.java	Fri Apr 17 15:04:16 2015 +0200
+++ b/backend/src/main/java/org/dive4elements/river/model/MeasurementStation.java	Mon Apr 20 10:21:45 2015 +0200
@@ -29,11 +29,14 @@
 
 import org.dive4elements.river.backend.SessionHolder;
 
+import org.apache.log4j.Logger;
+
 import static org.dive4elements.river.backend.utils.EpsilonComparator.CMP;
 
 @Entity
 @Table(name = "measurement_station")
 public class MeasurementStation {
+    private static final Logger log = Logger.getLogger(MeasurementStation.class);
 
     private Integer id;
 
@@ -70,15 +73,7 @@
                     "Stations not at same river");
             }
 
-            boolean kmUp = m1.getRange().getRiver().getKmUp();
-            double s1 = kmUp && m1.getRange().getB() != null
-                ? m1.getRange().getB().doubleValue()
-                : m1.getRange().getA().doubleValue();
-            double s2 = kmUp && m2.getRange().getB() != null
-                ? m2.getRange().getB().doubleValue()
-                : m2.getRange().getA().doubleValue();
-
-            int cmpStations = CMP.compare(s1, s2);
+            int cmpStations = CMP.compare(m1.retrieveKm(), m2.retrieveKm());
             if (cmpStations == 0) {
                 if (m1.getMeasurementType().equals(m2.getMeasurementType())) {
                     throw new IllegalArgumentException(
@@ -208,6 +203,30 @@
         this.comment = comment;
     }
 
+    /* Get the actual location of the measurement station at the river */
+    public double retrieveKm() {
+        // In case river is km_up, station is at larger value of range
+        return getRange().getRiver().getKmUp() && getRange().getB() != null
+            ? getRange().getB().doubleValue()
+            : getRange().getA().doubleValue();
+    }
+
+    /* Get measurement station of other type at the same location */
+    public MeasurementStation findCompanionStation() {
+        River  river = getRange().getRiver();
+        double km    = retrieveKm();
+        List<MeasurementStation> stations = getStationsAtKM(river, km);
+
+        for (MeasurementStation station: stations) {
+            if (!station.getMeasurementType().equals(getMeasurementType())) {
+                return station;
+            }
+        }
+        log.debug("No additional stations found at km " + km +
+            " at river " + river);
+        return null;
+    }
+
     public static List<MeasurementStation> getStationsAtRiver(River river) {
         Session session = SessionHolder.HOLDER.get();
 
@@ -234,19 +253,17 @@
         List<MeasurementStation> result = new ArrayList<MeasurementStation>();
         for (Iterator iter = query.iterate(); iter.hasNext();) {
             MeasurementStation st = (MeasurementStation)iter.next();
-            Double a = st.getRange().getA().doubleValue();
-            Double b = st.getRange().getB() == null
-                ? null
-                : st.getRange().getB().doubleValue();
-
-            // In case river is km_up, station is at larger value of range
-            if (river.getKmUp() && b != null && CMP.compare(b, river_km) == 0
-                || !river.getKmUp() && CMP.compare(a, river_km) == 0
-                || b == null && CMP.compare(a, river_km) == 0 // no end km given
-            ) {
+            if (CMP.compare(st.retrieveKm(), river_km) == 0) {
                 result.add(st);
             }
         }
+
+        if (result.size() > 2) {
+            // TODO: database schema should prevent this
+            log.warn("More than two measurement stations at km " + river_km +
+                " at river " + river.getName());
+        }
+
         return result;
     }
 }

http://dive4elements.wald.intevation.org