changeset 2843:6a9f45697f81

Added importer classes used for MINFO specific waterlevel import. flys-backend/trunk@4282 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 20 Apr 2012 08:16:39 +0000
parents 0d55b7722fae
children c4db0f75a94a
files flys-backend/ChangeLog flys-backend/src/main/java/de/intevation/flys/importer/ImportWaterlevel.java flys-backend/src/main/java/de/intevation/flys/importer/ImportWaterlevelQRange.java flys-backend/src/main/java/de/intevation/flys/importer/ImportWaterlevelValue.java
diffstat 4 files changed, 228 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/ChangeLog	Fri Apr 20 07:40:48 2012 +0000
+++ b/flys-backend/ChangeLog	Fri Apr 20 08:16:39 2012 +0000
@@ -1,3 +1,10 @@
+2012-04-20  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/importer/ImportWaterlevelValue.java,
+	  src/main/java/de/intevation/flys/importer/ImportWaterlevelQRange.java,
+	  src/main/java/de/intevation/flys/importer/ImportWaterlevel.java: New
+	  importer classes used for MINFO specific waterlevel import.
+
 2012-04-20  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/model/WaterlevelQRange.java,
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportWaterlevel.java	Fri Apr 20 08:16:39 2012 +0000
@@ -0,0 +1,85 @@
+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.River;
+import de.intevation.flys.model.Unit;
+import de.intevation.flys.model.Waterlevel;
+
+
+public class ImportWaterlevel {
+
+    private static final Logger log = Logger.getLogger(ImportWaterlevel.class);
+
+
+    private ImportUnit unit;
+
+    private String description;
+
+    private List<ImportWaterlevelQRange> qRanges;
+
+    private Waterlevel peer;
+
+
+    public ImportWaterlevel(ImportUnit unit, String description) {
+        this.qRanges = new ArrayList<ImportWaterlevelQRange>();
+
+        this.unit        = unit;
+        this.description = description;
+    }
+
+
+    public void storeDependencies(River river) {
+        Waterlevel peer = getPeer(river);
+
+        int i = 0;
+
+        for (ImportWaterlevelQRange qRange: qRanges) {
+            qRange.storeDependencies(peer);
+            i++;
+        }
+
+        log.info("stored " + i + " waterlevel q ranges");
+    }
+
+
+    public Waterlevel getPeer(River river) {
+        Unit u = unit != null ? unit.getPeer() : null;
+        if (u == null) {
+            log.warn("skip invalid waterlevel - no unit set!");
+            return null;
+        }
+
+        if (peer == null) {
+            Session session = ImporterSession.getInstance().getDatabaseSession();
+            Query query = session.createQuery(
+                "from Waterlevel where " +
+                "   river=:river and " +
+                "   unit=:unit and " +
+                "   description=:description"
+            );
+
+            query.setParameter("river", river);
+            query.setParameter("unit", u);
+            query.setParameter("description", description);
+
+            List<Waterlevel> wsts = query.list();
+            if (wsts.isEmpty()) {
+                peer = new Waterlevel(river, u, description);
+                session.save(peer);
+            }
+            else {
+                peer = wsts.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/ImportWaterlevelQRange.java	Fri Apr 20 08:16:39 2012 +0000
@@ -0,0 +1,77 @@
+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.Waterlevel;
+import de.intevation.flys.model.WaterlevelQRange;
+
+
+public class ImportWaterlevelQRange {
+
+    private static final Logger log =
+        Logger.getLogger(ImportWaterlevelQRange.class);
+
+
+    private Double q;
+
+    private List<ImportWaterlevelValue> values;
+
+    private WaterlevelQRange peer;
+
+
+    public ImportWaterlevelQRange(Double q) {
+        this.values = new ArrayList<ImportWaterlevelValue>();
+        this.q      = q;
+    }
+
+    public void addValue(ImportWaterlevelValue value) {
+        this.values.add(value);
+    }
+
+
+    public void storeDependencies(Waterlevel waterlevel) {
+        WaterlevelQRange peer = getPeer(waterlevel);
+
+        int i = 0;
+
+        for (ImportWaterlevelValue value: values) {
+            value.storeDependencies(peer);
+            i++;
+        }
+
+        log.info("stored " + i + " waterlevel values");
+    }
+
+
+    public WaterlevelQRange getPeer(Waterlevel waterlevel) {
+        if (peer == null) {
+            Session session = ImporterSession.getInstance().getDatabaseSession();
+            Query query = session.createQuery(
+                "from WaterlevelQRange where " +
+                "   waterlevel=:waterlevel and " +
+                "   q=:q"
+            );
+
+            query.setParameter("waterlevel", waterlevel);
+            query.setParameter("q", q);
+
+            List<WaterlevelQRange> qRanges = query.list();
+            if (qRanges.isEmpty()) {
+                peer = new WaterlevelQRange(waterlevel, q);
+                session.save(peer);
+            }
+            else {
+                peer = qRanges.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/ImportWaterlevelValue.java	Fri Apr 20 08:16:39 2012 +0000
@@ -0,0 +1,59 @@
+package de.intevation.flys.importer;
+
+import java.util.List;
+
+import org.hibernate.Session;
+import org.hibernate.Query;
+
+import de.intevation.flys.model.WaterlevelQRange;
+import de.intevation.flys.model.WaterlevelValue;
+
+
+public class ImportWaterlevelValue {
+
+    private Double station;
+    private Double w;
+
+    private WaterlevelValue peer;
+
+
+    public ImportWaterlevelValue(Double station, Double w) {
+        this.station = station;
+        this.w       = w;
+    }
+
+
+    public void storeDependencies(WaterlevelQRange qRange) {
+        getPeer(qRange);
+    }
+
+
+    public WaterlevelValue getPeer(WaterlevelQRange qRange) {
+        if (peer == null) {
+            Session session = ImporterSession.getInstance().getDatabaseSession();
+            Query query = session.createQuery(
+                "from WaterlevelValue where " +
+                "   qRange=:qRange and " +
+                "   station=:station and " +
+                "   w=:w"
+            );
+
+            query.setParameter("qRange", qRange);
+            query.setParameter("station", station);
+            query.setParameter("w", w);
+
+            List<WaterlevelValue> values = query.list();
+
+            if (values.isEmpty()) {
+                peer = new WaterlevelValue(qRange, station, w);
+                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