teichmann@5844: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5844: * Software engineering by Intevation GmbH teichmann@5844: * teichmann@5992: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5844: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5992: * documentation coming with Dive4Elements River for details. teichmann@5844: */ teichmann@5844: teichmann@5829: package org.dive4elements.river.importer.parsers; ingo@2806: ingo@2808: import java.math.BigDecimal; ingo@2808: ingo@2808: import java.text.ParseException; ingo@2808: ingo@2806: import org.apache.log4j.Logger; ingo@2806: teichmann@5829: import org.dive4elements.river.importer.ImportBedHeight; teichmann@5829: import org.dive4elements.river.importer.ImportBedHeightSingle; teichmann@5829: import org.dive4elements.river.importer.ImportBedHeightSingleValue; ingo@2806: ingo@2806: ingo@2811: public class BedHeightSingleParser extends BedHeightParser { ingo@2806: ingo@2806: private static final Logger log = ingo@2806: Logger.getLogger(BedHeightSingleParser.class); ingo@2806: ingo@2806: ingo@2808: ingo@2811: @Override ingo@2811: protected ImportBedHeight newImportBedHeight(String description) { ingo@2811: return new ImportBedHeightSingle(description); ingo@2808: } ingo@2808: ingo@2808: ingo@2808: felix@3955: /** felix@3955: * Create ImportBedHeightSingleValue from a line of csv file, add felix@3955: * it to the ImportBedHeight. felix@3955: */ ingo@2811: @Override ingo@2811: protected void handleDataLine(ImportBedHeight obj, String line) { ingo@2808: String[] values = line.split(SEPERATOR_CHAR); ingo@2808: felix@3955: if (values == null || (values.length != 1 && values.length < 6)) { sascha@3662: //log.warn("BSP: Error while parsing data line: '" + line + "'"); ingo@2808: return; ingo@2808: } ingo@2808: tom@6203: Double km; felix@3955: felix@3955: try { tom@6203: km = new Double(nf.parse(values[0]).doubleValue()); tom@6273: tom@6273: Double key = Double.valueOf(km); tom@6273: tom@6273: if (kmExists.contains(key)) { tom@6273: log.warn("duplicate station '" + km + "': -> ignored"); tom@6273: return; tom@6273: } tom@6273: tom@6273: kmExists.add(key); tom@6273: } tom@6273: felix@3955: catch (ParseException e) { felix@3955: // We expect a lot of ";;;;;;" lines. felix@3955: //log.warn("BSP: Error while parsing km of data row.", e); felix@3955: return; felix@3955: } felix@3955: felix@3955: // Handle gaps like "10,0;;;;;". felix@3955: if (values.length == 1) { tom@6047: // Do not import line without useful data felix@3955: return; felix@3955: } felix@3955: felix@3955: // Because we cannot enforce consistency of values with complete data felix@3955: // via null constraints in the database (as there are "gap" values), felix@3955: // do this checks manually. felix@3955: if (values[3].length() == 0 || values[4].length() == 0 felix@3955: || values[5].length() == 0) { felix@3955: //log.warn("BSP: Error while parsing data row (manual null constraint violated)."); felix@3955: return; felix@3955: } felix@3955: ingo@2808: try { ingo@2808: ImportBedHeightSingleValue value = new ImportBedHeightSingleValue( ingo@2811: (ImportBedHeightSingle) obj, felix@3955: km, tom@6203: new Double(nf.parse(values[1]).doubleValue()), tom@6203: new Double(nf.parse(values[2]).doubleValue()), tom@6203: new Double(nf.parse(values[3]).doubleValue()), rrenkert@5450: parseBigDecimal(values[4], line), tom@6203: new Double(nf.parse(values[5]).doubleValue()) ingo@2808: ); ingo@2808: ingo@2808: obj.addValue(value); ingo@2808: } ingo@2808: catch (ParseException e) { tom@5490: log.warn("BSP: unparseable value in data row.", e); ingo@2808: } ingo@2808: } teichmann@5992: rrenkert@5450: private BigDecimal parseBigDecimal(String value, String line) { rrenkert@5450: BigDecimal result = null; rrenkert@5450: try { rrenkert@5450: Double dValue = Double.valueOf(value.replace(",", ".")); rrenkert@5450: result = new BigDecimal(dValue.doubleValue()); rrenkert@5450: } rrenkert@5450: catch (NumberFormatException nfe) { rrenkert@5450: log.warn( tom@5490: "Could not parse " + rrenkert@5450: value + rrenkert@5450: " in bed heigt single row: " rrenkert@5450: + line); rrenkert@5450: } rrenkert@5450: return result; rrenkert@5450: } ingo@2806: } ingo@2806: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :