comparison flys-backend/src/main/java/de/intevation/flys/model/Wst.java @ 3820:8a75cf0841b1 pre2.7-2012-03-16

merged flys-backend/pre2.7-2012-03-16
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:59 +0200
parents c41bb1293acb
children
comparison
equal deleted inserted replaced
3818:dc18457b1cef 3820:8a75cf0841b1
1 package de.intevation.flys.model;
2
3 import java.io.Serializable;
4 import java.math.BigDecimal;
5 import java.util.List;
6
7 import javax.persistence.Entity;
8 import javax.persistence.Id;
9 import javax.persistence.Table;
10 import javax.persistence.GeneratedValue;
11 import javax.persistence.Column;
12 import javax.persistence.SequenceGenerator;
13 import javax.persistence.GenerationType;
14 import javax.persistence.JoinColumn;
15 import javax.persistence.OneToOne;
16 import javax.persistence.OneToMany;
17
18 import org.apache.log4j.Logger;
19
20 import org.hibernate.Session;
21 import org.hibernate.SQLQuery;
22 import org.hibernate.Query;
23 import org.hibernate.type.StandardBasicTypes;
24
25 import de.intevation.flys.backend.SessionHolder;
26
27
28 @Entity
29 @Table(name = "wsts")
30 public class Wst
31 implements Serializable
32 {
33 private static Logger logger = Logger.getLogger(Wst.class);
34
35 private Integer id;
36 private River river;
37 private String description;
38 private Integer kind;
39
40 private List<WstColumn> columns;
41
42
43 public static final String SQL_SELECT_MINMAX =
44 "select min(q) as minQ, max(q) as maxQ from wst_q_values " +
45 "where wst_id = :wst and not (a > :km or b < :km)";
46
47 public Wst() {
48 }
49
50 public Wst(River river, String description) {
51 this(river, description, 0);
52 }
53
54 public Wst(River river, String description, Integer kind) {
55 this.river = river;
56 this.description = description;
57 this.kind = kind;
58 }
59
60 @Id
61 @SequenceGenerator(
62 name = "SEQUENCE_WSTS_ID_SEQ",
63 sequenceName = "WSTS_ID_SEQ",
64 allocationSize = 1)
65 @GeneratedValue(
66 strategy = GenerationType.SEQUENCE,
67 generator = "SEQUENCE_WSTS_ID_SEQ")
68 @Column(name = "id")
69 public Integer getId() {
70 return id;
71 }
72
73 public void setId(Integer id) {
74 this.id = id;
75 }
76
77 @OneToOne
78 @JoinColumn(name = "river_id" )
79 public River getRiver() {
80 return river;
81 }
82
83 public void setRiver(River river) {
84 this.river = river;
85 }
86
87 @Column(name = "description")
88 public String getDescription() {
89 return description;
90 }
91
92 public void setDescription(String description) {
93 this.description = description;
94 }
95
96 @Column(name = "kind")
97 public Integer getKind() {
98 return kind;
99 }
100
101 public void setKind(Integer kind) {
102 this.kind = kind;
103 }
104
105 @OneToMany
106 @JoinColumn(name="wst_id")
107 public List<WstColumn> getColumns() {
108 return columns;
109 }
110
111 public void setColumns(List<WstColumn> columns) {
112 this.columns = columns;
113 }
114
115
116 /**
117 * Determines the min and max Q values of this WST. The min value is placed
118 * in the first field of the resulting array - the max value is placed in
119 * the second field.
120 *
121 * @return the min and max Q values of this WST.
122 */
123 public double[] determineMinMaxQ() {
124 double[] ab = river.determineMinMaxDistance();
125 return determineMinMaxQ(new Range(ab[0], ab[1], river));
126 }
127
128
129 /**
130 * Determines the min and max Q values of this WST in the given range. The
131 * min value is placed in the first field of the resulting array - the max
132 * value is placed in the second field.
133 *
134 * @param range The range used for querying the Q values.
135 *
136 * @return the min and max Q values of this WST.
137 */
138 public double[] determineMinMaxQ(Range range) {
139 if (range != null) {
140 return determineMinMaxQ(
141 range.getA().doubleValue(),
142 range.getB().doubleValue());
143 }
144
145 return null;
146 }
147
148
149 /**
150 * Determines the min and max Q values of this WST in the given range. The
151 * min value is placed in the first field of the resulting array - the max
152 * value is placed in the second field.
153 *
154 * @param fromKm the lower km value.
155 * @param toKm the upper km value.
156 *
157 * @return the min and max Q values of this WST.
158 */
159 public double[] determineMinMaxQ(double fromKm, double toKm) {
160 Session session = SessionHolder.HOLDER.get();
161
162 Query query = session.createQuery(
163 "select min(q), max(q) from WstQRange where " +
164 " id in " +
165 " (select wstQRange.id from WstColumnQRange where " +
166 " wstColumn.id in (select id from WstColumn where wst.id = :wst)) " +
167 " and range.id in " +
168 " (select id from Range where not (a > :end or b < :start))");
169
170 query.setParameter("wst", getId());
171 query.setParameter("start", new BigDecimal(fromKm));
172 query.setParameter("end", new BigDecimal(toKm));
173
174 List<Object []> results = query.list();
175
176 if (results.isEmpty()) {
177 return null;
178 }
179
180 Object [] result = results.get(0);
181
182 return new double [] {
183 ((BigDecimal)result[0]).doubleValue(),
184 ((BigDecimal)result[1]).doubleValue() };
185 }
186
187
188 public double[] determineMinMaxQFree(double km) {
189 Session session = SessionHolder.HOLDER.get();
190
191 SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_MINMAX)
192 .addScalar("minQ", StandardBasicTypes.DOUBLE)
193 .addScalar("maxQ", StandardBasicTypes.DOUBLE);
194
195 sqlQuery.setInteger("wst", getId());
196 sqlQuery.setDouble("km", km);
197
198 List<Object[]> minmaxQ = sqlQuery.list();
199
200
201 if (minmaxQ.isEmpty()) {
202 return null;
203 }
204
205 Object[] mm = minmaxQ.get(0);
206
207 if (mm[0] == null || mm[1] == null) {
208 logger.warn ("No min/max Q for km " + km + " found.");
209 return null;
210 }
211
212 return new double[] { (Double) mm[0], (Double) mm[1] };
213 }
214 }
215 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org