comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WstValueTableFactory.java @ 1909:4b64692b2d1e

Added methods to create WstValueTables ('interpolators') for specific columns of wsts. flys-artifacts/trunk@3269 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Wed, 16 Nov 2011 14:47:02 +0000
parents 7053e3255ab4
children 1f90fdd4fa04
comparison
equal deleted inserted replaced
1908:19c53705a2eb 1909:4b64692b2d1e
39 39
40 public static final String SQL_SELECT_NAMES_POS = 40 public static final String SQL_SELECT_NAMES_POS =
41 "SELECT position, name FROM wst_columns " + 41 "SELECT position, name FROM wst_columns " +
42 "WHERE wst_id = :wst_id ORDER BY position"; 42 "WHERE wst_id = :wst_id ORDER BY position";
43 43
44 /** Select Qs for wst (view sorted by column). */
44 public static final String SQL_SELECT_QS = 45 public static final String SQL_SELECT_QS =
45 "SELECT column_pos, q, a, b FROM wst_q_values " + 46 "SELECT column_pos, q, a, b FROM wst_q_values " +
46 "WHERE wst_id = :wst_id"; 47 "WHERE wst_id = :wst_id";
47 48
49 // (sorted by km)
48 public static final String SQL_SELECT_WS = 50 public static final String SQL_SELECT_WS =
49 "SELECT km, w, column_pos FROM wst_w_values " + 51 "SELECT km, w, column_pos FROM wst_w_values " +
50 "WHERE wst_id = :wst_id"; 52 "WHERE wst_id = :wst_id";
51 53
54 /** Statement to query qranges of a single column. */
55 public static final String SQL_SELECT_QS_AT_COL =
56 "SELECT column_pos, 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
52 private WstValueTableFactory() { 65 private WstValueTableFactory() {
53 } 66 }
67
54 68
55 public static WstValueTable getTable(River river) { 69 public static WstValueTable getTable(River river) {
56 return getTable(river, DEFAULT_KIND); 70 return getTable(river, DEFAULT_KIND);
57 } 71 }
58 72
96 } 110 }
97 111
98 return valueTable; 112 return valueTable;
99 } 113 }
100 114
101 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 /** @TODO cached/uncached */
121
122 Session session = SessionHolder.HOLDER.get();
123
124 // Fetch data for one column only.
125
126 WstValueTable.Column [] columns = loadColumn(session, wst_id, col_pos);
127 loadQRanges(session, columns, wst_id, col_pos);
128 List<WstValueTable.Row> rows = loadRowsOneColumn(session, wst_id, col_pos);
129
130 return new WstValueTable(columns, rows);
131 }
132
133
134 /**
135 * Get table for first wst of given kind at given river.
136 */
102 public static WstValueTable getTable(River river, int kind) { 137 public static WstValueTable getTable(River river, int kind) {
103 138
104 Cache cache = CacheFactory.getCache(WstValueTableCacheKey.CACHE_NAME); 139 Cache cache = CacheFactory.getCache(WstValueTableCacheKey.CACHE_NAME);
105 140
106 WstValueTableCacheKey cacheKey; 141 WstValueTableCacheKey cacheKey;
163 198
164 // TODO Multiple wsts can match, why return just the first one? 199 // TODO Multiple wsts can match, why return just the first one?
165 return wsts.isEmpty() ? null : wsts.get(0); 200 return wsts.isEmpty() ? null : wsts.get(0);
166 } 201 }
167 202
203
204 /**
205 * Load rows with a single columns result.
206 * @param session session to use for querying db.
207 * @param wst_id id of wst (in db).
208 * @param column_pos the column_pos (within the db) of the wst_value_table
209 * of which the values shall be fetched.
210 * @return resultant rows.
211 */
212 protected static List<WstValueTable.Row> loadRowsOneColumn(
213 Session session,
214 int wst_id,
215 int column_pos
216 ) {
217 SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_WS_AT_COL)
218 .addScalar("km", StandardBasicTypes.DOUBLE)
219 .addScalar("w", StandardBasicTypes.DOUBLE);
220
221 sqlQuery.setInteger("wst_id", wst_id);
222 sqlQuery.setInteger("column_pos", column_pos);
223
224 List<Object []> results = sqlQuery.list();
225
226 double [] ws = null;
227
228 ArrayList<WstValueTable.Row> rows = new ArrayList<WstValueTable.Row>();
229
230 // Walk over rows.
231 for (Object [] result: results) {
232 ws = new double[1];
233 WstValueTable.Row row =
234 new WstValueTable.Row((Double) result[0], ws);
235 rows.add(row);
236
237 Double w = (Double) result[1];
238 ws[0] = w != null ? w : Double.NaN;
239 }
240
241 rows.trimToSize();
242 return rows;
243 }
244
168 protected static List<WstValueTable.Row> loadRows( 245 protected static List<WstValueTable.Row> loadRows(
169 Session session, 246 Session session,
170 int wst_id, 247 int wst_id,
171 int numColumns 248 int numColumns
172 ){ 249 ) {
173 SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_WS) 250 SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_WS)
174 .addScalar("km", StandardBasicTypes.DOUBLE) 251 .addScalar("km", StandardBasicTypes.DOUBLE)
175 .addScalar("w", StandardBasicTypes.DOUBLE) 252 .addScalar("w", StandardBasicTypes.DOUBLE)
176 .addScalar("column_pos", StandardBasicTypes.INTEGER); 253 .addScalar("column_pos", StandardBasicTypes.INTEGER);
177 254
198 lastColumn = column; 275 lastColumn = column;
199 } 276 }
200 277
201 rows.trimToSize(); 278 rows.trimToSize();
202 return rows; 279 return rows;
203
204 } 280 }
205 281
206 protected static List<WstValueTable.Row> loadRows( 282 protected static List<WstValueTable.Row> loadRows(
207 Session session, 283 Session session,
208 Wst wst, 284 Wst wst,
209 int numColumns 285 int numColumns
210 ) { 286 ) {
211 return loadRows(session, wst.getId(), numColumns); 287 return loadRows(session, wst.getId(), numColumns);
288 }
289
290
291 protected static WstValueTable.Column [] loadColumn(
292 Session session,
293 int wst_id,
294 int col_pos
295 ) {
296 return new WstValueTable.Column [] {
297 new WstValueTable.Column(WKmsFactory.getWKmsName(col_pos, wst_id))};
212 } 298 }
213 299
214 300
215 /** 301 /**
216 * Get columns from wst-id. 302 * Get columns from wst-id.
243 protected static WstValueTable.Column [] loadColumns( 329 protected static WstValueTable.Column [] loadColumns(
244 Session session, 330 Session session,
245 Wst wst 331 Wst wst
246 ) { 332 ) {
247 return loadColumns(session, wst.getId()); 333 return loadColumns(session, wst.getId());
334 }
335
336
337 /**
338 * Build a QRange-Tree.
339 */
340 protected static void loadQRanges(
341 Session session,
342 WstValueTable.Column [] columns,
343 int wst_id,
344 int column_pos
345 ) {
346 SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_QS_AT_COL)
347 .addScalar("column_pos", StandardBasicTypes.INTEGER) // keep to maintain order.
348 .addScalar("q", StandardBasicTypes.DOUBLE)
349 .addScalar("a", StandardBasicTypes.DOUBLE)
350 .addScalar("b", StandardBasicTypes.DOUBLE);
351
352 sqlQuery.setInteger("wst_id", wst_id);
353 sqlQuery.setInteger("column_pos", column_pos);
354
355 List<Object []> qRanges = sqlQuery.list();
356
357 int qSize = qRanges.size();
358
359 QRangeTree qRangeTree = new QRangeTree(qRanges, 0, qSize);
360 columns[0].setQRangeTree(qRangeTree);
248 } 361 }
249 362
250 protected static void loadQRanges( 363 protected static void loadQRanges(
251 Session session, 364 Session session,
252 WstValueTable.Column [] columns, 365 WstValueTable.Column [] columns,

http://dive4elements.wald.intevation.org