comparison flys-backend/src/main/java/de/intevation/flys/model/Wst.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 0acf28a3d28a
children 027736510a30
comparison
equal deleted inserted replaced
3812:f788d2d901d6 3813:6aeee2250418
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.Query;
22
23 import de.intevation.flys.backend.SessionHolder;
24
25
26 @Entity
27 @Table(name = "wsts")
28 public class Wst
29 implements Serializable
30 {
31 private static Logger logger = Logger.getLogger(Wst.class);
32
33 private Integer id;
34 private River river;
35 private String description;
36 private Integer kind;
37
38 private List<WstColumn> columns;
39
40 public Wst() {
41 }
42
43 public Wst(River river, String description) {
44 this(river, description, 0);
45 }
46
47 public Wst(River river, String description, Integer kind) {
48 this.river = river;
49 this.description = description;
50 this.kind = kind;
51 }
52
53 @Id
54 @SequenceGenerator(
55 name = "SEQUENCE_WSTS_ID_SEQ",
56 sequenceName = "WSTS_ID_SEQ",
57 allocationSize = 1)
58 @GeneratedValue(
59 strategy = GenerationType.SEQUENCE,
60 generator = "SEQUENCE_WSTS_ID_SEQ")
61 @Column(name = "id")
62 public Integer getId() {
63 return id;
64 }
65
66 public void setId(Integer id) {
67 this.id = id;
68 }
69
70 @OneToOne
71 @JoinColumn(name = "river_id" )
72 public River getRiver() {
73 return river;
74 }
75
76 public void setRiver(River river) {
77 this.river = river;
78 }
79
80 @Column(name = "description")
81 public String getDescription() {
82 return description;
83 }
84
85 public void setDescription(String description) {
86 this.description = description;
87 }
88
89 @Column(name = "kind")
90 public Integer getKind() {
91 return kind;
92 }
93
94 public void setKind(Integer kind) {
95 this.kind = kind;
96 }
97
98 @OneToMany
99 @JoinColumn(name="wst_id")
100 public List<WstColumn> getColumns() {
101 return columns;
102 }
103
104 public void setColumns(List<WstColumn> columns) {
105 this.columns = columns;
106 }
107
108
109 /**
110 * Determines the min and max Q values of this WST. The min value is placed
111 * in the first field of the resulting array - the max value is placed in
112 * the second field.
113 *
114 * @return the min and max Q values of this WST.
115 */
116 public double[] determineMinMaxQ() {
117 double[] ab = river.determineMinMaxDistance();
118 return determineMinMaxQ(new Range(ab[0], ab[1], river));
119 }
120
121
122 /**
123 * Determines the min and max Q values of this WST in the given range. The
124 * min value is placed in the first field of the resulting array - the max
125 * value is placed in the second field.
126 *
127 * @param range The range used for querying the Q values.
128 *
129 * @return the min and max Q values of this WST.
130 */
131 public double[] determineMinMaxQ(Range range) {
132 Session session = SessionHolder.HOLDER.get();
133
134 Query query = session.createQuery(
135 "select min(q), max(q) from WstQRange where " +
136 " id in " +
137 " (select wstQRange.id from WstColumnQRange where " +
138 " wstColumn.id in (select id from WstColumn where wst.id = :wst)) " +
139 " and range.id in " +
140 " (select id from Range where not (a > :end or b < :start))");
141
142 query.setParameter("wst", getId());
143 query.setParameter("start", range.getA());
144 query.setParameter("end", range.getB());
145
146 List<Object []> results = query.list();
147
148 if (results.isEmpty()) {
149 return null;
150 }
151
152 Object [] result = results.get(0);
153
154 return new double [] {
155 ((BigDecimal)result[0]).doubleValue(),
156 ((BigDecimal)result[1]).doubleValue() };
157 }
158 }
159 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org