view backend/src/main/java/org/dive4elements/river/importer/sinfo/parsers/SelectedAdditionalParser.java @ 8988:ae76f618d990

Checks added for missing import directory
author mschaefer
date Sun, 08 Apr 2018 18:09:32 +0200
parents bf8a9df86f32
children
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.HashMap;
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_FILENAME = "Zus_Laengsschnitte.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;
        }

        public static SelectionType parse(final String path) {
            if (path.toLowerCase().endsWith(".wst"))
                return WITH_Q;
            else
                return WITHOUT_Q;
        }
    }

    private final File importPath;

    private final File rootRelativePath;

    private final ImportRiver river;

    private final HashMap<String, SelectionType> links;


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

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


    /***** 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<>();
        final File importFile = new File(importDir, IMPORT_FILENAME);
        if (importFile.exists())
            parsers.add(new SelectedAdditionalParser(importFile, new File(relativeDir, IMPORT_FILENAME), river));
        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.containsKey(line.trim()))
                    this.links.put(line.trim(), SelectionType.parse(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 IS NOT NULL)");
        reset.setParameter("river_id", this.river.getPeer().getId());
        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.getPeer());
        int count = 0;
        for (final String wstfile : this.links.keySet()) {
            count += updateWst(session, query, this.river.getPeer(), wstfile, this.links.get(wstfile));
        }
        log.info("Updated " + count + " wsts for selected additionals");
    }

    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