comparison flys-backend/src/main/java/de/intevation/flys/model/Gauge.java @ 3813:6aeee2250418 pre2.6-2011-12-05

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

http://dive4elements.wald.intevation.org