view flys-backend/src/main/java/de/intevation/flys/importer/Importer.java @ 4198:1cdbd8a0c994

Added two new tables ClickableQDTable and ClickableWTable and made Ws and Qs clickable in historical discharge calculation. The new tables define listener interfaces (clicked lower or upper icon) to listen to user clicks. In addition to this, there is an enum ClickMode with NONE, SINGLE and RANGE options, which allows to specifiy, which icons are displayed in the tables. NONE means no icon for user clicks, SINGLE has 1 icon, RANGE 2 icons for lower and upper.
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 22 Oct 2012 13:31:25 +0200
parents 36edf9a71cbd
children 2b0426b79a92
line wrap: on
line source
package de.intevation.flys.importer;

import de.intevation.artifacts.common.utils.XMLUtils;

import de.intevation.flys.importer.parsers.InfoGewParser;
import de.intevation.flys.importer.parsers.AnnotationClassifier;

import java.io.File;
import java.io.IOException;

import java.util.List;

import java.sql.SQLException;

import org.apache.log4j.Logger;

import org.hibernate.Transaction;
import org.hibernate.HibernateException;

import org.w3c.dom.Document;

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

    protected List<ImportRiver> rivers;

    public Importer() {
    }

    public Importer(List<ImportRiver> rivers) {
        this.rivers = rivers;
    }

    public List<ImportRiver> getRivers() {
        return rivers;
    }

    public void setRivers(List<ImportRiver> rivers) {
        this.rivers = rivers;
    }

    public void writeRivers() {
        log.debug("write rivers started");

        for (ImportRiver river: rivers) {
            log.debug("writing river '" + river.getName() + "'");
            river.storeDependencies();
            ImporterSession.getInstance().getDatabaseSession().flush();
        }

        log.debug("write rivers finished");
    }

    public void writeToDatabase() {

        Transaction tx = null;

        try {
            tx = ImporterSession.getInstance()
                .getDatabaseSession().beginTransaction();

            try {
                writeRivers();
            }
            catch (HibernateException he) {
                Throwable t = he.getCause();
                while (t instanceof SQLException) {
                    SQLException sqle = (SQLException)t;
                    log.error("SQL exeception chain:", sqle);
                    t = sqle.getNextException();
                }
                throw he;
            }

            tx.commit();
        }
        catch (RuntimeException re) {
            if (tx != null) {
                tx.rollback();
            }
            throw re;
        }
    }

    public static AnnotationClassifier getAnnotationClassifier() {
        String annotationTypes = Config.INSTANCE.getAnnotationTypes();

        if (annotationTypes == null) {
            log.info("no annotation types file configured.");
            return null;
        }

        File file = new File(annotationTypes);

        log.info("use annotation types file '" + file + "'");

        if (!(file.isFile() && file.canRead())) {
            log.warn("annotation type file '" + file + "' is not readable.");
            return null;
        }

        Document rules = XMLUtils.parseDocument(file);

        if (rules == null) {
            log.warn("cannot parse annotation types file.");
            return null;
        }

        return new AnnotationClassifier(rules);
    }

    public static void main(String [] args) {

        InfoGewParser infoGewParser = new InfoGewParser(
            getAnnotationClassifier());

        log.info("Start parsing rivers...");

        for (String gew: args) {
            log.info("parsing info gew file: " + gew);
            try {
                infoGewParser.parse(new File(gew));
            }
            catch (IOException ioe) {
                log.error("error while parsing gew: " + gew);
            }
        }

        String gew = Config.INSTANCE.getInfoGewFile();
        if (gew != null && gew.length() > 0) {
            log.info("parsing info gew file: " + gew);
            try {
                infoGewParser.parse(new File(gew));
            }
            catch (IOException ioe) {
                log.error("error while parsing gew: " + gew);
            }
        }

        if (!Config.INSTANCE.dryRun()) {
            new Importer(infoGewParser.getRivers()).writeToDatabase();
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org