changeset 9663:7c1da1b3f6b8

Importer (s/u-info) extensions: further year/date checks for collision files
author mschaefer
date Fri, 03 Apr 2020 11:56:45 +0200 (2020-04-03)
parents f7738e64fce6
children 692f49b7de63
files backend/src/main/java/org/dive4elements/river/importer/sinfo/importitem/CollisionSeriesImport.java backend/src/main/java/org/dive4elements/river/importer/sinfo/parsers/CollisionParser.java
diffstat 2 files changed, 28 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/backend/src/main/java/org/dive4elements/river/importer/sinfo/importitem/CollisionSeriesImport.java	Fri Apr 03 11:55:16 2020 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/sinfo/importitem/CollisionSeriesImport.java	Fri Apr 03 11:56:45 2020 +0200
@@ -48,6 +48,10 @@
         this.year = year;
     }
 
+    public int getYear() {
+        return this.year;
+    }
+
     @Override
     public Logger getLog() {
         return log;
--- a/backend/src/main/java/org/dive4elements/river/importer/sinfo/parsers/CollisionParser.java	Fri Apr 03 11:55:16 2020 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/sinfo/parsers/CollisionParser.java	Fri Apr 03 11:56:45 2020 +0200
@@ -14,6 +14,7 @@
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
+import java.util.Calendar;
 import java.util.Date;
 import java.util.EnumMap;
 import java.util.HashMap;
@@ -47,7 +48,9 @@
 
     private static final Logger log = Logger.getLogger(CollisionParser.class);
 
-    private static final Pattern META_YEAR = Pattern.compile("^#\\sJahr:\\s*([12]\\d\\d\\d).*", Pattern.CASE_INSENSITIVE);
+    private static final Pattern IMPORT_FILENAME = Pattern.compile("Grundberuehrungen_([12]\\d{3})\\.csv", Pattern.CASE_INSENSITIVE);
+
+    private static final Pattern META_YEAR = Pattern.compile("^#\\sJahr:\\s*([12]\\d{3}).*", Pattern.CASE_INSENSITIVE);
 
     private enum ColTitlePattern {
         DATE("Datum.*"), //
@@ -72,16 +75,19 @@
 
     private final HashMap<String, CollisionTypeImport> types;
 
+    private final int filenameyear;
+
 
     /***** CONSTRUCTORS *****/
 
-    public CollisionParser(final File importPath, final File rootRelativePath, final ImportRiver river) {
+    public CollisionParser(final File importPath, final File rootRelativePath, final ImportRiver river, final int filenameyear) {
         super(importPath, rootRelativePath, river);
         logDebug("CollisionParser.new: calling ImporterSession.getInstance");
         final Session session = ImporterSession.getInstance().getDatabaseSession();
         this.types = new HashMap<>();
         for (final CollisionType type : CollisionType.getTypes(session))
             this.types.put(type.getName().trim().toLowerCase(), new CollisionTypeImport(type.getName()));
+        this.filenameyear = filenameyear;
     }
 
 
@@ -105,8 +111,12 @@
     public static List<CollisionParser> createParsers(final File importDir, final File relativeDir, final ImportRiver river) {
         final List<CollisionParser> parsers = new ArrayList<>();
         if (importDir.exists()) {
-            for (final File file : listFiles(importDir, ".csv"))
-                parsers.add(new CollisionParser(file, new File(relativeDir, file.getName()), river));
+            for (final File file : listFiles(importDir, IMPORT_FILENAME)) {
+                final Matcher m = IMPORT_FILENAME.matcher(file.getName());
+                m.matches();
+                final String fnyear = m.group(1);
+                parsers.add(new CollisionParser(file, new File(relativeDir, file.getName()), river, Integer.valueOf(fnyear)));
+            }
         }
         return parsers;
     }
@@ -133,7 +143,10 @@
         final Matcher m = META_YEAR.matcher(this.currentLine);
         if (m.matches()) {
             this.metaPatternsMatched.add(META_YEAR);
-            this.seriesHeader.setYear(Integer.parseInt(m.group(1)));
+            final int year = Integer.parseInt(m.group(1));
+            this.seriesHeader.setYear(year);
+            if (year != this.filenameyear)
+                logLineWarning("Year in file name differs from meta data year");
             return true;
         }
         return false;
@@ -191,6 +204,12 @@
             logLineWarning("Invalid or missing date");
             return null;
         }
+        final Calendar cal = Calendar.getInstance();
+        cal.setTime(eventDate);
+        if (cal.get(Calendar.YEAR) != this.seriesHeader.getYear()) {
+            logLineWarning("Invalid or missing date");
+            return null;
+        }
         final String typeName = values[this.cols.get(ColTitlePattern.TYPE)].trim();
         final String typeKey = typeName.toLowerCase();
         CollisionTypeImport type = null;

http://dive4elements.wald.intevation.org