Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WQKmsFactory.java @ 1825:02cd002205a3
Added 'static' wqkms data access.
flys-artifacts/trunk@3154 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Felix Wolfsteller <felix.wolfsteller@intevation.de> |
---|---|
date | Thu, 03 Nov 2011 13:22:55 +0000 |
parents | |
children | 853cd2120d69 |
comparison
equal
deleted
inserted
replaced
1824:982956bde69e | 1825:02cd002205a3 |
---|---|
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.model.WQKms; | |
16 | |
17 import de.intevation.flys.artifacts.cache.CacheFactory; | |
18 | |
19 import de.intevation.flys.backend.SessionHolder; | |
20 | |
21 /** | |
22 * Factory to access ready-made WQKms for other (than computed) 'kinds' of | |
23 * WST-data. | |
24 */ | |
25 public class WQKmsFactory | |
26 { | |
27 private static Logger log = Logger.getLogger(WQKmsFactory.class); | |
28 | |
29 /** Query to get km and wqs for wst_id and column_pos. */ | |
30 public static final String SQL_SELECT_WQS = | |
31 "SELECT position, w, q FROM wst_value_table " + | |
32 "WHERE wst_id = :wst_id AND column_pos = :column_pos"; | |
33 | |
34 /** Query to get name for wst_id and column_pos. */ | |
35 public static final String SQL_SELECT_NAME = | |
36 "SELECT name " + | |
37 "FROM wst_columns "+ | |
38 "WHERE wst_id = :wst_id AND position = :column_pos"; | |
39 | |
40 | |
41 /** Hidden constructor, use static methods instead. */ | |
42 private WQKmsFactory() { | |
43 ; | |
44 } | |
45 | |
46 | |
47 /** | |
48 * Get WKms for given column and wst_id, caring about the cache. | |
49 */ | |
50 public static WQKms getWQKms(int column, int wst_id) { | |
51 log.debug("WQKmsFactory.getWQKms"); | |
52 Cache cache = CacheFactory.getCache(StaticWQKmsCacheKey.CACHE_NAME); | |
53 | |
54 StaticWQKmsCacheKey cacheKey; | |
55 | |
56 if (cache != null) { | |
57 cacheKey = new StaticWQKmsCacheKey(wst_id, column); | |
58 Element element = cache.get(cacheKey); | |
59 if (element != null) { | |
60 log.debug("Got static wst values from cache"); | |
61 return (WQKms)element.getValue(); | |
62 } | |
63 } | |
64 else { | |
65 cacheKey = null; | |
66 } | |
67 | |
68 WQKms values = getWQKmsUncached(column, wst_id); | |
69 | |
70 if (values != null && cacheKey != null) { | |
71 log.debug("Store static wst values in cache."); | |
72 Element element = new Element(cacheKey, values); | |
73 cache.put(element); | |
74 } | |
75 return values; | |
76 } | |
77 | |
78 | |
79 /** | |
80 * Get WQKms from db. | |
81 * @param column the position columns value | |
82 * @param wst_id database id of the wst | |
83 * @return respective WQKms. | |
84 */ | |
85 public static WQKms getWQKmsUncached(int column, int wst_id) { | |
86 | |
87 if (log.isDebugEnabled()) { | |
88 log.debug("WQKmsFactory.getWQKmsUncached, column " | |
89 + column + ", wst_id " + wst_id); | |
90 } | |
91 | |
92 WQKms wqkms = new WQKms(WKmsFactory.getWKmsName(column, wst_id)); | |
93 | |
94 Session session = SessionHolder.HOLDER.get(); | |
95 SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_WQS) | |
96 .addScalar("position", StandardBasicTypes.DOUBLE) | |
97 .addScalar("w", StandardBasicTypes.DOUBLE) | |
98 .addScalar("q", StandardBasicTypes.DOUBLE); | |
99 sqlQuery.setInteger("wst_id", wst_id); | |
100 sqlQuery.setInteger("column_pos", column); | |
101 | |
102 List<Object []> results = sqlQuery.list(); | |
103 | |
104 double kms [] = new double[results.size()]; | |
105 double ws [] = new double[results.size()]; | |
106 double qs [] = new double[results.size()]; | |
107 | |
108 int lastColumn = Integer.MAX_VALUE; | |
109 | |
110 for (int i = 0, N = results.size(); i < N; i++) { | |
111 Object[] row = results.get(i); | |
112 // add(w, q, km) | |
113 wqkms.add((Double) row[1], (Double) row[2], (Double) row[0]); | |
114 } | |
115 | |
116 return wqkms; | |
117 } | |
118 } | |
119 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |