comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/SedimentLoadFactory.java @ 4370:6a65e7ef43c0

Updated data object and factory for sediment load. * Added new data fields to object and cache key. * Fixed SQL statement in factory and fill load objects correctly. *
author Raimund Renkert <rrenkert@intevation.de>
date Fri, 02 Nov 2012 14:48:16 +0100
parents 3051bc28ac43
children b03f2e74025c
comparison
equal deleted inserted replaced
4369:8ddab49ff297 4370:6a65e7ef43c0
56 56
57 public static final String SQL_SELECT_SINGLES_DATA = 57 public static final String SQL_SELECT_SINGLES_DATA =
58 "SELECT" + 58 "SELECT" +
59 " sy.description AS description, " + 59 " sy.description AS description, " +
60 " ti.start_time AS year, " + 60 " ti.start_time AS year, " +
61 " syv.value AS load " + 61 " syv.value AS load, " +
62 " syv.station AS km " +
62 " FROM sediment_yield sy " + 63 " FROM sediment_yield sy " +
63 " JOIN rivers r ON sy.river_id = r.id " + 64 " JOIN rivers r ON sy.river_id = r.id " +
64 " JOIN time_intervals ti ON sy.time_interval_id = ti.id " + 65 " JOIN time_intervals ti ON sy.time_interval_id = ti.id " +
65 " JOIN sediment_yield_vales syv ON sy.id = syv.sediment_yield_id " + 66 " JOIN sediment_yield_values syv ON sy.id = syv.sediment_yield_id " +
66 " JOIN grain_fraction gf ON sy.grain_fraction_id = gf.id " + 67 " JOIN grain_fraction gf ON sy.grain_fraction_id = gf.id " +
67 " WHERE r.name = :name " + 68 " WHERE r.name = :name " +
68 " AND ti.start_time BETWEEN :begin AND :end " + 69 " AND ti.start_time BETWEEN :begin AND :end " +
69 " AND ti_stop_time IS NULL " + 70 " AND ti.stop_time IS NULL " +
70 " AND gf.name = :grain " + 71 " AND gf.name = :grain " +
71 " AND syv.station BETWEEN :startKm AND :endKm"; 72 " AND syv.station BETWEEN :startKm AND :endKm";
72 73
73 public static final String SQL_SELECT_EPOCHS_DATA = 74 public static final String SQL_SELECT_EPOCHS_DATA =
74 "SELECT" + 75 "SELECT" +
75 " sy.description AS description," + 76 " sy.description AS description, " +
76 " ti.start_time AS year," + 77 " ti.start_time AS year, " +
77 " syv.value AS load" + 78 " syv.value AS load, " +
79 " syv.station AS km " +
78 " FROM sediment_yield sy" + 80 " FROM sediment_yield sy" +
79 " JOIN rivers r ON sy.river_id = r.id " + 81 " JOIN rivers r ON sy.river_id = r.id " +
80 " JOIN time_intervals ti ON sy.time_interval_id = ti.id" + 82 " JOIN time_intervals ti ON sy.time_interval_id = ti.id" +
81 " JOIN sediment_yield_vales syv ON sy.id = syv.sediment_yield_id" + 83 " JOIN sediment_yield_values syv ON sy.id = syv.sediment_yield_id" +
82 " JOIN grain_fraction gf ON sy.grain_fraction_id = gf.id" + 84 " JOIN grain_fraction gf ON sy.grain_fraction_id = gf.id" +
83 " WHERE r.name = :name" + 85 " WHERE r.name = :name" +
84 " AND ti.start_time BETWEEN :sbegin AND :send" + 86 " AND ti.start_time BETWEEN :sbegin AND :send" +
85 " AND ti_stop_time IS NOT NULL" + 87 " AND ti.stop_time IS NOT NULL" +
86 " AND ti_stop_time BETWEEN :ebegin AND :eend" + 88 " AND ti.stop_time BETWEEN :ebegin AND :eend" +
87 " AND gf.name = :grain " + 89 " AND gf.name = :grain " +
88 " AND syv.station BETWEEN :startKm AND :endKm"; 90 " AND syv.station BETWEEN :startKm AND :endKm";
89 91
90 private SedimentLoadFactory() { 92 private SedimentLoadFactory() {
91 } 93 }
106 log.debug("Cache not configured."); 108 log.debug("Cache not configured.");
107 return getSedimentLoadsUncached(river, type, startKm, endKm); 109 return getSedimentLoadsUncached(river, type, startKm, endKm);
108 } 110 }
109 111
110 StaticSedimentLoadCacheKey key = 112 StaticSedimentLoadCacheKey key =
111 new StaticSedimentLoadCacheKey(river, startKm, endKm, null); 113 new StaticSedimentLoadCacheKey(river, startKm, endKm, 0, 0);
112 114
113 Element element = cache.get(key); 115 Element element = cache.get(key);
114 116
115 if (element != null) { 117 if (element != null) {
116 log.debug("SedimentLoad found in cache"); 118 log.debug("SedimentLoad found in cache");
119 121
120 SedimentLoad[] values = 122 SedimentLoad[] values =
121 getSedimentLoadsUncached(river, type, startKm, endKm); 123 getSedimentLoadsUncached(river, type, startKm, endKm);
122 124
123 if (values != null && key != null) { 125 if (values != null && key != null) {
124 log.debug("Store static sediment loads values in cache."); 126 log.debug("Store static sediment load values in cache.");
125 element = new Element(key, values); 127 element = new Element(key, values);
126 cache.put(element); 128 cache.put(element);
127 } 129 }
128 return values; 130 return values;
129 } 131 }
131 public static SedimentLoad getLoadwithData( 133 public static SedimentLoad getLoadwithData(
132 String river, 134 String river,
133 String type, 135 String type,
134 double startKm, 136 double startKm,
135 double endKm, 137 double endKm,
136 Date startDate, 138 int syear,
137 Date endDate 139 int eyear
138 ) { 140 ) {
139 log.debug("SedimentLoadFactory.getLoadWithData"); 141 log.debug("SedimentLoadFactory.getLoadWithData");
140 Cache cache = CacheFactory.getCache(LOAD_DATA_CACHE_NAME); 142 Cache cache = CacheFactory.getCache(LOAD_DATA_CACHE_NAME);
141 143
142 if (cache == null) { 144 if (cache == null) {
144 return getSedimentLoadWithDataUncached( 146 return getSedimentLoadWithDataUncached(
145 river, 147 river,
146 type, 148 type,
147 startKm, 149 startKm,
148 endKm, 150 endKm,
149 startDate, 151 syear,
150 endDate); 152 eyear);
151 } 153 }
152 154
153 StaticSedimentLoadCacheKey key = 155 StaticSedimentLoadCacheKey key =
154 new StaticSedimentLoadCacheKey(river, startKm, endKm, startDate); 156 new StaticSedimentLoadCacheKey(river, startKm, endKm, syear, eyear);
155 157
156 Element element = cache.get(key); 158 Element element = cache.get(key);
157 159
158 if (element != null) { 160 if (element != null) {
159 log.debug("SedimentLoad found in cache"); 161 log.debug("SedimentLoad found in cache");
160 return (SedimentLoad)element.getValue(); 162 return (SedimentLoad)element.getValue();
161 } 163 }
162 164
163 SedimentLoad values = 165 SedimentLoad values = getSedimentLoadWithDataUncached(
164 getSedimentLoadWithDataUncached(river, type, startKm, endKm, startDate, endDate); 166 river,
167 type,
168 startKm,
169 endKm,
170 syear,
171 eyear);
165 172
166 if (values != null && key != null) { 173 if (values != null && key != null) {
167 log.debug("Store static bed height values in cache."); 174 log.debug("Store static bed height values in cache.");
168 element = new Element(key, values); 175 element = new Element(key, values);
169 cache.put(element); 176 cache.put(element);
241 public static SedimentLoad getSedimentLoadWithDataUncached( 248 public static SedimentLoad getSedimentLoadWithDataUncached(
242 String river, 249 String river,
243 String type, 250 String type,
244 double startKm, 251 double startKm,
245 double endKm, 252 double endKm,
246 Date sdate, 253 int syear,
247 Date edate 254 int eyear
248 ) { 255 ) {
249 log.debug("SedimentLoadFactory.getBedHeightUncached"); 256 log.debug("SedimentLoadFactory.getSedimentLoadWithDataUncached");
250 257
251 Session session = SessionHolder.HOLDER.get(); 258 Session session = SessionHolder.HOLDER.get();
252 SQLQuery sqlQuery = null; 259 SQLQuery sqlQuery = null;
253 260
254 Calendar cal = Calendar.getInstance(); 261 Calendar start = Calendar.getInstance();
255 cal.setTime(sdate); 262 start.set(syear, 1, 1);
256 int year = cal.get(Calendar.YEAR);
257 cal.set(year, 1, 1);
258 Calendar end = Calendar.getInstance(); 263 Calendar end = Calendar.getInstance();
259 end.set(year, 12, 31); 264 end.set(syear, 12, 31);
260 265
261 if (type.equals("single")) { 266 if (type.equals("year") || type.equals("epoch")) {
262 sqlQuery = session.createSQLQuery(SQL_SELECT_SINGLES_DATA) 267 sqlQuery = session.createSQLQuery(SQL_SELECT_SINGLES_DATA)
263 .addScalar("description", StandardBasicTypes.STRING) 268 .addScalar("description", StandardBasicTypes.STRING)
264 .addScalar("year", StandardBasicTypes.DATE) 269 .addScalar("year", StandardBasicTypes.DATE)
265 .addScalar("load", StandardBasicTypes.DOUBLE); 270 .addScalar("load", StandardBasicTypes.DOUBLE)
271 .addScalar("km", StandardBasicTypes.DOUBLE);
266 sqlQuery.setString("name", river); 272 sqlQuery.setString("name", river);
267 sqlQuery.setDouble("startKm", startKm); 273 sqlQuery.setDouble("startKm", startKm);
268 sqlQuery.setDouble("endKm", endKm); 274 sqlQuery.setDouble("endKm", endKm);
269 sqlQuery.setDate("begin", cal.getTime()); 275 sqlQuery.setDate("begin", start.getTime());
270 sqlQuery.setDate("end", end.getTime()); 276 sqlQuery.setDate("end", end.getTime());
271 sqlQuery.setString("grain", "total"); 277 sqlQuery.setString("grain", "total");
272 List<Object []> results = sqlQuery.list(); 278 List<Object []> results = sqlQuery.list();
273 SedimentLoad load = new SedimentLoad(); 279 SedimentLoad load = new SedimentLoad();
274 if (results.size() != 1) {
275 // should not happen. throw some exception.
276 return new SedimentLoad();
277 }
278 Object[] row = results.get(0); 280 Object[] row = results.get(0);
279 load = new SedimentLoad( 281 load = new SedimentLoad(
280 (String) row[0], 282 (String) row[0],
281 (Date) row[1], 283 (Date) row[1],
282 null, 284 null,
283 false); 285 false);
284 load.addCoarseValues(getValues("coarse", sqlQuery)); 286 getValues("coarse", sqlQuery, load);
285 load.addFineMiddleValues(getValues("fine_middle", sqlQuery)); 287 getValues("fine_middle", sqlQuery, load);
286 load.addSandValues(getValues("sand", sqlQuery)); 288 getValues("sand", sqlQuery, load);
287 load.addSuspSandBedValues(getValues("suspended_sediment", sqlQuery)); 289 getValues("suspended_sediment", sqlQuery, load);
288 load.addSuspSandBedValues(getValues("susp_sand_bed", sqlQuery)); 290 getValues("susp_sand_bed", sqlQuery, load);
291 getValues("susp_sand", sqlQuery, load);
289 return load; 292 return load;
290 } 293 }
291 else if (type.equals("epoch")) { 294 else if (type.equals("off_epoch")) {
292 Calendar send = Calendar.getInstance(); 295 Calendar toStart = Calendar.getInstance();
293 send.setTime(edate); 296 toStart.set(eyear, 1, 1);
294 int eyear = send.get(Calendar.YEAR); 297 Calendar toEnd = Calendar.getInstance();
295 send.set(year, 1, 1); 298 toEnd.set(eyear, 12, 31);
296 Calendar eend = Calendar.getInstance();
297 eend.set(eyear, 12, 31);
298 299
299 sqlQuery = session.createSQLQuery(SQL_SELECT_EPOCHS) 300 sqlQuery = session.createSQLQuery(SQL_SELECT_EPOCHS)
300 .addScalar("description", StandardBasicTypes.STRING) 301 .addScalar("description", StandardBasicTypes.STRING)
301 .addScalar("start_time", StandardBasicTypes.DATE) 302 .addScalar("start_time", StandardBasicTypes.DATE)
302 .addScalar("stop_time", StandardBasicTypes.DATE) 303 .addScalar("stop_time", StandardBasicTypes.DATE)
303 .addScalar("load", StandardBasicTypes.DOUBLE); 304 .addScalar("load", StandardBasicTypes.DOUBLE);
304 sqlQuery.setString("name", river); 305 sqlQuery.setString("name", river);
305 sqlQuery.setDouble("startKm", startKm); 306 sqlQuery.setDouble("startKm", startKm);
306 sqlQuery.setDouble("endKm", endKm); 307 sqlQuery.setDouble("endKm", endKm);
307 sqlQuery.setDate("sbegin", cal.getTime()); 308 sqlQuery.setDate("sbegin", start.getTime());
308 sqlQuery.setDate("sbegin", end.getTime()); 309 sqlQuery.setDate("sbegin", end.getTime());
309 sqlQuery.setDate("ebegin",send.getTime()); 310 sqlQuery.setDate("ebegin",toStart.getTime());
310 sqlQuery.setDate("eend", eend.getTime()); 311 sqlQuery.setDate("eend", toEnd.getTime());
311 sqlQuery.setString("grain", "total"); 312 sqlQuery.setString("grain", "total");
312 313
313 List<Object []> results = sqlQuery.list(); 314 List<Object []> results = sqlQuery.list();
314 315
315 SedimentLoad load = new SedimentLoad(); 316 SedimentLoad load = new SedimentLoad();
316 if (results.size() != 1) {
317 // should not happen. throw some exception.
318 return new SedimentLoad();
319 }
320 Object[] row = results.get(0); 317 Object[] row = results.get(0);
321 load = new SedimentLoad( 318 load = new SedimentLoad(
322 (String) row[0], 319 (String) row[0],
323 (Date) row[1], 320 (Date) row[1],
324 null, 321 null,
325 false); 322 false);
326 load.addCoarseValues(getValues("coarse", sqlQuery)); 323 TDoubleArrayList kms = new TDoubleArrayList();
327 load.addFineMiddleValues(getValues("fine_middle", sqlQuery)); 324 for (int i = 0; i < results.size(); i++) {
328 load.addSandValues(getValues("sand", sqlQuery)); 325 row = results.get(i);
329 load.addSuspSandBedValues(getValues("suspended_sediment", sqlQuery)); 326 kms.add((Double)row[3]);
330 load.addSuspSandBedValues(getValues("susp_sand_bed", sqlQuery)); 327 }
328 getValues("coarse", sqlQuery, load);
329 getValues("fine_middle", sqlQuery, load);
330 getValues("sand", sqlQuery, load);
331 getValues("suspended_sediment", sqlQuery, load);
332 getValues("susp_sand_bed", sqlQuery, load);
333 getValues("susp_sand", sqlQuery, load);
331 return load; 334 return load;
332 } 335 }
333 return new SedimentLoad(); 336 return new SedimentLoad();
334 } 337 }
335 338
336 /** 339 /**
337 * 340 *
338 */ 341 */
339 protected static TDoubleArrayList getValues ( 342 protected static void getValues (
340 String fraction, 343 String fraction,
341 SQLQuery query 344 SQLQuery query,
345 SedimentLoad load
342 ) { 346 ) {
343 query.setString("grain", fraction); 347 query.setString("grain", fraction);
344 List<Object[]> results = query.list(); 348 List<Object[]> results = query.list();
345 TDoubleArrayList values = new TDoubleArrayList();
346 for (int i = 0; i < results.size(); i++) { 349 for (int i = 0; i < results.size(); i++) {
347 Object[] row = results.get(i); 350 Object[] row = results.get(i);
348 values.add(((Double)row[2]).doubleValue()); 351 double km = (Double)row[3];
349 } 352 double v = -1;
350 return values; 353 if (row[2] != null) {
354 v = ((Double)row[2]).doubleValue();
355 }
356 if (fraction.equals("coarse")) {
357 load.setCoarse(km, v);
358 }
359 else if (fraction.equals("sand")) {
360 load.setSand(km, v);
361 }
362 else if (fraction.equals("fine_middle")) {
363 load.setFineMiddle(km, v);
364 }
365 else if (fraction.equals("suspended_sediment")) {
366 load.setSuspSediment(km, v);
367 }
368 else if (fraction.equals("sups_sand")) {
369 load.setSuspSand(km, v);
370 }
371 else if (fraction.equals("susp_sand_bed")) {
372 load.setSuspSandBed(km, v);
373 }
374 }
351 } 375 }
352 } 376 }

http://dive4elements.wald.intevation.org