changeset 5240:9c024718bffb

WaterlevelParser: Use Wst-structures directly (instead of Waterlevel*).
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Tue, 12 Mar 2013 10:55:14 +0100
parents ea4129a696da
children 31b464f17392
files flys-backend/src/main/java/de/intevation/flys/importer/parsers/WaterlevelParser.java
diffstat 1 files changed, 49 insertions(+), 64 deletions(-) [+]
line wrap: on
line diff
--- 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);
-
-            if (currentQ != null) {
-                if (current != null) {
-                    current.addValue(currentQ);
+            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);
+                    
                 }
-                else {
-                    // this should never happen
-                    log.warn("Try to add Q range without waterlevel!");
-                }
-            }
+                currentQRange = new ImportWstQRange(null,
+                    new BigDecimal(nf.parse(valueStr).doubleValue()));
+                currentRange = new ImportRange();
 
-            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 + "'");

http://dive4elements.wald.intevation.org