view backend/src/main/java/org/dive4elements/river/importer/parsers/MeasurementStationsParser.java @ 9650:a2a42a6bac6b

Importer (s/u-info) extensions: outer try/catch for parse and log of line no, catching parsing exception if not enough value fields, parsing error and warning log messages with line number, detecting and rejecting duplicate data series, better differentiation between error and warning log messages
author mschaefer
date Mon, 23 Mar 2020 14:57:03 +0100
parents c43d8c1a4455
children
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
 * Software engineering by Intevation GmbH
 *
 * 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.parsers;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.dive4elements.river.importer.ImportMeasurementStation;
import org.dive4elements.river.importer.ImportRange;
import org.dive4elements.river.importer.ImportTimeInterval;
import org.dive4elements.river.importer.common.AbstractParser;
import org.dive4elements.river.model.MeasurementStation;


public class MeasurementStationsParser extends LineParser {

    public static class MeasurementStationParserException extends Exception {

        private static final long serialVersionUID = 1L;

        public MeasurementStationParserException(final String msg) {
            super(msg);
        }
    }

    public static final int MIN_COLUMNS = 9;

    public static final int MAX_COMMENT_LENGTH = 512;

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

    private List<ImportMeasurementStation> measurementStations;
    private ImportMeasurementStation current;

    @Override
    protected void reset() {
        this.measurementStations = new ArrayList<>();
    }

    @Override
    protected void finish() {
    }

    @Override
    protected void handleLine(final int lineNum, final String line) {
        if (line == null || line.startsWith(START_META_CHAR)) {
            log.info("skip meta information at line " + lineNum);
            return;
        }

        try {
            this.current = new ImportMeasurementStation();
            handleDataLine(lineNum, line);
            this.measurementStations.add(this.current);
        }
        catch (final MeasurementStationParserException e) {
            log.warn("Problem in line " + lineNum + ": " + e.getMessage());
        }
    }

    public List<ImportMeasurementStation> getMeasurementStations() {
        return this.measurementStations;
    }

    protected void handleDataLine(final int lineNum, final String line)
            throws MeasurementStationParserException {
        final String[] cols = line.split(SEPERATOR_CHAR);

        if (cols == null || cols.length < MIN_COLUMNS) {
            final int num = cols != null ? cols.length : 0;
            throw new MeasurementStationParserException("Not enough columns: "
                    + num);
        }

        this.current.name = getName(cols, lineNum);
        this.current.range = getRange(cols, lineNum);
        this.current.measurementType = getMeasurementType(cols, lineNum);
        this.current.riverside = getRiverside(cols, lineNum);
        this.current.gauge = getGauge(cols, lineNum);
        this.current.observationTimerange = getObservationTimerange(cols, lineNum);
        this.current.operator = getOperator(cols, lineNum);
        this.current.comment = getComment(cols, lineNum);
    }

    protected String getName(final String[] cols, final int lineNum)
            throws MeasurementStationParserException {
        if (cols[0] == null || cols[0].length() == 0) {
            throw new MeasurementStationParserException("invalid name in line "
                    + lineNum);
        }

        return cols[0];
    }

    protected ImportRange getRange(final String[] cols, final int lineNum) {
        final String from = cols[1];
        final String to   = cols[4];
        if (from == null || from.length() == 0) {
            log.error("No station found in line" + lineNum);
            return null;
        }

        try {
            if (to == null || to.length() == 0) {
                log.warn("No end km found in line " + lineNum);
                return new ImportRange(AbstractParser.parseDecimal(from));
            }

            try {
                return new ImportRange(AbstractParser.parseDecimal(from), AbstractParser.parseDecimal(to));
            }
            catch (final NumberFormatException e) {
                log.warn("Unparseable end km in line " + lineNum +
                        ". Error: " + e.getMessage());
                return new ImportRange(AbstractParser.parseDecimal(from));
            }

        }
        catch (final ParseException e) {
            log.error("Unparseable station in line " + lineNum +
                    ". Error: " + e.getMessage());
            return null;
        }
    }

    protected String getMeasurementType(final String[] cols, final int lineNum)
            throws MeasurementStationParserException {
        final String mtype = cols[2].trim();
        if (!(MeasurementStation.MEASUREMENT_TYPE_BEDLOAD.equals(mtype) ||
                MeasurementStation.MEASUREMENT_TYPE_SUSP.equals(mtype))) {
            throw new MeasurementStationParserException(
                    "invalid measurement type in line " + lineNum);
        }

        return mtype;
    }

    protected String getRiverside(final String[] cols, final int lineNum) {
        final String col = cols[3];
        if (col == null || col.length() == 0) {
            log.warn("No river side given in line " + lineNum);
        }
        return col;
    }

    protected String getGauge(final String[] cols, final int lineNum) {
        final String col = cols[5];
        if (col == null || col.length() == 0) {
            log.warn("Invalid gauge found in line " + lineNum);
        }
        return col;
    }

    protected ImportTimeInterval getObservationTimerange(
            final String[] cols,
            final int lineNum
            ) {
        final String col = cols[7];
        if (col == null || col.length() == 0) {
            log.warn("Observation time invalid in line " + lineNum);
            return null;
        }

        try {
            final Date date = getDate(col);

            if (date != null) {
                return new ImportTimeInterval(date);
            }
            log.warn("Observation time invalid in line " + lineNum);
        }
        catch (final ParseException pe) {
            log.warn("Unparseable observation time '" + col +
                    "' in line " + lineNum);
        }
        return null;
    }

    protected String getOperator(final String[] cols, final int lineNum) {
        final String col = cols[8];
        if (col == null || col.length() == 0) {
            log.warn("No operator given in line " + lineNum);
        }
        return col;
    }

    protected String getComment(final String[] cols, final int lineNum) {
        if (cols.length > MIN_COLUMNS) {
            final String col = cols[9];
            if (col.length() > MAX_COMMENT_LENGTH) {
                log.warn("Comment in line " + lineNum +
                        " longer than allowed " + MAX_COMMENT_LENGTH +
                        " characters. Truncated.");
                return col.substring(0, MAX_COMMENT_LENGTH);
            }
            return col;
        }
        return null;
    }
}

http://dive4elements.wald.intevation.org