changeset 8764:d5917ff74d8a

(issue1838) Get min and max directly from database. The former implementation led to a connection leak.
author Tom Gottfried <tom@intevation.de>
date Mon, 17 Aug 2015 10:18:05 +0200
parents 8179cca1796a
children a5e450af498b
files backend/src/main/java/org/dive4elements/river/model/River.java
diffstat 1 files changed, 14 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/backend/src/main/java/org/dive4elements/river/model/River.java	Fri Aug 14 18:24:04 2015 +0200
+++ b/backend/src/main/java/org/dive4elements/river/model/River.java	Mon Aug 17 10:18:05 2015 +0200
@@ -379,42 +379,23 @@
      * @return the min and max distance of this river.
      */
     public double[] determineMinMaxDistance() {
-        List<Gauge> gauges = getGauges();
+        Session session = SessionHolder.HOLDER.get();
 
-        if (gauges == null || gauges.isEmpty()) {
-            return null;
+        Query query = session.createQuery(
+            "select min(range.a), max(range.b) from Gauge "
+            + "where river=:river "
+            + "and range is not null");
+        query.setParameter("river", this);
+
+        List<Object[]> result = query.list();
+
+        if (!result.isEmpty()) {
+            Object[] minMax = result.get(0);
+            return new double[] { ((BigDecimal)minMax[0]).doubleValue(),
+                ((BigDecimal)minMax[1]).doubleValue() };
         }
 
-        double minmax[] = new double[] { Double.MAX_VALUE, -Double.MAX_VALUE };
-
-        for (Gauge g: gauges) {
-            Range r = g.getRange();
-
-            if (r == null) {
-                continue;
-            }
-
-            double a = r.getA().doubleValue();
-            if (a < minmax[0]) {
-                minmax[0] = a;
-            }
-            if (a > minmax[1]) {
-                minmax[1] = a;
-            }
-
-            BigDecimal bigB = r.getB();
-            if (bigB != null) {
-                double b = bigB.doubleValue();
-                if (b < minmax[0]) {
-                    minmax[0] = b;
-                }
-                if (b > minmax[1]) {
-                    minmax[1] = b;
-                }
-            }
-        }
-
-        return minmax;
+        return null;
     }
 
     public Map<Double, Double> queryGaugeDatumsKMs() {

http://dive4elements.wald.intevation.org