Mercurial > dive4elements > river
comparison flys-backend/src/main/java/de/intevation/flys/model/Gauge.java @ 3471:e4250c6e1538 2.8.1
merged flys-backend/2.8.1
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:40 +0200 |
parents | 8df746f374cc |
children | c959444ff395 |
comparison
equal
deleted
inserted
replaced
3468:f37e7e8907cb | 3471:e4250c6e1538 |
---|---|
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 @Entity | |
26 @Table(name = "gauges") | |
27 public class Gauge | |
28 implements Serializable | |
29 { | |
30 public static final int DEFAULT_SCALE = 100; | |
31 | |
32 public static final int MASTER_DISCHARGE_TABLE = 0; | |
33 | |
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 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |