[Dive4elements-commits] [PATCH 5 of 6] WaterlevelParser: Use Wst-structures directly (instead of Waterlevel*)

Wald Commits scm-commit at wald.intevation.org
Tue Mar 12 10:52:39 CET 2013


# HG changeset patch
# User Felix Wolfsteller <felix.wolfsteller at intevation.de>
# Date 1363082114 -3600
# Node ID 9c024718bffb5b889c3c5964855e1f7a8d5ea11b
# Parent  ea4129a696da3362fc21448079b5312f8f16d761
WaterlevelParser: Use Wst-structures directly (instead of Waterlevel*).

diff -r ea4129a696da -r 9c024718bffb flys-backend/src/main/java/de/intevation/flys/importer/parsers/WaterlevelParser.java
--- a/flys-backend/src/main/java/de/intevation/flys/importer/parsers/WaterlevelParser.java	Tue Mar 12 10:51:43 2013 +0100
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/parsers/WaterlevelParser.java	Tue Mar 12 10:55:14 2013 +0100
@@ -20,6 +20,7 @@
 import de.intevation.flys.importer.ImportRange;
 import de.intevation.flys.importer.ImportWst;
 import de.intevation.flys.importer.ImportWstColumn;
+import de.intevation.flys.importer.ImportWstColumnValue;
 import de.intevation.flys.importer.ImportWstQRange;
 
 
@@ -43,71 +44,40 @@
     public static final Pattern META_UNIT =
         Pattern.compile("^Einheit: \\[(.*)\\].*");
 
-    private List<ImportWaterlevel> waterlevels;
+    private List<ImportWst> waterlevels;
 
-    private ImportWaterlevel current;
+    private ImportWst current;
+
+    /** The Waterlevel-Wst s will always have but one column. */
+    private ImportWstColumn column;
 
     private ImportWaterlevelQRange currentQ;
 
+    /** The current (incomplete) Q Range. */
+    private ImportWstQRange currentQRange;
+
+    /** The current (incomplete) km range for Q Range. */
+    private ImportRange currentRange;
+
     private String currentDescription;
 
 
     public WaterlevelParser() {
-        waterlevels = new ArrayList<ImportWaterlevel>();
+        waterlevels = new ArrayList<ImportWst>();
     }
 
 
     /**
      * Create ImportWst objects from ImportWaterlevel
      * objects.
+     * TODO deprecate
      */
     public List<ImportWst> exportWsts() {
-        List<ImportWst> wsts = new ArrayList<ImportWst>();
-        for(ImportWaterlevel waterlevel: getWaterlevels()) {
-            String description = waterlevel.getDescription();
-            ImportWst wst = new ImportWst();
-            wsts.add(wst);
-            wst.setDescription(description);
-            // Fixation kind.
-            wst.setKind(6);
-            wst.setUnit(waterlevel.getUnit());
-
-            // Fake WST has but 1 column.
-            wst.setNumberColumns(1);
-            ImportWstColumn column = wst.getColumn(0);
-            column.setDescription(description);
-            column.setName(description);
-            column.setPosition(0);
-
-            // Build Q Range.
-            List<ImportWaterlevelQRange> qRanges = waterlevel.getQRanges();
-            for(ImportWaterlevelQRange range: qRanges) {
-                List<ImportWaterlevelValue> values = range.getValues();
-                if (values.size() < 2) {
-                    log.warn ("Not enough values to build valid QRange");
-                    continue;
-                }
-                ImportRange iRange = new ImportRange(
-                   BigDecimal.valueOf(values.get(0).getStation()),
-                   BigDecimal.valueOf(values.get(values.size() -1).getStation()));
-                column.addColumnQRange(
-                     new ImportWstQRange(iRange, BigDecimal.valueOf(range.getQ())));
-            }
-
-            // The other W/KM values.
-            for(ImportWaterlevelQRange range: qRanges) {
-                for(ImportWaterlevelValue value: range.getValues()) {
-                    column.addColumnValue(BigDecimal.valueOf(value.getStation()),
-                                          BigDecimal.valueOf(value.getW()));
-                }
-            }
-            // TODO Maybe set a timeinterval.
-        }
-        return wsts;
+        return waterlevels;
     }
 
 
-    public List<ImportWaterlevel> getWaterlevels() {
+    public List<ImportWst> getWaterlevels() {
         return waterlevels;
     }
 
@@ -122,16 +92,25 @@
 
     @Override
     protected void reset() {
-        currentQ = null;
-        current  = new ImportWaterlevel(currentDescription);
+        currentQRange = null;
+        current       = new ImportWst(currentDescription);
+        column        = new ImportWstColumn(current,
+            currentDescription,
+            currentDescription,
+            0);
+        current.setKind(6);
     }
 
 
     @Override
     protected void finish() {
         if (current != null) {
-            if (currentQ != null) {
-                current.addValue(currentQ);
+            if (currentQRange != null) {
+                List<ImportWstColumnValue> cValues = column.getColumnValues();
+                // Set end of range to last station.
+                currentRange.setB(cValues.get(cValues.size() -1).getPosition());
+                currentQRange.setRange(currentRange);
+                column.addColumnQRange(currentQRange);
             }
 
             waterlevels.add(current);
@@ -172,23 +151,22 @@
         if (m.matches()) {
             String unitStr  = m.group(1);
             String valueStr = m.group(2);
+            try {
+                if (currentQRange != null) {
+                    // Finish off the last one.
+                    List<ImportWstColumnValue> cValues = column.getColumnValues();
+                    // Set end of range to last station.
+                    currentRange.setB(cValues.get(cValues.size() -1).getPosition());
+                    currentQRange.setRange(currentRange);
+                    column.addColumnQRange(currentQRange);
+                    
+                }
+                currentQRange = new ImportWstQRange(null,
+                    new BigDecimal(nf.parse(valueStr).doubleValue()));
+                currentRange = new ImportRange();
 
-            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) {
@@ -209,10 +187,17 @@
         }
 
         try {
+            // Store the value and remember the position for QRange, if needed.
             Double station = nf.parse(cols[0]).doubleValue();
             Double value   = nf.parse(cols[1]).doubleValue();
 
-            currentQ.addValue(new ImportWaterlevelValue(station, value));
+            BigDecimal stationBD = new BigDecimal(station);
+
+            column.addColumnValue(stationBD, new BigDecimal(value));
+
+            if (currentRange.getA() == null) {
+                currentRange.setA(stationBD);
+            }
         }
         catch (ParseException pe) {
             log.warn("Error while parsing number values: '" + line + "'");


More information about the Dive4elements-commits mailing list