diff backend/src/main/java/org/dive4elements/river/importer/parsers/MeasurementStationsParser.java @ 8412:17db08570637

SCHEMA CHANGE: removed superfluous columns station and river_id from measurement_stations and adapted other components accordingly.
author Tom Gottfried <tom@intevation.de>
date Wed, 15 Oct 2014 19:20:26 +0200
parents 4c3ccf2b0304
children 9db1f48bfea9
line wrap: on
line diff
--- a/backend/src/main/java/org/dive4elements/river/importer/parsers/MeasurementStationsParser.java	Wed Oct 15 14:58:46 2014 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/parsers/MeasurementStationsParser.java	Wed Oct 15 19:20:26 2014 +0200
@@ -32,7 +32,9 @@
         }
     }
 
-    public static final int MIN_COLUMNS = 10;
+    public static final int MIN_COLUMNS = 9;
+
+    public static final int MAX_COMMENT_LENGTH = 512;
 
     private static final Logger log = Logger
         .getLogger(MeasurementStationsParser.class);
@@ -58,7 +60,7 @@
 
         try {
             current = new ImportMeasurementStation();
-            handleDataLine(line);
+            handleDataLine(lineNum, line);
             measurementStations.add(current);
         }
         catch (MeasurementStationParserException e) {
@@ -70,7 +72,7 @@
         return measurementStations;
     }
 
-    protected void handleDataLine(String line)
+    protected void handleDataLine(int lineNum, String line)
         throws MeasurementStationParserException {
         String[] cols = line.split(SEPERATOR_CHAR);
 
@@ -80,116 +82,132 @@
                 + num);
         }
 
-        current.name = getName(cols);
-        current.station = getStation(cols);
-        current.range = getRange(cols);
-        current.measurementType = getMeasurementType(cols);
-        current.riverside = getRiverside(cols);
-        current.gauge = getGauge(cols);
-        current.observationTimerange = getObservationTimerange(cols);
-        current.operator = getOperator(cols);
-        current.description = getDescription(cols);
-
-        log.debug("Found new measurement station '" + current.name + "' at km "
-            + current.station);
+        current.name = getName(cols, lineNum);
+        current.range = getRange(cols, lineNum);
+        current.measurementType = getMeasurementType(cols, lineNum);
+        current.riverside = getRiverside(cols, lineNum);
+        current.gauge = getGauge(cols, lineNum);
+        current.observationTimerange = getObservationTimerange(cols, lineNum);
+        current.operator = getOperator(cols, lineNum);
+        current.comment = getComment(cols, lineNum);
     }
 
-    protected String getName(String[] cols)
+    protected String getName(String[] cols, int lineNum)
         throws MeasurementStationParserException {
         if (cols[0] == null || cols[0].length() == 0) {
-            throw new MeasurementStationParserException("invalid name '"
-                + cols[0] + "'");
+            throw new MeasurementStationParserException("invalid name in line "
+                + lineNum);
         }
 
         return cols[0];
     }
 
