comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadFactory.java @ 7181:805021c04861

issue1435: Add method to SedimentLoadFactory to fetch a SedimentLoad by id.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Thu, 26 Sep 2013 21:44:53 +0200
parents e0b6b6cf4708
children 73d7886fa59e
comparison
equal deleted inserted replaced
7180:e0b6b6cf4708 7181:805021c04861
83 " JOIN time_intervals ti ON sy.time_interval_id = ti.id " + 83 " JOIN time_intervals ti ON sy.time_interval_id = ti.id " +
84 " JOIN units u ON u.id = sy.unit_id " + 84 " JOIN units u ON u.id = sy.unit_id " +
85 " WHERE r.name = :name " + 85 " WHERE r.name = :name " +
86 " AND ti.stop_time IS NOT NULL " + 86 " AND ti.stop_time IS NOT NULL " +
87 " AND syv.station BETWEEN :startKm AND :endKm"; 87 " AND syv.station BETWEEN :startKm AND :endKm";
88
89 public static final String SQL_SELECT_SINGLES_DATA_BY_ID =
90 "SELECT" +
91 " sy.description AS description, " +
92 " syv.value AS load, " +
93 " syv.station AS km, " +
94 " u.name AS unit " +
95 " gf.name AS fraction " +
96 " FROM sediment_yield sy " +
97 " JOIN sediment_yield_values syv ON sy.id = syv.sediment_yield_id " +
98 " JOIN units u ON u.id = sy.unit_id" +
99 " JOIN grain_fraction gf ON sy.grain_fraction_id = gf.id " +
100 " WHERE sy.id = :id" +
101 " ORDER BY syv.station";
88 102
89 public static final String SQL_SELECT_SINGLES_DATA = 103 public static final String SQL_SELECT_SINGLES_DATA =
90 "SELECT" + 104 "SELECT" +
91 " sy.description AS description, " + 105 " sy.description AS description, " +
92 " ti.start_time AS year, " + 106 " ti.start_time AS year, " +
341 } 355 }
342 return new SedimentLoad[0]; 356 return new SedimentLoad[0];
343 } 357 }
344 358
345 /** 359 /**
360 * Get a specific sediment load from db.
361 *
362 * @param id the sediment yields id.
363 *
364 * @return according sediment load.
365 */
366 public static SedimentLoad getSedimentLoadWithDataUncached(
367 String id,
368 String river
369 ) {
370 log.debug("SedimentLoadFactory.getSedimentLoadWithDataUncached / id " + id);
371 Session session = SessionHolder.HOLDER.get();
372 SQLQuery sqlQuery = null;
373
374 // Measurement stations: all, for float-stuff, for suspended stuff.
375 // Because they need fast sorted access, use TreeMaps.
376 // They map the starting validity range km to the station itself.
377 List<MeasurementStation> allStations =
378 RiverFactory.getRiver(river).getMeasurementStations();
379 TreeMap<Double,MeasurementStation> floatStations =
380 new TreeMap<Double, MeasurementStation>();
381 TreeMap<Double,MeasurementStation> suspStations =
382 new TreeMap<Double, MeasurementStation>();
383
384 // From all stations, sort into the two kinds, skip undefined ones.
385 for (MeasurementStation measurementStation: allStations) {
386 if (measurementStation.getMeasurementType() == null ||
387 measurementStation.getRange() == null) {
388 continue;
389 }
390 if (measurementStation.getMeasurementType().equals("Schwebstoff")) {
391 suspStations.put(
392 measurementStation.getRange().getA().doubleValue(),
393 measurementStation);
394 }
395 else if (measurementStation.getMeasurementType().equals("Geschiebe")) {
396 floatStations.put(
397 measurementStation.getRange().getA().doubleValue(),
398 measurementStation);
399 }
400 }
401
402 sqlQuery = session.createSQLQuery(SQL_SELECT_SINGLES_DATA_BY_ID)
403 .addScalar("description", StandardBasicTypes.STRING)
404 .addScalar("load", StandardBasicTypes.DOUBLE)
405 .addScalar("km", StandardBasicTypes.DOUBLE)
406 .addScalar("fraction", StandardBasicTypes.STRING)
407 .addScalar("unit", StandardBasicTypes.STRING);
408 sqlQuery.setInteger("id", Integer.valueOf(id));
409
410 List<Object []> results = sqlQuery.list();
411 SedimentLoad load = new SedimentLoad();
412 if (results.isEmpty()) {
413 log.warn("Empty result for year calculation.");
414 }
415 else {
416 Object[] row = results.get(0);
417 load = new SedimentLoad(
418 (String) row[0], //description
419 null,//(Date) row[1], //start
420 null, //end
421 false, //isEpoch
422 (String) row[4]); //unit
423
424 String fraction = (String) row[3];
425
426 TreeMap<Double,MeasurementStation> relevantStations =
427 fraction.equals("suspended_sediment")
428 ? suspStations
429 : floatStations;
430
431 for (int i = 0; i < results.size(); i++) {
432 row = results.get(i);
433 double km = (Double) row[2];
434 Range range = findMeasurementStationRange(relevantStations, km);
435 if (range == null) {
436 log.warn("No measurement station for " + fraction + " km " + km);
437 continue;
438 }
439
440 double v = -1;
441
442 if (row[1] != null) {
443 v = ((Double)row[1]).doubleValue();
444 }
445
446 setLoadFraction(load, km, v, range, fraction);
447 }
448
449 }
450
451 return load;
452 }
453
454 /**
346 * Get sediment loads from db. 455 * Get sediment loads from db.
456 *
347 * @param river the river 457 * @param river the river
348 * @param type the sediment load type (year, epoch or off_epoch) 458 * @param type the sediment load type (year, epoch or off_epoch)
349 * 459 *
350 * @return according sediment loads. 460 * @return according sediment loads.
351 */ 461 */

http://dive4elements.wald.intevation.org