view backend/src/main/java/org/dive4elements/river/importer/sinfo/parsers/SelectedAdditionalParser.java @ 8971:50416a0df385

Importer for the Schifffahrt (S-INFO) and Oekologie (U-INFO) files
author mschaefer
date Tue, 03 Apr 2018 10:18:30 +0200
parents
children bf8a9df86f32
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.sinfo.parsers;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.dive4elements.river.importer.Config;
import org.dive4elements.river.importer.ImportRiver;
import org.dive4elements.river.importer.ImporterSession;
import org.dive4elements.river.importer.common.AbstractParser;
import org.dive4elements.river.importer.common.ImportParser;
import org.dive4elements.river.model.River;
import org.dive4elements.river.model.Wst;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;

/**
 * Reads and parses a selected WST additionals link file
 *
 * @author Matthias Schäfer
 *
 */
public class SelectedAdditionalParser implements ImportParser {

    /***** FIELDS *****/

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

    private static final String IMPORT_Q_FILENAME = "Mit_Abflussdaten.txt";

    private static final String IMPORT_W_FILENAME = "Ohne_Abflussdaten.txt";

    private enum SelectionType {
        WITH_Q("Q", "with discharge"), //
        WITHOUT_Q("W", "without discharge");

        private final String key;
        private final String logText;

        SelectionType(final String key, final String logText) {
            this.key = key;
            this.logText = logText;
        }

        public String getKey() {
            return this.key;
        }

        public String getLogText() {
            return this.logText;
        }
    }

    private final File importPath;

    private final File rootRelativePath;

    private final ImportRiver river;

    private final SelectionType selectionType;

    private final List<String> links;


    /***** CONSTRUCTORS *****/

    public SelectedAdditionalParser(final File importPath, final File rootRelativePath, final ImportRiver river, final SelectionType selectionType) {
        this.importPath = importPath;
        this.rootRelativePath = rootRelativePath;
        this.river = river;
        this.selectionType = selectionType;
        this.links = new ArrayList<>();
    }


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

    /**
     * Whether this import type shall be skipped
     */
    public static boolean shallSkip() {
        return Config.INSTANCE.skipSInfoSelectedAdditional();
    }

    /**
     * Creates a list of parsers for all selected additionals import files in a directory
     */
    public static List<SelectedAdditionalParser> createParsers(final File importDir, final File relativeDir, final ImportRiver river) {
        final List<SelectedAdditionalParser> parsers = new ArrayList<>();
        parsers.add(new SelectedAdditionalParser(new File(importDir, IMPORT_Q_FILENAME), new File(relativeDir, IMPORT_Q_FILENAME),
                river, SelectionType.WITH_Q));
        parsers.add(new SelectedAdditionalParser(new File(importDir, IMPORT_W_FILENAME), new File(relativeDir, IMPORT_W_FILENAME),
                river, SelectionType.WITHOUT_Q));
        return parsers;
    }

    @Override
    public void parse() throws IOException {
        this.links.clear();
        log.info("Parse '... " + this.rootRelativePath + "'");
        LineNumberReader in = null;
        try {
            in = new LineNumberReader(new InputStreamReader(new FileInputStream(this.importPath), AbstractParser.ENCODING));
            String line;
            while (true) {
                line = in.readLine();
                if (line == null)
                    break;
                if (!line.trim().isEmpty() && !line.trim().startsWith(AbstractParser.START_META_CHAR))
                    this.links.add(line.trim());
            }
            log.info("Number of file links found: " + this.links.size());
        }
        finally {
            if (in != null)
                in.close();
        }
    }

    @Override
    public void store() {
        final Session session = ImporterSession.getInstance().getDatabaseSession();
        final SQLQuery reset = session.createSQLQuery("UPDATE wsts SET sinfo_selection = NULL WHERE (river_id=:river_id) AND (kind=1)"
                + " AND (sinfo_selection=:seltype)");
        reset.setParameter("river_id", this.river.getPeer().getId());
        reset.setParameter("seltype", this.selectionType.getKey());
        reset.executeUpdate();
        final Query query = session.createQuery("FROM Wst WHERE (river=:river) AND (kind=1) AND (lower(description) LIKE :path)");
        query.setParameter("river", this.river);
        int count = 0;
        for (final String wstfile : this.links) {
            count += updateWst(session, query, this.river.getPeer(), wstfile, this.selectionType);
        }
        log.info("Updated " + count + " wsts for selected additionals " + this.selectionType.getLogText());
    }

    private int updateWst(final Session session, final Query query, final River river, final String path, final SelectionType selectionType) {
        final String pathPattern = path.toLowerCase().replace('/', '_').replace('\\', '_');
        query.setParameter("path", pathPattern);
        final List<Wst> rows = query.list();
        if (rows.isEmpty()) {
            log.warn("Wst not found for description '" + path + "'" + ";" + this.rootRelativePath);
            return 0;
        } else {
            final Wst wst = rows.get(0);
            wst.setSInfoSelection(selectionType.getKey());
            final Serializable id = session.save(wst);
            return 1;
        }
    }
}

http://dive4elements.wald.intevation.org