[Dive4elements-commits] [PATCH] Backend: Parse time intervals for main values

Wald Commits scm-commit at wald.intevation.org
Tue Apr 16 18:07:10 CEST 2013


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1366128425 -7200
# Node ID c75be5205a690aaaa01c145790d78c5a9dfd156e
# Parent  06adfba17091915979ecaa84302f326e85e3c05c
Backend: Parse time intervals for main values.

diff -r 06adfba17091 -r c75be5205a69 flys-backend/src/main/java/de/intevation/flys/importer/ImportMainValue.java
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportMainValue.java	Tue Apr 16 17:33:55 2013 +0200
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportMainValue.java	Tue Apr 16 18:07:05 2013 +0200
@@ -8,6 +8,7 @@
 import de.intevation.flys.model.Gauge;
 import de.intevation.flys.model.River;
 import de.intevation.flys.model.NamedMainValue;
+import de.intevation.flys.model.TimeInterval;
 
 import org.hibernate.Session;
 import org.hibernate.Query;
@@ -17,6 +18,7 @@
     protected ImportGauge          gauge;
     protected ImportNamedMainValue mainValue;
     protected BigDecimal           value;
+    protected ImportTimeInterval   timeInterval;
 
     protected MainValue peer;
 
@@ -26,11 +28,13 @@
     public ImportMainValue(
         ImportGauge          gauge,
         ImportNamedMainValue mainValue,
-        BigDecimal           value
+        BigDecimal           value,
+        ImportTimeInterval   timeInterval
     ) {
-        this.gauge     = gauge;
-        this.mainValue = mainValue;
-        this.value     = value;
+        this.gauge        = gauge;
+        this.mainValue    = mainValue;
+        this.value        = value;
+        this.timeInterval = timeInterval;
     }
 
     public ImportGauge getGauge() {
@@ -62,12 +66,17 @@
             Session session = ImporterSession.getInstance().getDatabaseSession();
             Query query = session.createQuery("from MainValue where "
                 + "gauge.id=:gauge_id and mainValue.id=:name_id "
+                + "and timeInterval = :time "
                 + "and value=:value");
             Gauge          g = gauge.getPeer(river);
             NamedMainValue n = mainValue.getPeer();
+            TimeInterval   t = timeInterval != null
+                ? timeInterval.getPeer()
+                : null;
             query.setParameter("gauge_id", g.getId());
             query.setParameter("name_id",  n.getId());
             query.setParameter("value",    value);
+            query.setParameter("time",     t);
             List<MainValue> values = query.list();
             if (values.isEmpty()) {
                 peer = new MainValue(g, n, value, null);
diff -r 06adfba17091 -r c75be5205a69 flys-backend/src/main/java/de/intevation/flys/importer/parsers/StaFileParser.java
--- a/flys-backend/src/main/java/de/intevation/flys/importer/parsers/StaFileParser.java	Tue Apr 16 17:33:55 2013 +0200
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/parsers/StaFileParser.java	Tue Apr 16 18:07:05 2013 +0200
@@ -11,8 +11,10 @@
 import java.util.regex.Pattern;
 import java.util.regex.Matcher;
 
+import java.util.Date;
 import java.util.HashMap;
 import java.util.ArrayList;
+import java.util.List;
 
 import org.apache.log4j.Logger;
 
@@ -20,6 +22,8 @@
 import de.intevation.flys.importer.ImportMainValue;
 import de.intevation.flys.importer.ImportNamedMainValue;
 import de.intevation.flys.importer.ImportGauge;
+import de.intevation.flys.importer.ImportTimeInterval;
+import de.intevation.flys.utils.DateGuesser;
 
 public class StaFileParser
 {
@@ -37,6 +41,28 @@
         Pattern.compile("\\s*([^\\s]+)\\s+([^\\s]+)\\s+([" +
             Pattern.quote(TYPES) + "]).*");
 
+    public static final class NameAndTimeInterval {
+        private String             name;
+        private ImportTimeInterval timeInterval;
+
+        public NameAndTimeInterval(String name) {
+            this(name, null);
+        }
+
+        public NameAndTimeInterval(String name, ImportTimeInterval timeInterval) {
+            this.name         = name;
+            this.timeInterval = timeInterval;
+        }
+
+        public String getName() {
+            return name;
+        }
+
+        public ImportTimeInterval getTimeInterval() {
+            return timeInterval;
+        }
+    } // class NameAndTimeInterval
+
     public StaFileParser() {
     }
 
@@ -160,12 +186,16 @@
                         types.put(typeString, type);
                     }
                     String name = m.group(1);
+                    NameAndTimeInterval nat = parseName(name);
                     ImportNamedMainValue namedMainValue =
-                        new ImportNamedMainValue(type, name);
+                        new ImportNamedMainValue(type, nat.getName());
                     namedMainValues.add(namedMainValue);
 
-                    ImportMainValue mainValue =
-                        new ImportMainValue(gauge, namedMainValue, value);
+                    ImportMainValue mainValue = new ImportMainValue(
+                        gauge,
+                        namedMainValue,
+                        value,
+                        nat.getTimeInterval());
 
                     mainValues.add(mainValue);
                 }
@@ -186,5 +216,120 @@
         log.info("finished parsing STA file: " + file);
         return true;
     }
