[PATCH 11 of 12] Importer (s/u-info) extensions: iota (salix): detecting, logging, cancelling in case of wrong column titles/units,

Wald Commits scm-commit at wald.intevation.org
Mon Mar 23 16:38:43 CET 2020


# HG changeset patch
# User mschaefer
# Date 1584974412 -3600
#      Mon Mar 23 15:40:12 2020 +0100
# Node ID f0cad5212f49d2a3b7b5c97f5489c2e69d852732
# Parent  75bd347147ade9d5485000f3e8420685f32f22a6
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

diff -r 75bd347147ad -r f0cad5212f49 backend/src/main/java/org/dive4elements/river/importer/uinfo/importitem/SalixSeriesImport.java
--- 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());
diff -r 75bd347147ad -r f0cad5212f49 backend/src/main/java/org/dive4elements/river/importer/uinfo/parsers/SalixParser.java
--- 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());
     }
 }


More information about the Dive4Elements-commits mailing list