comparison flys-backend/src/main/java/de/intevation/flys/model/Wst.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 9aa0eddc5221
children f834b411ca57
comparison
equal deleted inserted replaced
462:ebf049a1eb53 508:a9c7f6ec3a5a
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 * Determines the min and max Q values of this WST. The min value is placed
110 * in the first field of the resulting array - the max value is placed in
111 * the second field.
112 *
113 * @return the min and max Q values of this WST.
114 */
115 public double[] determineMinMaxQ() {
116 double[] ab = river.determineMinMaxDistance();
117 return determineMinMaxQ(new Range(ab[0], ab[1], river));
118 }
119
120
121 /**
122 * Determines the min and max Q values of this WST in the given range. The
123 * min value is placed in the first field of the resulting array - the max
124 * value is placed in the second field.
125 *
126 * @param range The range used for querying the Q values.
127 *
128 * @return the min and max Q values of this WST.
129 */
130 public double[] determineMinMaxQ(Range range) {
131 Session session = SessionHolder.HOLDER.get();
132
133 Query query = session.createQuery(
134 "select min(q), max(q) from WstQRange where " +
135 " id in " +
136 " (select wstQRange.id from WstColumnQRange where " +
137 " wstColumn.id in (select id from WstColumn where wst.id = :wst)) " +
138 " and range.id in " +
139 " (select id from Range where not (a > :end or b < :start))");
140
141 query.setParameter("wst", getId());
142 query.setParameter("start", range.getA());
143 query.setParameter("end", range.getB());
144
145 List<Object []> results = query.list();
146
147 if (results.isEmpty()) {
148 return null;
149 }
150
151 Object [] result = results.get(0);
152
153 return new double [] {
154 ((BigDecimal)result[0]).doubleValue(),
155 ((BigDecimal)result[1]).doubleValue() };
156 }
157 }
158 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org