[PATCH] Added method to importer session to fetch the measurement station for given (river, km)

Wald Commits scm-commit at wald.intevation.org
Thu Jul 10 18:02:14 CEST 2014


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1405008128 -7200
# Node ID cdb9f6d97f6aa3fca8d6745781362b8ea80cd47a
# Parent  aa054f72e88705d4a728724d29551e8ef72fe869
Added method to importer session to fetch the measurement station for given (river, km).

diff -r aa054f72e887 -r cdb9f6d97f6a backend/src/main/java/org/dive4elements/river/importer/ImportMeasurementStation.java
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportMeasurementStation.java	Thu Jul 10 16:57:32 2014 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/ImportMeasurementStation.java	Thu Jul 10 18:02:08 2014 +0200
@@ -40,6 +40,10 @@
     public ImportMeasurementStation() {
     }
 
+    public ImportMeasurementStation(MeasurementStation peer) {
+        this.peer = peer;
+    }
+
     private Gauge getGaugeFromDB() {
         Session session = ImporterSession.getInstance().getDatabaseSession();
 
@@ -53,9 +57,7 @@
     }
 
     public boolean storeDependencies(River river) {
-        getPeer(river);
-
-        return peer != null;
+        return getPeer(river) != null;
     }
 
     public MeasurementStation getPeer(River river) {
diff -r aa054f72e887 -r cdb9f6d97f6a backend/src/main/java/org/dive4elements/river/importer/ImporterSession.java
--- a/backend/src/main/java/org/dive4elements/river/importer/ImporterSession.java	Thu Jul 10 16:57:32 2014 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/ImporterSession.java	Thu Jul 10 18:02:08 2014 +0200
@@ -8,8 +8,10 @@
 
 package org.dive4elements.river.importer;
 
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 import java.util.TreeMap;
 import java.math.BigDecimal;
@@ -17,22 +19,20 @@
 import org.hibernate.SessionFactory;
 import org.hibernate.Session;
 import org.hibernate.Query;
-import org.hibernate.FlushMode;
 import org.dive4elements.river.backend.SessionFactoryProvider;
 import org.dive4elements.river.model.GrainFraction;
+import org.dive4elements.river.model.MeasurementStation;
 import org.dive4elements.river.model.WstColumnValue;
 import org.dive4elements.river.model.WstColumn;
 import org.dive4elements.river.model.DischargeTableValue;
 import org.dive4elements.river.model.DischargeTable;
 import org.dive4elements.river.model.Range;
 import org.dive4elements.river.model.River;
-import org.apache.log4j.Logger;
+import org.dive4elements.river.utils.EpsilonComparator;
 import org.dive4elements.artifacts.common.utils.LRUCache;
 
 public class ImporterSession
 {
-    private static Logger log = Logger.getLogger(ImporterSession.class);
-
     private static final ThreadLocal<ImporterSession> SESSION =
         new ThreadLocal<ImporterSession>() {
             @Override
@@ -43,17 +43,20 @@
 
     protected Session databaseSession;
 
-    protected LRUCache<Integer, Map<ValueKey, WstColumnValue>>
+    private LRUCache<Integer, Map<ValueKey, WstColumnValue>>
         wstColumnValues;
 
-    protected LRUCache<Integer, Map<ValueKey, DischargeTableValue>>
+    private LRUCache<Integer, Map<ValueKey, DischargeTableValue>>
         dischargeTableValues;
 
-    protected LRUCache<Integer, Map<ValueKey, Range>>
+    private LRUCache<Integer, Map<ValueKey, Range>>
         ranges;
 
     private Map<String, GrainFraction> grainFractions;
 
+    private Map<String, Map<Double, List<MeasurementStation>>>
+        riversToMeasurementStations;
+
     public static ImporterSession getInstance() {
         return SESSION.get();
     }
@@ -202,5 +205,34 @@
 
         return range;
     }
+
+    public List<MeasurementStation> getMeasurementStations(String river, double station) {
+        if (riversToMeasurementStations == null) {
+            riversToMeasurementStations =
+                new HashMap<String, Map<Double, List<MeasurementStation>>>();
+        }
+
+        Map<Double, List<MeasurementStation>> km2Stations =
+            riversToMeasurementStations.get(river);
+        if (km2Stations == null) {
+            km2Stations =
+                new TreeMap<Double, List<MeasurementStation>>(EpsilonComparator.CMP);
+            riversToMeasurementStations.put(river, km2Stations);
+            Query query = databaseSession.createQuery(
+                "from MeasurementStation where river.name = :name");
+            query.setParameter("name", river);
+            for (Iterator iter = query.iterate(); iter.hasNext();) {
+                MeasurementStation st = (MeasurementStation)iter.next();
+                List<MeasurementStation> ms = km2Stations.get(st.getStation());
+                if (ms == null) {
+                    ms = new ArrayList<MeasurementStation>(2);
+                    km2Stations.put(st.getStation(), ms);
+                }
+                ms.add(st);
+            }
+
+        }
+        return km2Stations.get(station);
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :


More information about the Dive4Elements-commits mailing list