Mercurial > dive4elements > river
comparison backend/src/main/java/org/dive4elements/river/model/River.java @ 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 | 26dedebbe39f |
children | dbdb7c9bd51b |
comparison
equal
deleted
inserted
replaced
8763:8179cca1796a | 8764:d5917ff74d8a |
---|---|
377 * distance. | 377 * distance. |
378 * | 378 * |
379 * @return the min and max distance of this river. | 379 * @return the min and max distance of this river. |
380 */ | 380 */ |
381 public double[] determineMinMaxDistance() { | 381 public double[] determineMinMaxDistance() { |
382 List<Gauge> gauges = getGauges(); | 382 Session session = SessionHolder.HOLDER.get(); |
383 | 383 |
384 if (gauges == null || gauges.isEmpty()) { | 384 Query query = session.createQuery( |
385 return null; | 385 "select min(range.a), max(range.b) from Gauge " |
386 } | 386 + "where river=:river " |
387 | 387 + "and range is not null"); |
388 double minmax[] = new double[] { Double.MAX_VALUE, -Double.MAX_VALUE }; | 388 query.setParameter("river", this); |
389 | 389 |
390 for (Gauge g: gauges) { | 390 List<Object[]> result = query.list(); |
391 Range r = g.getRange(); | 391 |
392 | 392 if (!result.isEmpty()) { |
393 if (r == null) { | 393 Object[] minMax = result.get(0); |
394 continue; | 394 return new double[] { ((BigDecimal)minMax[0]).doubleValue(), |
395 } | 395 ((BigDecimal)minMax[1]).doubleValue() }; |
396 | 396 } |
397 double a = r.getA().doubleValue(); | 397 |
398 if (a < minmax[0]) { | 398 return null; |
399 minmax[0] = a; | |
400 } | |
401 if (a > minmax[1]) { | |
402 minmax[1] = a; | |
403 } | |
404 | |
405 BigDecimal bigB = r.getB(); | |
406 if (bigB != null) { | |
407 double b = bigB.doubleValue(); | |
408 if (b < minmax[0]) { | |
409 minmax[0] = b; | |
410 } | |
411 if (b > minmax[1]) { | |
412 minmax[1] = b; | |
413 } | |
414 } | |
415 } | |
416 | |
417 return minmax; | |
418 } | 399 } |
419 | 400 |
420 public Map<Double, Double> queryGaugeDatumsKMs() { | 401 public Map<Double, Double> queryGaugeDatumsKMs() { |
421 List<Gauge> gauges = getGauges(); | 402 List<Gauge> gauges = getGauges(); |
422 Map<Double, Double> result = new TreeMap<Double, Double>(KM_CMP); | 403 Map<Double, Double> result = new TreeMap<Double, Double>(KM_CMP); |