diff backend/src/main/java/org/dive4elements/river/importer/common/AbstractKmLineImport.java @ 8971:50416a0df385

Importer for the Schifffahrt (S-INFO) and Oekologie (U-INFO) files
author mschaefer
date Tue, 03 Apr 2018 10:18:30 +0200
parents
children ae76f618d990
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/backend/src/main/java/org/dive4elements/river/importer/common/AbstractKmLineImport.java	Tue Apr 03 10:18:30 2018 +0200
@@ -0,0 +1,86 @@
+/* Copyright (C) 2017 by Bundesanstalt für Gewässerkunde
+ * Software engineering by
+ *  Björnsen Beratende Ingenieure GmbH
+ *  Dr. Schumacher Ingenieurbüro für Wasser und Umwelt
+ *
+ * This file is Free Software under the GNU AGPL (>=v3)
+ * and comes with ABSOLUTELY NO WARRANTY! Check out the
+ * documentation coming with Dive4Elements River for details.
+ */
+
+package org.dive4elements.river.importer.common;
+
+import org.dive4elements.river.importer.ImporterSession;
+import org.hibernate.Session;
+
+/**
+ * Abstract base class of a river station with associated data importing from a file
+ *
+ * @author Matthias Schäfer
+ *
+ */
+public abstract class AbstractKmLineImport<SERIES, KMTUPLE> {
+
+    /***** FIELDS *****/
+
+    protected double station;
+
+    private KMTUPLE peer;
+
+    protected StoreMode storeMode;
+
+    /***** CONSTRUCTOR *****/
+
+    public AbstractKmLineImport(final double km) {
+        this.station = km;
+    }
+
+
+    /***** METHODS *****/
+
+    /**
+     * Stores the station value record in the database
+     */
+    public StoreMode store(final SERIES parent, final StoreMode parentStoreMode) {
+        getPeer(parent, parentStoreMode);
+        return this.storeMode;
+    }
+
+
+    /**
+     * Gets the station value record from the database if existing, or creates a database record from this object and adds
+     * it
+     */
+    protected KMTUPLE getPeer(final SERIES parent, final StoreMode parentStoreMode) {
+        if (this.peer != null) {
+            this.storeMode = StoreMode.NONE;
+            return this.peer;
+        }
+        final Session session = ImporterSession.getInstance().getDatabaseSession();
+        KMTUPLE value = null;
+        if (parentStoreMode != StoreMode.INSERT) {
+            value = queryValueItem(session, parent);
+        }
+        if (value == null) {
+            this.peer = createValueItem(parent);
+            session.save(this.peer);
+            this.storeMode = StoreMode.INSERT;
+        } else {
+            this.peer = value;
+            this.storeMode = StoreMode.UPDATE;
+        }
+        return this.peer;
+    }
+
+    /**
+     * Queries the (first matching) value item from the database
+     *
+     * @return first matching database value item, or null
+     */
+    protected abstract KMTUPLE queryValueItem(final Session session, final SERIES parent);
+
+    /**
+     * Creates a new value item
+     */
+    protected abstract KMTUPLE createValueItem(final SERIES parent);
+}

http://dive4elements.wald.intevation.org