[PATCH 2 of 3] issue1435: Add method to SedimentLoadFactory to fetch a SedimentLoad by id

Wald Commits scm-commit at wald.intevation.org
Thu Sep 26 21:53:13 CEST 2013


# HG changeset patch
# User Felix Wolfsteller <felix.wolfsteller at intevation.de>
# Date 1380224693 -7200
# Node ID 805021c04861ccf95a57ad2c4b65abf7dfa8202e
# Parent  e0b6b6cf4708e54e8fca98d2d354a898d0f2ce8f
issue1435: Add method to SedimentLoadFactory to fetch a SedimentLoad by id.

diff -r e0b6b6cf4708 -r 805021c04861 artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadFactory.java
--- 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)
      *


More information about the Dive4elements-commits mailing list