changeset 9660:f0cad5212f49

Importer (s/u-info) extensions: iota (salix): detecting, logging, cancelling in case of wrong column titles/units, detecting, logging and skipping lines with missing values
author mschaefer
date Mon, 23 Mar 2020 15:40:12 +0100
parents 75bd347147ad
children 9b8ba3b83a15
files backend/src/main/java/org/dive4elements/river/importer/uinfo/importitem/SalixSeriesImport.java backend/src/main/java/org/dive4elements/river/importer/uinfo/parsers/SalixParser.java
diffstat 2 files changed, 40 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/backend/src/main/java/org/dive4elements/river/importer/uinfo/importitem/SalixSeriesImport.java	Mon Mar 23 15:37:37 2020 +0100
+++ b/backend/src/main/java/org/dive4elements/river/importer/uinfo/importitem/SalixSeriesImport.java	Mon Mar 23 15:40:12 2020 +0100
@@ -54,7 +54,7 @@
     }
 
     @Override
-    public List<Salix> querySeriesItem(final Session session, final River river) {
+    public List<Salix> querySeriesItem(final Session session, final River river, final boolean doQueryParent) {
         final Query query = session.createQuery("FROM Salix WHERE river=:river AND lower(filename)=:filename");
         query.setParameter("river", river);
         query.setParameter("filename", this.filename.toLowerCase());
--- a/backend/src/main/java/org/dive4elements/river/importer/uinfo/parsers/SalixParser.java	Mon Mar 23 15:37:37 2020 +0100
+++ b/backend/src/main/java/org/dive4elements/river/importer/uinfo/parsers/SalixParser.java	Mon Mar 23 15:40:12 2020 +0100
@@ -41,6 +41,8 @@
 
     private static final String IMPORT_FILENAME = "Iota.csv";
 
+    private static final Pattern META_FIRST = Pattern.compile("^#\\sIota.*", Pattern.CASE_INSENSITIVE);
+
     private static final Pattern META_EVALUATOR = Pattern.compile("^#\\sAuswerter:\\s*([^;]*).*", Pattern.CASE_INSENSITIVE);
 
     private enum ColTitlePattern {
@@ -58,12 +60,17 @@
         }
     }
 
-    private final EnumMap<ColTitlePattern, Integer> cols = new EnumMap<>(ColTitlePattern.class);
+    private final EnumMap<ColTitlePattern, Integer> cols;
+
+    private final EnumMap<ColTitlePattern, String> units;
+
 
     /***** CONSTRUCTORS *****/
 
     public SalixParser(final File importPath, final File rootRelativePath, final ImportRiver river) {
         super(importPath, rootRelativePath, river);
+        this.cols = new EnumMap<>(ColTitlePattern.class);
+        this.units = new EnumMap<>(ColTitlePattern.class);
     }
 
     /***** METHODS *****/
@@ -98,12 +105,23 @@
 
     @Override
     protected boolean handleMetaOther() {
-        if (handleMetaEvaluator())
+        if (handleMetaFirst())
+            return true;
+        else if (handleMetaEvaluator())
             return true;
         else
             return false;
     }
 
+    private boolean handleMetaFirst() {
+        final Matcher m = META_FIRST.matcher(this.currentLine);
+        if (m.matches()) {
+            this.metaPatternsMatched.add(META_FIRST);
+            return true;
+        }
+        return false;
+    }
+
     private boolean handleMetaEvaluator() {
         final Matcher m = META_EVALUATOR.matcher(this.currentLine);
         if (m.matches()) {
@@ -118,35 +136,44 @@
     protected boolean handleMetaColumnTitles() {
         if (!super.handleMetaColumnTitles())
             return false;
-        for (final ColTitlePattern col : ColTitlePattern.values())
+        for (final ColTitlePattern col : ColTitlePattern.values()) {
             this.cols.put(col, -1);
+            this.units.put(col, "");
+        }
         for (int i = 1; i <= this.columnTitles.size() - 1; i++) {
             for (final ColTitlePattern col : ColTitlePattern.values()) {
-                if (col.getPattern().matcher(this.columnTitles.get(i)).matches()) {
+                final Matcher m = col.getPattern().matcher(this.columnTitles.get(i));
+                if (m.matches()) {
                     this.cols.put(col, i);
+                    this.units.put(col, m.group(1));
                     break;
                 }
             }
         }
         if ((this.cols.get(ColTitlePattern.FACTOR) < 0) || (this.cols.get(ColTitlePattern.MWMNW) < 0)) {
-            logError("Column of the iota value and/or mnw-mw-diff could not be identified");
+            logLineError("Column of the iota value and/or mnw-mw-diff could not be identified");
             this.headerParsingState = ParsingState.STOP;
-            return false;
+            return true;
+        }
+        if (!this.units.get(ColTitlePattern.FACTOR).equals("m") || !this.units.get(ColTitlePattern.MWMNW).equals("m")) {
+            logLineError("Column of the iota value and/or mnw-mw-diff have unsupported units");
+            this.headerParsingState = ParsingState.STOP;
         }
         return true;
     }
 
     @Override
     protected SalixKmLineImport createKmLineImport(final Double km, final String[] values) {
-        if (Double.isNaN(parseDoubleWithNull(values[this.cols.get(ColTitlePattern.FACTOR)]).doubleValue())) {
-            logError("Iota not found in line " + this.in.getLineNumber());
+        final Number factor = parseDoubleCheckNull(values, this.cols.get(ColTitlePattern.FACTOR));
+        if ((factor == null) || Double.isNaN(factor.doubleValue())) {
+            logLineWarning(INVALID_VALUE_ERROR_FORMAT, "iota");
             return null;
         }
-        if (Double.isNaN(parseDoubleWithNull(values[this.cols.get(ColTitlePattern.MWMNW)]).doubleValue())) {
-            logError("MNW-MW-diff not found in line " + this.in.getLineNumber());
+        final Number mnwmw = parseDoubleCheckNull(values, this.cols.get(ColTitlePattern.MWMNW));
+        if ((mnwmw == null) || Double.isNaN(mnwmw.doubleValue())) {
+            logLineWarning(INVALID_VALUE_ERROR_FORMAT, "MNW-MW-diff");
             return null;
         }
-        return new SalixKmLineImport(km, parseDoubleWithNull(values[this.cols.get(ColTitlePattern.FACTOR)]).doubleValue(),
-                parseDoubleWithNull(values[this.cols.get(ColTitlePattern.MWMNW)]).doubleValue());
+        return new SalixKmLineImport(km, factor.doubleValue(), mnwmw.doubleValue());
     }
 }

http://dive4elements.wald.intevation.org