changeset 2845:a65b5b0ade6f

Completed the MINFO waterlevel import. flys-backend/trunk@4288 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 24 Apr 2012 12:46:26 +0000
parents c4db0f75a94a
children c1c6c9190636
files flys-backend/ChangeLog flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java 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 flys-backend/src/main/java/de/intevation/flys/importer/parsers/WaterlevelParser.java flys-backend/src/main/java/de/intevation/flys/model/WaterlevelValue.java
diffstat 7 files changed, 166 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/ChangeLog	Tue Apr 24 11:16:06 2012 +0000
+++ b/flys-backend/ChangeLog	Tue Apr 24 12:46:26 2012 +0000
@@ -1,3 +1,20 @@
+2012-04-24  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/model/WaterlevelValue.java: Renamed a
+	  property ('qRange' -> 'qrange') because there have been problem during the
+	  import.
+
+	* src/main/java/de/intevation/flys/importer/parsers/WaterlevelParser.java:
+	  Filled the stub with code.
+
+	* 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: Some
+	  adaptions and missing methods which are required during the import.
+
+	* src/main/java/de/intevation/flys/importer/ImportRiver.java: Use the
+	  correct directory to search for waterlevel files.
+
 2012-04-24  Ingo Weinzierl <ingo@intevation.de>
 
 	* doc/schema/oracle-minfo.sql: Small type adaptions in the waterlevel_values
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java	Tue Apr 24 11:16:06 2012 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java	Tue Apr 24 12:46:26 2012 +0000
@@ -464,7 +464,7 @@
 
         File minfo  = getMinfoDir();
         File fixDir = new File(minfo, MINFO_FIXATIONS_DIR);
-        File wspDir = new File(minfo, MINFO_WATERLEVELS_DIR);
+        File wspDir = new File(fixDir, MINFO_WATERLEVELS_DIR);
 
         File[] files = wspDir.listFiles();
 
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportWaterlevel.java	Tue Apr 24 11:16:06 2012 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportWaterlevel.java	Tue Apr 24 12:46:26 2012 +0000
@@ -27,15 +27,26 @@
     private Waterlevel peer;
 
 
