changeset 2357:2e18e12fac11

Added date guesser for WST columns. flys-backend/trunk@3064 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 24 Oct 2011 16:48:21 +0000
parents b8e01307c138
children 441028ca9a28
files flys-backend/ChangeLog flys-backend/src/main/java/de/intevation/flys/importer/ImportWstColumn.java flys-backend/src/main/java/de/intevation/flys/importer/parsers/WstParser.java flys-backend/src/main/java/de/intevation/flys/utils/DateGuesser.java
diffstat 4 files changed, 270 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/ChangeLog	Mon Oct 24 12:33:05 2011 +0000
+++ b/flys-backend/ChangeLog	Mon Oct 24 16:48:21 2011 +0000
@@ -1,3 +1,15 @@
+2011-10-05	Sascha L. Teichmann	<sascha.teichmann@intevation.de>:
+
+	* src/main/java/de/intevation/flys/utils/DateGuesser.java:
+	  New. Date guesser from desktop FLYS.
+
+	* src/main/java/de/intevation/flys/importer/parsers/WstParser.java:
+	  Try to parse the name of a WST column as a date and store
+	  the date into the database
+	  
+	* src/main/java/de/intevation/flys/importer/ImportWstColumn.java:
+	  Added code to store the date of the column in the database.
+
 2011-10-24  Bjoern Schilberg <bjoern.schilberg@intevation.de>
 
 	* doc/schema/postgresql-spatial.sql: 
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportWstColumn.java	Mon Oct 24 12:33:05 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportWstColumn.java	Mon Oct 24 16:48:21 2011 +0000
@@ -3,6 +3,7 @@
 import de.intevation.flys.model.Wst;
 import de.intevation.flys.model.WstColumn;
 import de.intevation.flys.model.River;
+import de.intevation.flys.model.TimeInterval;
 
 import org.hibernate.Session;
 import org.hibernate.Query;
@@ -23,6 +24,8 @@
     protected String    description;
     protected Integer   position;
 
+    protected ImportTimeInterval timeInterval;
+
     protected List<ImportWstColumnQRange> columnQRanges;
     protected List<ImportWstColumnValue>  columnValues;
 
@@ -101,6 +104,14 @@
         }
     }
 
