changeset 4089:859b4781554a

Prefetch existing time intervals from FLYS db. flys-aft/trunk@3574 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 03 Jan 2012 12:25:06 +0000
parents 52cde7fe742a
children d556e29592f5
files flys-aft/ChangeLog flys-aft/src/main/java/de/intevation/aft/Sync.java flys-aft/src/main/java/de/intevation/aft/SyncContext.java flys-aft/src/main/java/de/intevation/aft/TimeInterval.java flys-aft/src/main/resources/sql/flys-common.properties
diffstat 5 files changed, 130 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/flys-aft/ChangeLog	Tue Jan 03 11:36:07 2012 +0000
+++ b/flys-aft/ChangeLog	Tue Jan 03 12:25:06 2012 +0000
@@ -1,3 +1,18 @@
+2012-01-03	Sascha L. Teichmann	<sascha.teichmann@inteavtion.de>
+
+	* src/main/java/de/intevation/aft/TimeInterval.java: New.
+	  Model for FLYS time intervls.
+
+	* src/main/java/de/intevation/aft/SyncContext.java: Preload
+	  existing time intervals from FLYS.
+
+	* src/main/java/de/intevation/aft/Sync.java: Call init()
+	  after construction to ensure that the db connections are
+	  closed properly.
+
+	* src/main/resources/sql/flys-common.properties: Added statement
+	  to fetch the time intervals from FLYS.
+
 2012-01-03	Sascha L. Teichmann	<sascha.teichmann@inteavtion.de>
 
 	* src/main/java/de/intevation/aft/River.java: Fetch discharge table
--- a/flys-aft/src/main/java/de/intevation/aft/Sync.java	Tue Jan 03 11:36:07 2012 +0000
+++ b/flys-aft/src/main/java/de/intevation/aft/Sync.java	Tue Jan 03 12:25:06 2012 +0000
@@ -97,6 +97,7 @@
                 aftConnectionBuilder.getConnectedStatements(),
                 flysConnectionBuilder.getConnectedStatements(),
                 dips);
+            syncContext.init();
             Rivers rivers = new Rivers();
             rivers.sync(syncContext);
         }
--- a/flys-aft/src/main/java/de/intevation/aft/SyncContext.java	Tue Jan 03 11:36:07 2012 +0000
+++ b/flys-aft/src/main/java/de/intevation/aft/SyncContext.java	Tue Jan 03 12:25:06 2012 +0000
@@ -1,7 +1,13 @@
 package de.intevation.aft;
 
 import java.util.Map;
+import java.util.Date;
 import java.util.HashMap;
+import java.util.Set;
+import java.util.TreeSet;
+
+import java.sql.SQLException;
+import java.sql.ResultSet;
 
 import org.w3c.dom.Document;
 
@@ -20,6 +26,7 @@
     protected ConnectedStatements  flysStatements;
     protected Document             dips;
     protected Map<Long, DIPSGauge> numberToGauge;
+    protected Set<TimeInterval>    flysTimeIntervals;
 
     public SyncContext() {
     }
@@ -32,7 +39,11 @@
         this.aftStatements  = aftStatements;
         this.flysStatements = flysStatements;
         this.dips           = dips;
+    }
+
+    public void init() throws SQLException {
         numberToGauge       = indexByNumber(dips);
+        flysTimeIntervals   = loadTimeIntervals();
     }
 
     public ConnectedStatements getAftStatements() {
@@ -101,6 +112,46 @@
         return map;
     }
 
+    protected Set<TimeInterval> loadTimeIntervals() throws SQLException {
+
+        boolean debug = log.isDebugEnabled();
+
+        Set<TimeInterval> intervals = new TreeSet<TimeInterval>();
+        ResultSet rs = null;
+        
+        try {
+            rs = flysStatements
+                .getStatement("select.timeintervals")
+                .executeQuery();
+
+            while (rs.next()) {
+                int  id    = rs.getInt("id");
+                Date start = rs.getDate("start_time");
+                Date stop  = rs.getDate("stop_time");
+
+                if (debug) {
+                    log.debug("id:    " + id);
+                    log.debug("start: " + start);
+                    log.debug("stop:  " + stop);
+                }
+
+                TimeInterval ti = new TimeInterval(id, start, stop);
+                intervals.add(ti);
+            }
+        }
+        finally {
+            if (rs != null) {
+                rs.close();
+            }
+        }
+
+        if (debug) {
+            log.debug("loaded time intervals: " + intervals.size());
+        }
+
+        return intervals;
+    }
+
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-aft/src/main/java/de/intevation/aft/TimeInterval.java	Tue Jan 03 12:25:06 2012 +0000
@@ -0,0 +1,62 @@
+package de.intevation.aft;
+
+import java.util.Date;
+
+public class TimeInterval
+implements   Comparable<TimeInterval>
+{
+    protected int  id;
+    protected Date start;
+    protected Date stop;
+
+    public TimeInterval() {
+    }
+
+    public TimeInterval(int id, Date start, Date stop) {
+        this.id    = id;
+        this.start = start;
+        this.stop  = stop;
+    }
+
+    protected static int compare(Date d1, Date d2) {
+        long s1 = d1 != null ? d1.getTime()/1000L : 0L;
+        long s2 = d2 != null ? d2.getTime()/1000L : 0L;
+        long diff = s1 - s2;
+        return diff < 0L 
+            ? -1
+            : diff > 0L ? 1 : 0;
+    }
+
+    @Override
+    public int compareTo(TimeInterval other) {
+        int cmp = compare(start, other.start);
+        return cmp != 0 
+            ? cmp
+            : compare(stop, other.stop);
+    }
+
+    public int getId() {
+        return id;
+    }
+
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    public Date getStart() {
+        return start;
+    }
+
+    public void setStart(Date start) {
+        this.start = start;
+    }
+
+    public Date getStop() {
+        return stop;
+    }
+
+    public void setStop(Date stop) {
+        this.stop = stop;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-aft/src/main/resources/sql/flys-common.properties	Tue Jan 03 11:36:07 2012 +0000
+++ b/flys-aft/src/main/resources/sql/flys-common.properties	Tue Jan 03 12:25:06 2012 +0000
@@ -3,3 +3,4 @@
 next.gauge.id = SELECT NEXTVAL('GAUGES_ID_SEQ') AS gauge_id
 insert.gauge = INSERT INTO gauges (id, name, river_id, station, aeo, official_number, datum) \
                       VALUES(:id, :name, :river_id, :station, :aeo, :official_number, :datum)
+select.timeintervals = SELECT id, start_time, stop_time FROM time_intervals

http://dive4elements.wald.intevation.org