Mercurial > dive4elements > river
comparison flys-backend/src/main/java/de/intevation/flys/model/River.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 | 056b3a5aa181 |
children | 6605dcd6745f |
comparison
equal
deleted
inserted
replaced
3814:8083f6384023 | 3815:ecab7e7804a9 |
---|---|
1 package de.intevation.flys.model; | |
2 | |
3 import java.io.Serializable; | |
4 | |
5 import java.math.BigDecimal; | |
6 import java.math.MathContext; | |
7 | |
8 import javax.persistence.Entity; | |
9 import javax.persistence.Id; | |
10 import javax.persistence.Table; | |
11 import javax.persistence.GeneratedValue; | |
12 import javax.persistence.Column; | |
13 import javax.persistence.SequenceGenerator; | |
14 import javax.persistence.OneToMany; | |
15 import javax.persistence.OneToOne; | |
16 import javax.persistence.JoinColumn; | |
17 import javax.persistence.GenerationType; | |
18 | |
19 import java.util.List; | |
20 | |
21 import org.hibernate.Session; | |
22 import org.hibernate.Query; | |
23 | |
24 import de.intevation.flys.backend.SessionHolder; | |
25 | |
26 | |
27 @Entity | |
28 @Table(name = "rivers") | |
29 public class River | |
30 implements Serializable | |
31 { | |
32 public static final MathContext PRECISION = new MathContext(6); | |
33 | |
34 private Integer id; | |
35 | |
36 private String name; | |
37 | |
38 private boolean kmUp; | |
39 | |
40 private List<Gauge> gauges; | |
41 | |
42 private Unit wstUnit; | |
43 | |
44 @Id | |
45 @SequenceGenerator( | |
46 name = "SEQUENCE_RIVERS_ID_SEQ", | |
47 sequenceName = "RIVERS_ID_SEQ", | |
48 allocationSize = 1) | |
49 @GeneratedValue( | |
50 strategy = GenerationType.SEQUENCE, | |
51 generator = "SEQUENCE_RIVERS_ID_SEQ") | |
52 @Column(name = "id") | |
53 public Integer getId() { | |
54 return id; | |
55 } | |
56 | |
57 public void setId(Integer id) { | |
58 this.id = id; | |
59 } | |
60 | |
61 @Column(name = "name") | |
62 public String getName() { | |
63 return name; | |
64 } | |
65 | |
66 public void setName(String name) { | |
67 this.name = name; | |
68 } | |
69 | |
70 @Column(name = "km_up") | |
71 public boolean getKmUp() { | |
72 return kmUp; | |
73 } | |
74 | |
75 public void setKmUp(boolean kmUp) { | |
76 this.kmUp = kmUp; | |
77 } | |
78 | |
79 public River() { | |
80 } | |
81 | |
82 public River(String name, Unit wstUnit) { | |
83 this.name = name; | |
84 this.wstUnit = wstUnit; | |
85 } | |
86 | |
87 @OneToMany | |
88 @JoinColumn(name="river_id") | |
89 public List<Gauge> getGauges() { | |
90 return gauges; | |
91 } | |
92 | |
93 public void setGauges(List<Gauge> gauges) { | |
94 this.gauges = gauges; | |
95 } | |
96 | |
97 | |
98 @OneToOne | |
99 @JoinColumn(name = "wst_unit_id" ) | |
100 public Unit getWstUnit() { | |
101 return wstUnit; | |
102 } | |
103 | |
104 public void setWstUnit(Unit wstUnit) { | |
105 this.wstUnit = wstUnit; | |
106 } | |
107 | |
108 | |
109 | |
110 public String toString() { | |
111 return name != null ? name : ""; | |
112 } | |
113 | |
114 | |
115 /** | |
116 * This method returns the gauges that intersect with <i>a</i> and | |
117 * <i>b</i>, | |
118 * | |
119 * @param a A start point. | |
120 * @param b An end point. | |
121 * | |
122 * @return the intersecting gauges. | |
123 */ | |
124 public List<Gauge> determineGauges(double a, double b) { | |
125 Session session = SessionHolder.HOLDER.get(); | |
126 | |
127 if (a > b) { double t = a; a = b; b = t; } | |
128 | |
129 Query query = session.createQuery( | |
130 "from Gauge where river=:river " + | |
131 "and not (range.a > :b or range.b < :a) order by a"); | |
132 query.setParameter("river", this); | |
133 query.setParameter("a", new BigDecimal(a, PRECISION)); | |
134 query.setParameter("b", new BigDecimal(b, PRECISION)); | |
135 | |
136 return query.list(); | |
137 } | |
138 | |
139 public Gauge maxOverlap(double a, double b) { | |
140 List<Gauge> gauges = determineGauges(a, b); | |
141 if (gauges == null) { | |
142 return null; | |
143 } | |
144 | |
145 if (a > b) { double t = a; a = b; b = t; } | |
146 | |
147 double max = -Double.MAX_VALUE; | |
148 | |
149 Gauge result = null; | |
150 | |
151 for (Gauge gauge: gauges) { | |
152 Range r = gauge.getRange(); | |
153 double c = r.getA().doubleValue(); | |
154 double d = r.getB().doubleValue(); | |
155 | |
156 double start = c >= a ? c : a; | |
157 double stop = d <= b ? d : b; | |
158 | |
159 double length = stop - start; | |
160 | |
161 if (length > max) { | |
162 max = length; | |
163 result = gauge; | |
164 } | |
165 } | |
166 | |
167 return result; | |
168 } | |
169 | |
170 public Gauge determineGaugeByName(String name) { | |
171 Session session = SessionHolder.HOLDER.get(); | |
172 Query query = session.createQuery( | |
173 "from Gauge where river=:river and name=:name"); | |
174 query.setParameter("river", this); | |
175 query.setParameter("name", name); | |
176 List<Gauge> gauges = query.list(); | |
177 return gauges.isEmpty() ? null : gauges.get(0); | |
178 } | |
179 | |
180 public Gauge determineGaugeByPosition(double p) { | |
181 Session session = SessionHolder.HOLDER.get(); | |
182 Query query = session.createQuery( | |
183 "from Gauge g where river=:river " + | |
184 "and :p between g.range.a and g.range.b"); | |
185 query.setParameter("river", this); | |
186 query.setParameter("p", new BigDecimal(p, PRECISION)); | |
187 List<Gauge> gauges = query.list(); | |
188 return gauges.isEmpty() ? null : gauges.get(0); | |
189 } | |
190 | |
191 public Gauge determineGaugeByStation(double a, double b) { | |
192 | |
193 if (a > b) { double t = a; a = b; b = t; } | |
194 | |
195 Session session = SessionHolder.HOLDER.get(); | |
196 | |
197 Query query = session.createQuery( | |
198 "from Gauge where river.id=:river " + | |
199 "and station between :a and :b"); | |
200 query.setParameter("river", getId()); | |
201 query.setParameter("a", new BigDecimal(a)); | |
202 query.setParameter("b", new BigDecimal(b)); | |
203 | |
204 List<Gauge> gauges = query.list(); | |
205 return gauges.isEmpty() ? null : gauges.get(0); | |
206 } | |
207 | |
208 | |
209 /** | |
210 * This method returns the first gauge that is intersected by <i>a</i> and | |
211 * <i>b</i>, | |
212 * | |
213 * @param a A start point. | |
214 * @param b An end point. | |
215 * | |
216 * @return the first intersecting gauge. | |
217 */ | |
218 public Gauge determineGauge(double a, double b) { | |
219 List<Gauge> gauges = determineGauges(a, b); | |
220 | |
221 int idx = a < b ? 0 : gauges.size() - 1; | |
222 | |
223 return gauges.isEmpty() ? null : gauges.get(idx); | |
224 } | |
225 | |
226 /** | |
227 * Returns the min and max distance of this river. The first position in the | |
228 * resulting array contains the min distance, the second position the max | |
229 * distance. | |
230 * | |
231 * @return the min and max distance of this river. | |
232 */ | |
233 public double[] determineMinMaxDistance() { | |
234 List<Gauge> gauges = getGauges(); | |
235 | |
236 if (gauges == null || gauges.isEmpty()) { | |
237 return null; | |
238 } | |
239 | |
240 double minmax[] = new double[] { Double.MAX_VALUE, Double.MIN_VALUE }; | |
241 | |
242 for (Gauge g: gauges) { | |
243 Range r = g.getRange(); | |
244 | |
245 if (r == null) { | |
246 continue; | |
247 } | |
248 | |
249 double a = r.getA().doubleValue(); | |
250 minmax[0] = minmax[0] < a ? minmax[0] : a; | |
251 | |
252 BigDecimal bigB = r.getB(); | |
253 if (bigB != null) { | |
254 double b = bigB.doubleValue(); | |
255 minmax[1] = minmax[1] > b ? minmax[1] : b; | |
256 } | |
257 } | |
258 | |
259 return minmax; | |
260 } | |
261 } | |
262 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |