view flys-aft/src/main/java/de/intevation/aft/River.java @ 4081:7a7b0f0cb653

Check if AFT gauges are in DIPS, too. flys-aft/trunk@3456 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 16 Dec 2011 19:25:31 +0000
parents 42094f01afa6
children d13011e53022
line wrap: on
line source
package de.intevation.aft;

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

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

import org.apache.log4j.Logger;

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

import de.intevation.db.ConnectedStatements;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class River
extends      IdPair
{
    private static Logger log = Logger.getLogger(River.class);

    public static final Pattern NUMBER = Pattern.compile("0*([0-9]+)");

    protected String name;

    public River() {
    }

    public River(int id1, int id2, String name) {
        super(id1, id2);
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public static Long numberToLong(String s) {
        Matcher m = NUMBER.matcher(s);
        if (!m.matches()) return null;
        String ns = m.group(1);
        return Long.valueOf(ns.length() == 0 ? "0": ns);
    }

    public 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);
            Long number = numberToLong(element.getAttribute("NUMMER"));
            if (number != null) {
                map.put(number, new DIPSGauge(element));
            }
        }
        return map;
    }

    public void sync(SyncContext context) throws SQLException {
        log.info("sync river: " + this);

        Map<Long, DIPSGauge> dipsGauges = indexByNumber(context.getDips());

        ConnectedStatements flysStatements = context.getFlysStatements();
        ConnectedStatements aftStatements  = context.getAftStatements();

        ResultSet messstellenRs = aftStatements
            .getStatement("select.messstelle")
            .clearParameters()
            .setInt("GEWAESSER_NR", id2).executeQuery();

        while (messstellenRs.next()) {
            String name = messstellenRs.getString("NAME");
            String num  = messstellenRs.getString("MESSSTELLE_NR");
            Long number = numberToLong(num);
            if (number == null) {
                log.warn("Invalid MESSSTELLE_NR for MESSSTELLE '"+name+"'");
                continue;
            }
            DIPSGauge dipsGauge = dipsGauges.get(number);
            if (dipsGauges == null) {
                log.warn("MESSSTELLE '" + name + "' not found in DIPS.");
                continue;
            }
        }

        messstellenRs.close();

        ResultSet gaugesRs = flysStatements
            .getStatement("select.gauges")
            .clearParameters()
            .setInt("river_id", id1).executeQuery();

        while (gaugesRs.next()) {
            int gaugeId = gaugesRs.getInt("id");
            String name = gaugesRs.getString("name");
            System.err.println(name + ": " + gaugeId);
        }

        gaugesRs.close();

    }

    public String toString() {
        return "[River: name=" + name + ", " + super.toString() + "]";
    }

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

http://dive4elements.wald.intevation.org