changeset 8029:cdb9f6d97f6a

Added method to importer session to fetch the measurement station for given (river, km).
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 10 Jul 2014 18:02:08 +0200
parents aa054f72e887
children fac1aa67931b
files backend/src/main/java/org/dive4elements/river/importer/ImportMeasurementStation.java backend/src/main/java/org/dive4elements/river/importer/ImporterSession.java
diffstat 2 files changed, 44 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- 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) {
--- 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 :

http://dive4elements.wald.intevation.org