Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WKmsFactory.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 | cd5eb8f5f6f1 |
children | 58bdf95df5e4 |
comparison
equal
deleted
inserted
replaced
3719:e82acd5c86f7 | 3786:4adc35aa655c |
---|---|
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.cache.CacheFactory; | |
16 | |
17 import de.intevation.flys.backend.SessionHolder; | |
18 | |
19 | |
20 /** | |
21 * Factory to access ready-made WKms for other (than computed) 'kinds' of | |
22 * WST-data. | |
23 */ | |
24 public class WKmsFactory | |
25 { | |
26 /** Private logger to use here. */ | |
27 private static Logger log = Logger.getLogger(WKmsFactory.class); | |
28 | |
29 /** Query to get km and ws for wst_id and column_pos. */ | |
30 public static final String SQL_SELECT_WS = | |
31 "SELECT km, w FROM wst_w_values " + | |
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 /** Query to get name (description) for wst_id. */ | |
41 public static final String SQL_SELECT_WST_NAME = | |
42 "SELECT description from wsts "+ | |
43 "WHERE id = :wst_id"; | |
44 | |
45 | |
46 private WKmsFactory() { | |
47 } | |
48 | |
49 | |
50 /** | |
51 * Get WKms for given column and wst_id, caring about the cache. | |
52 */ | |
53 public static WKms getWKms(int column, int wst_id) { | |
54 log.debug("WKmsFactory.getWKms"); | |
55 Cache cache = CacheFactory.getCache(StaticWKmsCacheKey.CACHE_NAME); | |
56 | |
57 StaticWKmsCacheKey cacheKey; | |
58 | |
59 if (cache != null) { | |
60 cacheKey = new StaticWKmsCacheKey(wst_id, column); | |
61 Element element = cache.get(cacheKey); | |
62 if (element != null) { | |
63 log.debug("Got static wst values from cache"); | |
64 return (WKms)element.getValue(); | |
65 } | |
66 } | |
67 else { | |
68 cacheKey = null; | |
69 } | |
70 | |
71 WKms values = getWKmsUncached(column, wst_id); | |
72 | |
73 if (values != null && cacheKey != null) { | |
74 log.debug("Store static wst values in cache."); | |
75 Element element = new Element(cacheKey, values); | |
76 cache.put(element); | |
77 } | |
78 return values; | |
79 } | |
80 | |
81 /** Get name for a WKms. */ | |
82 public static String getWKmsName(int wst_id) { | |
83 log.debug("WKmsFactory.getWKmsName wst_id/" + wst_id); | |
84 | |
85 String name = null; | |
86 Session session = SessionHolder.HOLDER.get(); | |
87 | |
88 SQLQuery nameQuery = session.createSQLQuery(SQL_SELECT_WST_NAME) | |
89 .addScalar("description", StandardBasicTypes.STRING); | |
90 nameQuery.setInteger("wst_id", wst_id); | |
91 | |
92 List<String> names = nameQuery.list(); | |
93 if (names.size() >= 1) { | |
94 name = names.get(0); | |
95 } | |
96 | |
97 return name; | |
98 } | |
99 | |
100 /** Get name for a WKms. */ | |
101 public static String getWKmsName(int column, int wst_id) { | |
102 log.debug("WKmsFactory.getWKmsName c/" + column + ", wst_id/" + wst_id); | |
103 | |
104 String name = null; | |
105 Session session = SessionHolder.HOLDER.get(); | |
106 | |
107 SQLQuery nameQuery = session.createSQLQuery(SQL_SELECT_NAME) | |
108 .addScalar("name", StandardBasicTypes.STRING); | |
109 nameQuery.setInteger("wst_id", wst_id); | |
110 nameQuery.setInteger("column_pos", column); | |
111 | |
112 List<String> names = nameQuery.list(); | |
113 if (names.size() >= 1) { | |
114 name = names.get(0); | |
115 } | |
116 | |
117 return name; | |
118 } | |
119 | |
120 | |
121 /** | |
122 * Get WKms from db. | |
123 * @param column the position columns value | |
124 * @param wst_id database id of the wst | |
125 * @return according WKms. | |
126 */ | |
127 public static WKms getWKmsUncached(int column, int wst_id) { | |
128 | |
129 if (log.isDebugEnabled()) { | |
130 log.debug("WKmsFactory.getWKmsUncached c/" + column + ", wst_id/" + wst_id); | |
131 } | |
132 | |
133 WKmsImpl wkms = new WKmsImpl(getWKmsName(column, wst_id)); | |
134 | |
135 Session session = SessionHolder.HOLDER.get(); | |
136 SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_WS) | |
137 .addScalar("km", StandardBasicTypes.DOUBLE) | |
138 .addScalar("w", StandardBasicTypes.DOUBLE); | |
139 sqlQuery.setInteger("wst_id", wst_id); | |
140 sqlQuery.setInteger("column_pos", column); | |
141 | |
142 List<Object []> results = sqlQuery.list(); | |
143 | |
144 double kms [] = new double[results.size()]; | |
145 double ws [] = new double[results.size()]; | |
146 | |
147 int lastColumn = Integer.MAX_VALUE; | |
148 | |
149 for (int i = 0, N = results.size(); i < N; i++) { | |
150 Object[] row = results.get(i); | |
151 wkms.add((Double) row[0], (Double) row[1]); | |
152 } | |
153 | |
154 return wkms; | |
155 } | |
156 } | |
157 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |