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

http://dive4elements.wald.intevation.org