view flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java @ 201:3169b559ca3c

Build models for wsts, wst columns and q ranges and store them in the backend. flys-backend/trunk@1549 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 23 Mar 2011 15:22:32 +0000
parents c0dcc2357106
children 3a99d0295006
line wrap: on
line source
package de.intevation.flys.importer;

import java.util.List;

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

import org.apache.log4j.Logger;

import de.intevation.flys.model.River;

import de.intevation.flys.utils.FileTools;

import org.hibernate.Session;
import org.hibernate.Query;

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

    public static final String PEGEL_GLT = "PEGEL.GLT";

    protected String name;

    protected File   wstFile;

    protected File   bbInfoFile;

    protected List<ImportGauge> gauges;

    protected List<ImportAnnotation> annotations;

    protected ImportWst wst;

    protected River peer;

    public ImportRiver() {
    }

    public ImportRiver(String name, File wstFile, File bbInfoFile) {
        this.name       = name;
        this.wstFile    = wstFile;
        this.bbInfoFile = bbInfoFile;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public File getWstFile() {
        return wstFile;
    }

    public void setWstFile(File wstFile) {
        this.wstFile = wstFile;
    }

    public File getBBInfo() {
        return bbInfoFile;
    }

    public void setBBInfo(File bbInfoFile) {
        this.bbInfoFile = bbInfoFile;
    }

    public ImportWst getWst() {
        return wst;
    }

    public void setWst(ImportWst wst) {
        this.wst = wst;
    }

    public void parseDependencies() throws IOException {
        parseGauges();
        parseAnnotations();
        parseWst();
    }

    public void parseWst() throws IOException {
        WstParser wstParser = new WstParser();
        wstParser.parse(wstFile);
        wst = wstParser.getWst();
    }

    public void parseGauges() throws IOException {
        File gltFile = new File(wstFile.getParentFile(), PEGEL_GLT);
        gltFile = FileTools.repair(gltFile);

        if (!gltFile.isFile() || !gltFile.canRead()) {
            log.warn("cannot read gauges from '" + gltFile + "'");
            return;
        }

        PegelGltParser pgltp = new PegelGltParser();
        pgltp.parse(gltFile);

        gauges = pgltp.getGauges();

        for (ImportGauge gauge: gauges) {
            gauge.parseDependencies();
        }
    }

    public void parseAnnotations() throws IOException {
        File riverDir = wstFile.getParentFile().getParentFile();
        AnnotationsParser aparser = new AnnotationsParser();
        aparser.parse(riverDir);

        annotations = aparser.getAnnotations();
    }

    public void storeDependencies() {
        storeAnnotations();
        storeGauges();
        storeWst();
    }

    public void storeWst() {
        River river = getPeer();
        wst.storeDependencies(river);
    }

    public void storeAnnotations() {
        River river = getPeer();
        for (ImportAnnotation annotation: annotations) {
            annotation.getPeer(river);
        }
    }

    public void storeGauges() {
        log.info("store gauges:");
        River river = getPeer();
        for (ImportGauge gauge: gauges) {
            log.info("\tgauge: " + gauge.getName());
            gauge.storeDependencies(river);
        }
    }

    public River getPeer() {
        if (peer == null) {
            Session session = Importer.sessionHolder.get();
            Query query = session.createQuery("from River where name=:name");
            query.setString("name", name);
            List<River> rivers = query.list();
            if (rivers.isEmpty()) {
                peer = new River(name);
                session.save(peer);
            }
            else {
                peer = rivers.get(0);
            }
        }
        return peer;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org