-    public ImportWaterlevel(ImportUnit unit, String description) {
+    public ImportWaterlevel(String description) {
         this.qRanges = new ArrayList<ImportWaterlevelQRange>();
 
-        this.unit        = unit;
         this.description = description;
     }
 
 
+    public void setUnit(ImportUnit unit) {
+        this.unit = unit;
+    }
+
+
+    public void addValue(ImportWaterlevelQRange qRange) {
+        this.qRanges.add(qRange);
+    }
+
+
     public void storeDependencies(River river) {
+        log.info("store dependencies");
+
         Waterlevel peer = getPeer(river);
 
         int i = 0;
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportWaterlevelQRange.java	Tue Apr 24 11:16:06 2012 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportWaterlevelQRange.java	Tue Apr 24 12:46:26 2012 +0000
@@ -36,6 +36,8 @@
 
 
     public void storeDependencies(Waterlevel waterlevel) {
+        log.info("store dependencies");
+
         WaterlevelQRange peer = getPeer(waterlevel);
 
         int i = 0;
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportWaterlevelValue.java	Tue Apr 24 11:16:06 2012 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportWaterlevelValue.java	Tue Apr 24 12:46:26 2012 +0000
@@ -33,12 +33,12 @@
             Session session = ImporterSession.getInstance().getDatabaseSession();
             Query query = session.createQuery(
                 "from WaterlevelValue where " +
-                "   qRange=:qRange and " +
+                "   qrange=:qrange and " +
                 "   station=:station and " +
                 "   w=:w"
             );
 
-            query.setParameter("qRange", qRange);
+            query.setParameter("qrange", qRange);
             query.setParameter("station", station);
             query.setParameter("w", w);
 
--- a/flys-backend/src/main/java/de/intevation/flys/importer/parsers/WaterlevelParser.java	Tue Apr 24 11:16:06 2012 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/parsers/WaterlevelParser.java	Tue Apr 24 12:46:26 2012 +0000
@@ -1,15 +1,47 @@
 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.ImportWaterlevel;
+import de.intevation.flys.importer.ImportWaterlevelQRange;
+import de.intevation.flys.importer.ImportWaterlevelValue;
 
 
 public class WaterlevelParser extends LineParser {
 
+    private static final Logger log = Logger.getLogger(WaterlevelParser.class);
+
+    private static final NumberFormat nf =
+        NumberFormat.getInstance(DEFAULT_LOCALE);
+
+    private static final Pattern META_Q_RANGE =
+        Pattern.compile("Abfluss\\s\\[(.*)\\];(.*)");
+
+    public static final Pattern META_UNIT =
+        Pattern.compile("^Einheit: \\[(.*)\\].*");
+
+
     private List<ImportWaterlevel> waterlevels;
 
+    private ImportWaterlevel current;
+
+    private ImportWaterlevelQRange currentQ;
+
+    private String currentDescription;
+
+
     public WaterlevelParser() {
+        waterlevels = new ArrayList<ImportWaterlevel>();
     }
 
 
@@ -19,19 +51,110 @@
 
 
     @Override
+    public void parse(File file) throws IOException {
+        currentDescription = file.getName();
+
+        super.parse(file);
+    }
+
+
+    @Override
     protected void reset() {
-
+        currentQ = null;
+        current  = new ImportWaterlevel(currentDescription);
     }
 
 
     @Override
     protected void finish() {
+        if (current != null) {
+            if (currentQ != null) {
+                current.addValue(currentQ);
+            }
 
+            waterlevels.add(current);
+        }
     }
 
     @Override
     protected void handleLine(String line) {
+        if (line.startsWith(START_META_CHAR)) {
+            handleMetaLine(stripMetaLine(line));
+            return;
+        }
+        else if (handleQRange(line)) {
+            return;
+        }
+        else {
+            handleDataLine(line);
+            return;
+        }
+    }
 
+
+    private void handleMetaLine(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));
+        }
+    }
+
+
+    private boolean handleQRange(String line) {
+        Matcher m = META_Q_RANGE.matcher(line);
+
+        if (m.matches()) {
+            String unitStr  = m.group(1);
+            String valueStr = m.group(2);
+
+            if (currentQ != null) {
+                if (current != null) {
+                    current.addValue(currentQ);
+                }
+                else {
+                    // this should never happen
+                    log.warn("Try to add Q range without waterlevel!");
+                }
+            }
+
+            try {
+                log.debug("Found new Q range: Q=" + valueStr);
+
+                currentQ = new ImportWaterlevelQRange(
+                    nf.parse(valueStr).doubleValue());
+
+                return true;
+            }
+            catch (ParseException pe) {
+                log.warn("Error while parsing Q range: '" + line + "'");
+            }
+        }
+
+        return false;
+    }
+
+
+    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();
+            Double value   = nf.parse(cols[1]).doubleValue();
+
+            currentQ.addValue(new ImportWaterlevelValue(station, value));
+        }
+        catch (ParseException pe) {
+            log.warn("Error while parsing number values: '" + line + "'");
+        }
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-backend/src/main/java/de/intevation/flys/model/WaterlevelValue.java	Tue Apr 24 11:16:06 2012 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/model/WaterlevelValue.java	Tue Apr 24 12:46:26 2012 +0000
@@ -26,7 +26,7 @@
 
     private Integer id;
 
-    private WaterlevelQRange qRange;
+    private WaterlevelQRange qrange;
 
     private Double station;
     private Double w;
@@ -35,8 +35,8 @@
     public WaterlevelValue() {
     }
 
-    public WaterlevelValue(WaterlevelQRange qRange, Double station, Double w) {
-        this.qRange  = qRange;
+    public WaterlevelValue(WaterlevelQRange qrange, Double station, Double w) {
+        this.qrange  = qrange;
         this.station = station;
         this.w       = w;
     }
@@ -61,12 +61,12 @@
 
     @OneToOne
     @JoinColumn(name = "waterlevel_q_range_id" )
-    public WaterlevelQRange getQRange() {
-        return qRange;
+    public WaterlevelQRange getQrange() {
+        return qrange;
     }
 
-    public void setQRange(WaterlevelQRange qRange) {
-        this.qRange = qRange;
+    public void setQrange(WaterlevelQRange qrange) {
+        this.qrange = qrange;
     }
 
     @Column(name = "station")

http://dive4elements.wald.intevation.org