comparison flys-backend/src/main/java/de/intevation/flys/model/River.java @ 508:a9c7f6ec3a5a 2.3.1

merged flys-backend/2.3.1
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:12 +0200
parents ce9c6f05f464
children ca13926b8871
comparison
equal deleted inserted replaced
462:ebf049a1eb53 508:a9c7f6ec3a5a
1 package de.intevation.flys.model;
2
3 import java.io.Serializable;
4
5 import java.math.BigDecimal;
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.OneToMany;
14 import javax.persistence.JoinColumn;
15 import javax.persistence.GenerationType;
16
17 import java.util.List;
18
19 import org.hibernate.Session;
20 import org.hibernate.Query;
21
22 import de.intevation.flys.backend.SessionHolder;
23
24
25 @Entity
26 @Table(name = "rivers")
27 public class River
28 implements Serializable
29 {
30 private Integer id;
31
32 private String name;
33
34 private boolean kmUp;
35
36 private List<Gauge> gauges;
37
38 @Id
39 @SequenceGenerator(
40 name = "SEQUENCE_RIVERS_ID_SEQ",
41 sequenceName = "RIVERS_ID_SEQ",
42 allocationSize = 1)
43 @GeneratedValue(
44 strategy = GenerationType.SEQUENCE,
45 generator = "SEQUENCE_RIVERS_ID_SEQ")
46 @Column(name = "id")
47 public Integer getId() {
48 return id;
49 }
50
51 public void setId(Integer id) {
52 this.id = id;
53 }
54
55 @Column(name = "name")
56 public String getName() {
57 return name;
58 }
59
60 public void setName(String name) {
61 this.name = name;
62 }
63
64 @Column(name = "km_up")
65 public boolean getKmUp() {
66 return kmUp;
67 }
68
69 public void setKmUp(boolean kmUp) {
70 this.kmUp = kmUp;
71 }
72
73 public River() {
74 }
75
76 public River(String name) {
77 this.name = name;
78 }
79
80 @OneToMany
81 @JoinColumn(name="river_id")
82 public List<Gauge> getGauges() {
83 return gauges;
84 }
85
86 public void setGauges(List<Gauge> gauges) {
87 this.gauges = gauges;
88 }
89
90 public String toString() {
91 return name != null ? name : "";
92 }
93
94
95 /**
96 * This method returns the gauges that intersect with <i>a</i> and
97 * <i>b</i>,
98 *
99 * @param a A start point.
100 * @param b An end point.
101 *
102 * @return the intersecting gauges.
103 */
104 public List<Gauge> determineGauges(double a, double b) {
105 Session session = SessionHolder.HOLDER.get();
106
107 Query query = session.createQuery(
108 "from Gauge where river=:river " +
109 "and not (range.a > :b or range.b < :a) order by a");
110 query.setParameter("river", this);
111 query.setParameter("a", new BigDecimal(a));
112 query.setParameter("b", new BigDecimal(b));
113
114 return query.list();
115 }
116
117
118 /**
119 * This method returns the first gauge that is intersected by <i>a</i> and
120 * <i>b</i>,
121 *
122 * @param a A start point.
123 * @param b An end point.
124 *
125 * @return the first intersecting gauge.
126 */
127 public Gauge determineGauge(double a, double b) {
128 List<Gauge> gauges = determineGauges(a, b);
129
130 return gauges != null && gauges.size() > 0 ? gauges.get(0) : null;
131 }
132
133 /**
134 * Returns the min and max distance of this river. The first position in the
135 * resulting array contains the min distance, the second position the max
136 * distance.
137 *
138 * @return the min and max distance of this river.
139 */
140 public double[] determineMinMaxDistance() {
141 if (gauges == null) {
142 return null;
143 }
144
145 double minmax[] = new double[] { Double.MAX_VALUE, Double.MIN_VALUE };
146
147 for (Gauge g: gauges) {
148 Range r = g.getRange();
149
150 double a = r.getA().doubleValue();
151 minmax[0] = minmax[0] < a ? minmax[0] : a;
152
153 BigDecimal bigB = r.getB();
154 if (bigB != null) {
155 double b = bigB.doubleValue();
156 minmax[1] = minmax[1] > b ? minmax[1] : b;
157 }
158 }
159
160 return minmax;
161 }
162 }
163 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org