+    public ImportTimeInterval getTimeInterval() {
+        return timeInterval;
+    }
+
+    public void setTimeInterval(ImportTimeInterval timeInterval) {
+        this.timeInterval = timeInterval;
+    }
+
     public WstColumn getPeer(River river) {
         if (peer == null) {
             Wst w = wst.getPeer(river);
@@ -113,9 +124,14 @@
             query.setParameter("name",        name);
             query.setParameter("description", description);
             query.setParameter("position",    position);
+
+            TimeInterval ti = timeInterval != null
+                ? timeInterval.getPeer()
+                : null;
+
             List<WstColumn> columns = query.list();
             if (columns.isEmpty()) {
-                peer = new WstColumn(w, name, description, position, null);
+                peer = new WstColumn(w, name, description, position, ti);
                 session.save(peer);
             }
             else {
--- a/flys-backend/src/main/java/de/intevation/flys/importer/parsers/WstParser.java	Mon Oct 24 12:33:05 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/parsers/WstParser.java	Mon Oct 24 16:48:21 2011 +0000
@@ -14,6 +14,7 @@
 import org.apache.log4j.Logger;
 
 import de.intevation.flys.utils.StringUtil;
+import de.intevation.flys.utils.DateGuesser;
 
 import java.util.regex.Pattern;
 import java.util.regex.Matcher;
@@ -22,6 +23,7 @@
 
 import de.intevation.flys.importer.ImportWstQRange;
 import de.intevation.flys.importer.ImportWstColumn;
+import de.intevation.flys.importer.ImportTimeInterval;
 import de.intevation.flys.importer.ImportRange;
 import de.intevation.flys.importer.ImportUnit;
 import de.intevation.flys.importer.ImportWst;
@@ -68,6 +70,16 @@
         this.wst = wst;
     }
 
+    public ImportTimeInterval guessDate(String string) {
+        try {
+            return new ImportTimeInterval(
+                DateGuesser.guessDate(string));
+        }
+        catch (IllegalArgumentException iae) {
+        }
+        return null;
+    }
+
     public void parse(File file) throws IOException {
 
         log.info("Parsing WST file '" + file + "'");
@@ -241,7 +253,9 @@
                                     " (" + collision + ")";
                                 ++collision;
                             }
-                            wst.getColumn(i).setName(candidate);
+                            ImportWstColumn iwc = wst.getColumn(i);
+                            iwc.setName(candidate);
+                            iwc.setTimeInterval(guessDate(candidate));
                         }
                         columnHeaderChecked = true;
                     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/utils/DateGuesser.java	Mon Oct 24 16:48:21 2011 +0000
@@ -0,0 +1,226 @@
+package de.intevation.flys.utils;
+
+/**
+ * Copyright (c) 2006 by Intevation GmbH
+ *
+ * @author Sascha L. Teichmann (teichmann@intevation.de)
+ *
+ * This program is free software under the LGPL (&gt;=v2.1)
+ * Read the file LGPL coming with FLYS for details.
+ */
+
+import java.util.Date;
+import java.util.Calendar;
+
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
+
+public final class DateGuesser {
+    public static final String [] MONTH = {
+        "jan", "feb", "mrz", "apr", "mai", "jun",
+        "jul", "aug", "sep", "okt", "nov", "dez"
+    };
+
+    public static final int guessMonth(String s) {
+        s = s.toLowerCase();
+        for (int i = 0; i < MONTH.length; ++i)
+            if (MONTH[i].equals(s)) {
+                return i;
+            }
+        return -1;
+    }
+
+    public static final Pattern YYYY_MM_DD =
+        Pattern.compile("^(\\d{4})-(\\d{2})-(\\d{2})$");
+
+    public static final Pattern DD_MM_YYYY =
+        Pattern.compile("^(\\d{1,2})\\.(\\d{1,2})\\.(\\d{2,4})$");
+
+    public static final Pattern MMM_YYYY =
+        Pattern.compile("^(\\d{0,2})\\.?(\\w{3})\\.?(\\d{2,4})$");
+
+    public static final Pattern GARBAGE_YYYY =
+        Pattern.compile("^\\D*(\\d{2,4})$");
+
+    public static final Pattern YYYY_MM_DDThh_mm =
+        Pattern.compile("^(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2})$");
+
+    public static final Pattern YYYY_MM_DDThh_mm_ss =
+        Pattern.compile("^(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2})$");
+
+    public static final Pattern DD_MM_YYYYThh_mm =
+        Pattern.compile("^(\\d{1,2})\\.(\\d{1,2})\\.(\\d{2,4})T(\\d{1,2}):(\\d{2})$");
+
+    public static final Pattern DD_MM_YYYYThh_mm_ss =
+        Pattern.compile("^(\\d{1,2})\\.(\\d{1,2})\\.(\\d{2,4})T(\\d{1,2}):(\\d{2}):(\\d{2})$");
+
+    private DateGuesser() {
+    }
+
+    public static final int calendarMonth(String month) {
+        return calendarMonth(Integer.parseInt(month));
+    }
+
+    public static final int calendarMonth(int month) {
+        return Math.max(Math.min(month-1, 11), 0);
+    }
+
+    public static Date guessDate(String s) {
+        if (s == null || (s = s.trim()).length() == 0) {
+            throw new IllegalArgumentException();
+        }
+
+        Matcher m;
+
+        m = YYYY_MM_DD.matcher(s);
+
+        if (m.matches()) {
+            Calendar cal = Calendar.getInstance();
+            String year  = m.group(1);
+            String month = m.group(2);
+            String day   = m.group(3);
+            cal.set(
+                Integer.parseInt(year),
+                calendarMonth(month),
+                Integer.parseInt(day),
+                12, 0, 0);
+            return cal.getTime();
+        }
+
+        m = DD_MM_YYYY.matcher(s);
+
+        if (m.matches()) {
+            Calendar cal = Calendar.getInstance();
+            String year  = m.group(3);
+            String month = m.group(2);
+            String day   = m.group(1);
+            cal.set(
+                Integer.parseInt(year) + (year.length() == 2 ? 1900 : 0),
+                calendarMonth(month),
+                Integer.parseInt(m.group(1)),
+                12, 0, 0);
+            return cal.getTime();
+        }
+
+        m = MMM_YYYY.matcher(s);
+
+        if (m.matches()) {
+            int month = guessMonth(m.group(2));
+            if (month >= 0) {
+                Calendar cal = Calendar.getInstance();
+                String year = m.group(3);
+                String day  = m.group(1);
+                cal.set(
+                    Integer.parseInt(year) + (year.length() == 2 ? 1900 : 0),
+                    month,
+                    day.length() == 0 ? 15 : Integer.parseInt(day),
+                    12, 0, 0);
+                return cal.getTime();
+            }
+        }
+
+        m = YYYY_MM_DDThh_mm.matcher(s);
+
+        if (m.matches()) {
+            Calendar cal = Calendar.getInstance();
+            String year = m.group(1);
+            String month = m.group(2);
+            String day = m.group(3);
+            String hour = m.group(4);
+            String minute = m.group(5);
+            cal.set(
+                Integer.parseInt(year),
+                calendarMonth(month),
+                Integer.parseInt(day),
+                Integer.parseInt(hour),
+                Integer.parseInt(minute),
+                0
+            );
+            return cal.getTime();
+        }
+
+        m = YYYY_MM_DDThh_mm_ss.matcher(s);
+
+        if (m.matches()) {
+            Calendar cal = Calendar.getInstance();
+            String year = m.group(1);
+            String month = m.group(2);
+            String day = m.group(3);
+            String hour = m.group(4);
+            String minute = m.group(5);
+            String second = m.group(6);
+            cal.set(
+                Integer.parseInt(year),
+                calendarMonth(month),
+                Integer.parseInt(day),
+                Integer.parseInt(hour),
+                Integer.parseInt(minute),
+                Integer.parseInt(second)
+            );
+            return cal.getTime();
+        }
+
+        m = DD_MM_YYYYThh_mm.matcher(s);
+
+        if (m.matches()) {
+            Calendar cal = Calendar.getInstance();
+            String year = m.group(3);
+            String month = m.group(2);
+            String day = m.group(1);
+            String hour = m.group(4);
+            String minute = m.group(5);
+            cal.set(
+                Integer.parseInt(year) + (year.length() == 2 ? 1900 : 0),
+                calendarMonth(month),
+                Integer.parseInt(day),
+                Integer.parseInt(hour),
+                Integer.parseInt(minute),
+                0
+            );
+            return cal.getTime();
+        }
+
+        m = DD_MM_YYYYThh_mm_ss.matcher(s);
+
+        if (m.matches()) {
+            Calendar cal = Calendar.getInstance();
+            String year = m.group(3);
+            String month = m.group(2);
+            String day = m.group(1);
+            String hour = m.group(4);
+            String minute = m.group(5);
+            String second = m.group(6);
+            cal.set(
+                Integer.parseInt(year) + (year.length() == 2 ? 1900 : 0),
+                calendarMonth(month),
+                Integer.parseInt(day),
+                Integer.parseInt(hour),
+                Integer.parseInt(minute),
+                Integer.parseInt(second)
+            );
+            return cal.getTime();
+        }
+
+        m = GARBAGE_YYYY.matcher(s);
+
+        if (m.matches()) {
+            Calendar cal = Calendar.getInstance();
+            String year = m.group(1);
+            cal.set(
+                Integer.parseInt(year) + (year.length() == 2 ? 1900 : 0),
+                5,  // month
+                15, // day
+                12, 0, 0);
+            return cal.getTime();
+        }
+
+        throw new IllegalArgumentException();
+    }
+
+    public static void main(String [] args) {
+        for (int i = 0; i < args.length; ++i) {
+            System.out.println(args[i] + ": " + guessDate(args[i]));
+        }
+    }
+}
+// end of file

http://dive4elements.wald.intevation.org