changeset 9653:3b3c7513472e

Importer (s/u-info) extensions: support of multiple channel data series, check for non-overlapping year ranges
author mschaefer
date Mon, 23 Mar 2020 15:06:26 +0100
parents dd93bb84561d
children 8a2a777a8372
files backend/src/main/java/org/dive4elements/river/importer/sinfo/importitem/ChannelSeriesImport.java backend/src/main/java/org/dive4elements/river/importer/sinfo/parsers/ChannelParser.java
diffstat 2 files changed, 47 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/backend/src/main/java/org/dive4elements/river/importer/sinfo/importitem/ChannelSeriesImport.java	Mon Mar 23 15:03:11 2020 +0100
+++ b/backend/src/main/java/org/dive4elements/river/importer/sinfo/importitem/ChannelSeriesImport.java	Mon Mar 23 15:06:26 2020 +0100
@@ -60,13 +60,16 @@
     }
 
     @Override
-    public List<Channel> querySeriesItem(final Session session, final River river) {
-        final Query query = session.createQuery("FROM Channel WHERE river=:river");
+    public List<Channel> querySeriesItem(final Session session, final River river, final boolean doQueryParent) {
+        final Query query = session.createQuery("FROM Channel WHERE river=:river AND (lower(filename)=:filename"
+                + " OR (:newfrom BETWEEN year_from AND year_to) OR (:newto BETWEEN year_from AND year_to))");
         query.setParameter("river", river);
+        query.setParameter("filename", this.filename.toLowerCase());
+        query.setParameter("newfrom", this.year_from);
+        query.setParameter("newto", this.year_to);
         return query.list();
     }
 
-
     @Override
     public Channel createSeriesItem(final River river) {
         return new Channel(river, this.filename, this.kmrange_info, this.notes, this.year_from, this.year_to);
--- a/backend/src/main/java/org/dive4elements/river/importer/sinfo/parsers/ChannelParser.java	Mon Mar 23 15:03:11 2020 +0100
+++ b/backend/src/main/java/org/dive4elements/river/importer/sinfo/parsers/ChannelParser.java	Mon Mar 23 15:06:26 2020 +0100
@@ -38,9 +38,11 @@
 
     private static final Logger log = Logger.getLogger(ChannelParser.class);
 
-    private static final String IMPORT_FILENAME = "Fahrrinne.csv";
+    private static final Pattern IMPORT_FILENAME = Pattern.compile("Fahrrinne.*\\.csv", Pattern.CASE_INSENSITIVE);
 
-    protected static final Pattern META_YEARS = Pattern.compile("^#\\sZeitraum:\\s*([12]\\d\\d\\d)*\\s*-\\s*([12]\\d\\d\\d)*.*", Pattern.CASE_INSENSITIVE);
+    private static final Pattern META_FIRST = Pattern.compile("^#\\sFahrrinne.*", Pattern.CASE_INSENSITIVE);
+
+    private static final Pattern META_YEARS = Pattern.compile("^#\\sZeitraum:\\s*([12]\\d\\d\\d)*\\s*-\\s*([12]\\d\\d\\d)*.*", Pattern.CASE_INSENSITIVE);
 
     private static final Pattern WIDTH_COLUMNTITLE = Pattern.compile("Sollbreite\\s*\\[(.*)\\].*", Pattern.CASE_INSENSITIVE);
 
@@ -79,32 +81,49 @@
      */
     public static List<ChannelParser> createParsers(final File importDir, final File relativeDir, final ImportRiver river) {
         final List<ChannelParser> parsers = new ArrayList<>();
-        final File importFile = new File(importDir, IMPORT_FILENAME);
-        if (importFile.exists())
-            parsers.add(new ChannelParser(importFile, new File(relativeDir, IMPORT_FILENAME), river));
+        if (importDir.exists())
+            for (final File file : listFiles(importDir, IMPORT_FILENAME))
+                parsers.add(new ChannelParser(file, new File(relativeDir, file.getName()), river));
         return parsers;
     }
 
     @Override
     protected boolean handleMetaOther() {
-        if (handleMetaYears())
+        if (handleMetaFirst())
+            return true;
+        else if (handleMetaYears())
             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 handleMetaYears() {
         final Matcher m = META_YEARS.matcher(this.currentLine);
         if (m.matches()) {
             this.metaPatternsMatched.add(META_YEARS);
-            if (m.group(1) != null)
-                this.seriesHeader.setYear_from(Integer.valueOf(m.group(1)));
-            else
-                this.seriesHeader.setYear_from(null);
-            if (m.group(2) != null)
-                this.seriesHeader.setYear_to(Integer.valueOf(m.group(2)));
-            else
-                this.seriesHeader.setYear_to(null);
+            try {
+                if (m.group(1) != null)
+                    this.seriesHeader.setYear_from(Integer.valueOf(m.group(1)));
+                else
+                    throw new NumberFormatException();
+                if (m.group(2) != null)
+                    this.seriesHeader.setYear_to(Integer.valueOf(m.group(2)));
+                else
+                    throw new NumberFormatException();
+            }
+            catch (final Exception e) {
+                logLineError("Invalid or missing start and/or end year");
+                this.headerParsingState = ParsingState.STOP;
+            }
             return true;
         }
         return false;
@@ -120,9 +139,8 @@
                     this.widthColIndex = i;
             }
             if ((this.widthColIndex < 0) || (this.depthColIndex < 0)) {
-                logError("Columns of width and/or depth values could not be identified, missing column title 'Sollbreite...'/'Sollhöhe...'");
+                logLineError("Columns of width and/or depth values could not be identified, missing column title 'Sollbreite...'/'Sollhöhe...'");
                 this.headerParsingState = ParsingState.STOP;
-                return false;
             }
             return true;
         }
@@ -137,15 +155,16 @@
 
     @Override
     protected ChannelKmLineImport createKmLineImport(final Double km, final String[] values) {
-        if (parseDoubleWithNull(values[this.widthColIndex]) == null) {
-            logError("Invalid width value in line " + this.in.getLineNumber());
+        final Number width = parseDoubleCheckNull(values, this.widthColIndex);
+        if ((width == null) || Double.isNaN(width.doubleValue())) {
+            logLineError(INVALID_VALUE_ERROR_FORMAT, "width");
             return null;
         }
-        if (parseDoubleWithNull(values[this.depthColIndex]) == null) {
-            logError("Invalid depth value in line " + this.in.getLineNumber());
+        final Number depth = parseDoubleCheckNull(values, this.depthColIndex);
+        if ((depth == null) || Double.isNaN(depth.doubleValue())) {
+            logLineError(INVALID_VALUE_ERROR_FORMAT, "depth");
             return null;
         }
-        return new ChannelKmLineImport(km, parseDoubleWithNull(values[this.widthColIndex]).doubleValue(),
-                parseDoubleWithNull(values[this.depthColIndex]).doubleValue());
+        return new ChannelKmLineImport(km, width.doubleValue(), depth.doubleValue());
     }
 }

http://dive4elements.wald.intevation.org