Mercurial > dive4elements > river
comparison flys-backend/src/main/java/de/intevation/flys/model/CrossSection.java @ 2877:f0a67bc0e777 2.7
merged flys-backend/2.7
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:31 +0200 |
parents | e237a08acf6e |
children | 2ae732e2c65c |
comparison
equal
deleted
inserted
replaced
2793:6310b1582f2d | 2877:f0a67bc0e777 |
---|---|
1 package de.intevation.flys.model; | |
2 | |
3 import java.io.Serializable; | |
4 | |
5 import java.util.List; | |
6 import java.util.ArrayList; | |
7 | |
8 import java.awt.geom.Point2D; | |
9 | |
10 import javax.persistence.Entity; | |
11 import javax.persistence.Id; | |
12 import javax.persistence.Table; | |
13 import javax.persistence.GeneratedValue; | |
14 import javax.persistence.Column; | |
15 import javax.persistence.SequenceGenerator; | |
16 import javax.persistence.GenerationType; | |
17 import javax.persistence.OneToOne; | |
18 import javax.persistence.OneToMany; | |
19 import javax.persistence.OrderBy; | |
20 import javax.persistence.JoinColumn; | |
21 | |
22 import java.math.MathContext; | |
23 import java.math.BigDecimal; | |
24 | |
25 import org.hibernate.Session; | |
26 import org.hibernate.SQLQuery; | |
27 import org.hibernate.Query; | |
28 | |
29 import org.hibernate.type.StandardBasicTypes; | |
30 | |
31 import de.intevation.flys.backend.SessionHolder; | |
32 | |
33 @Entity | |
34 @Table(name = "cross_sections") | |
35 public class CrossSection | |
36 implements Serializable | |
37 { | |
38 public static final MathContext PRECISION = new MathContext(6); | |
39 | |
40 public static final String SQL_FAST_CROSS_SECTION_LINES = | |
41 "SELECT km, x, y, csl.id AS csl_id " + | |
42 "FROM cross_section_lines csl JOIN cross_section_points csp " + | |
43 "ON csp.cross_section_line_id = csl.id " + | |
44 "WHERE csl.cross_section_id = :cs_id AND " + | |
45 "km between :from_km AND :to_km " + | |
46 "ORDER BY csl.id, csp.col_pos"; | |
47 | |
48 private Integer id; | |
49 private River river; | |
50 private TimeInterval timeInterval; | |
51 private String description; | |
52 private List<CrossSectionLine> lines; | |
53 | |
54 public CrossSection() { | |
55 } | |
56 | |
57 public CrossSection( | |
58 River river, | |
59 TimeInterval timeInterval, | |
60 String description | |
61 ) { | |
62 this.river = river; | |
63 this.timeInterval = timeInterval; | |
64 this.description = description; | |
65 } | |
66 | |
67 @Id | |
68 @SequenceGenerator( | |
69 name = "SEQUENCE_CROSS_SECTIONS_ID_SEQ", | |
70 sequenceName = "CROSS_SECTIONS_ID_SEQ", | |
71 allocationSize = 1) | |
72 @GeneratedValue( | |
73 strategy = GenerationType.SEQUENCE, | |
74 generator = "SEQUENCE_CROSS_SECTIONS_ID_SEQ") | |
75 @Column(name = "id") | |
76 public Integer getId() { | |
77 return id; | |
78 } | |
79 | |
80 public void setId(Integer id) { | |
81 this.id = id; | |
82 } | |
83 | |
84 @OneToOne | |
85 @JoinColumn(name = "river_id") | |
86 public River getRiver() { | |
87 return river; | |
88 } | |
89 | |
90 public void setRiver(River river) { | |
91 this.river = river; | |
92 } | |
93 | |
94 @OneToOne | |
95 @JoinColumn(name = "time_interval_id") | |
96 public TimeInterval getTimeInterval() { | |
97 return timeInterval; | |
98 } | |
99 | |
100 public void setTimeInterval(TimeInterval timeInterval) { | |
101 this.timeInterval = timeInterval; | |
102 } | |
103 | |
104 @Column(name = "description") | |
105 public String getDescription() { | |
106 return description; | |
107 } | |
108 | |
109 public void setDescription(String description) { | |
110 this.description = description; | |
111 } | |
112 | |
113 @OneToMany | |
114 @OrderBy("km") | |
115 @JoinColumn(name="cross_section_id") | |
116 public List<CrossSectionLine> getLines() { | |
117 return lines; | |
118 } | |
119 | |
120 public void setLines(List<CrossSectionLine> lines) { | |
121 this.lines = lines; | |
122 } | |
123 | |
124 public List<CrossSectionLine> getLines(double startKm, double endKm) { | |
125 Session session = SessionHolder.HOLDER.get(); | |
126 Query query = session.createQuery( | |
127 "from CrossSectionLine where crossSection=:crossSection " + | |
128 "and km between :startKm and :endKm order by km"); | |
129 query.setParameter("crossSection", this); | |
130 query.setParameter("startKm", new BigDecimal(startKm, PRECISION)); | |
131 query.setParameter("endKm", new BigDecimal(endKm, PRECISION)); | |
132 | |
133 return query.list(); | |
134 } | |
135 | |
136 public List<FastCrossSectionLine> getFastLines( | |
137 double startKm, | |
138 double endKm | |
139 ) { | |
140 Session session = SessionHolder.HOLDER.get(); | |
141 | |
142 SQLQuery sqlQuery = session.createSQLQuery(SQL_FAST_CROSS_SECTION_LINES) | |
143 .addScalar("km", StandardBasicTypes.DOUBLE) | |
144 .addScalar("x", StandardBasicTypes.DOUBLE) | |
145 .addScalar("y", StandardBasicTypes.DOUBLE) | |
146 .addScalar("csl_id", StandardBasicTypes.INTEGER); | |
147 | |
148 sqlQuery | |
149 .setInteger("cs_id", getId()) | |
150 .setDouble("from_km", startKm) | |
151 .setDouble("to_km", endKm); | |
152 | |
153 List<Object []> results = sqlQuery.list(); | |
154 | |
155 ArrayList<Point2D> points = new ArrayList<Point2D>(500); | |
156 ArrayList<FastCrossSectionLine> lines = | |
157 new ArrayList<FastCrossSectionLine>(); | |
158 | |
159 Integer lastId = null; | |
160 Double lastKm = null; | |
161 | |
162 for (Object [] result: results) { | |
163 Double km = (Double)result[0]; | |
164 Double x = (Double)result[1]; | |
165 Double y = (Double)result[2]; | |
166 Integer id = (Integer)result[3]; | |
167 | |
168 if (lastId != null && !lastId.equals(id)) { | |
169 points.trimToSize(); | |
170 FastCrossSectionLine line = | |
171 new FastCrossSectionLine(lastKm, points); | |
172 lines.add(line); | |
173 points = new ArrayList<Point2D>(500); | |
174 } | |
175 | |
176 Point2D p = new Point2D.Double(x, y); | |
177 | |
178 if (CrossSectionLine.isValid(p)) { | |
179 points.add(p); | |
180 } | |
181 | |
182 lastKm = km; | |
183 lastId = id; | |
184 } | |
185 | |
186 if (lastId != null) { | |
187 points.trimToSize(); | |
188 FastCrossSectionLine line = | |
189 new FastCrossSectionLine(lastKm, points); | |
190 lines.add(line); | |
191 } | |
192 | |
193 lines.trimToSize(); | |
194 | |
195 return lines; | |
196 } | |
197 } | |
198 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |