changeset 8694:9bb7f19cbb6f

(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).
author Tom Gottfried <tom@intevation.de>
date Mon, 20 Apr 2015 10:23:40 +0200
parents 851ea37d35f3
children 5abbccb13e45
files artifacts/src/main/java/org/dive4elements/river/artifacts/model/sq/StaticSQFactory.java
diffstat 1 files changed, 30 insertions(+), 80 deletions(-) [+]
line wrap: on
line diff
--- 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);
-        /* Take the first container for the station requested. */
-        StaticSQContainer retval = getSQRelations(river, measurementStation);
-
-        /* 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;
-        }
+        Query query = session.createQuery(
+            "from MeasurementStation " +
+            "where id=:ms_id")
+            .setParameter("ms_id", mStationId);
+        MeasurementStation mStation = (MeasurementStation)query.list().get(0);
 
-        if (list.size() > 2) {
-            log.error("More then two measurement stations found at the same range. Bad Data!");
-            return retval;
-        }
+        /* Take the first container for the station requested. */
+        StaticSQContainer retval = getSQRelations(river, mStationId);
 
-        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;
-            }
-
-            if (retval == null || retval.getSQRelations() == null || retval.getSQRelations().isEmpty()) {
-                /* Old one is empty, just take the new one. */
-                retval = additional;
-                continue;
+                /* New one is empty, just take the old one. */
+                return retval;
             }
-
-            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);
+            if (retval == null ||
+                retval.getSQRelations() == null ||
+                retval.getSQRelations().isEmpty()) {
+                /* Old one is empty, just take the new one. */
+                return additional;
             }
-
+            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();
         }
 

http://dive4elements.wald.intevation.org