Mercurial > dive4elements > river
annotate flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/StaticSQFactory.java @ 5500:700ac898ab0b
Added Factory and model for static sq relations.
author | Raimund Renkert <rrenkert@intevation.de> |
---|---|
date | Thu, 28 Mar 2013 15:15:33 +0100 |
parents | |
children |
rev | line source |
---|---|
5500
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
1 package de.intevation.flys.artifacts.model.sq; |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
2 |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
3 import java.math.BigDecimal; |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
4 import java.util.Date; |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
5 import java.util.List; |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
6 |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
7 import net.sf.ehcache.Cache; |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
8 import net.sf.ehcache.Element; |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
9 |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
10 import org.apache.log4j.Logger; |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
11 import org.hibernate.Query; |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
12 import org.hibernate.Session; |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
13 |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
14 import de.intevation.flys.artifacts.cache.CacheFactory; |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
15 import de.intevation.flys.backend.SessionHolder; |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
16 |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
17 |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
18 public class StaticSQFactory |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
19 { |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
20 private static final Logger log = |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
21 Logger.getLogger(StaticSQFactory.class); |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
22 |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
23 public static final String SQL_SQ = |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
24 "SELECT " + |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
25 "sq.description AS description,"+ |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
26 "ti.start_time AS start_time," + |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
27 "ti.stop_time AS stop_time, " + |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
28 "ms.name AS station_name, " + |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
29 "ms.station AS station_km, " + |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
30 "ms.measurement_type AS measurement_type, " + |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
31 "sqv.parameter AS parameter, " + |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
32 "sqv.a AS a, " + |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
33 "sqv.b AS b, " + |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
34 "sqv.qmax AS qmax " + |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
35 "FROM sq_relation sq " + |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
36 "JOIN time_intervals ti ON ti.id = sq.time_interval_id " + |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
37 "JOIN rivers r ON r.id = sq.river_id " + |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
38 "JOIN sq_relation_value sqv ON sqv.sq_relation_id = sq.id " + |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
39 "JOIN measurement_station ms ON sqv.measurement_station_id = ms.id " + |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
40 "WHERE " + |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
41 "r.name = :river " + |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
42 "AND ms.id = :ms_id "; |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
43 |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
44 |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
45 private StaticSQFactory() { |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
46 } |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
47 |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
48 public static StaticSQContainer getSQRelations( |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
49 String river, |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
50 int measurementStation |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
51 ) { |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
52 Cache cache = CacheFactory.getCache(StaticSQCacheKey.CACHE_NAME); |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
53 |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
54 StaticSQCacheKey cacheKey; |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
55 |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
56 if (cache != null) { |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
57 cacheKey = new StaticSQCacheKey(river, measurementStation); |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
58 Element element = cache.get(cacheKey); |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
59 if (element != null) { |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
60 log.debug("Got static bedheight values from cache"); |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
61 return (StaticSQContainer)element.getValue(); |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
62 } |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
63 } |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
64 else { |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
65 cacheKey = null; |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
66 } |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
67 |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
68 StaticSQContainer values = getUncached(river, measurementStation); |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
69 |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
70 if (values != null && cacheKey != null) { |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
71 log.debug("Store static sq relations in cache."); |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
72 Element element = new Element(cacheKey, values); |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
73 cache.put(element); |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
74 } |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
75 return values; |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
76 } |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
77 |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
78 private static StaticSQContainer getUncached( |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
79 String river, |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
80 int measurementStation |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
81 ) { |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
82 Session session = SessionHolder.HOLDER.get(); |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
83 |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
84 Query query = session.createSQLQuery(SQL_SQ) |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
85 .addScalar("description") |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
86 .addScalar("start_time") |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
87 .addScalar("stop_time") |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
88 .addScalar("station_name") |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
89 .addScalar("station_km") |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
90 .addScalar("measurement_type") |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
91 .addScalar("parameter") |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
92 .addScalar("a") |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
93 .addScalar("b") |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
94 .addScalar("qmax"); |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
95 |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
96 query.setParameter("river", river); |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
97 query.setParameter("ms_id", measurementStation); |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
98 |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
99 List<Object []> list = query.list(); |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
100 |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
101 if (list.isEmpty()) { |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
102 return new StaticSQContainer(); |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
103 } |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
104 |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
105 StaticSQContainer sq = new StaticSQContainer(); |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
106 sq.setDescription((String)list.get(0)[0]); |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
107 sq.setStationName((String)list.get(0)[3]); |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
108 sq.setKm(((BigDecimal)list.get(0)[4]).doubleValue()); |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
109 |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
110 for (Object[] row : list) { |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
111 StaticSQRelation relation = new StaticSQRelation(); |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
112 relation.setStartTime((Date)row[1]); |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
113 relation.setStopTime((Date)row[2]); |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
114 relation.setType((String)row[5]); |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
115 relation.setParameter((String)row[6]); |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
116 relation.setA(((BigDecimal)row[7]).doubleValue()); |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
117 relation.setB(((BigDecimal)row[8]).doubleValue()); |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
118 relation.setQmax(((BigDecimal)row[9]).doubleValue()); |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
119 sq.addSQRelation(relation); |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
120 } |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
121 return sq; |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
122 } |
700ac898ab0b
Added Factory and model for static sq relations.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
123 } |