# HG changeset patch # User Sascha L. Teichmann # Date 1325593506 0 # Node ID 859b4781554a04f84e2f3374391384aec12a188d # Parent 52cde7fe742addc94e7f99138ab62f970dd3bf5a Prefetch existing time intervals from FLYS db. flys-aft/trunk@3574 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 52cde7fe742a -r 859b4781554a flys-aft/ChangeLog --- 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 + + * 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 * src/main/java/de/intevation/aft/River.java: Fetch discharge table diff -r 52cde7fe742a -r 859b4781554a flys-aft/src/main/java/de/intevation/aft/Sync.java --- 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); } diff -r 52cde7fe742a -r 859b4781554a flys-aft/src/main/java/de/intevation/aft/SyncContext.java --- 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 numberToGauge; + protected Set 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 loadTimeIntervals() throws SQLException { + + boolean debug = log.isDebugEnabled(); + + Set intervals = new TreeSet(); + 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 : diff -r 52cde7fe742a -r 859b4781554a flys-aft/src/main/java/de/intevation/aft/TimeInterval.java --- /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 +{ + 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 : diff -r 52cde7fe742a -r 859b4781554a flys-aft/src/main/resources/sql/flys-common.properties --- 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