changeset 8543:9a5b3079aad4

(issue1750) Take Static SQ Relations based on a range and merge them together
author Andre Heinecke <andre.heinecke@intevation.de>
date Fri, 13 Feb 2015 14:59:16 +0100
parents 7b210881d6db
children 76113b975829
files artifacts/src/main/java/org/dive4elements/river/artifacts/model/sq/StaticSQFactory.java artifacts/src/main/java/org/dive4elements/river/artifacts/states/sq/SQStaticState.java
diffstat 2 files changed, 72 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/sq/StaticSQFactory.java	Fri Feb 13 13:44:52 2015 +0100
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/sq/StaticSQFactory.java	Fri Feb 13 14:59:16 2015 +0100
@@ -28,6 +28,17 @@
     private static final Logger log =
         Logger.getLogger(StaticSQFactory.class);
 
+    public static final String SQL_STATIONS_AT_RANGE =
+        "SELECT "+
+            "ms.id AS ms_id " +
+        "FROM measurement_station ms " +
+            "JOIN ranges ra ON ra.id = ms.range_id " +
+            "JOIN rivers r ON r.id = ra.river_id " +
+            "WHERE r.name = :river AND " +
+            "ms.range_id = (SELECT range_id " +
+                           "FROM measurement_station " +
+                           "WHERE id = :ms_id)";
+
     public static final String SQL_SQ =
         "SELECT " +
             "sq.description AS description,"+
@@ -63,6 +74,64 @@
     private StaticSQFactory() {
     }
 
+    /** Get SQ relations for a measurement station's location.
+     * Returns all SQRelations for the location of the station and
+     * not just for the station. E.g. for a "Geschiebemessstelle"
+     * and a "Schwebstoffmesstelle" at the same place.*/
+    public static StaticSQContainer getSQRelationsForLocation(
+        String river,
+        int measurementStation
+    ) {
+        Session session = SessionHolder.HOLDER.get();
+        Query query = session.createSQLQuery(SQL_STATIONS_AT_RANGE)
+            .setParameter("river", river)
+            .setParameter("ms_id", measurementStation);
+        /* Take the first container for the station requested. */
+        StaticSQContainer retval = getSQRelations(river, measurementStation);
+
+        /* And some others */
+        List<Integer> list = query.list();
+        if (list == null || list.isEmpty()) {
+            log.error("Did not even find one measurement station. Broken Query?");
+            return retval;
+        }
+
+        for (Integer stationId: list) {
+            log.debug("Collecting SQ Relations for: "+ stationId);
+            if (stationId == measurementStation) {
+                /* Skip the same station */
+                continue;
+            }
+
+            StaticSQContainer additional = getSQRelations(river, stationId);
+            if (additional == null || additional.getSQRelations() == null) {
+                continue;
+            }
+
+            if (retval == null || retval.getSQRelations() == null || retval.getSQRelations().isEmpty()) {
+                /* Old one is empty, just take the new one. */
+                retval = additional;
+                continue;
+            }
+
+            for (StaticSQRelation rel: additional.getSQRelations()) {
+                /* Check if we already have one for this parameter.
+                 * This is highly unlikely in the data scheme of things. */
+                List<StaticSQRelation> old = retval.getRelationsByParameter(
+                        rel.getParameter());
+                if (old != null || !old.isEmpty()) {
+                    log.warn("Multiple SQ relation Parameters found for different " +
+                             "measurement_stations at the same range. This should not happen.");
+                    continue;
+                }
+                retval.addSQRelation(rel);
+            }
+
+        }
+        return retval;
+    }
+
+
     public static StaticSQContainer getSQRelations(
         String river,
         int measurementStation
@@ -75,7 +144,7 @@
             cacheKey = new StaticSQCacheKey(river, measurementStation);
             Element element = cache.get(cacheKey);
             if (element != null) {
-                log.debug("Got static bedheight values from cache");
+                log.debug("Got static sq relations from cache");
                 return (StaticSQContainer)element.getValue();
             }
         }
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/states/sq/SQStaticState.java	Fri Feb 13 13:44:52 2015 +0100
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/states/sq/SQStaticState.java	Fri Feb 13 14:59:16 2015 +0100
@@ -68,7 +68,7 @@
         }
         log.debug("Parsed measurement station: " + ms);
 
-        sqRelations = StaticSQFactory.getSQRelations(river, ms);
+        sqRelations = StaticSQFactory.getSQRelationsForLocation(river, ms);
         DateFormat df = new SimpleDateFormat("yyyy");
 
         for (StaticSQRelation.Parameter p: StaticSQRelation.Parameter.values()) {
@@ -143,7 +143,7 @@
             }
             log.debug("Parsed measurement station: " + ms);
 
-            sqRelations = StaticSQFactory.getSQRelations(river, ms);
+            sqRelations = StaticSQFactory.getSQRelationsForLocation(river, ms);
         }
 
         DateFormat df = new SimpleDateFormat("yyyy");

http://dive4elements.wald.intevation.org