comparison backend/src/main/java/org/dive4elements/river/model/CrossSection.java @ 6538:de62db0f2035

issue1157: Let CrossSection be able to find out whether it should be active & master.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Thu, 04 Jul 2013 11:52:06 +0200
parents c155f8f57b7e
children 7e697c6eb379
comparison
equal deleted inserted replaced
6537:f2722602936c 6538:de62db0f2035
36 36
37 import org.hibernate.type.StandardBasicTypes; 37 import org.hibernate.type.StandardBasicTypes;
38 38
39 import org.dive4elements.river.backend.SessionHolder; 39 import org.dive4elements.river.backend.SessionHolder;
40 40
41 import org.apache.log4j.Logger;
42
41 @Entity 43 @Entity
42 @Table(name = "cross_sections") 44 @Table(name = "cross_sections")
43 public class CrossSection 45 public class CrossSection
44 implements Serializable 46 implements Serializable
45 { 47 {
48 private static Logger logger =
49 Logger.getLogger(CrossSection.class);
50
46 public static final MathContext PRECISION = new MathContext(6); 51 public static final MathContext PRECISION = new MathContext(6);
47 52
48 public static final String SQL_FAST_CROSS_SECTION_LINES = 53 public static final String SQL_FAST_CROSS_SECTION_LINES =
49 "SELECT km, x, y, csl.id AS csl_id " + 54 "SELECT km, x, y, csl.id AS csl_id " +
50 "FROM cross_section_lines csl JOIN cross_section_points csp " + 55 "FROM cross_section_lines csl JOIN cross_section_points csp " +
58 "SELECT cross_section_id, MIN(km) AS minkm, MAX(km) AS maxkm " + 63 "SELECT cross_section_id, MIN(km) AS minkm, MAX(km) AS maxkm " +
59 "FROM cross_section_lines " + 64 "FROM cross_section_lines " +
60 "WHERE cross_section_id IN " + 65 "WHERE cross_section_id IN " +
61 " (SELECT id FROM cross_sections WHERE river_id = :river_id) " + 66 " (SELECT id FROM cross_sections WHERE river_id = :river_id) " +
62 " GROUP BY cross_section_id" + 67 " GROUP BY cross_section_id" +
63 ") AS cs_ranges WHERE :km BETWEEN minkm AND maxkm ORDER BY :km - minkm "; 68 ") AS cs_ranges " +
69 "JOIN cross_sections as CS on cs_ranges.cross_section_id = cs.id " +
70 "LEFT OUTER JOIN time_intervals on cs.time_interval_id = time_intervals.id " +
71 "WHERE :km BETWEEN minkm AND maxkm " +
72 "ORDER BY stop_time desc, start_time asc, :km - minkm";
64 // Order by time interval missing. 73 // Order by time interval missing.
65 74
66 private Integer id; 75 private Integer id;
67 private River river; 76 private River river;
68 private TimeInterval timeInterval; 77 private TimeInterval timeInterval;
211 220
212 lines.trimToSize(); 221 lines.trimToSize();
213 222
214 return lines; 223 return lines;
215 } 224 }
225
226 /**
227 * True if the given section is the "newest" for that river and has values at km.
228 * @param km Given station.
229 * @return true if the section has the most advanced end of its validity interval
230 * or the most advanced start of its validity interval.
231 */
232 public boolean shouldBeMaster(double km) {
233 Session session = SessionHolder.HOLDER.get();
234
235 SQLQuery sqlQuery = session.createSQLQuery(SQL_MIN_MAX)
236 .addScalar("cross_section_id", StandardBasicTypes.INTEGER);
237
238 sqlQuery
239 .setInteger("river_id", getRiver().getId())
240 .setDouble("km", km);
241
242 List<Integer> results = sqlQuery.list();
243
244 for (Integer result: results) {
245 if (result == getId()) {
246 return true;
247 }
248 }
249 return false;
250 }
216 } 251 }
217 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 252 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org