comparison backend/src/main/java/org/dive4elements/river/model/Gauge.java @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-backend/src/main/java/org/dive4elements/river/model/Gauge.java@18619c1e7c2a
children 4dd33b86dc61
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.model;
2
3 import java.math.BigDecimal;
4
5 import java.io.Serializable;
6
7 import java.util.List;
8
9 import javax.persistence.Entity;
10 import javax.persistence.Id;
11 import javax.persistence.Table;
12 import javax.persistence.GeneratedValue;
13 import javax.persistence.Column;
14 import javax.persistence.SequenceGenerator;
15 import javax.persistence.GenerationType;
16 import javax.persistence.JoinColumn;
17 import javax.persistence.OneToOne;
18 import javax.persistence.OneToMany;
19
20 import org.hibernate.Session;
21 import org.hibernate.Query;
22
23 import org.dive4elements.river.backend.SessionHolder;
24
25 /** Database-mapped Gauge with all info about it. */
26 @Entity
27 @Table(name = "gauges")
28 public class Gauge
29 implements Serializable, Comparable<Gauge>
30 {
31 public static final int DEFAULT_SCALE = 100;
32
33 public static final int MASTER_DISCHARGE_TABLE = 0;
34
35 private Integer id;
36 private String name;
37 private River river;
38 private BigDecimal station;
39 private BigDecimal aeo;
40 private BigDecimal datum;
41 private Long officialNumber;
42 private Range range;
43
44 private List<DischargeTable> dischargeTables;
45
46 /** MainValues at this Gauge. */
47 protected List<MainValue> mainValues;
48
49 public Gauge() {
50 }
51
52 public Gauge(
53 String name,
54 River river,
55 BigDecimal station,
56 BigDecimal aeo,
57 BigDecimal datum,
58 Long officialNumber,
59 Range range
60 ) {
61 this.name = name;
62 this.river = river;
63 this.station = station;
64 this.aeo = aeo;
65 this.datum = datum;
66 this.officialNumber = officialNumber;
67 this.range = range;
68 }
69
70 @Id
71 @SequenceGenerator(
72 name = "SEQUENCE_GAUGES_ID_SEQ",
73 sequenceName = "GAUGES_ID_SEQ",
74 allocationSize = 1)
75 @GeneratedValue(
76 strategy = GenerationType.SEQUENCE,
77 generator = "SEQUENCE_GAUGES_ID_SEQ")
78 @Column(name = "id")
79 public Integer getId() {
80 return id;
81 }
82
83 public void setId(Integer id) {
84 this.id = id;
85 }
86
87 @OneToOne
88 @JoinColumn(name = "river_id" )
89 public River getRiver() {
90 return river;
91 }
92
93 public void setRiver(River river) {
94 this.river = river;
95 }
96
97 @Column(name = "name")
98 public String getName() {
99 return name;
100 }
101
102 public void setName(String name) {
103 this.name = name;
104 }
105
106 @Column(name = "station") // FIXME: type mapping needed
107 public BigDecimal getStation() {
108 return station;
109 }
110
111 public void setStation(BigDecimal station) {
112 this.station = station;
113 }
114
115 @Column(name = "aeo") // FIXME: type mapping needed
116 public BigDecimal getAeo() {
117 return aeo;
118 }
119
120 public void setAeo(BigDecimal aeo) {
121 this.aeo = aeo;
122 }
123
124 @Column(name = "datum") // FIXME: type mapping needed
125 public BigDecimal getDatum() {
126 return datum;
127 }
128
129 public void setDatum(BigDecimal datum) {
130 this.datum = datum;
131 }
132
133 @Column(name = "official_number")
134 public Long getOfficialNumber() {
135 return officialNumber;
136 }
137
138 public void setOfficialNumber(Long officialNumber) {
139 this.officialNumber = officialNumber;
140 }
141
142 @OneToOne
143 @JoinColumn(name = "range_id" )
144 public Range getRange() {
145 return range;
146 }
147
148 public void setRange(Range range) {
149 this.range = range;
150 }
151
152 @OneToMany
153 @JoinColumn(name = "gauge_id")
154 public List<DischargeTable> getDischargeTables() {
155 return dischargeTables;
156 }
157
158 public void setDischargeTables(List<DischargeTable> dischargeTables) {
159 this.dischargeTables = dischargeTables;
160 }
161
162
163 /**
164 * Returns min and max W values of this gauge based with a DEFAULT_SCALE.
165 *
166 * @return min and max W value of this gauge [min,max].
167 */
168 public double[] determineMinMaxW() {
169 return determineMinMaxW(DEFAULT_SCALE);
170 }
171
172
173 /**
174 * Returns min and max W values of this gauge.
175 *
176 * @return the min and max W value of this gauge [min,max].
177 */
178 public double[] determineMinMaxW(int scale) {
179 Session session = SessionHolder.HOLDER.get();
180
181 List<DischargeTable> tables = getDischargeTables();
182 DischargeTable dischargeTable = null;
183
184 for (DischargeTable tmp: tables) {
185 if (tmp.getKind() == 0) {
186 dischargeTable = tmp;
187 break;
188 }
189 }
190
191 if (dischargeTable == null) {
192 return null;
193 }
194
195 Query query = session.createQuery(
196 "select min(w) as min, max(w) as max from DischargeTableValue " +
197 "where table_id =:table");
198 query.setParameter("table", dischargeTable.getId());
199
200 List results = query.list();
201 Object[] result = (Object[]) results.get(0);
202
203 return result != null
204 ? new double[] {
205 ((BigDecimal) result[0]).doubleValue() * scale,
206 ((BigDecimal) result[1]).doubleValue() * scale}
207 : null;
208 }
209
210 @OneToMany
211 @JoinColumn(name = "gauge_id")
212 public List<MainValue> getMainValues() {
213 return mainValues;
214 }
215
216 public void setMainValues(List<MainValue> mainValues) {
217 this.mainValues = mainValues;
218 }
219
220
221 public static Gauge getGaugeByOfficialNumber(long number) {
222 Session session = SessionHolder.HOLDER.get();
223
224 Query query = session.createQuery(
225 "from Gauge where officialNumber=:number");
226
227 query.setParameter("number", number);
228
229 List<Gauge> results = query.list();
230
231 return results.isEmpty() ? null : results.get(0);
232 }
233
234
235 public DischargeTable fetchMasterDischargeTable() {
236 for (DischargeTable dt: dischargeTables) {
237 if (dt.getKind() == MASTER_DISCHARGE_TABLE) {
238 return dt;
239 }
240 }
241
242 return null;
243 }
244
245 /**
246 * Returns an array of [days, qs] necessary to create duration curves.
247 *
248 * @return a 2dim array of [days, qs] where days is an int[] and qs is
249 * an double[].
250 */
251 public Object[] fetchDurationCurveData() {
252 Session session = SessionHolder.HOLDER.get();
253
254 Query query = session.createQuery(
255 "select cast(nmv.name as integer) as days, mv.value as q " +
256 "from MainValue as mv " +
257 "join mv.mainValue as nmv " +
258 "join nmv.type mvt " +
259 "where mvt.name = 'D' and mv.gauge.id = :gauge_id " +
260 "order by days");
261
262 query.setParameter("gauge_id", getId());
263
264 List<Object> results = query.list();
265 int[] days = new int[results.size()];
266 double[] qs = new double[results.size()];
267
268 int idx = 0;
269
270 for (Object obj: results) {
271 Object[] arr = (Object[]) obj;
272
273 try {
274 int day = ((Integer) arr[0]).intValue();
275 double q = ((BigDecimal) arr[1]).doubleValue();
276
277 days[idx] = day;
278 qs[idx++] = q;
279 }
280 catch (NumberFormatException nfe) {
281 }
282 }
283
284 return new Object[] { days, qs };
285 }
286
287 /**
288 * Calculates the maximum and minimum W and Q values
289 *
290 * @return the MaxMinWQ object representing the calculated values
291 */
292 public MinMaxWQ fetchMaxMinWQ() {
293 Session session = SessionHolder.HOLDER.get();
294
295 Query query = session.createQuery(
296 "select max(mv.value) as max, min(mv.value) as min " +
297 "from MainValue as mv " +
298 "join mv.mainValue as nmv " +
299 "join nmv.type mvt " +
300 "where mvt.name in ('W', 'Q') " +
301 "and mv.gauge.id = :gauge_id " +
302 "group by mvt.name order by mvt.name"
303 );
304
305 query.setParameter("gauge_id", getId());
306
307 List<Object> results = query.list();
308 if (results.isEmpty()) {
309 // No values found
310 return new MinMaxWQ();
311 }
312
313 Object[] arr = (Object[]) results.get(0);
314 BigDecimal maxw = (BigDecimal)arr[0];
315 BigDecimal minw = (BigDecimal)arr[1];
316 BigDecimal maxq = null;
317 BigDecimal minq = null;
318
319
320 if (results.size() > 1) {
321 arr = (Object[]) results.get(1);
322 maxq = (BigDecimal)arr[0];
323 minq = (BigDecimal)arr[1];
324 }
325
326 return new MinMaxWQ(minw, maxw, minq, maxq);
327 }
328
329 @Override
330 public int compareTo(Gauge o) {
331 return getName().compareTo(o.getName());
332 }
333 }
334 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org