comparison flys-backend/src/main/java/de/intevation/flys/model/Gauge.java @ 2877:f0a67bc0e777 2.7

merged flys-backend/2.7
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:31 +0200
parents 83abe19d4f40
children 8df746f374cc
comparison
equal deleted inserted replaced
2793:6310b1582f2d 2877:f0a67bc0e777
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 import de.intevation.flys.model.MainValue;
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 public static final int MASTER_DISCHARGE_TABLE = 0;
34
35
36 private Integer id;
37 private String name;
38 private River river;
39 private BigDecimal station;
40 private BigDecimal aeo;
41 private BigDecimal datum;
42 private Long officialNumber;
43 private Range range;
44
45 private List<DischargeTable> dischargeTables;
46
47 /** MainValues at this Gauge. */
48 protected List<MainValue> mainValues;
49
50 public Gauge() {
51 }
52
53 public Gauge(
54 String name,
55 River river,
56 BigDecimal station,
57 BigDecimal aeo,
58 BigDecimal datum,
59 Long officialNumber,
60 Range range
61 ) {
62 this.name = name;
63 this.river = river;
64 this.station = station;
65 this.aeo = aeo;
66 this.datum = datum;
67 this.officialNumber = officialNumber;
68 this.range = range;
69 }
70
71 @Id
72 @SequenceGenerator(
73 name = "SEQUENCE_GAUGES_ID_SEQ",
74 sequenceName = "GAUGES_ID_SEQ",
75 allocationSize = 1)
76 @GeneratedValue(
77 strategy = GenerationType.SEQUENCE,
78 generator = "SEQUENCE_GAUGES_ID_SEQ")
79 @Column(name = "id")
80 public Integer getId() {
81 return id;
82 }
83
84 public void setId(Integer id) {
85 this.id = id;
86 }
87
88 @OneToOne
89 @JoinColumn(name = "river_id" )
90 public River getRiver() {
91 return river;
92 }
93
94 public void setRiver(River river) {
95 this.river = river;
96 }
97
98 @Column(name = "name")
99 public String getName() {
100 return name;
101 }
102
103 public void setName(String name) {
104 this.name = name;
105 }
106
107 @Column(name = "station") // FIXME: type mapping needed
108 public BigDecimal getStation() {
109 return station;
110 }
111
112 public void setStation(BigDecimal station) {
113 this.station = station;
114 }
115
116 @Column(name = "aeo") // FIXME: type mapping needed
117 public BigDecimal getAeo() {
118 return aeo;
119 }
120
121 public void setAeo(BigDecimal aeo) {
122 this.aeo = aeo;
123 }
124
125 @Column(name = "datum") // FIXME: type mapping needed
126 public BigDecimal getDatum() {
127 return datum;
128 }
129
130 public void setDatum(BigDecimal datum) {
131 this.datum = datum;
132 }
133
134 @Column(name = "official_number")
135 public Long getOfficialNumber() {
136 return officialNumber;
137 }
138
139 public void setOfficialNumber(Long officialNumber) {
140 this.officialNumber = officialNumber;
141 }
142
143 @OneToOne
144 @JoinColumn(name = "range_id" )
145 public Range getRange() {
146 return range;
147 }
148
149 public void setRange(Range range) {
150 this.range = range;
151 }
152
153 @OneToMany
154 @JoinColumn(name = "gauge_id")
155 public List<DischargeTable> getDischargeTables() {
156 return dischargeTables;
157 }
158
159 public void setDischargeTables(List<DischargeTable> dischargeTables) {
160 this.dischargeTables = dischargeTables;
161 }
162
163
164 /**
165 * Returns min and max W values of this gauge based with a DEFAULT_SCALE.
166 *
167 * @return min and max W value of this gauge [min,max].
168 */
169 public double[] determineMinMaxW() {
170 return determineMinMaxW(DEFAULT_SCALE);
171 }
172
173
174 /**
175 * Returns min and max W values of this gauge.
176 *
177 * @return the min and max W value of this gauge [min,max].
178 */
179 public double[] determineMinMaxW(int scale) {
180 Session session = SessionHolder.HOLDER.get();
181
182 List<DischargeTable> tables = getDischargeTables();
183 DischargeTable dischargeTable = null;
184
185 for (DischargeTable tmp: tables) {
186 if (tmp.getKind() == 0) {
187 dischargeTable = tmp;
188 break;
189 }
190 }
191
192 if (dischargeTable == null) {
193 return null;
194 }
195
196 Query query = session.createQuery(
197 "select min(w) as min, max(w) as max from DischargeTableValue " +
198 "where table_id =:table");
199 query.setParameter("table", dischargeTable.getId());
200
201 List results = query.list();
202 Object[] result = (Object[]) results.get(0);
203
204 return result != null
205 ? new double[] {
206 ((BigDecimal) result[0]).doubleValue() * scale,
207 ((BigDecimal) result[1]).doubleValue() * scale}
208 : null;
209 }
210
211 @OneToMany
212 @JoinColumn(name = "gauge_id")
213 public List<MainValue> getMainValues() {
214 return mainValues;
215 }
216
217 public void setMainValues(List<MainValue> mainValues) {
218 this.mainValues = mainValues;
219 }
220
221
222 public static Gauge getGaugeByOfficialNumber(long number) {
223 Session session = SessionHolder.HOLDER.get();
224
225 Query query = session.createQuery(
226 "from Gauge where officialNumber=:number");
227
228 query.setParameter("number", number);
229
230 List<Gauge> results = query.list();
231
232 return results.isEmpty() ? null : results.get(0);
233 }
234
235
236 public DischargeTable fetchMasterDischargeTable() {
237 for (DischargeTable dt: dischargeTables) {
238 if (dt.getKind() == MASTER_DISCHARGE_TABLE) {
239 return dt;
240 }
241 }
242
243 return null;
244 }
245 }
246 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org