comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/WQKmsFactory.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/WQKmsFactory.java@bd047b71ab37
children 4897a58c8746
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.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 org.dive4elements.river.artifacts.cache.CacheFactory;
16
17 import org.dive4elements.river.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 /** Get wst_id and position from wst_columns. */
33 public static final String SQL_SELECT_COLUMN =
34 "SELECT wst_id, position FROM wst_columns WHERE id = :column_id";
35
36 /** Query to get name for wst_id and column_pos. */
37 public static final String SQL_SELECT_NAME =
38 "SELECT name " +
39 "FROM wst_columns "+
40 "WHERE id = :column_id";
41
42
43 /** Hidden constructor, use static methods instead. */
44 private WQKmsFactory() {
45 }
46
47
48 /**
49 * Get WKms for given column (pos) and wst_id, caring about the cache.
50 */
51 public static WQKms getWQKms(int columnPos, int wst_id) {
52 log.debug("WQKmsFactory.getWQKms");
53 Cache cache = CacheFactory.getCache(StaticWQKmsCacheKey.CACHE_NAME);
54
55 StaticWQKmsCacheKey cacheKey;
56
57 if (cache != null) {
58 cacheKey = new StaticWQKmsCacheKey(wst_id, columnPos);
59 Element element = cache.get(cacheKey);
60 if (element != null) {
61 log.debug("Got static wst values from cache");
62 return (WQKms)element.getValue();
63 }
64 }
65 else {
66 cacheKey = null;
67 }
68
69 WQKms values = getWQKmsUncached(columnPos, wst_id);
70
71 if (values != null && cacheKey != null) {
72 log.debug("Store static wst values in cache.");
73 Element element = new Element(cacheKey, values);
74 cache.put(element);
75 }
76 return values;
77 }
78
79 /**
80 * Get WKms for given column (id), caring about the cache.
81 */
82 public static WQKms getWQKmsCID(int columnID) {
83 log.debug("WQKmsFactory.getWQKms");
84 Cache cache = CacheFactory.getCache(StaticWQKmsCacheKey.CACHE_NAME);
85
86 StaticWQKmsCacheKey cacheKey;
87
88 if (cache != null) {
89 cacheKey = new StaticWQKmsCacheKey(-columnID, -columnID);
90 Element element = cache.get(cacheKey);
91 if (element != null) {
92 log.debug("Got static wst values from cache");
93 return (WQKms)element.getValue();
94 }
95 }
96 else {
97 cacheKey = null;
98 }
99
100 int[] cInfo = getColumn(columnID);
101 if (cInfo == null) return null;
102 WQKms values = getWQKmsUncached(cInfo[1], cInfo[0]);
103
104
105 if (values != null && cacheKey != null) {
106 log.debug("Store static wst values in cache.");
107 Element element = new Element(cacheKey, values);
108 cache.put(element);
109 }
110 return values;
111 }
112
113
114 /**
115 * Get WQKms from db.
116 * @param column the position columns value
117 * @param wst_id database id of the wst
118 * @return respective WQKms.
119 */
120 public static WQKms getWQKmsUncached(int column, int wst_id) {
121
122 if (log.isDebugEnabled()) {
123 log.debug("WQKmsFactory.getWQKmsUncached, column "
124 + column + ", wst_id " + wst_id);
125 }
126
127 WQKms wqkms = new WQKms(WKmsFactory.getWKmsName(column, wst_id));
128
129 Session session = SessionHolder.HOLDER.get();
130 SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_WQS)
131 .addScalar("position", StandardBasicTypes.DOUBLE)
132 .addScalar("w", StandardBasicTypes.DOUBLE)
133 .addScalar("q", StandardBasicTypes.DOUBLE);
134 sqlQuery.setInteger("wst_id", wst_id);
135 sqlQuery.setInteger("column_pos", column);
136
137 List<Object []> results = sqlQuery.list();
138
139 for (int i = 0, N = results.size(); i < N; i++) {
140 Object[] row = results.get(i);
141 // add(w, q, km)
142 wqkms.add((Double) row[1], (Double) row[2], (Double) row[0]);
143 }
144
145 return wqkms;
146 }
147
148
149 /**
150 * Get WQKms from db.
151 * @param columnID the columns database id value
152 * @param wst_id database id of the wst
153 * @return respective WQKms.
154 */
155 public static int[] getColumn(int columnID) {
156
157 if (log.isDebugEnabled()) {
158 log.debug("WQKmsFactory.getColumn, columnID "
159 + columnID);
160 }
161
162 Session session = SessionHolder.HOLDER.get();
163
164 SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_COLUMN)
165 .addScalar("wst_id", StandardBasicTypes.INTEGER)
166 .addScalar("position", StandardBasicTypes.INTEGER);
167 sqlQuery.setInteger("column_id", columnID);
168
169 List<Object []> results = sqlQuery.list();
170
171 for (int i = 0, N = results.size(); i < N; i++) {
172 Object[] row = results.get(i);
173 return new int[] {(Integer)row[0], (Integer)row[1]};
174 }
175
176 return null;
177 }
178
179
180 /** Get name for a WKms. */
181 public static String getWQKmsName(int columnID) {
182 log.debug("WQKmsFactory.getWQKmsName c/" + columnID);
183
184 String name = null;
185 Session session = SessionHolder.HOLDER.get();
186
187 SQLQuery nameQuery = session.createSQLQuery(SQL_SELECT_NAME)
188 .addScalar("name", StandardBasicTypes.STRING);
189 nameQuery.setInteger("column_id", columnID);
190
191 List<String> names = nameQuery.list();
192 if (names.size() >= 1) {
193 name = names.get(0);
194 }
195
196 return name;
197 }
198 }
199 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org