view flys-aft/src/main/java/de/intevation/aft/SyncContext.java @ 4545:bb8b1c182913

New doOut for total load in sediment load calculation.
author Raimund Renkert <rrenkert@intevation.de>
date Thu, 15 Nov 2012 17:38:48 +0100
parents 06891562e633
children b195fede1c3b
line wrap: on
line source
package de.intevation.aft;

import java.util.Map;
import java.util.Date;
import java.util.HashMap;
import java.util.TreeMap;

import java.sql.SQLException;
import java.sql.ResultSet;

import org.w3c.dom.Document;

import de.intevation.db.ConnectedStatements;

import org.w3c.dom.NodeList;
import org.w3c.dom.Element;

import org.apache.log4j.Logger;

public class SyncContext
{
    private static Logger log = Logger.getLogger(SyncContext.class);

    protected ConnectedStatements             aftStatements;
    protected ConnectedStatements             flysStatements;
    protected Document                        dips;

    protected Map<Long, DIPSGauge>            numberToGauge;
    protected Map<TimeInterval, TimeInterval> flysTimeIntervals;

    public SyncContext() {
    }

    public SyncContext(
        ConnectedStatements aftStatements,
        ConnectedStatements flysStatements,
        Document            dips
    ) {
        this.aftStatements  = aftStatements;
        this.flysStatements = flysStatements;
        this.dips           = dips;
    }

    public void init() throws SQLException {
        numberToGauge       = indexByNumber(dips);
        flysTimeIntervals   = loadTimeIntervals();
    }

    public ConnectedStatements getAftStatements() {
        return aftStatements;
    }

    public void setAftStatements(ConnectedStatements aftStatements) {
        this.aftStatements = aftStatements;
    }

    public ConnectedStatements getFlysStatements() {
        return flysStatements;
    }

    public void setFlysStatements(ConnectedStatements flysStatements) {
        this.flysStatements = flysStatements;
    }

    public Document getDips() {
        return dips;
    }

    public void setDips(Document dips) {
        this.dips = dips;
    }

    void close() {
        aftStatements.close();
        flysStatements.close();
    }

    public static Long numberToLong(String s) {
        try {
            return Long.valueOf(s.trim());
        }
        catch (NumberFormatException nfe) {
        }
        return null;
    }

    public Map<Long, DIPSGauge> getDIPSGauges() {
        return numberToGauge;
    }

    protected static Map<Long, DIPSGauge> indexByNumber(Document document) {
        Map<Long, DIPSGauge> map = new HashMap<Long, DIPSGauge>();
        NodeList nodes = document.getElementsByTagName("PEGELSTATION");
        for (int i = nodes.getLength()-1; i >= 0; --i) {
            Element element = (Element)nodes.item(i);
            String numberString = element.getAttribute("NUMMER");
            Long number = numberToLong(numberString);
            if (number != null) {
                DIPSGauge newG = new DIPSGauge(element);
                DIPSGauge oldG = map.put(number, newG);
                if (oldG != null) {
                    log.warn("DIPS: '" + newG.getName() +
                        "' collides with '" + oldG.getName() + 
                        "' on gauge number " + number + ".");
                }
            }
            else {
                log.warn("DIPS: Gauge '" + element.getAttribute("NAME") +
                    "' has invalid gauge number '" + numberString + "'.");
            }
        }
        return map;
    }

    protected Map<TimeInterval, TimeInterval> loadTimeIntervals() 
    throws SQLException {

        boolean debug = log.isDebugEnabled();

        Map<TimeInterval, TimeInterval> intervals =
            new TreeMap<TimeInterval, 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.put(ti, ti);
            }
        }
        finally {
            if (rs != null) {
                rs.close();
            }
        }

        if (debug) {
            log.debug("loaded time intervals: " + intervals.size());
        }

        return intervals;
    }

    public TimeInterval fetchOrCreateFLYSTimeInterval(TimeInterval key)
    throws SQLException
    {
        TimeInterval old = flysTimeIntervals.get(key);
        if (old != null) {
            return old;
        }

        ResultSet rs = null;
        try {
            rs = flysStatements.getStatement("next.timeinterval.id")
                .executeQuery();
            rs.next();
            key.setId(rs.getInt("time_interval_id"));
            rs.close(); rs = null;

            if (log.isDebugEnabled()) {
                log.debug("FLYS: Created time interval id: " + key.getId());
                log.debug("FLYS: " + key);
            }

            flysStatements.getStatement("insert.timeinterval")
                .clearParameters()
                .setInt("id", key.getId())
                .setObject("start_time", key.getStart())
                .setObject("stop_time", key.getStop())
                .execute();
        }
        finally {
            if (rs != null) {
                rs.close();
            }
        }

        flysTimeIntervals.put(key, key);

        return key;
    }

}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org