view flys-aft/src/main/java/de/intevation/aft/SyncContext.java @ 4085:067341e86375

Separate FLYS gauges to be updated or to create. flys-aft/trunk@3523 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 22 Dec 2011 12:07:29 +0000
parents 9178beeb7b05
children 859b4781554a
line wrap: on
line source
package de.intevation.aft;

import java.util.Map;
import java.util.HashMap;

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;

    public SyncContext() {
    }

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

    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;
    }

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

http://dive4elements.wald.intevation.org