view backend/src/main/java/org/dive4elements/river/importer/uinfo/UInfoImporter.java @ 9012:abe069eb180c

Import of vegetation zones added
author mschaefer
date Wed, 18 Apr 2018 07:43:55 +0200
parents d046997281bc
children 63c086139391
line wrap: on
line source
/* Copyright (C) 2017 by Bundesanstalt für Gewässerkunde
 * Software engineering by
 *  Björnsen Beratende Ingenieure GmbH
 *  Dr. Schumacher Ingenieurbüro für Wasser und Umwelt
 *
 * This file is Free Software under the GNU AGPL (>=v3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out the
 * documentation coming with Dive4Elements River for details.
 */

package org.dive4elements.river.importer.uinfo;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.dive4elements.river.importer.ImportRiver;
import org.dive4elements.river.importer.common.ImportParser;
import org.dive4elements.river.importer.uinfo.parsers.SalixParser;
import org.dive4elements.river.importer.uinfo.parsers.VegetationParser;

/**
 * Import all U-INFO files of a river from its import directory and subdirectories<br />
 * <br />
 * Requires river and its gauges to exist in the database already
 *
 * @author Matthias Schäfer
 *
 */
public class UInfoImporter
{
    /***** FIELDS *****/

    private static final Logger log = Logger.getLogger(UInfoImporter.class);

    private static final String UINFO_DIR = "Oekologie";

    private enum UInfoDirName {
        BASICS("Basisdaten"), //
        SALIX("Salix-Linie" + File.separator + "Salix-Linie"), //
        VEGETATION("Vegetationszonen");

        private final String dirname;

        UInfoDirName(final String dirname) {
            this.dirname = dirname;
        }

        public String getDir() {
            return this.dirname;
        }
        public File getFile() {
            return new File(getDir());
        }

        public File buildPath(final File rootDir) {
            return new File(rootDir, getDir());
        }
    }

    /**
     * Series of river's stations with bed mobility flag.
     */
    private final List<ImportParser> parsers;

    /**
     * Path of the S-INFO data directory of the importing river.
     */
    private File rootDir;


    /***** CONSTRUCTOR *****/

    public UInfoImporter() {
        this.parsers = new ArrayList<>();
    }

    /***** METHODS *****/

    /**
     * Inits the parser list regarding the skip flags.
     */
    public void setup(final File riverDir, final ImportRiver river) {
        this.rootDir = new File(riverDir, UINFO_DIR);
        log.info("Parse U-INFO files from " + this.rootDir);
        this.parsers.clear();
        if (!SalixParser.shallSkip()) {
            if (!this.parsers.addAll(SalixParser.createParsers(UInfoDirName.SALIX.buildPath(this.rootDir), UInfoDirName.SALIX.getFile(), river)))
                log.info("Salix: no files found");
        }
        else {
            log.info("Salix: skipped");
        }
        if (!VegetationParser.shallSkip()) {
            if (!this.parsers.addAll(VegetationParser.createParsers(UInfoDirName.VEGETATION.buildPath(this.rootDir), UInfoDirName.VEGETATION.getFile(), river)))
                log.info("Vegetation: no files found");
        }
        else {
            log.info("Vegetation: skipped");
        }
    }

    /**
     * Imports the files according to the active parser list
     */
    public void parse() throws IOException {
        for (final ImportParser parser : this.parsers)
            parser.parse();
    }

    /**
     * Stores all pending import objects
     */
    public void store() {
        for (final ImportParser parser : this.parsers)
            parser.store();
    }

}

http://dive4elements.wald.intevation.org