changeset 2850:a1402c16152f

Added a parser and importer classes used to import waterlevel differences. flys-backend/trunk@4304 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 26 Apr 2012 09:46:32 +0000
parents c36106f15ac9
children acb8d08f59a2
files flys-backend/ChangeLog flys-backend/src/main/java/de/intevation/flys/importer/ImportWaterlevelDifference.java flys-backend/src/main/java/de/intevation/flys/importer/ImportWaterlevelDifferenceColumn.java flys-backend/src/main/java/de/intevation/flys/importer/ImportWaterlevelDifferenceValue.java flys-backend/src/main/java/de/intevation/flys/importer/parsers/WaterlevelDifferencesParser.java
diffstat 5 files changed, 433 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/ChangeLog	Thu Apr 26 09:44:39 2012 +0000
+++ b/flys-backend/ChangeLog	Thu Apr 26 09:46:32 2012 +0000
@@ -1,3 +1,14 @@
+2012-04-26  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/importer/parsers/WaterlevelDifferencesParser.java:
+	  New parser for MINFO specific waterlevel differences.
+
+	* src/main/java/de/intevation/flys/importer/ImportWaterlevelDifferenceValue.java,
+	  src/main/java/de/intevation/flys/importer/ImportWaterlevelDifference.java,
+	  src/main/java/de/intevation/flys/importer/ImportWaterlevelDifferenceColumn.java:
+	  New importer classes used during the import process of MINFO specific
+	  waterlevel differences.
+
 2012-04-26  Ingo Weinzierl <ingo@intevation.de>
 
 	* doc/schema/oracle-minfo.sql,
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportWaterlevelDifference.java	Thu Apr 26 09:46:32 2012 +0000
@@ -0,0 +1,101 @@
+package de.intevation.flys.importer;
+
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+
+import org.hibernate.Session;
+import org.hibernate.Query;
+import org.hibernate.exception.ConstraintViolationException;
+
+import de.intevation.flys.model.River;
+import de.intevation.flys.model.Unit;
+import de.intevation.flys.model.WaterlevelDifference;
+
+
+public class ImportWaterlevelDifference {
+
+    private static final Logger log =
+        Logger.getLogger(ImportWaterlevelDifference.class);
+
+
+    private ImportUnit unit;
+
+    private String description;
+
+    private List<ImportWaterlevelDifferenceColumn> columns;
+
+    private WaterlevelDifference peer;
+
+
+    public ImportWaterlevelDifference(String description) {
+        this.columns = new ArrayList<ImportWaterlevelDifferenceColumn>();
+
+        this.description = description;
+    }
+
+
+    public void setUnit(ImportUnit unit) {
+        this.unit = unit;
+    }
+
+
+    public void addValue(ImportWaterlevelDifferenceColumn column) {
+        this.columns.add(column);
+    }
+
+
+    public void storeDependencies(River river)
+    throws SQLException, ConstraintViolationException
+    {
+        log.info("store dependencies");
+
+        WaterlevelDifference peer = getPeer(river);
+
+        int i = 0;
+
+        for (ImportWaterlevelDifferenceColumn column: columns) {
+            column.storeDependencies(peer);
+            i++;
+        }
+
+        log.info("stored " + i + " waterlevel difference columns");
+    }
+
+
+    public WaterlevelDifference getPeer(River river) {
+        Unit u = unit != null ? unit.getPeer() : null;
+        if (u == null) {
+            log.warn("skip invalid waterlevel difference - no unit set!");
+            return null;
+        }
+
+        if (peer == null) {
+            Session session = ImporterSession.getInstance().getDatabaseSession();
+            Query query = session.createQuery(
+                "from WaterlevelDifference where " +
+                "   river=:river and " +
+                "   unit=:unit and " +
+                "   description=:description"
+            );
+
+            query.setParameter("river", river);
+            query.setParameter("unit", u);
+            query.setParameter("description", description);
+
+            List<WaterlevelDifference> diffs = query.list();
+            if (diffs.isEmpty()) {
+                peer = new WaterlevelDifference(river, u, description);
+                session.save(peer);
+            }
+            else {
+                peer = diffs.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/ImportWaterlevelDifferenceColumn.java	Thu Apr 26 09:46:32 2012 +0000
@@ -0,0 +1,82 @@
+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.WaterlevelDifference;
+import de.intevation.flys.model.WaterlevelDifferenceColumn;
+
+
+public class ImportWaterlevelDifferenceColumn {
+
+    private static final Logger log =
+        Logger.getLogger(ImportWaterlevelDifferenceColumn.class);
+
+
+    private String description;
+
+    private List<ImportWaterlevelDifferenceValue> values;
+
+    private WaterlevelDifferenceColumn peer;
+
+
+    public ImportWaterlevelDifferenceColumn(String description) {
+        this.values = new ArrayList<ImportWaterlevelDifferenceValue>();
+
+        this.description = description;
+    }
+
+
+    public void addValue(ImportWaterlevelDifferenceValue value) {
+        this.values.add(value);
+    }
+
+
+    public void storeDependencies(WaterlevelDifference difference) {
+        log.info("store dependencies");
+
+        WaterlevelDifferenceColumn peer = getPeer(difference);
+
+        int i = 0;
+
+        for (ImportWaterlevelDifferenceValue value: values) {
+            value.storeDependencies(peer);
+            i++;
+        }
+
+        log.info("stored " + i + " waterlevel difference values");
+    }
+
+
+    public WaterlevelDifferenceColumn getPeer(WaterlevelDifference diff) {
+        if (peer == null) {
+            Session session = ImporterSession.getInstance().getDatabaseSession();
+            Query query = session.createQuery(
+                "from WaterlevelDifferenceColumn where " +
+                "   difference=:difference and " +
+                "   description=:description"
+            );
+
+            query.setParameter("difference", diff);
+            query.setParameter("description", description);
+
+            List<WaterlevelDifferenceColumn> cols = query.list();
+            if (cols.isEmpty()) {
+                peer = new WaterlevelDifferenceColumn(diff, description);
+                session.save(peer);
+            }
+            else {
+                peer = cols.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/ImportWaterlevelDifferenceValue.java	Thu Apr 26 09:46:32 2012 +0000
@@ -0,0 +1,65 @@
+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.WaterlevelDifferenceColumn;
+import de.intevation.flys.model.WaterlevelDifferenceValue;
+
+
+public class ImportWaterlevelDifferenceValue {
+
+    private static final Logger log =
+        Logger.getLogger(ImportWaterlevelDifferenceValue.class);
+
+
+    private Double station;
+    private Double value;
+
+    private WaterlevelDifferenceValue peer;
+
+
+    public ImportWaterlevelDifferenceValue(Double station, Double value) {
+        this.station = station;
+        this.value   = value;
+    }
+
+
+    public void storeDependencies(WaterlevelDifferenceColumn column) {
+        getPeer(column);
+    }
+
+
+    public WaterlevelDifferenceValue getPeer(WaterlevelDifferenceColumn column) {
+        if (peer == null) {
+            Session session = ImporterSession.getInstance().getDatabaseSession();
+            Query query = session.createQuery(
+                "from WaterlevelDifferenceValue where " +
+                "   column=:column and " +
+                "   station=:station and " +
+                "   value=:value"
+            );
+
+            query.setParameter("column", column);
+            query.setParameter("station", station);
+            query.setParameter("value", value);
+
+            List<WaterlevelDifferenceValue> values = query.list();
+            if (values.isEmpty()) {
+                peer = new WaterlevelDifferenceValue(column, 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 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/parsers/WaterlevelDifferencesParser.java	Thu Apr 26 09:46:32 2012 +0000
@@ -0,0 +1,174 @@
+package de.intevation.flys.importer.parsers;
+
+import java.io.File;
+import java.io.IOException;
+import java.text.NumberFormat;
+import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.log4j.Logger;
+
+import de.intevation.flys.importer.ImportUnit;
+import de.intevation.flys.importer.ImportWaterlevelDifference;
+import de.intevation.flys.importer.ImportWaterlevelDifferenceColumn;
+import de.intevation.flys.importer.ImportWaterlevelDifferenceValue;
+
+
+public class WaterlevelDifferencesParser extends LineParser {
+
+    private static final Logger log =
+        Logger.getLogger(WaterlevelDifferencesParser.class);
+
+    private static final NumberFormat nf =
+        NumberFormat.getInstance(DEFAULT_LOCALE);
+
+    public static final Pattern META_UNIT =
+        Pattern.compile("^Einheit: \\[(.*)\\].*");
+
+
+    private List<ImportWaterlevelDifference> differences;
+
+    private ImportWaterlevelDifferenceColumn[] columns;
+
+    private ImportWaterlevelDifference current;
+
+
+    public WaterlevelDifferencesParser() {
+        differences = new ArrayList<ImportWaterlevelDifference>();
+    }
+
+
+    public List<ImportWaterlevelDifference> getDifferences() {
+        return differences;
+    }
+
+
+    @Override
+    public void parse(File file) throws IOException {
+        current = new ImportWaterlevelDifference(file.getName());
+
+        super.parse(file);
+    }
+
+
+    @Override
+    protected void reset() {
+    }
+
+
+    @Override
+    protected void finish() {
+        if (columns != null && current != null) {
+            for (ImportWaterlevelDifferenceColumn col: columns) {
+                current.addValue(col);
+            }
+
+            differences.add(current);
+        }
+
+        current = null;
+        columns = null;
+    }
+
+    @Override
+    protected void handleLine(String line) {
+        if (line.startsWith(START_META_CHAR)) {
+            handleMetaLine(stripMetaLine(line));
+        }
+        else {
+            handleDataLine(line);
+        }
+    }
+
+
+    private void handleMetaLine(String meta) {
+        if (handleMetaUnit(meta)) {
+            return;
+        }
+        else {
+            handleMetaColumnNames(meta);
+        }
+    }
+
+
+    private boolean handleMetaUnit(String meta) {
+        Matcher m = META_UNIT.matcher(meta);
+
+        if (m.matches()) {
+            String unit = m.group(1);
+            log.debug("Found unit: '" + unit + "'");
+
+            current.setUnit(new ImportUnit(unit));
+
+            return true;
+        }
+
+        return false;
+    }
+
+
+    private boolean handleMetaColumnNames(String meta) {
+        Pattern META_COLUMN_NAMES = Pattern.compile("Fluss-km;(.*)");
+        Matcher m = META_COLUMN_NAMES.matcher(meta);
+
+        if (m.matches()) {
+            String colStr = m.group(1);
+            String[] cols = colStr.split(SEPERATOR_CHAR);
+
+            log.debug("Found " + cols.length + " columns.");
+
+            initColumns(cols);
+
+            return true;
+        }
+
+        return false;
+    }
+
+
+    private void initColumns(String[] cols) {
+        columns = new ImportWaterlevelDifferenceColumn[cols.length];
+
+        for (int i = 0; i < cols.length; i++) {
+            String name = cols[i].replace("\"", "");
+
+            log.debug("Create new column '" + name + "'");
+            columns[i] = new ImportWaterlevelDifferenceColumn(name);
+        }
+    }
+
+
+    private void handleDataLine(String line) {
+        String[] cols = line.split(SEPERATOR_CHAR);
+
+        if (cols == null || cols.length < 2) {
+            log.warn("skip invalid waterlevel line: '" + line + "'");
+            return;
+        }
+
+        try {
+            Double station = nf.parse(cols[0]).doubleValue();
+
+            for (int i = 0; i < columns.length; i++) {
+                String value = cols[i+1];
+
+                try {
+                    columns[i].addValue(new ImportWaterlevelDifferenceValue(
+                        station,
+                        nf.parse(value).doubleValue()
+                    ));
+                }
+                catch (ParseException pe) {
+                    log.warn("Error while parsing value: '" + value + "'");
+                }
+            }
+        }
+        catch (ParseException pe) {
+            log.warn("Error while parsing station: '" + line + "'");
+        }
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org