comparison flys-backend/src/main/java/de/intevation/flys/model/Gauge.java @ 3815:ecab7e7804a9 pre2.6-2012-01-04

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

http://dive4elements.wald.intevation.org