# HG changeset patch # User Felix Wolfsteller # Date 1363082114 -3600 # Node ID 9c024718bffb5b889c3c5964855e1f7a8d5ea11b # Parent ea4129a696da3362fc21448079b5312f8f16d761 WaterlevelParser: Use Wst-structures directly (instead of Waterlevel*). diff -r ea4129a696da -r 9c024718bffb flys-backend/src/main/java/de/intevation/flys/importer/parsers/WaterlevelParser.java --- a/flys-backend/src/main/java/de/intevation/flys/importer/parsers/WaterlevelParser.java Tue Mar 12 10:51:43 2013 +0100 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/parsers/WaterlevelParser.java Tue Mar 12 10:55:14 2013 +0100 @@ -20,6 +20,7 @@ import de.intevation.flys.importer.ImportRange; import de.intevation.flys.importer.ImportWst; import de.intevation.flys.importer.ImportWstColumn; +import de.intevation.flys.importer.ImportWstColumnValue; import de.intevation.flys.importer.ImportWstQRange; @@ -43,71 +44,40 @@ public static final Pattern META_UNIT = Pattern.compile("^Einheit: \\[(.*)\\].*"); - private List waterlevels; + private List waterlevels; - private ImportWaterlevel current; + private ImportWst current; + + /** The Waterlevel-Wst s will always have but one column. */ + private ImportWstColumn column; private ImportWaterlevelQRange currentQ; + /** The current (incomplete) Q Range. */ + private ImportWstQRange currentQRange; + + /** The current (incomplete) km range for Q Range. */ + private ImportRange currentRange; + private String currentDescription; public WaterlevelParser() { - waterlevels = new ArrayList(); + waterlevels = new ArrayList(); } /** * Create ImportWst objects from ImportWaterlevel * objects. + * TODO deprecate */ public List exportWsts() { - List wsts = new ArrayList(); - for(ImportWaterlevel waterlevel: getWaterlevels()) { - String description = waterlevel.getDescription(); - ImportWst wst = new ImportWst(); - wsts.add(wst); - wst.setDescription(description); - // Fixation kind. - wst.setKind(6); - wst.setUnit(waterlevel.getUnit()); - - // Fake WST has but 1 column. - wst.setNumberColumns(1); - ImportWstColumn column = wst.getColumn(0); - column.setDescription(description); - column.setName(description); - column.setPosition(0); - - // Build Q Range. - List qRanges = waterlevel.getQRanges(); - for(ImportWaterlevelQRange range: qRanges) { - List values = range.getValues(); - if (values.size() < 2) { - log.warn ("Not enough values to build valid QRange"); - continue; - } - ImportRange iRange = new ImportRange( - BigDecimal.valueOf(values.get(0).getStation()), - BigDecimal.valueOf(values.get(values.size() -1).getStation())); - column.addColumnQRange( - new ImportWstQRange(iRange, BigDecimal.valueOf(range.getQ()))); - } - - // The other W/KM values. - for(ImportWaterlevelQRange range: qRanges) { - for(ImportWaterlevelValue value: range.getValues()) { - column.addColumnValue(BigDecimal.valueOf(value.getStation()), - BigDecimal.valueOf(value.getW())); - } - } - // TODO Maybe set a timeinterval. - } - return wsts; + return waterlevels; } - public List getWaterlevels() { + public List getWaterlevels() { return waterlevels; } @@ -122,16 +92,25 @@ @Override protected void reset() { - currentQ = null; - current = new ImportWaterlevel(currentDescription); + currentQRange = null; + current = new ImportWst(currentDescription); + column = new ImportWstColumn(current, + currentDescription, + currentDescription, + 0); + current.setKind(6); } @Override protected void finish() { if (current != null) { - if (currentQ != null) { - current.addValue(currentQ); + if (currentQRange != null) { + List cValues = column.getColumnValues(); + // Set end of range to last station. + currentRange.setB(cValues.get(cValues.size() -1).getPosition()); + currentQRange.setRange(currentRange); + column.addColumnQRange(currentQRange); } waterlevels.add(current); @@ -172,23 +151,22 @@ if (m.matches()) { String unitStr = m.group(1); String valueStr = m.group(2); - - if (currentQ != null) { - if (current != null) { - current.addValue(currentQ); + try { + if (currentQRange != null) { + // Finish off the last one. + List cValues = column.getColumnValues(); + // Set end of range to last station. + currentRange.setB(cValues.get(cValues.size() -1).getPosition()); + currentQRange.setRange(currentRange); + column.addColumnQRange(currentQRange); + } - else { - // this should never happen - log.warn("Try to add Q range without waterlevel!"); - } - } + currentQRange = new ImportWstQRange(null, + new BigDecimal(nf.parse(valueStr).doubleValue())); + currentRange = new ImportRange(); - try { log.debug("Found new Q range: Q=" + valueStr); - currentQ = new ImportWaterlevelQRange( - nf.parse(valueStr).doubleValue()); - return true; } catch (ParseException pe) { @@ -209,10 +187,17 @@ } try { + // Store the value and remember the position for QRange, if needed. Double station = nf.parse(cols[0]).doubleValue(); Double value = nf.parse(cols[1]).doubleValue(); - currentQ.addValue(new ImportWaterlevelValue(station, value)); + BigDecimal stationBD = new BigDecimal(station); + + column.addColumnValue(stationBD, new BigDecimal(value)); + + if (currentRange.getA() == null) { + currentRange.setA(stationBD); + } } catch (ParseException pe) { log.warn("Error while parsing number values: '" + line + "'");