changeset 8028:aa054f72e887

Added importer models for sediment load and sediment load values.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 10 Jul 2014 16:57:32 +0200
parents 1e327d86c898
children cdb9f6d97f6a
files backend/src/main/java/org/dive4elements/river/importer/ImportMeasurementStation.java backend/src/main/java/org/dive4elements/river/importer/ImportSedimentLoad.java backend/src/main/java/org/dive4elements/river/importer/ImportSedimentLoadValue.java backend/src/main/java/org/dive4elements/river/model/SedimentLoad.java backend/src/main/java/org/dive4elements/river/model/SedimentLoadValue.java
diffstat 5 files changed, 164 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportMeasurementStation.java	Thu Jul 10 16:11:55 2014 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/ImportMeasurementStation.java	Thu Jul 10 16:57:32 2014 +0200
@@ -58,7 +58,7 @@
         return peer != null;
     }
 
-    public Object getPeer(River river) {
+    public MeasurementStation getPeer(River river) {
         if (peer == null) {
             Gauge gauge = null;
             try {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/backend/src/main/java/org/dive4elements/river/importer/ImportSedimentLoad.java	Thu Jul 10 16:57:32 2014 +0200
@@ -0,0 +1,70 @@
+package org.dive4elements.river.importer;
+
+import java.util.List;
+
+import org.dive4elements.river.model.GrainFraction;
+import org.dive4elements.river.model.SedimentLoad;
+import org.dive4elements.river.model.TimeInterval;
+import org.hibernate.Query;
+import org.hibernate.Session;
+
+public class ImportSedimentLoad
+{
+    private SedimentLoad peer;
+
+    private ImportGrainFraction grainFraction;
+    private ImportTimeInterval  timeInterval;
+    private ImportTimeInterval  sqTimeInterval;
+    private String              description;
+    private Integer             kind;
+
+    public ImportSedimentLoad() {
+    }
+
+    public void storeDependencies() {
+        grainFraction.getPeer();
+        timeInterval.getPeer();
+        sqTimeInterval.getPeer();
+
+        getPeer();
+    }
+
+    public SedimentLoad getPeer() {
+
+        if (peer == null) {
+            Session session = ImporterSession.getInstance().getDatabaseSession();
+            Query query = session.createQuery(
+                "from SedimentLoad where " +
+                "   grainFraction = :grainFraction and " +
+                "   timeInterval = :timeInterval and " +
+                "   sqTimeInterval = :sqTimeInterval and " +
+                "   description = :description and " +
+                "   kind = :kind");
+
+            GrainFraction gf = grainFraction.getPeer();
+            TimeInterval  ti = timeInterval.getPeer();
+
+            TimeInterval sqti = sqTimeInterval != null
+                ? sqTimeInterval.getPeer()
+                : null;
+
+            query.setParameter("grainFraction", gf);
+            query.setParameter("timeInterval", ti);
+            query.setParameter("sqTimeInterval", sqti);
+            query.setParameter("description", description);
+            query.setParameter("kind", kind);
+
+            List<SedimentLoad> loads = query.list();
+            if (loads.isEmpty()) {
+                peer = new SedimentLoad(gf, ti, sqti, description, kind);
+                session.save(peer);
+            }
+            else {
+                peer = loads.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/backend/src/main/java/org/dive4elements/river/importer/ImportSedimentLoadValue.java	Thu Jul 10 16:57:32 2014 +0200
@@ -0,0 +1,69 @@
+package org.dive4elements.river.importer;
+
+import java.util.List;
+
+import org.dive4elements.river.model.MeasurementStation;
+import org.dive4elements.river.model.River;
+import org.dive4elements.river.model.SedimentLoad;
+import org.dive4elements.river.model.SedimentLoadValue;
+import org.hibernate.Query;
+import org.hibernate.Session;
+
+public class ImportSedimentLoadValue {
+
+    private SedimentLoadValue peer;
+
+    private ImportMeasurementStation station;
+    private ImportSedimentLoad       sedimentLoad;
+    private Double                   value;
+
+    public ImportSedimentLoadValue() {
+    }
+
+    public ImportSedimentLoadValue(
+        ImportMeasurementStation station,
+        ImportSedimentLoad       sedimentLoad,
+        Double                   value
+    ) {
+        this.station      = station;
+        this.sedimentLoad = sedimentLoad;
+        this.value        = value;
+    }
+
+    protected SedimentLoadValue getPeer(River river) {
+
+        if (peer == null) {
+            Session session = ImporterSession.getInstance().getDatabaseSession();
+            Query query = session.createQuery(
+                "from SedimentLoadValue where " +
+                "   station = :station and " +
+                "   sedimentLoad = :sedimentLoad and " +
+                "   value = :value");
+
+            MeasurementStation ms = station.getPeer(river);
+            SedimentLoad sl = sedimentLoad.getPeer();
+
+            query.setParameter("station", ms);
+            query.setParameter("sedimentLoad", sl);
+            query.setParameter("value", value);
+
+            List<SedimentLoadValue> values = query.list();
+            if (values.isEmpty()) {
+                peer = new SedimentLoadValue(sl, ms, value);
+                session.save(peer);
+            }
+            else {
+                peer = values.get(0);
+            }
+        }
+
+        return peer;
+    }
+
+    public void storeDependencies(River river) {
+        station.storeDependencies(river);
+        sedimentLoad.storeDependencies();
+        getPeer(river);
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/backend/src/main/java/org/dive4elements/river/model/SedimentLoad.java	Thu Jul 10 16:11:55 2014 +0200
+++ b/backend/src/main/java/org/dive4elements/river/model/SedimentLoad.java	Thu Jul 10 16:57:32 2014 +0200
@@ -43,6 +43,20 @@
     public SedimentLoad() {
     }
 
+    public SedimentLoad(
+        GrainFraction grainFraction,
+        TimeInterval  timeInterval,
+        TimeInterval  sqTimeInterval,
+        String        description,
+        Integer       kind
+    ) {
+        this.grainFraction  = grainFraction;
+        this.timeInterval   = timeInterval;
+        this.sqTimeInterval = sqTimeInterval;
+        this.description    = description;
+        this.kind           = kind;
+    }
+
     @Id
     @SequenceGenerator(
         name           = "SEQUENCE_SEDIMENT_LOAD_ID_SEQ",
--- a/backend/src/main/java/org/dive4elements/river/model/SedimentLoadValue.java	Thu Jul 10 16:11:55 2014 +0200
+++ b/backend/src/main/java/org/dive4elements/river/model/SedimentLoadValue.java	Thu Jul 10 16:57:32 2014 +0200
@@ -35,6 +35,16 @@
     public SedimentLoadValue() {
     }
 
+    public SedimentLoadValue(
+        SedimentLoad       sedimentLoad,
+        MeasurementStation measurementStation,
+        Double             value
+    ) {
+        this.sedimentLoad       = sedimentLoad;
+        this.measurementStation = measurementStation;
+        this.value              = value;
+    }
+
     @Id
     @SequenceGenerator(
         name           = "SEQUENCE_SEDIMENT_LOAD_VALUES_ID_SEQ",

http://dive4elements.wald.intevation.org