[PATCH 2 of 2] Load grain fractions from database only. Do not store new ones

Wald Commits scm-commit at wald.intevation.org
Thu Jul 10 16:12:05 CEST 2014


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1405001515 -7200
# Node ID 1e327d86c898db96f1ce1063e4e585357639a4ac
# Parent  4b3054edbbaf6cc5d3cb50282a3a19a2d76b6351
Load grain fractions from database only. Do not store new ones.

diff -r 4b3054edbbaf -r 1e327d86c898 backend/src/main/java/org/dive4elements/river/importer/ImportGrainFraction.java
--- 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 :
diff -r 4b3054edbbaf -r 1e327d86c898 backend/src/main/java/org/dive4elements/river/importer/ImporterSession.java
--- 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();
 


More information about the Dive4Elements-commits mailing list