Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WstValueTableFactory.java @ 1839:c3b44da22a3a
Minor refactoring in preparation of generation of interpolators (WstValueTable) for static WQKms.
flys-artifacts/trunk@3175 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Felix Wolfsteller <felix.wolfsteller@intevation.de> |
---|---|
date | Mon, 07 Nov 2011 12:37:41 +0000 |
parents | b503d92dd709 |
children | d5e51cc7da23 |
comparison
equal
deleted
inserted
replaced
1838:853cd2120d69 | 1839:c3b44da22a3a |
---|---|
19 import org.hibernate.Query; | 19 import org.hibernate.Query; |
20 import org.hibernate.SQLQuery; | 20 import org.hibernate.SQLQuery; |
21 | 21 |
22 import org.hibernate.type.StandardBasicTypes; | 22 import org.hibernate.type.StandardBasicTypes; |
23 | 23 |
24 /** | |
25 * Creates WstValueTable s from database. | |
26 * WstValueTable s are used to interpolate given w/q/km values. | |
27 */ | |
24 public class WstValueTableFactory | 28 public class WstValueTableFactory |
25 { | 29 { |
26 private static Logger log = Logger.getLogger(WstValueTableFactory.class); | 30 private static Logger log = Logger.getLogger(WstValueTableFactory.class); |
27 | 31 |
28 public static final int DEFAULT_KIND = 0; | 32 public static final int DEFAULT_KIND = 0; |
48 } | 52 } |
49 | 53 |
50 public static WstValueTable getTable(River river) { | 54 public static WstValueTable getTable(River river) { |
51 return getTable(river, DEFAULT_KIND); | 55 return getTable(river, DEFAULT_KIND); |
52 } | 56 } |
57 | |
53 | 58 |
54 public static WstValueTable getTable(River river, int kind) { | 59 public static WstValueTable getTable(River river, int kind) { |
55 | 60 |
56 Cache cache = CacheFactory.getCache(WstValueTableCacheKey.CACHE_NAME); | 61 Cache cache = CacheFactory.getCache(WstValueTableCacheKey.CACHE_NAME); |
57 | 62 |
111 query.setParameter("river", river); | 116 query.setParameter("river", river); |
112 query.setInteger("kind", kind); | 117 query.setInteger("kind", kind); |
113 | 118 |
114 List<Wst> wsts = query.list(); | 119 List<Wst> wsts = query.list(); |
115 | 120 |
121 // TODO Multiple wsts can match, why return just the first one? | |
116 return wsts.isEmpty() ? null : wsts.get(0); | 122 return wsts.isEmpty() ? null : wsts.get(0); |
117 } | 123 } |
118 | 124 |
119 protected static List<WstValueTable.Row> loadRows( | 125 protected static List<WstValueTable.Row> loadRows( |
120 Session session, | 126 Session session, |
121 Wst wst, | 127 int wst_id, |
122 int numColumns | 128 int numColumns |
123 ) { | 129 ){ |
124 SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_WS) | 130 SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_WS) |
125 .addScalar("km", StandardBasicTypes.DOUBLE) | 131 .addScalar("km", StandardBasicTypes.DOUBLE) |
126 .addScalar("w", StandardBasicTypes.DOUBLE) | 132 .addScalar("w", StandardBasicTypes.DOUBLE) |
127 .addScalar("column_pos", StandardBasicTypes.INTEGER); | 133 .addScalar("column_pos", StandardBasicTypes.INTEGER); |
128 | 134 |
129 sqlQuery.setInteger("wst_id", wst.getId()); | 135 sqlQuery.setInteger("wst_id", wst_id); |
130 | 136 |
131 List<Object []> results = sqlQuery.list(); | 137 List<Object []> results = sqlQuery.list(); |
132 | 138 |
133 int lastColumn = Integer.MAX_VALUE; | 139 int lastColumn = Integer.MAX_VALUE; |
134 double [] ws = null; | 140 double [] ws = null; |
148 lastColumn = column; | 154 lastColumn = column; |
149 } | 155 } |
150 | 156 |
151 rows.trimToSize(); | 157 rows.trimToSize(); |
152 return rows; | 158 return rows; |
153 } | 159 |
154 | 160 } |
161 | |
162 protected static List<WstValueTable.Row> loadRows( | |
163 Session session, | |
164 Wst wst, | |
165 int numColumns | |
166 ) { | |
167 return loadRows(session, wst.getId(), numColumns); | |
168 } | |
169 | |
170 | |
171 /** | |
172 * Get columns from wst-id. | |
173 */ | |
155 protected static WstValueTable.Column [] loadColumns( | 174 protected static WstValueTable.Column [] loadColumns( |
156 Session session, | 175 Session session, |
157 Wst wst | 176 int wst_id |
158 ) { | 177 ) { |
159 SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_NAMES_POS) | 178 SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_NAMES_POS) |
160 .addScalar("position", StandardBasicTypes.INTEGER) | 179 .addScalar("position", StandardBasicTypes.INTEGER) |
161 .addScalar("name", StandardBasicTypes.STRING); | 180 .addScalar("name", StandardBasicTypes.STRING); |
162 | 181 |
163 sqlQuery.setInteger("wst_id", wst.getId()); | 182 sqlQuery.setInteger("wst_id", wst_id); |
164 | 183 |
165 List<Object []> columnNames = sqlQuery.list(); | 184 List<Object []> columnNames = sqlQuery.list(); |
166 | 185 |
167 WstValueTable.Column [] columns = | 186 WstValueTable.Column [] columns = |
168 new WstValueTable.Column[columnNames.size()]; | 187 new WstValueTable.Column[columnNames.size()]; |
172 (String)columnNames.get(i)[1]); | 191 (String)columnNames.get(i)[1]); |
173 } | 192 } |
174 return columns; | 193 return columns; |
175 } | 194 } |
176 | 195 |
196 /** | |
197 * Get columns from Wst. | |
198 */ | |
199 protected static WstValueTable.Column [] loadColumns( | |
200 Session session, | |
201 Wst wst | |
202 ) { | |
203 return loadColumns(session, wst.getId()); | |
204 } | |
205 | |
177 protected static void loadQRanges( | 206 protected static void loadQRanges( |
178 Session session, | 207 Session session, |
179 WstValueTable.Column [] columns, | 208 WstValueTable.Column [] columns, |
180 Wst wst | 209 int wst_id |
181 ) { | 210 ) { |
182 SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_QS) | 211 SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_QS) |
183 .addScalar("column_pos", StandardBasicTypes.INTEGER) | 212 .addScalar("column_pos", StandardBasicTypes.INTEGER) |
184 .addScalar("q", StandardBasicTypes.DOUBLE) | 213 .addScalar("q", StandardBasicTypes.DOUBLE) |
185 .addScalar("a", StandardBasicTypes.DOUBLE) | 214 .addScalar("a", StandardBasicTypes.DOUBLE) |
186 .addScalar("b", StandardBasicTypes.DOUBLE); | 215 .addScalar("b", StandardBasicTypes.DOUBLE); |
187 | 216 |
188 sqlQuery.setInteger("wst_id", wst.getId()); | 217 sqlQuery.setInteger("wst_id", wst_id); |
189 | 218 |
190 List<Object []> qRanges = sqlQuery.list(); | 219 List<Object []> qRanges = sqlQuery.list(); |
191 | 220 |
192 int start = -1; | 221 int start = -1; |
193 int Q = qRanges.size(); | 222 int Q = qRanges.size(); |
239 if (out != null) { | 268 if (out != null) { |
240 out.close(); | 269 out.close(); |
241 } | 270 } |
242 } | 271 } |
243 */ | 272 */ |
273 | |
274 } | |
275 | |
276 protected static void loadQRanges( | |
277 Session session, | |
278 WstValueTable.Column [] columns, | |
279 Wst wst | |
280 ) { | |
281 loadQRanges(session, columns, wst.getId()); | |
244 } | 282 } |
245 | 283 |
246 } | 284 } |
247 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 285 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |