comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/sq/StaticSQFactory.java @ 5838:5aa05a7a34b7

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

http://dive4elements.wald.intevation.org