-    protected double getStation(String[] cols)
-        throws MeasurementStationParserException {
-        if (cols[1] == null || cols[1].length() == 0) {
-            throw new MeasurementStationParserException("invalid station '"
-                + cols[1] + "'");
-        }
-
-        try {
-            return getDouble(cols[1]);
-        }
-        catch (ParseException e) {
-            throw new MeasurementStationParserException(
-                "unable to parse station: " + e.getMessage());
-        }
-    }
-
-    protected ImportRange getRange(String[] cols) {
-        if (cols[4] == null || cols[4].length() == 0) {
-            log.warn("No upper value for range found in '" + cols[4] + "'");
-            return null;
-        }
-
-        if (cols[5] == null || cols[5].length() == 0) {
-            log.warn("No upper value for range found in '" + cols[5] + "'");
+    protected ImportRange getRange(String[] cols, int lineNum) {
+        String from = cols[1];
+        String to   = cols[4];
+        if (from == null || from.length() == 0) {
+            log.error("No station found in line" + lineNum);
             return null;
         }
 
         try {
-            double lower = getDouble(cols[4]);
-            double upper = getDouble(cols[5]);
+            double lower = getDouble(from);
 
-            return new ImportRange(new BigDecimal(lower), new BigDecimal(upper));
+            if (to == null || to.length() == 0) {
+                log.warn("No end km found in line " + lineNum);
+                return new ImportRange(new BigDecimal(lower));
+            }
+
+            try {
+                double upper = getDouble(to);
+
+                return new ImportRange(new BigDecimal(lower),
+                    new BigDecimal(upper));
+            }
+            catch (ParseException e) {
+                log.warn("Unparseable end km in line " + lineNum +
+                    ". Error: " + e.getMessage());
+                return new ImportRange(new BigDecimal(lower));
+            }
+
         }
         catch (ParseException e) {
-            log.warn("unable to parse range: " + e.getMessage());
+            log.error("Unparseable station in line " + lineNum +
+                    ". Error: " + e.getMessage());
             return null;
         }
     }
 
-    protected String getMeasurementType(String[] cols)
+    protected String getMeasurementType(String[] cols, int lineNum)
         throws MeasurementStationParserException {
         if (cols[2] == null || cols[2].length() == 0) {
             throw new MeasurementStationParserException(
-                "invalid measurement type '" + cols[2] + "'");
+                "invalid measurement type in line " + lineNum);
         }
 
         return cols[2];
     }
 
-    protected String getRiverside(String[] cols) {
-        return cols[3];
+    protected String getRiverside(String[] cols, int lineNum) {
+        String col = cols[3];
+        if (col == null || col.length() == 0) {
+            log.warn("No river side given in line " + lineNum);
+        }
+        return col;
     }
 
-    protected String getGauge(String[] cols) {
-        if (cols[6] == null || cols[6].length() == 0) {
-            log.warn("invalid gauge found: '" + cols[6] + "'");
+    protected String getGauge(String[] cols, int lineNum) {
+        String col = cols[5];
+        if (col == null || col.length() == 0) {
+            log.warn("Invalid gauge found in line " + lineNum);
         }
-
-        return cols[6];
+        return col;
     }
 
-    protected ImportTimeInterval getObservationTimerange(String[] cols) {
-        if (cols[8] == null || cols[8].length() == 0) {
-                log.warn("Found invalid observation time '" + cols[8] + "'");
+    protected ImportTimeInterval getObservationTimerange(
+        String[] cols,
+        int lineNum
+    ) {
+        String col = cols[7];
+        if (col == null || col.length() == 0) {
+            log.warn("Observation time invalid in line " + lineNum);
+            return null;
         }
 
         try {
-            Date date = getDate(cols[8]);
+            Date date = getDate(col);
 
             if (date != null) {
                 return new ImportTimeInterval(date);
             }
-            log.warn("Observation time date invalid: '" + cols[8] + "'");
+            log.warn("Observation time invalid in line " + lineNum);
         }
         catch (ParseException pe) {
-            log.warn("Observation time date not parseable: '" + cols[8] + "'");
-            return null;
+            log.warn("Unparseable observation time '" + col +
+                "' in line " + lineNum);
         }
         return null;
     }
 
-    protected String getOperator(String[] cols) {
-        return cols[9];
+    protected String getOperator(String[] cols, int lineNum) {
+        String col = cols[8];
+        if (col == null || col.length() == 0) {
+            log.warn("No operator given in line " + lineNum);
+        }
+        return col;
     }
 
-    protected String getDescription(String[] cols) {
-        return cols.length > 10 ? cols[10] : null;
+    protected String getComment(String[] cols, int lineNum) {
+        if (cols.length > MIN_COLUMNS) {
+            String col = cols[9];
+            if (col.length() > MAX_COMMENT_LENGTH) {
+                log.warn("Comment in line " + lineNum +
+                    " longer than allowed " + MAX_COMMENT_LENGTH +
+                    " characters. Truncated.");
+                return col.substring(0, MAX_COMMENT_LENGTH);
+            }
+            return col;
+        }
+        return null;
     }
 }

http://dive4elements.wald.intevation.org