comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/GaugeFinder.java @ 3468:f37e7e8907cb

merged flys-artifacts/2.8.1
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:39 +0200
parents 086326be721c
children 05eeedc5b156
comparison
equal deleted inserted replaced
3387:5ffad8bde8ad 3468:f37e7e8907cb
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 List<GaugeRange> getGauges() {
88 return gauges;
89 }
90
91 public boolean loadDischargeSectors(Session session, int riverId) {
92
93 SQLQuery query = session.createSQLQuery(SQL_DISCHARGE_SECTORS)
94 .addScalar("gauge_id", StandardBasicTypes.INTEGER)
95 .addScalar("name", StandardBasicTypes.STRING)
96 .addScalar("value", StandardBasicTypes.DOUBLE);
97
98 query.setInteger("river_id", riverId);
99
100 List<Object []> list = query.list();
101
102 if (list.isEmpty()) {
103 log.warn("River " + riverId + " has no discharge sectors.");
104 return false;
105 }
106
107 GaugeRange gauge = null;
108
109 for (Object [] row: list) {
110 int gaugeId = (Integer)row[0];
111 String label = (String) row[1];
112 Double value = (Double) row[2];
113
114 if (gauge == null || gauge.gaugeId != gaugeId) {
115 if ((gauge = find(gaugeId)) == null) {
116 log.warn("Cannot find gauge for id " + gaugeId + ".");
117 continue;
118 }
119 }
120
121 gauge.addMainValue(label, value);
122 }
123
124 for (GaugeRange g: gauges) {
125 g.buildClasses();
126 }
127
128 return true;
129 }
130 }
131 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org