Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WKmsFactory.java @ 1722:fb4fb1c67c35
Added WKmsFactory.
flys-artifacts/trunk@3004 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Felix Wolfsteller <felix.wolfsteller@intevation.de> |
---|---|
date | Tue, 18 Oct 2011 12:44:02 +0000 |
parents | |
children | e3b9164a85fe |
comparison
equal
deleted
inserted
replaced
1721:eb35570df0e8 | 1722:fb4fb1c67c35 |
---|---|
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.WKms; | |
16 import de.intevation.flys.artifacts.model.WKmsImpl; | |
17 | |
18 import de.intevation.flys.artifacts.cache.CacheFactory; | |
19 | |
20 import de.intevation.flys.backend.SessionHolder; | |
21 | |
22 /** | |
23 * Factory to access ready-made WKms for other (than computed) 'kinds' of | |
24 * WST-data. | |
25 */ | |
26 public class WKmsFactory | |
27 { | |
28 private static Logger log = Logger.getLogger(WKmsFactory.class); | |
29 | |
30 /** | |
31 * Value for wsts 'kind' typically used for "additional | |
32 * longitudinal sections. | |
33 */ | |
34 public static final int KIND_ZUS = 1; | |
35 | |
36 /** | |
37 * Value for wsts 'kind' typically used for flood protection measures | |
38 * (e.g. dikes). | |
39 */ | |
40 public static final int KIND_PROTECTION = 5; | |
41 | |
42 /** Query to get km and ws for wst_id and column_pos. */ | |
43 public static final String SQL_SELECT_WS = | |
44 "SELECT km, w FROM wst_w_values " + | |
45 "WHERE wst_id = :wst_id AND column_pos = :column_pos"; | |
46 | |
47 | |
48 private WKmsFactory() { | |
49 } | |
50 | |
51 | |
52 /** | |
53 * Get WKms for given column and wst_id, caring about the cache. | |
54 */ | |
55 public static WKms getWKms(int kind, int column, int wst_id) { | |
56 log.debug("WKmsFactory.getWKms"); | |
57 Cache cache = CacheFactory.getCache(StaticWKmsCacheKey.CACHE_NAME); | |
58 | |
59 StaticWKmsCacheKey cacheKey; | |
60 | |
61 if (cache != null) { | |
62 cacheKey = new StaticWKmsCacheKey(wst_id, column); | |
63 Element element = cache.get(cacheKey); | |
64 if (element != null) { | |
65 log.debug("Got static wst values from cache"); | |
66 return (WKms)element.getValue(); | |
67 } | |
68 } | |
69 else { | |
70 cacheKey = null; | |
71 } | |
72 | |
73 WKms values = getWKmsUncached(kind, column, wst_id); | |
74 | |
75 if (values != null && cacheKey != null) { | |
76 log.debug("Store static wst values in cache."); | |
77 Element element = new Element(cacheKey, values); | |
78 cache.put(element); | |
79 } | |
80 return values; | |
81 } | |
82 | |
83 | |
84 /** | |
85 * Get WKms from db. | |
86 */ | |
87 public static WKms getWKmsUncached(int kind, int column, int wst_id) { | |
88 log.debug("WKmsFactory.getWKmsUncached c/" + column + ", wst_id/" + wst_id); | |
89 Session session = SessionHolder.HOLDER.get(); | |
90 | |
91 SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_WS) | |
92 .addScalar("km", StandardBasicTypes.DOUBLE) | |
93 .addScalar("w", StandardBasicTypes.DOUBLE); | |
94 sqlQuery.setInteger("wst_id", wst_id); | |
95 sqlQuery.setInteger("column_pos", column); | |
96 | |
97 List<Object []> results = sqlQuery.list(); | |
98 | |
99 // TODO Fetch name in a separate query. | |
100 String name = "todo"; | |
101 | |
102 WKmsImpl wkms = new WKmsImpl(name); | |
103 double kms [] = new double[results.size()]; | |
104 double ws [] = new double[results.size()]; | |
105 | |
106 int lastColumn = Integer.MAX_VALUE; | |
107 | |
108 for (int i = 0; i < results.size(); i++) { | |
109 Object[] row = results.get(i); | |
110 wkms.add((Double) row[0], (Double) row[1]); | |
111 } | |
112 | |
113 return wkms; | |
114 } | |
115 } | |
116 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |