changeset 2838:75446c47ef77

Added new importer classes for importing sediment yield data. flys-backend/trunk@4271 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 19 Apr 2012 07:15:25 +0000
parents 8b910e3277b0
children 163c037f2c7e
files flys-backend/ChangeLog flys-backend/src/main/java/de/intevation/flys/importer/ImportGrainFraction.java flys-backend/src/main/java/de/intevation/flys/importer/ImportSedimentYield.java flys-backend/src/main/java/de/intevation/flys/importer/ImportSedimentYieldValue.java
diffstat 4 files changed, 273 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/ChangeLog	Wed Apr 18 12:54:51 2012 +0000
+++ b/flys-backend/ChangeLog	Thu Apr 19 07:15:25 2012 +0000
@@ -1,3 +1,10 @@
+2012-04-19  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/importer/ImportGrainFraction.java,
+	  src/main/java/de/intevation/flys/importer/ImportSedimentYieldValue.java,
+	  src/main/java/de/intevation/flys/importer/ImportSedimentYield.java: New
+	  importer classes for importing sediment yield data.
+
 2012-04-18  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/model/SedimentYield.java,
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportGrainFraction.java	Thu Apr 19 07:15:25 2012 +0000
@@ -0,0 +1,84 @@
+package de.intevation.flys.importer;
+
+import java.util.List;
+
+import org.apache.log4j.Logger;
+
+import org.hibernate.Session;
+import org.hibernate.Query;
+
+import de.intevation.flys.model.GrainFraction;
+import de.intevation.flys.model.Unit;
+
+
+public class ImportGrainFraction {
+
+    private static final Logger log =
+        Logger.getLogger(ImportGrainFraction.class);
+
+    private String name;
+
+    private Double lower;
+    private Double upper;
+
+    private ImportUnit unit;
+
+    private GrainFraction peer;
+
+
+    public ImportGrainFraction(
+        String     name,
+        Double     lower,
+        Double     upper,
+        ImportUnit unit
+    ) {
+        this.name  = name;
+        this.lower = lower;
+        this.upper = upper;
+        this.unit  = unit;
+    }
+
+
+    public void storeDependencies() {
+        log.debug("store dependencies");
+
+        getPeer();
+    }
+
+
+    public GrainFraction getPeer() {
+        log.debug("get peer");
+
+        Unit u = unit != null ? unit.getPeer() : null;
+
+        if (peer == null) {
+            Session session = ImporterSession.getInstance().getDatabaseSession();
+            Query query = session.createQuery(
+                "from GrainFraction where " +
+                "   name=:name and " +
+                "   lower=:lower and " +
+                "   upper=:upper and " +
+                "   unit=:unit"
+            );
+
+            query.setParameter("name", name);
+            query.setParameter("lower", lower);
+            query.setParameter("upper", upper);
+            query.setParameter("unit", u);
+
+            List<GrainFraction> fractions = query.list();
+            if (fractions.isEmpty()) {
+                log.info("create new GrainFraction");
+
+                peer = new GrainFraction(name, lower, upper, u);
+                session.save(peer);
+            }
+            else {
+                peer = fractions.get(0);
+            }
+        }
+
+        return peer;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportSedimentYield.java	Thu Apr 19 07:15:25 2012 +0000
@@ -0,0 +1,124 @@
+package de.intevation.flys.importer;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+
+import org.hibernate.Session;
+import org.hibernate.Query;
+
+import de.intevation.flys.model.GrainFraction;
+import de.intevation.flys.model.River;
+import de.intevation.flys.model.SedimentYield;
+import de.intevation.flys.model.TimeInterval;
+import de.intevation.flys.model.Unit;
+
+
+public class ImportSedimentYield {
+
+    private static Logger log = Logger.getLogger(ImportSedimentYield.class);
+
+    private ImportGrainFraction grainFraction;
+
+    private ImportUnit unit;
+
+    private ImportTimeInterval timeInterval;
+
+    private List<ImportSedimentYieldValue> values;
+
+    private SedimentYield peer;
+
+
+    public ImportSedimentYield() {
+        this.values = new ArrayList<ImportSedimentYieldValue>();
+    }
+
+
+    public void setTimeInterval(ImportTimeInterval timeInterval) {
+        this.timeInterval = timeInterval;
+    }
+
+
+    public void setUnit(ImportUnit unit) {
+        this.unit = unit;
+    }
+
+
+    public void setGrainFraction(ImportGrainFraction grainFraction) {
+        this.grainFraction = grainFraction;
+    }
+
+
+    public void addValue(ImportSedimentYieldValue value) {
+        this.values.add(value);
+    }
+
+
+    public void storeDependencies(River river) {
+        log.debug("store dependencies");
+
+        if (grainFraction != null) {
+            grainFraction.storeDependencies();
+        }
+
+        SedimentYield peer = getPeer(river);
+
+        int i = 0;
+
+        for (ImportSedimentYieldValue value: values) {
+            value.storeDependencies(peer);
+            i++;
+        }
+
+        log.info("stored " + i + " sediment yield values.");
+    }
+
+
+    public SedimentYield getPeer(River river) {
+        log.debug("get peer");
+
+        GrainFraction gf = grainFraction != null
+            ? grainFraction.getPeer()
+            : null;
+
+        Unit u = unit != null ? unit.getPeer() : null;
+
+        TimeInterval ti = timeInterval != null ? timeInterval.getPeer() : null;
+
+        if (ti == null || u == null) {
+            log.warn("Skip invalid SedimentYield: time interval or unit null!");
+            return null;
+        }
+
+        if (peer == null) {
+            Session session = ImporterSession.getInstance().getDatabaseSession();
+            Query query = session.createQuery(
+                "from SedimentYield where " +
+                "   river=:river and " +
+                "   grainFraction=:grainFraction and " +
+                "   unit=:unit and " +
+                "   timeInterval=:timeInterval"
+            );
+
+            query.setParameter("river", river);
+            query.setParameter("grainFraction", gf);
+            query.setParameter("unit", u);
+            query.setParameter("timeInterval", ti);
+
+            List<SedimentYield> yields = query.list();
+            if (yields.isEmpty()) {
+                log.debug("create new SedimentYield");
+
+                peer = new SedimentYield(river, u, ti, gf);
+                session.save(peer);
+            }
+            else {
+                peer = yields.get(0);
+            }
+        }
+
+        return peer;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportSedimentYieldValue.java	Thu Apr 19 07:15:25 2012 +0000
@@ -0,0 +1,58 @@
+package de.intevation.flys.importer;
+
+import java.util.List;
+
+import org.hibernate.Session;
+import org.hibernate.Query;
+
+import de.intevation.flys.model.SedimentYield;
+import de.intevation.flys.model.SedimentYieldValue;
+
+
+public class ImportSedimentYieldValue {
+
+    private Double station;
+    private Double value;
+
+    private SedimentYieldValue peer;
+
+
+    public ImportSedimentYieldValue(Double station, Double value) {
+        this.station = station;
+        this.value   = value;
+    }
+
+
+    public void storeDependencies(SedimentYield sedimentYield) {
+        getPeer(sedimentYield);
+    }
+
+
+    public SedimentYieldValue getPeer(SedimentYield sedimentYield) {
+        if (peer == null) {
+            Session session = ImporterSession.getInstance().getDatabaseSession();
+            Query query = session.createQuery(
+                "from SedimentYieldValue where " +
+                "   sedimentYield=:sedimentYield and " +
+                "   station=:station and " +
+                "   value=:value"
+            );
+
+            query.setParameter("sedimentYield", sedimentYield);
+            query.setParameter("station", station);
+            query.setParameter("value", value);
+
+            List<SedimentYieldValue> values = query.list();
+            if (values.isEmpty()) {
+                peer = new SedimentYieldValue(sedimentYield, station, value);
+                session.save(peer);
+            }
+            else {
+                peer = values.get(0);
+            }
+        }
+
+        return peer;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org