Mercurial > dive4elements > river
comparison flys-backend/src/main/java/de/intevation/flys/model/Gauge.java @ 508:a9c7f6ec3a5a 2.3.1
merged flys-backend/2.3.1
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:12 +0200 |
parents | 46bb2b9e0bdc |
children | 5a3109b8475f |
comparison
equal
deleted
inserted
replaced
462:ebf049a1eb53 | 508:a9c7f6ec3a5a |
---|---|
1 package de.intevation.flys.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 de.intevation.flys.backend.SessionHolder; | |
24 | |
25 | |
26 @Entity | |
27 @Table(name = "gauges") | |
28 public class Gauge | |
29 implements Serializable | |
30 { | |
31 public static final int DEFAULT_SCALE = 100; | |
32 | |
33 private Integer id; | |
34 private String name; | |
35 private River river; | |
36 private BigDecimal station; | |
37 private BigDecimal aeo; | |
38 private BigDecimal datum; | |
39 private Range range; | |
40 | |
41 private List<DischargeTable> dischargeTables; | |
42 | |
43 public Gauge() { | |
44 } | |
45 | |
46 public Gauge( | |
47 String name, | |
48 River river, | |
49 BigDecimal station, | |
50 BigDecimal aeo, | |
51 BigDecimal datum, | |
52 Range range | |
53 ) { | |
54 this.name = name; | |
55 this.river = river; | |
56 this.station = station; | |
57 this.aeo = aeo; | |
58 this.datum = datum; | |
59 this.range = range; | |
60 } | |
61 | |
62 @Id | |
63 @SequenceGenerator( | |
64 name = "SEQUENCE_GAUGES_ID_SEQ", | |
65 sequenceName = "GAUGES_ID_SEQ", | |
66 allocationSize = 1) | |
67 @GeneratedValue( | |
68 strategy = GenerationType.SEQUENCE, | |
69 generator = "SEQUENCE_GAUGES_ID_SEQ") | |
70 @Column(name = "id") | |
71 public Integer getId() { | |
72 return id; | |
73 } | |
74 | |
75 public void setId(Integer id) { | |
76 this.id = id; | |
77 } | |
78 | |
79 @OneToOne | |
80 @JoinColumn(name = "river_id" ) | |
81 public River getRiver() { | |
82 return river; | |
83 } | |
84 | |
85 public void setRiver(River river) { | |
86 this.river = river; | |
87 } | |
88 | |
89 @Column(name = "name") | |
90 public String getName() { | |
91 return name; | |
92 } | |
93 | |
94 public void setName(String name) { | |
95 this.name = name; | |
96 } | |
97 | |
98 @Column(name = "station") // FIXME: type mapping needed | |
99 public BigDecimal getStation() { | |
100 return station; | |
101 } | |
102 | |
103 public void setStation(BigDecimal station) { | |
104 this.station = station; | |
105 } | |
106 | |
107 @Column(name = "aeo") // FIXME: type mapping needed | |
108 public BigDecimal getAeo() { | |
109 return aeo; | |
110 } | |
111 | |
112 public void setAeo(BigDecimal aeo) { | |
113 this.aeo = aeo; | |
114 } | |
115 | |
116 @Column(name = "datum") // FIXME: type mapping needed | |
117 public BigDecimal getDatum() { | |
118 return datum; | |
119 } | |
120 | |
121 public void setDatum(BigDecimal datum) { | |
122 this.datum = datum; | |
123 } | |
124 | |
125 @OneToOne | |
126 @JoinColumn(name = "range_id" ) | |
127 public Range getRange() { | |
128 return range; | |
129 } | |
130 | |
131 public void setRange(Range range) { | |
132 this.range = range; | |
133 } | |
134 | |
135 @OneToMany | |
136 @JoinColumn(name = "gauge_id") | |
137 public List<DischargeTable> getDischargeTables() { | |
138 return dischargeTables; | |
139 } | |
140 | |
141 public void setDischargeTables(List<DischargeTable> dischargeTables) { | |
142 this.dischargeTables = dischargeTables; | |
143 } | |
144 | |
145 | |
146 /** | |
147 * Returns min and max W values of this gauge based with a DEFAULT_SCALE. | |
148 * | |
149 * @return min and max W value of this gauge [min,max]. | |
150 */ | |
151 public double[] determineMinMaxW() { | |
152 return determineMinMaxW(DEFAULT_SCALE); | |
153 } | |
154 | |
155 | |
156 /** | |
157 * Returns min and max W values of this gauge. | |
158 * | |
159 * @return the min and max W value of this gauge [min,max]. | |
160 */ | |
161 public double[] determineMinMaxW(int scale) { | |
162 Session session = SessionHolder.HOLDER.get(); | |
163 | |
164 List<DischargeTable> tables = getDischargeTables(); | |
165 DischargeTable dischargeTable = null; | |
166 | |
167 for (DischargeTable tmp: tables) { | |
168 if (tmp.getKind() == 0) { | |
169 dischargeTable = tmp; | |
170 break; | |
171 } | |
172 } | |
173 | |
174 if (dischargeTable == null) { | |
175 return null; | |
176 } | |
177 | |
178 Query query = session.createQuery( | |
179 "select min(w) as min, max(w) as max from DischargeTableValue " + | |
180 "where table_id =:table"); | |
181 query.setParameter("table", dischargeTable.getId()); | |
182 | |
183 List results = query.list(); | |
184 Object[] result = (Object[]) results.get(0); | |
185 | |
186 return result != null | |
187 ? new double[] { | |
188 ((BigDecimal) result[0]).doubleValue() * scale, | |
189 ((BigDecimal) result[1]).doubleValue() * scale} | |
190 : null; | |
191 } | |
192 } | |
193 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |