Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WQKmsFactory.java @ 3786:4adc35aa655c
merged flys-artifacts/2.9.1
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:47 +0200 |
parents | 0f7abd95c6e2 |
children | 58bdf95df5e4 |
comparison
equal
deleted
inserted
replaced
3719:e82acd5c86f7 | 3786:4adc35aa655c |
---|---|
1 package de.intevation.flys.artifacts.model; | |
2 | |
3 import java.util.List; | |
4 | |
5 import net.sf.ehcache.Cache; | |
6 import net.sf.ehcache.Element; | |
7 | |
8 import org.apache.log4j.Logger; | |
9 | |
10 import org.hibernate.Session; | |
11 | |
12 import org.hibernate.SQLQuery; | |
13 import org.hibernate.type.StandardBasicTypes; | |
14 | |
15 import de.intevation.flys.artifacts.cache.CacheFactory; | |
16 | |
17 import de.intevation.flys.backend.SessionHolder; | |
18 | |
19 /** | |
20 * Factory to access ready-made WQKms for other (than computed) 'kinds' of | |
21 * WST-data. | |
22 */ | |
23 public class WQKmsFactory | |
24 { | |
25 private static Logger log = Logger.getLogger(WQKmsFactory.class); | |
26 | |
27 /** Query to get km and wqs for wst_id and column_pos. */ | |
28 public static final String SQL_SELECT_WQS = | |
29 "SELECT position, w, q FROM wst_value_table " + | |
30 "WHERE wst_id = :wst_id AND column_pos = :column_pos"; | |
31 | |
32 /** Query to get name for wst_id and column_pos. */ | |
33 public static final String SQL_SELECT_NAME = | |
34 "SELECT name " + | |
35 "FROM wst_columns "+ | |
36 "WHERE wst_id = :wst_id AND position = :column_pos"; | |
37 | |
38 | |
39 /** Hidden constructor, use static methods instead. */ | |
40 private WQKmsFactory() { | |
41 } | |
42 | |
43 | |
44 /** | |
45 * Get WKms for given column and wst_id, caring about the cache. | |
46 */ | |
47 public static WQKms getWQKms(int column, int wst_id) { | |
48 log.debug("WQKmsFactory.getWQKms"); | |
49 Cache cache = CacheFactory.getCache(StaticWQKmsCacheKey.CACHE_NAME); | |
50 | |
51 StaticWQKmsCacheKey cacheKey; | |
52 | |
53 if (cache != null) { | |
54 cacheKey = new StaticWQKmsCacheKey(wst_id, column); | |
55 Element element = cache.get(cacheKey); | |
56 if (element != null) { | |
57 log.debug("Got static wst values from cache"); | |
58 return (WQKms)element.getValue(); | |
59 } | |
60 } | |
61 else { | |
62 cacheKey = null; | |
63 } | |
64 | |
65 WQKms values = getWQKmsUncached(column, wst_id); | |
66 | |
67 if (values != null && cacheKey != null) { | |
68 log.debug("Store static wst values in cache."); | |
69 Element element = new Element(cacheKey, values); | |
70 cache.put(element); | |
71 } | |
72 return values; | |
73 } | |
74 | |
75 | |
76 /** | |
77 * Get WQKms from db. | |
78 * @param column the position columns value | |
79 * @param wst_id database id of the wst | |
80 * @return respective WQKms. | |
81 */ | |
82 public static WQKms getWQKmsUncached(int column, int wst_id) { | |
83 | |
84 if (log.isDebugEnabled()) { | |
85 log.debug("WQKmsFactory.getWQKmsUncached, column " | |
86 + column + ", wst_id " + wst_id); | |
87 } | |
88 | |
89 WQKms wqkms = new WQKms(WKmsFactory.getWKmsName(column, wst_id)); | |
90 | |
91 Session session = SessionHolder.HOLDER.get(); | |
92 SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_WQS) | |
93 .addScalar("position", StandardBasicTypes.DOUBLE) | |
94 .addScalar("w", StandardBasicTypes.DOUBLE) | |
95 .addScalar("q", StandardBasicTypes.DOUBLE); | |
96 sqlQuery.setInteger("wst_id", wst_id); | |
97 sqlQuery.setInteger("column_pos", column); | |
98 | |
99 List<Object []> results = sqlQuery.list(); | |
100 | |
101 int lastColumn = Integer.MAX_VALUE; | |
102 | |
103 for (int i = 0, N = results.size(); i < N; i++) { | |
104 Object[] row = results.get(i); | |
105 // add(w, q, km) | |
106 wqkms.add((Double) row[1], (Double) row[2], (Double) row[0]); | |
107 } | |
108 | |
109 return wqkms; | |
110 } | |
111 } | |
112 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |