changeset 8027:1e327d86c898

Load grain fractions from database only. Do not store new ones.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 10 Jul 2014 16:11:55 +0200
parents 4b3054edbbaf
children aa054f72e887
files backend/src/main/java/org/dive4elements/river/importer/ImportGrainFraction.java backend/src/main/java/org/dive4elements/river/importer/ImporterSession.java
diffstat 2 files changed, 18 insertions(+), 49 deletions(-) [+]
line wrap: on
line diff
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportGrainFraction.java	Thu Jul 10 12:04:56 2014 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/ImportGrainFraction.java	Thu Jul 10 16:11:55 2014 +0200
@@ -8,29 +8,16 @@
 
 package org.dive4elements.river.importer;
 
-import java.util.List;
-
-import org.apache.log4j.Logger;
-
-import org.hibernate.Session;
-import org.hibernate.Query;
-
 import org.dive4elements.river.model.GrainFraction;
 
 
 public class ImportGrainFraction {
 
-    private static final Logger log =
-        Logger.getLogger(ImportGrainFraction.class);
-
     private String name;
 
     private Double lower;
     private Double upper;
 
-    private GrainFraction peer;
-
-
     public ImportGrainFraction(String name) {
         this.name = name;
     }
@@ -48,41 +35,12 @@
 
 
     public void storeDependencies() {
-        log.debug("store dependencies");
-
-        getPeer();
+        // Nothing to store because its prefilled in schema.
     }
 
 
     public GrainFraction getPeer() {
-        log.debug("get peer");
-
-        if (peer == null) {
-            Session session = ImporterSession.getInstance().getDatabaseSession();
-            Query query = session.createQuery(
-                "from GrainFraction where " +
-                "   name=:name and " +
-                "   lower=:lower and " +
-                "   upper=:upper"
-            );
-
-            query.setParameter("name", name);
-            query.setParameter("lower", lower);
-            query.setParameter("upper", upper);
-
-            List<GrainFraction> fractions = query.list();
-            if (fractions.isEmpty()) {
-                log.info("create new GrainFraction");
-
-                peer = new GrainFraction(name, lower, upper);
-                session.save(peer);
-            }
-            else {
-                peer = fractions.get(0);
-            }
-        }
-
-        return peer;
+        return ImporterSession.getInstance().getGrainFraction(name);
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/backend/src/main/java/org/dive4elements/river/importer/ImporterSession.java	Thu Jul 10 12:04:56 2014 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/ImporterSession.java	Thu Jul 10 16:11:55 2014 +0200
@@ -8,28 +8,25 @@
 
 package org.dive4elements.river.importer;
 
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.TreeMap;
-
 import java.math.BigDecimal;
 
 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.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.artifacts.common.utils.LRUCache;
 
 public class ImporterSession
@@ -55,6 +52,8 @@
     protected LRUCache<Integer, Map<ValueKey, Range>>
         ranges;
 
+    private Map<String, GrainFraction> grainFractions;
+
     public static ImporterSession getInstance() {
         return SESSION.get();
     }
@@ -157,6 +156,18 @@
         return dctv;
     }
 
+    public GrainFraction getGrainFraction(String name) {
+        if (grainFractions == null) {
+            grainFractions = new HashMap<String, GrainFraction>();
+            Query query = databaseSession.createQuery("from GrainFraction");
+            for (Iterator iter = query.iterate(); iter.hasNext();) {
+                GrainFraction gf = (GrainFraction)iter.next();
+                grainFractions.put(gf.getName(), gf);
+            }
+        }
+        return grainFractions.get(name);
+    }
+
     public Range getRange(River river, BigDecimal a, BigDecimal b) {
         Integer r = river.getId();
 

http://dive4elements.wald.intevation.org