+
+    protected NameAndTimeInterval parseName(String name) {
+        List<String> result = new ArrayList<String>();
+
+        unbracket(name, 0, result);
+
+        int length = result.size();
+
+        if (length < 1) { // Should not happen.
+            return new NameAndTimeInterval(name);
+        }
+
+        if (length == 1) { // No date at all -> use first part.
+            return new NameAndTimeInterval(result.get(0).trim());
+        }
+
+        if (length == 2) { // e.g. W(1994) or W(1994 - 1999)
+            String type = result.get(0).trim();
+
+            ImportTimeInterval timeInterval = getTimeInterval(
+                result.get(1).trim());
+
+            if (timeInterval == null) { // No date at all.
+                type = name;
+            }
+
+            return new NameAndTimeInterval(type, timeInterval);
+        }
+
+        if (length == 3) { // e.g W(Q(1994)) or W(Q(1994 - 1999))
+
+            String type =
+                result.get(0).trim() + "(" + 
+                result.get(1).trim() + ")";
+
+            ImportTimeInterval timeInterval = getTimeInterval(
+                result.get(2).trim());
+
+            if (timeInterval == null) { // No date at all.
+                type = name;
+            }
+
+            return new NameAndTimeInterval(type, timeInterval);
+        }
+
+        return null;
+    }
+
+    private static ImportTimeInterval getTimeInterval(String datePart) {
+
+        int minus = datePart.indexOf('-');
+
+        if (minus < 0) { // '-' not found
+
+            Date date = null;
+            try {
+                date = DateGuesser.guessDate(datePart);
+            }
+            catch (IllegalArgumentException iae) {
+                log.warn("STA: Invalid date '" + datePart + "'");
+                return null;
+            }
+
+            return new ImportTimeInterval(date);
+        }
+
+        // Found '-' so we have <from> - <to>
+        String startPart = datePart.substring(0, minus).trim();
+        String endPart   = datePart.substring(minus).trim();
+
+        Date startDate = null;
+        Date endDate   = null;
+
+        try {
+            startDate = DateGuesser.guessDate(startPart);
+        }
+        catch (IllegalArgumentException iae) {
+            log.warn("STA: Invalid start date '" + startPart + "'");
+        }
+
+        try {
+            endDate = DateGuesser.guessDate(endPart);
+        }
+        catch (IllegalArgumentException iae) {
+            log.warn("STA: Invalid end date '" + endPart + "'");
+        }
+
+        if (startDate == null) {
+            log.warn("STA: Need start date.");
+            return null;
+        }
+
+        return new ImportTimeInterval(startDate, endDate);
+    }
+
+    private static int unbracket(String s, int index, List<String> result) {
+        StringBuilder sb = new StringBuilder();
+        int length = s.length();
+        while (index < length) {
+            char c = s.charAt(index);
+            switch (c) {
+                case '(':
+                    index = unbracket(s, index, result);
+                    break;
+                case ')':
+                    result.add(0, sb.toString());
+                    return index+1;
+                default:
+                    sb.append(c);
+            }
+        }
+        result.add(0, sb.toString());
+
+        return index;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :


More information about the Dive4elements-commits mailing list