comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/GaugeFinder.java @ 3318:dbe2f85bf160

merged flys-artifacts/2.8
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:35 +0200
parents 29022c93027d
children 086326be721c
comparison
equal deleted inserted replaced
2987:98c7a46ec5ae 3318:dbe2f85bf160
1 package de.intevation.flys.artifacts.model;
2
3 import java.io.Serializable;
4
5 import java.util.List;
6
7 import org.apache.log4j.Logger;
8
9 import org.hibernate.SQLQuery;
10 import org.hibernate.Session;
11
12 import org.hibernate.type.StandardBasicTypes;
13
14 public class GaugeFinder
15 implements Serializable
16 {
17 private static Logger log = Logger.getLogger(GaugeFinder.class);
18
19 public static final String SQL_DISCHARGE_SECTORS =
20 "SELECT" +
21 " g.id AS gauge_id," +
22 " nmv.name AS name," +
23 " CAST(mv.value AS NUMERIC(38,2)) AS value " +
24 "FROM gauges g" +
25 " JOIN main_values mv ON g.id = mv.gauge_id" +
26 " JOIN named_main_values nmv ON nmv.id = mv.named_value_id" +
27 " JOIN main_value_types mvt ON nmv.type_id = mvt.id " +
28 "WHERE" +
29 " mvt.name = 'Q' AND (" +
30 " nmv.name = 'MNQ' OR" +
31 " nmv.name LIKE 'MNQ(%' OR" +
32 " nmv.name = 'MQ' OR" +
33 " nmv.name LIKE 'MQ(%' OR" +
34 " nmv.name = 'MHQ' OR" +
35 " nmv.name LIKE 'MHQ(%' OR" +
36 " nmv.name = 'HQ5' OR" +
37 " nmv.name LIKE 'HQ5(%') AND" +
38 " g.river_id = :river_id " +
39 "ORDER BY" +
40 " g.id";
41
42 protected List<GaugeRange> gauges;
43 protected boolean isKmUp;
44
45 public GaugeFinder(List<GaugeRange> gauges) {
46 this(gauges, true);
47 }
48
49 public GaugeFinder(
50 List<GaugeRange> gauges,
51 boolean isKmUp
52 ) {
53 this.gauges = gauges;
54 this.isKmUp = isKmUp;
55 }
56
57 public boolean getIsKmUp() {
58 return isKmUp;
59 }
60
61 public void setIsKmUp(boolean isKmUp) {
62 this.isKmUp = isKmUp;
63 }
64
65 public GaugeRange find(double km) {
66 for (GaugeRange gauge: gauges) {
67 if (gauge.inside(km)) {
68 return gauge;
69 }
70 }
71 return null;
72 }
73
74 public GaugeRange find(Range range) {
75 return find(isKmUp ? range.start : range.end);
76 }
77
78 public GaugeRange find(int gaugeId) {
79 for (GaugeRange gauge: gauges) {
80 if (gauge.gaugeId == gaugeId) {
81 return gauge;
82 }
83 }
84 return null;
85 }
86
87 public boolean loadDischargeSectors(Session session, int riverId) {
88
89 SQLQuery query = session.createSQLQuery(SQL_DISCHARGE_SECTORS)
90 .addScalar("gauge_id", StandardBasicTypes.INTEGER)
91 .addScalar("name", StandardBasicTypes.STRING)
92 .addScalar("value", StandardBasicTypes.DOUBLE);
93
94 query.setInteger("river_id", riverId);
95
96 List<Object []> list = query.list();
97
98 if (list.isEmpty()) {
99 log.warn("River " + riverId + " has no discharge sectors.");
100 return false;
101 }
102
103 GaugeRange gauge = null;
104
105 for (Object [] row: list) {
106 int gaugeId = (Integer)row[0];
107 String label = (String) row[1];
108 Double value = (Double) row[2];
109
110 if (gauge == null || gauge.gaugeId != gaugeId) {
111 if ((gauge = find(gaugeId)) == null) {
112 log.warn("Cannot find gauge for id " + gaugeId + ".");
113 continue;
114 }
115 }
116
117 gauge.addMainValue(label, value);
118 }
119
120 for (GaugeRange g: gauges) {
121 g.buildClasses();
122 }
123
124 return true;
125 }
126 }
127 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org