# HG changeset patch # User Ingo Weinzierl # Date 1335433592 0 # Node ID a1402c16152f795c845d91c1429503cd7c084ba1 # Parent c36106f15ac9875fb72b3ca288e3d94984818edb Added a parser and importer classes used to import waterlevel differences. flys-backend/trunk@4304 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r c36106f15ac9 -r a1402c16152f flys-backend/ChangeLog --- a/flys-backend/ChangeLog Thu Apr 26 09:44:39 2012 +0000 +++ b/flys-backend/ChangeLog Thu Apr 26 09:46:32 2012 +0000 @@ -1,3 +1,14 @@ +2012-04-26 Ingo Weinzierl + + * src/main/java/de/intevation/flys/importer/parsers/WaterlevelDifferencesParser.java: + New parser for MINFO specific waterlevel differences. + + * src/main/java/de/intevation/flys/importer/ImportWaterlevelDifferenceValue.java, + src/main/java/de/intevation/flys/importer/ImportWaterlevelDifference.java, + src/main/java/de/intevation/flys/importer/ImportWaterlevelDifferenceColumn.java: + New importer classes used during the import process of MINFO specific + waterlevel differences. + 2012-04-26 Ingo Weinzierl * doc/schema/oracle-minfo.sql, diff -r c36106f15ac9 -r a1402c16152f flys-backend/src/main/java/de/intevation/flys/importer/ImportWaterlevelDifference.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportWaterlevelDifference.java Thu Apr 26 09:46:32 2012 +0000 @@ -0,0 +1,101 @@ +package de.intevation.flys.importer; + +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.log4j.Logger; + +import org.hibernate.Session; +import org.hibernate.Query; +import org.hibernate.exception.ConstraintViolationException; + +import de.intevation.flys.model.River; +import de.intevation.flys.model.Unit; +import de.intevation.flys.model.WaterlevelDifference; + + +public class ImportWaterlevelDifference { + + private static final Logger log = + Logger.getLogger(ImportWaterlevelDifference.class); + + + private ImportUnit unit; + + private String description; + + private List columns; + + private WaterlevelDifference peer; + + + public ImportWaterlevelDifference(String description) { + this.columns = new ArrayList(); + + this.description = description; + } + + + public void setUnit(ImportUnit unit) { + this.unit = unit; + } + + + public void addValue(ImportWaterlevelDifferenceColumn column) { + this.columns.add(column); + } + + + public void storeDependencies(River river) + throws SQLException, ConstraintViolationException + { + log.info("store dependencies"); + + WaterlevelDifference peer = getPeer(river); + + int i = 0; + + for (ImportWaterlevelDifferenceColumn column: columns) { + column.storeDependencies(peer); + i++; + } + + log.info("stored " + i + " waterlevel difference columns"); + } + + + public WaterlevelDifference getPeer(River river) { + Unit u = unit != null ? unit.getPeer() : null; + if (u == null) { + log.warn("skip invalid waterlevel difference - no unit set!"); + return null; + } + + if (peer == null) { + Session session = ImporterSession.getInstance().getDatabaseSession(); + Query query = session.createQuery( + "from WaterlevelDifference where " + + " river=:river and " + + " unit=:unit and " + + " description=:description" + ); + + query.setParameter("river", river); + query.setParameter("unit", u); + query.setParameter("description", description); + + List diffs = query.list(); + if (diffs.isEmpty()) { + peer = new WaterlevelDifference(river, u, description); + session.save(peer); + } + else { + peer = diffs.get(0); + } + } + + return peer; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r c36106f15ac9 -r a1402c16152f flys-backend/src/main/java/de/intevation/flys/importer/ImportWaterlevelDifferenceColumn.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportWaterlevelDifferenceColumn.java Thu Apr 26 09:46:32 2012 +0000 @@ -0,0 +1,82 @@ +package de.intevation.flys.importer; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.log4j.Logger; + +import org.hibernate.Session; +import org.hibernate.Query; + +import de.intevation.flys.model.River; +import de.intevation.flys.model.WaterlevelDifference; +import de.intevation.flys.model.WaterlevelDifferenceColumn; + + +public class ImportWaterlevelDifferenceColumn { + + private static final Logger log = + Logger.getLogger(ImportWaterlevelDifferenceColumn.class); + + + private String description; + + private List values; + + private WaterlevelDifferenceColumn peer; + + + public ImportWaterlevelDifferenceColumn(String description) { + this.values = new ArrayList(); + + this.description = description; + } + + + public void addValue(ImportWaterlevelDifferenceValue value) { + this.values.add(value); + } + + + public void storeDependencies(WaterlevelDifference difference) { + log.info("store dependencies"); + + WaterlevelDifferenceColumn peer = getPeer(difference); + + int i = 0; + + for (ImportWaterlevelDifferenceValue value: values) { + value.storeDependencies(peer); + i++; + } + + log.info("stored " + i + " waterlevel difference values"); + } + + + public WaterlevelDifferenceColumn getPeer(WaterlevelDifference diff) { + if (peer == null) { + Session session = ImporterSession.getInstance().getDatabaseSession(); + Query query = session.createQuery( + "from WaterlevelDifferenceColumn where " + + " difference=:difference and " + + " description=:description" + ); + + query.setParameter("difference", diff); + query.setParameter("description", description); + + List cols = query.list(); + if (cols.isEmpty()) { + peer = new WaterlevelDifferenceColumn(diff, description); + session.save(peer); + } + else { + peer = cols.get(0); + } + } + + return peer; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r c36106f15ac9 -r a1402c16152f flys-backend/src/main/java/de/intevation/flys/importer/ImportWaterlevelDifferenceValue.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportWaterlevelDifferenceValue.java Thu Apr 26 09:46:32 2012 +0000 @@ -0,0 +1,65 @@ +package de.intevation.flys.importer; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.log4j.Logger; + +import org.hibernate.Session; +import org.hibernate.Query; + +import de.intevation.flys.model.WaterlevelDifferenceColumn; +import de.intevation.flys.model.WaterlevelDifferenceValue; + + +public class ImportWaterlevelDifferenceValue { + + private static final Logger log = + Logger.getLogger(ImportWaterlevelDifferenceValue.class); + + + private Double station; + private Double value; + + private WaterlevelDifferenceValue peer; + + + public ImportWaterlevelDifferenceValue(Double station, Double value) { + this.station = station; + this.value = value; + } + + + public void storeDependencies(WaterlevelDifferenceColumn column) { + getPeer(column); + } + + + public WaterlevelDifferenceValue getPeer(WaterlevelDifferenceColumn column) { + if (peer == null) { + Session session = ImporterSession.getInstance().getDatabaseSession(); + Query query = session.createQuery( + "from WaterlevelDifferenceValue where " + + " column=:column and " + + " station=:station and " + + " value=:value" + ); + + query.setParameter("column", column); + query.setParameter("station", station); + query.setParameter("value", value); + + List values = query.list(); + if (values.isEmpty()) { + peer = new WaterlevelDifferenceValue(column, station, value); + session.save(peer); + } + else { + peer = values.get(0); + } + } + + return peer; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r c36106f15ac9 -r a1402c16152f flys-backend/src/main/java/de/intevation/flys/importer/parsers/WaterlevelDifferencesParser.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/parsers/WaterlevelDifferencesParser.java Thu Apr 26 09:46:32 2012 +0000 @@ -0,0 +1,174 @@ +package de.intevation.flys.importer.parsers; + +import java.io.File; +import java.io.IOException; +import java.text.NumberFormat; +import java.text.ParseException; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.log4j.Logger; + +import de.intevation.flys.importer.ImportUnit; +import de.intevation.flys.importer.ImportWaterlevelDifference; +import de.intevation.flys.importer.ImportWaterlevelDifferenceColumn; +import de.intevation.flys.importer.ImportWaterlevelDifferenceValue; + + +public class WaterlevelDifferencesParser extends LineParser { + + private static final Logger log = + Logger.getLogger(WaterlevelDifferencesParser.class); + + private static final NumberFormat nf = + NumberFormat.getInstance(DEFAULT_LOCALE); + + public static final Pattern META_UNIT = + Pattern.compile("^Einheit: \\[(.*)\\].*"); + + + private List differences; + + private ImportWaterlevelDifferenceColumn[] columns; + + private ImportWaterlevelDifference current; + + + public WaterlevelDifferencesParser() { + differences = new ArrayList(); + } + + + public List getDifferences() { + return differences; + } + + + @Override + public void parse(File file) throws IOException { + current = new ImportWaterlevelDifference(file.getName()); + + super.parse(file); + } + + + @Override + protected void reset() { + } + + + @Override + protected void finish() { + if (columns != null && current != null) { + for (ImportWaterlevelDifferenceColumn col: columns) { + current.addValue(col); + } + + differences.add(current); + } + + current = null; + columns = null; + } + + @Override + protected void handleLine(String line) { + if (line.startsWith(START_META_CHAR)) { + handleMetaLine(stripMetaLine(line)); + } + else { + handleDataLine(line); + } + } + + + private void handleMetaLine(String meta) { + if (handleMetaUnit(meta)) { + return; + } + else { + handleMetaColumnNames(meta); + } + } + + + private boolean handleMetaUnit(String meta) { + Matcher m = META_UNIT.matcher(meta); + + if (m.matches()) { + String unit = m.group(1); + log.debug("Found unit: '" + unit + "'"); + + current.setUnit(new ImportUnit(unit)); + + return true; + } + + return false; + } + + + private boolean handleMetaColumnNames(String meta) { + Pattern META_COLUMN_NAMES = Pattern.compile("Fluss-km;(.*)"); + Matcher m = META_COLUMN_NAMES.matcher(meta); + + if (m.matches()) { + String colStr = m.group(1); + String[] cols = colStr.split(SEPERATOR_CHAR); + + log.debug("Found " + cols.length + " columns."); + + initColumns(cols); + + return true; + } + + return false; + } + + + private void initColumns(String[] cols) { + columns = new ImportWaterlevelDifferenceColumn[cols.length]; + + for (int i = 0; i < cols.length; i++) { + String name = cols[i].replace("\"", ""); + + log.debug("Create new column '" + name + "'"); + columns[i] = new ImportWaterlevelDifferenceColumn(name); + } + } + + + private void handleDataLine(String line) { + String[] cols = line.split(SEPERATOR_CHAR); + + if (cols == null || cols.length < 2) { + log.warn("skip invalid waterlevel line: '" + line + "'"); + return; + } + + try { + Double station = nf.parse(cols[0]).doubleValue(); + + for (int i = 0; i < columns.length; i++) { + String value = cols[i+1]; + + try { + columns[i].addValue(new ImportWaterlevelDifferenceValue( + station, + nf.parse(value).doubleValue() + )); + } + catch (ParseException pe) { + log.warn("Error while parsing value: '" + value + "'"); + } + } + } + catch (ParseException pe) { + log.warn("Error while parsing station: '" + line + "'"); + } + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :