sascha@4077: package de.intevation.aft; sascha@4077: sascha@4084: import java.util.Map; sascha@4089: import java.util.Date; sascha@4084: import java.util.HashMap; sascha@4090: import java.util.TreeMap; sascha@4089: sascha@4089: import java.sql.SQLException; sascha@4089: import java.sql.ResultSet; sascha@4084: sascha@4084: import org.w3c.dom.Document; sascha@4084: sascha@4077: import de.intevation.db.ConnectedStatements; sascha@4077: sascha@4084: import org.w3c.dom.NodeList; sascha@4084: import org.w3c.dom.Element; sascha@4084: sascha@4084: import org.apache.log4j.Logger; sascha@4077: sascha@4077: public class SyncContext sascha@4077: { sascha@4084: private static Logger log = Logger.getLogger(SyncContext.class); sascha@4084: sascha@4090: protected ConnectedStatements aftStatements; sascha@4090: protected ConnectedStatements flysStatements; sascha@4090: protected Document dips; sascha@4090: sascha@4090: protected Map numberToGauge; sascha@4090: protected Map flysTimeIntervals; sascha@4077: sascha@4077: public SyncContext() { sascha@4077: } sascha@4077: sascha@4077: public SyncContext( sascha@4077: ConnectedStatements aftStatements, sascha@4077: ConnectedStatements flysStatements, sascha@4077: Document dips sascha@4077: ) { sascha@4077: this.aftStatements = aftStatements; sascha@4077: this.flysStatements = flysStatements; sascha@4077: this.dips = dips; sascha@4089: } sascha@4089: sascha@4089: public void init() throws SQLException { sascha@4084: numberToGauge = indexByNumber(dips); sascha@4089: flysTimeIntervals = loadTimeIntervals(); sascha@4077: } sascha@4077: sascha@4077: public ConnectedStatements getAftStatements() { sascha@4077: return aftStatements; sascha@4077: } sascha@4077: sascha@4077: public void setAftStatements(ConnectedStatements aftStatements) { sascha@4077: this.aftStatements = aftStatements; sascha@4077: } sascha@4077: sascha@4077: public ConnectedStatements getFlysStatements() { sascha@4077: return flysStatements; sascha@4077: } sascha@4077: sascha@4077: public void setFlysStatements(ConnectedStatements flysStatements) { sascha@4077: this.flysStatements = flysStatements; sascha@4077: } sascha@4077: sascha@4077: public Document getDips() { sascha@4077: return dips; sascha@4077: } sascha@4077: sascha@4077: public void setDips(Document dips) { sascha@4077: this.dips = dips; sascha@4077: } sascha@4077: sascha@4077: void close() { sascha@4077: aftStatements.close(); sascha@4077: flysStatements.close(); sascha@4077: } sascha@4084: sascha@4084: public static Long numberToLong(String s) { sascha@4084: try { sascha@4084: return Long.valueOf(s.trim()); sascha@4084: } sascha@4084: catch (NumberFormatException nfe) { sascha@4084: } sascha@4084: return null; sascha@4084: } sascha@4084: sascha@4084: public Map getDIPSGauges() { sascha@4084: return numberToGauge; sascha@4084: } sascha@4084: sascha@4084: protected static Map indexByNumber(Document document) { sascha@4084: Map map = new HashMap(); sascha@4084: NodeList nodes = document.getElementsByTagName("PEGELSTATION"); sascha@4084: for (int i = nodes.getLength()-1; i >= 0; --i) { sascha@4084: Element element = (Element)nodes.item(i); sascha@4084: String numberString = element.getAttribute("NUMMER"); sascha@4084: Long number = numberToLong(numberString); sascha@4084: if (number != null) { sascha@4084: DIPSGauge newG = new DIPSGauge(element); sascha@4084: DIPSGauge oldG = map.put(number, newG); sascha@4084: if (oldG != null) { sascha@4084: log.warn("DIPS: '" + newG.getName() + sascha@4084: "' collides with '" + oldG.getName() + sascha@4084: "' on gauge number " + number + "."); sascha@4084: } sascha@4084: } sascha@4084: else { sascha@4110: log.warn("DIPS: Gauge '" + element.getAttribute("NAME") + sascha@4110: "' has invalid gauge number '" + numberString + "'."); sascha@4084: } sascha@4084: } sascha@4084: return map; sascha@4084: } sascha@4084: sascha@4090: protected Map loadTimeIntervals() sascha@4090: throws SQLException { sascha@4089: sascha@4089: boolean debug = log.isDebugEnabled(); sascha@4089: sascha@4090: Map intervals = sascha@4090: new TreeMap(); sascha@4090: sascha@4089: ResultSet rs = null; sascha@4089: sascha@4089: try { sascha@4089: rs = flysStatements sascha@4089: .getStatement("select.timeintervals") sascha@4089: .executeQuery(); sascha@4089: sascha@4089: while (rs.next()) { sascha@4089: int id = rs.getInt("id"); sascha@4089: Date start = rs.getDate("start_time"); sascha@4089: Date stop = rs.getDate("stop_time"); sascha@4089: sascha@4089: if (debug) { sascha@4089: log.debug("id: " + id); sascha@4089: log.debug("start: " + start); sascha@4089: log.debug("stop: " + stop); sascha@4089: } sascha@4089: sascha@4089: TimeInterval ti = new TimeInterval(id, start, stop); sascha@4090: intervals.put(ti, ti); sascha@4089: } sascha@4089: } sascha@4089: finally { sascha@4089: if (rs != null) { sascha@4089: rs.close(); sascha@4089: } sascha@4089: } sascha@4089: sascha@4089: if (debug) { sascha@4089: log.debug("loaded time intervals: " + intervals.size()); sascha@4089: } sascha@4089: sascha@4089: return intervals; sascha@4089: } sascha@4089: sascha@4090: public TimeInterval fetchOrCreateFLYSTimeInterval(TimeInterval key) sascha@4090: throws SQLException sascha@4090: { sascha@4090: TimeInterval old = flysTimeIntervals.get(key); sascha@4090: if (old != null) { sascha@4090: return old; sascha@4090: } sascha@4090: sascha@4090: ResultSet rs = null; sascha@4090: try { sascha@4090: rs = flysStatements.getStatement("next.timeinterval.id") sascha@4090: .executeQuery(); sascha@4090: rs.next(); sascha@4090: key.setId(rs.getInt("time_interval_id")); sascha@4090: rs.close(); rs = null; sascha@4090: sascha@4101: if (log.isDebugEnabled()) { sascha@4101: log.debug("FLYS: Created time interval id: " + key.getId()); sascha@4101: log.debug("FLYS: " + key); sascha@4101: } sascha@4101: sascha@4090: flysStatements.getStatement("insert.timeinterval") sascha@4090: .clearParameters() sascha@4090: .setInt("id", key.getId()) sascha@4090: .setObject("start_time", key.getStart()) sascha@4090: .setObject("stop_time", key.getStop()) sascha@4090: .execute(); sascha@4090: } sascha@4090: finally { sascha@4090: if (rs != null) { sascha@4090: rs.close(); sascha@4090: } sascha@4090: } sascha@4090: sascha@4090: flysTimeIntervals.put(key, key); sascha@4090: sascha@4090: return key; sascha@4090: } sascha@4090: sascha@4077: } sascha@4077: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : sascha@4077: