Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WstValueTableFactory.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 | b2ea89a665bc |
children | f4fd64a4d502 |
comparison
equal
deleted
inserted
replaced
3719:e82acd5c86f7 | 3786:4adc35aa655c |
---|---|
1 package de.intevation.flys.artifacts.model; | |
2 | |
3 import java.util.Arrays; | |
4 import java.util.List; | |
5 import java.util.ArrayList; | |
6 | |
7 import net.sf.ehcache.Cache; | |
8 import net.sf.ehcache.Element; | |
9 | |
10 import de.intevation.flys.artifacts.cache.CacheFactory; | |
11 | |
12 import de.intevation.flys.backend.SessionHolder; | |
13 | |
14 import org.apache.log4j.Logger; | |
15 | |
16 import de.intevation.flys.model.River; | |
17 import de.intevation.flys.model.Wst; | |
18 | |
19 import org.hibernate.Session; | |
20 import org.hibernate.Query; | |
21 import org.hibernate.SQLQuery; | |
22 | |
23 import org.hibernate.type.StandardBasicTypes; | |
24 | |
25 /** | |
26 * Creates WstValueTable s from database. | |
27 * WstValueTable s are used to interpolate given w/q/km values. | |
28 */ | |
29 public class WstValueTableFactory | |
30 { | |
31 private static Logger log = Logger.getLogger(WstValueTableFactory.class); | |
32 | |
33 public static final int DEFAULT_KIND = 0; | |
34 | |
35 // TODO: put this into a property file | |
36 | |
37 public static final String HQL_WST = | |
38 "from Wst where river=:river and kind=:kind"; | |
39 | |
40 public static final String SQL_SELECT_NAMES_POS = | |
41 "SELECT position, name FROM wst_columns " + | |
42 "WHERE wst_id = :wst_id ORDER BY position"; | |
43 | |
44 /** Select Qs for wst (view sorted by column). */ | |
45 public static final String SQL_SELECT_QS = | |
46 "SELECT column_pos, q, a, b FROM wst_q_values " + | |
47 "WHERE wst_id = :wst_id"; | |
48 | |
49 // (sorted by km) | |
50 public static final String SQL_SELECT_WS = | |
51 "SELECT km, w, column_pos FROM wst_w_values " + | |
52 "WHERE wst_id = :wst_id"; | |
53 | |
54 /** Statement to query qranges of a single column. */ | |
55 public static final String SQL_SELECT_QS_AT_COL = | |
56 "SELECT q, a, b FROM wst_q_values " + | |
57 "WHERE wst_id = :wst_id AND column_pos = :column_pos"; | |
58 | |
59 // (sorted by km) | |
60 public static final String SQL_SELECT_WS_AT_COL = | |
61 "SELECT km, w FROM wst_w_values " + | |
62 "WHERE wst_id = :wst_id AND column_pos = :column_pos"; | |
63 | |
64 | |
65 private WstValueTableFactory() { | |
66 } | |
67 | |
68 | |
69 public static WstValueTable getTable(River river) { | |
70 return getTable(river, DEFAULT_KIND); | |
71 } | |
72 | |
73 | |
74 /** | |
75 * Get WstValueTable to interpolate values of a given Wst. | |
76 */ | |
77 public static WstValueTable getTable(int wst_id) { | |
78 | |
79 Cache cache = CacheFactory.getCache(WstValueTableCacheKey.CACHE_NAME); | |
80 | |
81 WstValueTableCacheKey cacheKey; | |
82 | |
83 if (cache != null) { | |
84 // "-1" is the symbolic river-id for "no river, but wst_id". | |
85 cacheKey = new WstValueTableCacheKey(-1, wst_id); | |
86 Element element = cache.get(cacheKey); | |
87 if (element != null) { | |
88 log.debug("Got specific wst value table from cache"); | |
89 return (WstValueTable) element.getValue(); | |
90 } | |
91 } | |
92 else { | |
93 cacheKey = null; | |
94 } | |
95 | |
96 Session session = SessionHolder.HOLDER.get(); | |
97 | |
98 // Fetch data for one column only. | |
99 | |
100 WstValueTable.Column [] columns = loadColumns(session, wst_id); | |
101 loadQRanges(session, columns, wst_id); | |
102 List<WstValueTable.Row> rows = loadRows(session, wst_id, columns.length); | |
103 | |
104 WstValueTable valueTable = new WstValueTable(columns, rows); | |
105 | |
106 if (valueTable != null && cacheKey != null) { | |
107 log.debug("Store wst value table in cache"); | |
108 Element element = new Element(cacheKey, valueTable); | |
109 cache.put(element); | |
110 } | |
111 | |
112 return valueTable; | |
113 } | |
114 | |
115 /** | |
116 * Get Table for a specific column of a wst. | |
117 */ | |
118 public static WstValueTable getWstColumnTable(int wst_id, int col_pos) { | |
119 | |
120 Cache cache = CacheFactory.getCache(WstValueTableCacheKey.CACHE_NAME); | |
121 | |
122 WstValueTableCacheKey cacheKey; | |
123 | |
124 if (cache != null) { | |
125 // A negaitve/negative number is the symbolic 'river-id' for | |
126 // "no river and kind but wst_id and colpos". | |
127 cacheKey = new WstValueTableCacheKey(-wst_id, -col_pos); | |
128 Element element = cache.get(cacheKey); | |
129 if (element != null) { | |
130 log.debug("Got specific wst value table from cache"); | |
131 return (WstValueTable) element.getValue(); | |
132 } | |
133 } | |
134 else { | |
135 cacheKey = null; | |
136 } | |
137 | |
138 Session session = SessionHolder.HOLDER.get(); | |
139 | |
140 // Fetch data for one column only. | |
141 | |
142 WstValueTable.Column [] columns = loadColumn(session, wst_id, col_pos); | |
143 loadQRanges(session, columns, wst_id, col_pos); | |
144 List<WstValueTable.Row> rows = loadRowsOneColumn(session, wst_id, col_pos); | |
145 | |
146 WstValueTable valueTable = new WstValueTable(columns, rows); | |
147 | |
148 if (valueTable != null && cacheKey != null) { | |
149 log.debug("Store wst value table in cache (wst: " | |
150 + wst_id + "/ col: " + col_pos + ")"); | |
151 Element element = new Element(cacheKey, valueTable); | |
152 cache.put(element); | |
153 } | |
154 | |
155 return valueTable; | |
156 } | |
157 | |
158 | |
159 /** | |
160 * Get table for first wst of given kind at given river. | |
161 */ | |
162 public static WstValueTable getTable(River river, int kind) { | |
163 | |
164 Cache cache = CacheFactory.getCache(WstValueTableCacheKey.CACHE_NAME); | |
165 | |
166 WstValueTableCacheKey cacheKey; | |
167 | |
168 if (cache != null) { | |
169 cacheKey = new WstValueTableCacheKey(river.getId(), kind); | |
170 Element element = cache.get(cacheKey); | |
171 if (element != null) { | |
172 log.debug("got wst value table from cache"); | |
173 return (WstValueTable)element.getValue(); | |
174 } | |
175 } | |
176 else { | |
177 cacheKey = null; | |
178 } | |
179 | |
180 WstValueTable valueTable = getTableUncached(river, kind); | |
181 | |
182 if (valueTable != null && cacheKey != null) { | |
183 log.debug("store wst value table in cache"); | |
184 Element element = new Element(cacheKey, valueTable); | |
185 cache.put(element); | |
186 } | |
187 | |
188 return valueTable; | |
189 } | |
190 | |
191 public static WstValueTable getTableUncached(River river) { | |
192 return getTableUncached(river, DEFAULT_KIND); | |
193 } | |
194 | |
195 public static WstValueTable getTableUncached(River river, int kind) { | |
196 | |
197 Session session = SessionHolder.HOLDER.get(); | |
198 | |
199 Wst wst = loadWst(session, river, kind); | |
200 | |
201 if (wst == null) { | |
202 return null; | |
203 } | |
204 | |
205 WstValueTable.Column [] columns = loadColumns(session, wst); | |
206 | |
207 loadQRanges(session, columns, wst); | |
208 | |
209 List<WstValueTable.Row> rows = loadRows(session, wst, columns.length); | |
210 | |
211 return new WstValueTable(columns, rows); | |
212 } | |
213 | |
214 /** | |
215 * @param kind Kind of wst. | |
216 */ | |
217 protected static Wst loadWst(Session session, River river, int kind) { | |
218 Query query = session.createQuery(HQL_WST); | |
219 query.setParameter("river", river); | |
220 query.setInteger("kind", kind); | |
221 | |
222 List<Wst> wsts = query.list(); | |
223 | |
224 // TODO Multiple wsts can match, why return just the first one? | |
225 return wsts.isEmpty() ? null : wsts.get(0); | |
226 } | |
227 | |
228 | |
229 /** | |
230 * Load rows with a single columns result. | |
231 * | |
232 * @param session session to use for querying db. | |
233 * @param wstId id of wst (in db). | |
234 * @param column_pos the column_pos (within the db) of the wst_value_table | |
235 * of which the values shall be fetched. | |
236 * | |
237 * @return resultant rows. | |
238 */ | |
239 protected static List<WstValueTable.Row> loadRowsOneColumn( | |
240 Session session, | |
241 int wstId, | |
242 int column_pos | |
243 ) { | |
244 SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_WS_AT_COL) | |
245 .addScalar("km", StandardBasicTypes.DOUBLE) | |
246 .addScalar("w", StandardBasicTypes.DOUBLE); | |
247 | |
248 sqlQuery.setInteger("wst_id", wstId); | |
249 sqlQuery.setInteger("column_pos", column_pos); | |
250 | |
251 List<Object []> results = sqlQuery.list(); | |
252 | |
253 double [] ws = null; | |
254 | |
255 List<WstValueTable.Row> rows = | |
256 new ArrayList<WstValueTable.Row>(results.size()); | |
257 | |
258 // Walk over rows. | |
259 for (Object [] result: results) { | |
260 ws = new double[1]; | |
261 WstValueTable.Row row = | |
262 new WstValueTable.Row((Double) result[0], ws); | |
263 rows.add(row); | |
264 | |
265 Double w = (Double) result[1]; | |
266 ws[0] = w != null ? w : Double.NaN; | |
267 } | |
268 | |
269 return rows; | |
270 } | |
271 | |
272 protected static List<WstValueTable.Row> loadRows( | |
273 Session session, | |
274 int wst_id, | |
275 int numColumns | |
276 ) { | |
277 SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_WS) | |
278 .addScalar("km", StandardBasicTypes.DOUBLE) | |
279 .addScalar("w", StandardBasicTypes.DOUBLE) | |
280 .addScalar("column_pos", StandardBasicTypes.INTEGER); | |
281 | |
282 sqlQuery.setInteger("wst_id", wst_id); | |
283 | |
284 List<Object []> results = sqlQuery.list(); | |
285 | |
286 int lastColumn = Integer.MAX_VALUE; | |
287 double [] ws = null; | |
288 | |
289 ArrayList<WstValueTable.Row> rows = new ArrayList<WstValueTable.Row>(); | |
290 | |
291 for (Object [] result: results) { | |
292 int column = (Integer)result[2]; | |
293 if (column < lastColumn) { | |
294 ws = new double[numColumns]; | |
295 Arrays.fill(ws, Double.NaN); | |
296 WstValueTable.Row row = | |
297 new WstValueTable.Row((Double)result[0], ws); | |
298 rows.add(row); | |
299 } | |
300 Double w = (Double)result[1]; | |
301 ws[column] = w != null ? w : Double.NaN; | |
302 lastColumn = column; | |
303 } | |
304 | |
305 rows.trimToSize(); | |
306 return rows; | |
307 } | |
308 | |
309 protected static List<WstValueTable.Row> loadRows( | |
310 Session session, | |
311 Wst wst, | |
312 int numColumns | |
313 ) { | |
314 return loadRows(session, wst.getId(), numColumns); | |
315 } | |
316 | |
317 | |
318 protected static WstValueTable.Column [] loadColumn( | |
319 Session session, | |
320 int wst_id, | |
321 int col_pos | |
322 ) { | |
323 return new WstValueTable.Column [] { | |
324 new WstValueTable.Column(WKmsFactory.getWKmsName(col_pos, wst_id))}; | |
325 } | |
326 | |
327 | |
328 /** | |
329 * Get columns from wst-id. | |
330 */ | |
331 protected static WstValueTable.Column [] loadColumns( | |
332 Session session, | |
333 int wst_id | |
334 ) { | |
335 SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_NAMES_POS) | |
336 .addScalar("position", StandardBasicTypes.INTEGER) | |
337 .addScalar("name", StandardBasicTypes.STRING); | |
338 | |
339 sqlQuery.setInteger("wst_id", wst_id); | |
340 | |
341 List<Object []> columnNames = sqlQuery.list(); | |
342 | |
343 WstValueTable.Column [] columns = | |
344 new WstValueTable.Column[columnNames.size()]; | |
345 | |
346 for (int i = 0; i < columns.length; ++i) { | |
347 columns[i] = new WstValueTable.Column( | |
348 (String)columnNames.get(i)[1]); | |
349 } | |
350 return columns; | |
351 } | |
352 | |
353 /** | |
354 * Get columns from Wst. | |
355 */ | |
356 protected static WstValueTable.Column [] loadColumns( | |
357 Session session, | |
358 Wst wst | |
359 ) { | |
360 return loadColumns(session, wst.getId()); | |
361 } | |
362 | |
363 | |
364 /** | |
365 * Build a QRange-Tree. | |
366 */ | |
367 protected static void loadQRanges( | |
368 Session session, | |
369 WstValueTable.Column [] columns, | |
370 int wst_id, | |
371 int column_pos | |
372 ) { | |
373 SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_QS_AT_COL) | |
374 .addScalar("q", StandardBasicTypes.DOUBLE) | |
375 .addScalar("a", StandardBasicTypes.DOUBLE) | |
376 .addScalar("b", StandardBasicTypes.DOUBLE); | |
377 | |
378 sqlQuery.setInteger("wst_id", wst_id); | |
379 sqlQuery.setInteger("column_pos", column_pos); | |
380 | |
381 List<Object []> qRanges = sqlQuery.list(); | |
382 | |
383 int qSize = qRanges.size(); | |
384 | |
385 QRangeTree qRangeTree = new QRangeTree( | |
386 qRanges, QRangeTree.WITHOUT_COLUMN, 0, qSize); | |
387 columns[0].setQRangeTree(qRangeTree); | |
388 } | |
389 | |
390 protected static void loadQRanges( | |
391 Session session, | |
392 WstValueTable.Column [] columns, | |
393 int wst_id | |
394 ) { | |
395 SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_QS) | |
396 .addScalar("column_pos", StandardBasicTypes.INTEGER) | |
397 .addScalar("q", StandardBasicTypes.DOUBLE) | |
398 .addScalar("a", StandardBasicTypes.DOUBLE) | |
399 .addScalar("b", StandardBasicTypes.DOUBLE); | |
400 | |
401 sqlQuery.setInteger("wst_id", wst_id); | |
402 | |
403 List<Object []> qRanges = sqlQuery.list(); | |
404 | |
405 int start = -1; | |
406 int Q = qRanges.size(); | |
407 Integer lastColumn = null; | |
408 | |
409 for (int i = 0; i < Q; ++i) { | |
410 Object [] qRange = qRanges.get(i); | |
411 Integer columnId = (Integer)qRange[0]; | |
412 if (lastColumn == null) { | |
413 lastColumn = columnId; | |
414 start = i; | |
415 } | |
416 else if (!lastColumn.equals(columnId)) { | |
417 QRangeTree qRangeTree = new QRangeTree(qRanges, start, i); | |
418 columns[lastColumn].setQRangeTree(qRangeTree); | |
419 lastColumn = columnId; | |
420 start = i; | |
421 } | |
422 } | |
423 | |
424 if (start != -1) { | |
425 QRangeTree qRangeTree = new QRangeTree(qRanges, start, Q); | |
426 columns[lastColumn].setQRangeTree(qRangeTree); | |
427 } | |
428 | |
429 /* This is debug code to visualize the q ranges trees | |
430 | |
431 java.io.PrintWriter out = null; | |
432 try { | |
433 out = new java.io.PrintWriter( | |
434 new java.io.FileWriter( | |
435 "/tmp/qranges" + System.currentTimeMillis() + ".dot")); | |
436 | |
437 out.println("graph \"Q ranges trees\" {"); | |
438 | |
439 for (int i = 0; i < columns.length; ++i) { | |
440 QRangeTree tree = columns[i].getQRangeTree(); | |
441 out.println(tree.toGraph()); | |
442 } | |
443 | |
444 out.println("}"); | |
445 | |
446 out.flush(); | |
447 } | |
448 catch (java.io.IOException ioe) { | |
449 log.error(ioe); | |
450 } | |
451 finally { | |
452 if (out != null) { | |
453 out.close(); | |
454 } | |
455 } | |
456 */ | |
457 | |
458 } | |
459 | |
460 protected static void loadQRanges( | |
461 Session session, | |
462 WstValueTable.Column [] columns, | |
463 Wst wst | |
464 ) { | |
465 loadQRanges(session, columns, wst.getId()); | |
466 } | |
467 | |
468 } | |
469 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |