comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/WKmsFactory.java @ 8547:04367906f158

(issue1754) Add distantce handling to WINFO differences state
author Andre Heinecke <andre.heinecke@intevation.de>
date Mon, 16 Feb 2015 12:55:38 +0100
parents e4606eae8ea5
children 532da345d4a7
comparison
equal deleted inserted replaced
8546:522f72f43ae6 8547:04367906f158
36 36
37 /** Query to get km and ws for wst_id and column_pos. */ 37 /** Query to get km and ws for wst_id and column_pos. */
38 public static final String SQL_SELECT_WS = 38 public static final String SQL_SELECT_WS =
39 "SELECT km, w FROM wst_w_values " + 39 "SELECT km, w FROM wst_w_values " +
40 "WHERE wst_id = :wst_id AND column_pos = :column_pos"; 40 "WHERE wst_id = :wst_id AND column_pos = :column_pos";
41
42 public static final String SQL_SELECT_WS_FOR_RANGE =
43 "SELECT km, w FROM wst_w_values " +
44 "WHERE wst_id = :wst_id AND column_pos = :column_pos " +
45 "AND km BETWEEN :kmfrom AND :kmto";
41 46
42 /** Query to get name for wst_id and column_pos. */ 47 /** Query to get name for wst_id and column_pos. */
43 public static final String SQL_SELECT_NAME = 48 public static final String SQL_SELECT_NAME =
44 "SELECT name " + 49 "SELECT name " +
45 "FROM wst_columns "+ 50 "FROM wst_columns "+
80 "WHERE wc.wst_id = :wst_id"; 85 "WHERE wc.wst_id = :wst_id";
81 86
82 private WKmsFactory() { 87 private WKmsFactory() {
83 } 88 }
84 89
90 public static WKms getWKms(int column, int wst_id, double from, double to) {
91 log.debug("WKmsFactory.getWKms");
92 Cache cache = CacheFactory.getCache("waterlevels-static");
93
94 String cacheKey = Integer.toString(column) + ":" + Integer.toString(wst_id);
95
96 if (cache != null) {
97 if (!Double.isNaN(from) && ! Double.isNaN(to)) {
98 cacheKey += ":" + Double.toString(from) + ":" + Double.toString(to);
99 }
100 Element element = cache.get(cacheKey);
101 if (element != null) {
102 log.debug("Got static wst values from cache");
103 return (WKms)element.getValue();
104 }
105 }
106
107 WKms values = getWKmsUncached(column, wst_id, from, to);
108
109 if (values != null && cache != null) {
110 log.debug("Store static wst values in cache.");
111 Element element = new Element(cacheKey, values);
112 cache.put(element);
113 }
114 return values;
115 }
85 116
86 /** 117 /**
87 * Get WKms for given column and wst_id, caring about the cache. 118 * Get WKms for given column and wst_id, caring about the cache.
88 */ 119 */
89 public static WKms getWKms(int column, int wst_id) { 120 public static WKms getWKms(int column, int wst_id) {
90 log.debug("WKmsFactory.getWKms"); 121 return getWKms(column, wst_id, Double.NaN, Double.NaN);
91 Cache cache = CacheFactory.getCache(StaticWKmsCacheKey.CACHE_NAME);
92
93 StaticWKmsCacheKey cacheKey;
94
95 if (cache != null) {
96 cacheKey = new StaticWKmsCacheKey(wst_id, column);
97 Element element = cache.get(cacheKey);
98 if (element != null) {
99 log.debug("Got static wst values from cache");
100 return (WKms)element.getValue();
101 }
102 }
103 else {
104 cacheKey = null;
105 }
106
107 WKms values = getWKmsUncached(column, wst_id);
108
109 if (values != null && cacheKey != null) {
110 log.debug("Store static wst values in cache.");
111 Element element = new Element(cacheKey, values);
112 cache.put(element);
113 }
114 return values;
115 } 122 }
116 123
117 /** Get name for a WKms wrapped in W, if suitable. */ 124 /** Get name for a WKms wrapped in W, if suitable. */
118 public static String getWKmsNameWWrapped(int wst_id) { 125 public static String getWKmsNameWWrapped(int wst_id) {
119 return getWKmsNameWWrapped(-1, wst_id); 126 return getWKmsNameWWrapped(-1, wst_id);
208 * Get WKms from db. 215 * Get WKms from db.
209 * @param column the position columns value 216 * @param column the position columns value
210 * @param wst_id database id of the wst 217 * @param wst_id database id of the wst
211 * @return according WKms. 218 * @return according WKms.
212 */ 219 */
213 public static WKms getWKmsUncached(int column, int wst_id) { 220 public static WKms getWKmsUncached(int column, int wst_id, double from, double to) {
214 221
215 if (log.isDebugEnabled()) { 222 if (log.isDebugEnabled()) {
216 log.debug("WKmsFactory.getWKmsUncached c/" + column + ", wst_id/" + wst_id); 223 log.debug("WKmsFactory.getWKmsUncached c/" + column + ", wst_id/" + wst_id);
217 } 224 }
218 225
219 WKmsImpl wkms = new WKmsImpl(getWKmsName(column, wst_id)); 226 WKmsImpl wkms = new WKmsImpl(getWKmsName(column, wst_id));
220 227
221 Session session = SessionHolder.HOLDER.get(); 228 Session session = SessionHolder.HOLDER.get();
222 SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_WS) 229 SQLQuery sqlQuery;
223 .addScalar("km", StandardBasicTypes.DOUBLE) 230 if (Double.isNaN(from) || Double.isNaN(to)) {
224 .addScalar("w", StandardBasicTypes.DOUBLE); 231 sqlQuery = session.createSQLQuery(SQL_SELECT_WS);
232 } else {
233 sqlQuery = session.createSQLQuery(SQL_SELECT_WS_FOR_RANGE);
234 sqlQuery.setDouble("kmfrom", from);
235 sqlQuery.setDouble("kmto", to);
236 }
237
238 sqlQuery.addScalar("km", StandardBasicTypes.DOUBLE)
239 .addScalar("w", StandardBasicTypes.DOUBLE);
225 sqlQuery.setInteger("wst_id", wst_id); 240 sqlQuery.setInteger("wst_id", wst_id);
226 sqlQuery.setInteger("column_pos", column); 241 sqlQuery.setInteger("column_pos", column);
227 242
228 List<Object []> results = sqlQuery.list(); 243 List<Object []> results = sqlQuery.list();
229 244

http://dive4elements.wald.intevation.org