changeset 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 87c32adb7088
files artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadFactory.java
diffstat 1 files changed, 110 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadFactory.java	Thu Sep 26 21:30:19 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadFactory.java	Thu Sep 26 21:44:53 2013 +0200
@@ -86,6 +86,20 @@
         "       AND ti.stop_time IS NOT NULL " +
         "       AND syv.station BETWEEN :startKm AND :endKm";
 
+    public static final String SQL_SELECT_SINGLES_DATA_BY_ID =
+        "SELECT" +
+        "       sy.description AS description, " +
+        "       syv.value AS load, " +
+        "       syv.station AS km, " +
+        "       u.name AS unit " +
+        "       gf.name AS fraction " +
+        "   FROM     sediment_yield sy " +
+        "       JOIN sediment_yield_values syv ON sy.id = syv.sediment_yield_id " +
+        "       JOIN units u ON u.id = sy.unit_id" +
+        "       JOIN grain_fraction gf ON sy.grain_fraction_id = gf.id " +
+        "   WHERE   sy.id = :id" +
+        "   ORDER BY syv.station";
+
     public static final String SQL_SELECT_SINGLES_DATA =
         "SELECT" +
         "       sy.description AS description, " +
@@ -343,7 +357,103 @@
     }
 
     /**
+     * Get a specific sediment load from db.
+     *
+     * @param id the sediment yields id.
+     *
+     * @return according sediment load.
+     */
+    public static SedimentLoad getSedimentLoadWithDataUncached(
+        String id,
+        String river
+    ) {
+        log.debug("SedimentLoadFactory.getSedimentLoadWithDataUncached / id " + id);
+        Session session = SessionHolder.HOLDER.get();
+        SQLQuery sqlQuery = null;
+
+        // Measurement stations: all, for float-stuff, for suspended stuff.
+        // Because they need fast sorted access, use TreeMaps.
+        // They map the starting validity range km to the station itself.
+        List<MeasurementStation> allStations =
+            RiverFactory.getRiver(river).getMeasurementStations();
+        TreeMap<Double,MeasurementStation> floatStations =
+            new TreeMap<Double, MeasurementStation>();
+        TreeMap<Double,MeasurementStation> suspStations =
+            new TreeMap<Double, MeasurementStation>();
+
+        // From all stations, sort into the two kinds, skip undefined ones.
+        for (MeasurementStation measurementStation: allStations) {
+            if (measurementStation.getMeasurementType() == null ||
+                measurementStation.getRange() == null) {
+                continue;
+            }
+            if (measurementStation.getMeasurementType().equals("Schwebstoff")) {
+                suspStations.put(
+                    measurementStation.getRange().getA().doubleValue(),
+                    measurementStation);
+            }
+            else if (measurementStation.getMeasurementType().equals("Geschiebe")) {
+                floatStations.put(
+                    measurementStation.getRange().getA().doubleValue(),
+                    measurementStation);
+            }
+        }
+
+        sqlQuery = session.createSQLQuery(SQL_SELECT_SINGLES_DATA_BY_ID)
+            .addScalar("description", StandardBasicTypes.STRING)
+            .addScalar("load", StandardBasicTypes.DOUBLE)
+            .addScalar("km", StandardBasicTypes.DOUBLE)
+            .addScalar("fraction", StandardBasicTypes.STRING)
+            .addScalar("unit", StandardBasicTypes.STRING);
+        sqlQuery.setInteger("id", Integer.valueOf(id));
+
+        List<Object []> results = sqlQuery.list();
+        SedimentLoad load = new SedimentLoad();
+        if (results.isEmpty()) {
+            log.warn("Empty result for year calculation.");
+        }
+        else {
+            Object[] row = results.get(0);
+            load = new SedimentLoad(
+                    (String) row[0], //description
+                    null,//(Date) row[1], //start
+                    null,  //end
+                    false, //isEpoch
+                    (String) row[4]); //unit
+
+            String fraction = (String) row[3];
+
+            TreeMap<Double,MeasurementStation> relevantStations = 
+                fraction.equals("suspended_sediment")
+                ? suspStations
+                : floatStations;
+
+            for (int i = 0; i < results.size(); i++) {
+                row = results.get(i);
+                double km = (Double) row[2];
+                Range range = findMeasurementStationRange(relevantStations, km);
+                if (range == null) {
+                    log.warn("No measurement station for " + fraction + " km " + km);
+                    continue;
+                }
+
+                double v = -1;
+
+                if (row[1] != null) {
+                    v = ((Double)row[1]).doubleValue();
+                }
+
+                setLoadFraction(load, km, v, range, fraction);
+            }
+
+        }
+
+        return load;
+    }
+
+    /**
      * Get sediment loads from db.
+     *
      * @param river the river
      * @param type the sediment load type (year, epoch or off_epoch)
      *

http://dive4elements.wald.intevation.org