changeset 8418:536a1c0c8a81

Give ordered list of measurement stations at river.
author Tom Gottfried <tom@intevation.de>
date Thu, 16 Oct 2014 16:45:53 +0200 (2014-10-16)
parents 71144e25a6c9
children d7d74392cc46
files backend/src/main/java/org/dive4elements/river/model/MeasurementStation.java
diffstat 1 files changed, 43 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/backend/src/main/java/org/dive4elements/river/model/MeasurementStation.java	Thu Oct 16 13:39:22 2014 +0200
+++ b/backend/src/main/java/org/dive4elements/river/model/MeasurementStation.java	Thu Oct 16 16:45:53 2014 +0200
@@ -11,6 +11,8 @@
 import java.util.Iterator;
 import java.util.List;
 import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
 
 import java.math.BigDecimal;
 
@@ -29,6 +31,8 @@
 
 import org.dive4elements.river.backend.SessionHolder;
 
+import static org.dive4elements.river.backend.utils.EpsilonComparator.CMP;
+
 @Entity
 @Table(name = "measurement_station")
 public class MeasurementStation {
@@ -48,6 +52,42 @@
 
     private TimeInterval observationTimerange;
 
+    public static final class MeasurementStationComparator
+        implements Comparator<MeasurementStation> {
+
+        public MeasurementStationComparator() {
+        }
+
+        @Override
+        public int compare(MeasurementStation m1, MeasurementStation m2)
+            throws IllegalArgumentException {
+
+            if (m1.getRange().getRiver() != m2.getRange().getRiver()) {
+                throw new IllegalArgumentException(
+                    "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);
+            if (cmpStations == 0) {
+                if (m1.getMeasurementType().equals(m2.getMeasurementType())) {
+                    throw new IllegalArgumentException(
+                        "Two stations of same type at same km");
+                }
+
+                return m1.getMeasurementType().equals("Geschiebe") ? -1 : +1;
+            }
+            return cmpStations;
+        }
+    }
+
     public MeasurementStation() {
     }
 
@@ -169,10 +209,11 @@
         Query query = session.createQuery(
             "from MeasurementStation " +
             "where range.river = :river");
-
         query.setParameter("river", river);
 
-        return query.list();
+        List<MeasurementStation> result = query.list();
+        Collections.sort(result, new MeasurementStationComparator());
+        return result;
     }
 
     public static List<MeasurementStation> getStationsAtKM(

http://dive4elements.wald.intevation.org