Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/WQKmsFactory.java @ 8876:23264d1a528f
Allow access to staticwqkms data; also allow to acces by km range
author | gernotbelger |
---|---|
date | Thu, 08 Feb 2018 18:47:36 +0100 |
parents | 5e38e2924c07 |
children |
comparison
equal
deleted
inserted
replaced
8875:8d1df8639563 | 8876:23264d1a528f |
---|---|
31 public class WQKmsFactory | 31 public class WQKmsFactory |
32 { | 32 { |
33 private static Logger log = Logger.getLogger(WQKmsFactory.class); | 33 private static Logger log = Logger.getLogger(WQKmsFactory.class); |
34 | 34 |
35 /** Query to get km and wqs for wst_id and column_pos. */ | 35 /** Query to get km and wqs for wst_id and column_pos. */ |
36 public static final String SQL_SELECT_WQS = | 36 private static final String SQL_SELECT_WQS = |
37 "SELECT position, w, q FROM wst_value_table " + | 37 "SELECT position, w, q FROM wst_value_table " + |
38 "WHERE wst_id = :wst_id AND column_pos = :column_pos"; | 38 "WHERE wst_id = :wst_id AND column_pos = :column_pos"; |
39 | 39 /** Query to get km and wqs for wst_id and column_pos. */ |
40 | |
41 private static final String SQL_SELECT_WQS_RANGE = | |
42 "SELECT position, w, q FROM wst_value_table " + | |
43 "WHERE wst_id = :wst_id AND column_pos = :column_pos " + | |
44 "AND position BETWEEN :kmfrom AND :kmto"; | |
45 | |
40 /** Get wst_id and position from wst_columns. */ | 46 /** Get wst_id and position from wst_columns. */ |
41 public static final String SQL_SELECT_COLUMN = | 47 private static final String SQL_SELECT_COLUMN = |
42 "SELECT wst_id, position FROM wst_columns WHERE id = :column_id"; | 48 "SELECT wst_id, position FROM wst_columns WHERE id = :column_id"; |
43 | 49 |
44 /** Query to get name for wst_id and column_pos. */ | 50 /** Query to get name for wst_id and column_pos. */ |
45 public static final String SQL_SELECT_NAME = | 51 private static final String SQL_SELECT_NAME = |
46 "SELECT name " + | 52 "SELECT name " + |
47 "FROM wst_columns "+ | 53 "FROM wst_columns "+ |
48 "WHERE id = :column_id"; | 54 "WHERE id = :column_id"; |
49 | 55 |
50 | 56 |
54 | 60 |
55 | 61 |
56 /** | 62 /** |
57 * Get WKms for given column (pos) and wst_id, caring about the cache. | 63 * Get WKms for given column (pos) and wst_id, caring about the cache. |
58 */ | 64 */ |
59 public static WQKms getWQKms(int columnPos, int wst_id) { | 65 public static WQKms getWQKms(final int columnPos, final int wst_id) { |
66 return getWQKms(columnPos, wst_id, Double.NaN, Double.NaN); | |
67 } | |
68 | |
69 /** | |
70 * Get WKms for given column (pos) and wst_id, caring about the cache. | |
71 */ | |
72 public static WQKms getWQKms(final int columnPos, final int wst_id, final double from, final double to) { | |
60 log.debug("WQKmsFactory.getWQKms"); | 73 log.debug("WQKmsFactory.getWQKms"); |
61 Cache cache = CacheFactory.getCache(StaticWQKmsCacheKey.CACHE_NAME); | 74 final Cache cache = CacheFactory.getCache(StaticWQKmsCacheKey.CACHE_NAME); |
62 | 75 |
63 StaticWQKmsCacheKey cacheKey; | 76 final StaticWQKmsCacheKey cacheKey; |
64 | 77 |
65 if (cache != null) { | 78 if (cache != null) { |
66 cacheKey = new StaticWQKmsCacheKey(wst_id, columnPos); | 79 cacheKey = new StaticWQKmsCacheKey(wst_id, columnPos, from, to); |
67 Element element = cache.get(cacheKey); | 80 Element element = cache.get(cacheKey); |
68 if (element != null) { | 81 if (element != null) { |
69 log.debug("Got static wst values from cache"); | 82 log.debug("Got static wst values from cache"); |
70 return (WQKms)element.getValue(); | 83 return (WQKms)element.getValue(); |
71 } | 84 } |
72 } | 85 } |
73 else { | 86 else { |
74 cacheKey = null; | 87 cacheKey = null; |
75 } | 88 } |
76 | 89 |
77 WQKms values = getWQKmsUncached(columnPos, wst_id); | 90 final WQKms values = getWQKmsUncached(columnPos, wst_id, from, to); |
78 | 91 |
79 if (values != null && cacheKey != null) { | 92 if (values != null && cache != null && cacheKey != null) { |
80 log.debug("Store static wst values in cache."); | 93 log.debug("Store static wst values in cache."); |
81 Element element = new Element(cacheKey, values); | 94 final Element element = new Element(cacheKey, values); |
82 cache.put(element); | 95 cache.put(element); |
83 } | 96 } |
84 return values; | 97 return values; |
85 } | 98 } |
86 | 99 |
87 /** | 100 /** |
88 * Get WKms for given column (id), caring about the cache. | 101 * Get WKms for given column (id), caring about the cache. |
89 */ | 102 */ |
90 public static WQKms getWQKmsCID(int columnID) { | 103 public static WQKms getWQKmsCID(int columnID) { |
91 log.debug("WQKmsFactory.getWQKms"); | 104 log.debug("WQKmsFactory.getWQKms"); |
92 Cache cache = CacheFactory.getCache(StaticWQKmsCacheKey.CACHE_NAME); | 105 Cache cache = CacheFactory.getCache(StaticWQKmsCacheKey.CACHE_NAME); |
93 | 106 |
94 StaticWQKmsCacheKey cacheKey; | 107 StaticWQKmsCacheKey cacheKey; |
95 | 108 |
96 if (cache != null) { | 109 if (cache != null) { |
97 cacheKey = new StaticWQKmsCacheKey(-columnID, -columnID); | 110 cacheKey = new StaticWQKmsCacheKey(-columnID, -columnID, Double.NaN, Double.NaN); |
98 Element element = cache.get(cacheKey); | 111 Element element = cache.get(cacheKey); |
99 if (element != null) { | 112 if (element != null) { |
100 log.debug("Got static wst values from cache"); | 113 log.debug("Got static wst values from cache"); |
101 return (WQKms)element.getValue(); | 114 return (WQKms)element.getValue(); |
102 } | 115 } |
105 cacheKey = null; | 118 cacheKey = null; |
106 } | 119 } |
107 | 120 |
108 int[] cInfo = getColumn(columnID); | 121 int[] cInfo = getColumn(columnID); |
109 if (cInfo == null) return null; | 122 if (cInfo == null) return null; |
110 WQKms values = getWQKmsUncached(cInfo[1], cInfo[0]); | 123 WQKms values = getWQKmsUncached(cInfo[1], cInfo[0], Double.NaN, Double.NaN); |
111 | 124 |
112 | 125 |
113 if (values != null && cacheKey != null) { | 126 if (values != null && cacheKey != null) { |
114 log.debug("Store static wst values in cache."); | 127 log.debug("Store static wst values in cache."); |
115 Element element = new Element(cacheKey, values); | 128 Element element = new Element(cacheKey, values); |
123 * Get WQKms from db. | 136 * Get WQKms from db. |
124 * @param column the position columns value | 137 * @param column the position columns value |
125 * @param wst_id database id of the wst | 138 * @param wst_id database id of the wst |
126 * @return respective WQKms. | 139 * @return respective WQKms. |
127 */ | 140 */ |
128 public static WQKms getWQKmsUncached(int column, int wst_id) { | 141 private static WQKms getWQKmsUncached(final int column, final int wst_id, final double from, final double to) { |
129 | 142 |
130 if (log.isDebugEnabled()) { | 143 if (log.isDebugEnabled()) { |
131 log.debug("WQKmsFactory.getWQKmsUncached, column " | 144 log.debug("WQKmsFactory.getWQKmsUncached, column " |
132 + column + ", wst_id " + wst_id); | 145 + column + ", wst_id " + wst_id); |
133 } | 146 } |
134 | 147 |
135 WQKms wqkms = new WQKms(WKmsFactory.getWKmsName(column, wst_id)); | 148 final WQKms wqkms = new WQKms(WKmsFactory.getWKmsName(column, wst_id)); |
136 | 149 |
137 Session session = SessionHolder.HOLDER.get(); | 150 boolean hasRange = !Double.isNaN(from) && !Double.isNaN(to); |
138 SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_WQS) | 151 |
152 final String query = hasRange ? SQL_SELECT_WQS_RANGE : SQL_SELECT_WQS; | |
153 | |
154 final Session session = SessionHolder.HOLDER.get(); | |
155 final SQLQuery sqlQuery = session.createSQLQuery(query) | |
139 .addScalar("position", StandardBasicTypes.DOUBLE) | 156 .addScalar("position", StandardBasicTypes.DOUBLE) |
140 .addScalar("w", StandardBasicTypes.DOUBLE) | 157 .addScalar("w", StandardBasicTypes.DOUBLE) |
141 .addScalar("q", StandardBasicTypes.DOUBLE); | 158 .addScalar("q", StandardBasicTypes.DOUBLE); |
159 | |
142 sqlQuery.setInteger("wst_id", wst_id); | 160 sqlQuery.setInteger("wst_id", wst_id); |
143 sqlQuery.setInteger("column_pos", column); | 161 sqlQuery.setInteger("column_pos", column); |
144 | 162 if( hasRange ) |
145 List<Object []> results = sqlQuery.list(); | 163 { |
164 sqlQuery.setDouble("kmfrom", from); | |
165 sqlQuery.setDouble("kmto", to); | |
166 } | |
167 | |
168 final List<Object []> results = sqlQuery.list(); | |
146 | 169 |
147 for (int i = 0, N = results.size(); i < N; i++) { | 170 for (int i = 0, N = results.size(); i < N; i++) { |
148 Object[] row = results.get(i); | 171 final Object[] row = results.get(i); |
149 // add(w, q, km) | 172 // add(w, q, km) |
150 if (row == null | 173 if (row == null |
151 || row[0] == null | 174 || row[0] == null |
152 || row[1] == null | 175 || row[1] == null |
153 || row[2] == null | 176 || row[2] == null |
182 .addScalar("position", StandardBasicTypes.INTEGER); | 205 .addScalar("position", StandardBasicTypes.INTEGER); |
183 sqlQuery.setInteger("column_id", columnID); | 206 sqlQuery.setInteger("column_id", columnID); |
184 | 207 |
185 List<Object []> results = sqlQuery.list(); | 208 List<Object []> results = sqlQuery.list(); |
186 | 209 |
210 // FIXME: right? this will always return row[0]! | |
187 for (int i = 0, N = results.size(); i < N; i++) { | 211 for (int i = 0, N = results.size(); i < N; i++) { |
188 Object[] row = results.get(i); | 212 Object[] row = results.get(i); |
189 return new int[] {(Integer)row[0], (Integer)row[1]}; | 213 return new int[] {(Integer)row[0], (Integer)row[1]}; |
190 } | 214 } |
191 | 215 |