[PATCH 2 of 2] (issue1750) Use new MeasuremenStation-methods to find companion station and do less checking for inconsistent data (schema or at least importer do the job now)

Wald Commits scm-commit at wald.intevation.org
Mon Apr 20 10:26:49 CEST 2015


# HG changeset patch
# User Tom Gottfried <tom at intevation.de>
# Date 1429518220 -7200
# Node ID 9bb7f19cbb6fc2fc901c73d97b2419dfc9ac6e24
# Parent  851ea37d35f3fcfa675b339e159dd80cf90cfb24
(issue1750) Use new MeasuremenStation-methods to find companion station and do less checking for inconsistent data (schema or at least importer do the job now).

diff -r 851ea37d35f3 -r 9bb7f19cbb6f artifacts/src/main/java/org/dive4elements/river/artifacts/model/sq/StaticSQFactory.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/sq/StaticSQFactory.java	Mon Apr 20 10:21:45 2015 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/sq/StaticSQFactory.java	Mon Apr 20 10:23:40 2015 +0200
@@ -21,6 +21,7 @@
 
 import org.dive4elements.river.artifacts.cache.CacheFactory;
 import org.dive4elements.river.backend.SessionHolder;
+import org.dive4elements.river.model.MeasurementStation;
 
 
 public class StaticSQFactory
@@ -28,27 +29,6 @@
     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 " +
-            "CASE WHEN r.km_up = 1 AND ra.b IS NOT NULL " +
-                "THEN ra.b " +
-                "ELSE ra.a " +
-            "END = (SELECT " +
-                       "CASE WHEN r.km_up = 1 AND ra.b IS NOT NULL " +
-                           "THEN ra.b " +
-                           "ELSE ra.a " +
-                       "END " +
-                       "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.id = :ms_id)";
-
     public static final String SQL_SQ =
         "SELECT " +
             "sq.description AS description,"+
@@ -90,67 +70,37 @@
      * and a "Schwebstoffmesstelle" at the same place.*/
     public static StaticSQContainer getSQRelationsForLocation(
         String river,
-        int measurementStation
+        int mStationId
     ) {
         Session session = SessionHolder.HOLDER.get();
-        Query query = session.createSQLQuery(SQL_STATIONS_AT_RANGE)
-            .setParameter("river", river)
-            .setParameter("ms_id", measurementStation);
+        Query query = session.createQuery(
+            "from MeasurementStation " +
+            "where id=:ms_id")
+            .setParameter("ms_id", mStationId);
+        MeasurementStation mStation = (MeasurementStation)query.list().get(0);
+
         /* Take the first container for the station requested. */
-        StaticSQContainer retval = getSQRelations(river, measurementStation);
+        StaticSQContainer retval = getSQRelations(river, mStationId);
 
-        /* And some others */
-        List<Object> list = query.list();
-        if (list == null || list.isEmpty()) {
-            log.error("Did not even find one measurement station. Broken Query?");
-            log.debug("River: " + river);
-            log.debug("Mesurement station id: " + measurementStation);
-            return retval;
-        }
-
-        if (list.size() > 2) {
-            log.error("More then two measurement stations found at the same range. Bad Data!");
-            return retval;
-        }
-
-        for (Object stationIdO: list) {
-            Integer stationId;
-            if (stationIdO instanceof BigDecimal) {
-                stationId = ((BigDecimal)stationIdO).intValue();
-            } else {
-                /* If it is something else entirely we die here. */
-                stationId = (Integer) stationIdO;
-            }
-            log.debug("Collecting SQ Relations for: "+ stationId);
-            if (stationId == measurementStation) {
-                /* Skip the same station */
-                continue;
-            }
-
+        /* And it's companion station */
+        MeasurementStation companion = mStation.findCompanionStation();
+        if (companion != null) {
+            int stationId = companion.getId();
             StaticSQContainer additional = getSQRelations(river, stationId);
             if (additional == null || additional.getSQRelations() == null) {
-                continue;
+                /* New one is empty, just take the old one. */
+                return retval;
             }
-
-            if (retval == null || retval.getSQRelations() == null || retval.getSQRelations().isEmpty()) {
+            if (retval == null ||
+                retval.getSQRelations() == null ||
+                retval.getSQRelations().isEmpty()) {
                 /* Old one is empty, just take the new one. */
-                retval = additional;
-                continue;
+                return additional;
             }
-
-            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);
+            for (StaticSQRelation add: additional.getSQRelations()) {
+                /* Add SQ relations from new one to old one */
+                retval.addSQRelation(add);
             }
-
         }
         return retval;
     }
@@ -158,14 +108,14 @@
 
     public static StaticSQContainer getSQRelations(
         String river,
-        int measurementStation
+        int mStationId
     ) {
         Cache cache = CacheFactory.getCache(StaticSQCacheKey.CACHE_NAME);
 
         StaticSQCacheKey cacheKey;
 
         if (cache != null) {
-            cacheKey = new StaticSQCacheKey(river, measurementStation);
+            cacheKey = new StaticSQCacheKey(river, mStationId);
             Element element = cache.get(cacheKey);
             if (element != null) {
                 log.debug("Got static sq relations from cache");
@@ -176,7 +126,7 @@
             cacheKey = null;
         }
 
-        StaticSQContainer values = getUncached(river, measurementStation);
+        StaticSQContainer values = getUncached(river, mStationId);
 
         if (values != null && cacheKey != null) {
             log.debug("Store static sq relations in cache.");
@@ -231,7 +181,7 @@
 
     private static StaticSQContainer getUncached(
         String river,
-        int measurementStation
+        int mStationId
     ) {
         Session session = SessionHolder.HOLDER.get();
 
@@ -248,12 +198,12 @@
             .addScalar("qmax");
 
         query.setParameter("river", river);
-        query.setParameter("ms_id", measurementStation);
+        query.setParameter("ms_id", mStationId);
 
         List<Object []> list = query.list();
 
         if (list.isEmpty()) {
-            log.debug("Query returened empty");
+            log.debug("Query returned empty");
             return new StaticSQContainer();
         }
 


More information about the Dive4Elements-commits mailing list