view flys-backend/src/main/java/de/intevation/flys/importer/parsers/MeasurementStationsParser.java @ 5451:278d8759c92b

Allow null values in measurement_station columns without 'not null' constraints.
author Raimund Renkert <rrenkert@intevation.de>
date Wed, 27 Mar 2013 11:47:56 +0100
parents f63b39799d2d
children de3c77d35fef
line wrap: on
line source
package de.intevation.flys.importer.parsers;

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

import org.apache.log4j.Logger;

import de.intevation.flys.importer.ImportMeasurementStation;
import de.intevation.flys.importer.ImportRange;
import de.intevation.flys.importer.ImportTimeInterval;


public class MeasurementStationsParser extends LineParser {

    public static class MeasurementStationParserException extends Exception {

        private static final long serialVersionUID = 1L;

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

    public static final int MIN_COLUMNS = 10;

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

    private List<ImportMeasurementStation> measurementStations;
    private ImportMeasurementStation current;

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

    @Override
    protected void finish() {
    }

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

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

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

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

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

        current.name = getName(cols);
        current.station = getStation(cols);
        current.range = getRange(cols);
        current.measurementType = getMeasurementType(cols);
        current.riverside = getRiverside(cols);
        current.gauge = getGauge(cols);
        current.observationTimerange = getObservationTimerange(cols);
        current.operator = getOperator(cols);
        current.comment = getComment(cols);

        log.debug("Found new measurement station '" + current.name + "' at km "
            + current.station);
    }

    protected String getName(String[] cols)
        throws MeasurementStationParserException {
        if (cols[0] == null || cols[0].length() == 0) {
            throw new MeasurementStationParserException("invalid name '"
                + cols[0] + "'");
        }

        return cols[0];
    }

    protected double getStation(String[] cols)
        throws MeasurementStationParserException {
        if (cols[1] == null || cols[1].length() == 0) {
            throw new MeasurementStationParserException("invalid station '"
                + cols[1] + "'");
        }

        try {
            return getDouble(cols[1]);
        }
        catch (ParseException e) {
            throw new MeasurementStationParserException(
                "unable to parse station: " + e.getMessage());
        }
    }

    protected ImportRange getRange(String[] cols) {
        if (cols[4] == null || cols[4].length() == 0) {
            log.warn("No upper value for range found in '" + cols[4] + "'");
            return null;
        }

        if (cols[5] == null || cols[5].length() == 0) {
            log.warn("No upper value for range found in '" + cols[5] + "'");
            return null;
        }

        try {
            double lower = getDouble(cols[4]);
            double upper = getDouble(cols[5]);

            return new ImportRange(new BigDecimal(lower), new BigDecimal(upper));
        }
        catch (ParseException e) {
            log.warn("unable to parse range: " + e.getMessage());
            return null;
        }
    }

    protected String getMeasurementType(String[] cols)
        throws MeasurementStationParserException {
        if (cols[2] == null || cols[2].length() == 0) {
            throw new MeasurementStationParserException(
                "invalid measurement type '" + cols[2] + "'");
        }

        return cols[2];
    }

    protected String getRiverside(String[] cols) {
        return cols[3];
    }

    protected String getGauge(String[] cols) {
        if (cols[6] == null || cols[6].length() == 0) {
            log.warn("invalid gauge found: '" + cols[6] + "'");
        }

        return cols[6];
    }

    protected ImportTimeInterval getObservationTimerange(String[] cols) {
        if (cols[8] == null || cols[8].length() == 0) {
                log.warn("Found invalid observation time '" + cols[8] + "'");
        }

        try {
            Date date = getDate(cols[8]);

            if (date != null) {
                return new ImportTimeInterval(date);
            }
            log.warn("Observation time date invalid: '" + cols[8] + "'");
        }
        catch (ParseException pe) {
            log.warn("Observation time date not parseable: '" + cols[8] + "'");
            return null;
        }
        return null;
    }

    protected String getOperator(String[] cols) {
        return cols[9];
    }

    protected String getComment(String[] cols) {
        return cols.length > 10 ? cols[10] : null;
    }
}

http://dive4elements.wald.intevation.org