# HG changeset patch # User Sascha L. Teichmann # Date 1300893752 0 # Node ID 3169b559ca3c6f9ce36e79df77fbd8c608ccc87d # Parent 88048d4f6e4def822d9fb1831fc1e1fc99d86815 Build models for wsts, wst columns and q ranges and store them in the backend. flys-backend/trunk@1549 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 88048d4f6e4d -r 3169b559ca3c flys-backend/ChangeLog --- a/flys-backend/ChangeLog Wed Mar 23 10:46:58 2011 +0000 +++ b/flys-backend/ChangeLog Wed Mar 23 15:22:32 2011 +0000 @@ -1,3 +1,24 @@ +2011-03-22 Sascha L. Teichmann + + * src/main/java/de/intevation/flys/importer/WstParser.java: + Build models for wsts, wst columns and q ranges and + store them in the backend. TODO: store the w values. + + * src/main/java/de/intevation/flys/model/WstQRange.java + src/main/java/de/intevation/flys/model/Wst.java, + src/main/java/de/intevation/flys/model/Range.java, + src/main/java/de/intevation/flys/model/WstColumnQRange.java, + src/main/java/de/intevation/flys/model/WstColumn.java: + Added convinience constructors. + + * src/main/java/de/intevation/flys/importer/ImportWstQRange.java, + src/main/java/de/intevation/flys/importer/ImportWst.java, + src/main/java/de/intevation/flys/importer/ImportRiver.java, + src/main/java/de/intevation/flys/importer/ImportWstColumnQRange.java, + src/main/java/de/intevation/flys/importer/ImportWstColumn.java, + src/main/java/de/intevation/flys/importer/ImportRange.java: + Added getPeer() methods. + 2011-03-22 Sascha L. Teichmann * src/main/java/de/intevation/flys/importer/WstParser.java: diff -r 88048d4f6e4d -r 3169b559ca3c flys-backend/src/main/java/de/intevation/flys/importer/ImportRange.java --- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportRange.java Wed Mar 23 10:46:58 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportRange.java Wed Mar 23 15:22:32 2011 +0000 @@ -69,10 +69,11 @@ if (peer == null) { Session session = Importer.sessionHolder.get(); Query query = session.createQuery( - "from Range where a=:a and b=:b and river.id=:river"); + "from Range where a=:a and b=:b and river=:river"); query.setParameter("a", a); query.setParameter("b", b); - query.setParameter("river", river.getId()); + query.setParameter("river", river); + log.debug("a: " + a + " b: " + b + " river.id: " + river.getId()); List ranges = query.list(); if (ranges.isEmpty()) { peer = new Range(a, b, river); diff -r 88048d4f6e4d -r 3169b559ca3c flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java --- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java Wed Mar 23 10:46:58 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java Wed Mar 23 15:22:32 2011 +0000 @@ -30,6 +30,8 @@ protected List annotations; + protected ImportWst wst; + protected River peer; public ImportRiver() { @@ -65,6 +67,14 @@ this.bbInfoFile = bbInfoFile; } + public ImportWst getWst() { + return wst; + } + + public void setWst(ImportWst wst) { + this.wst = wst; + } + public void parseDependencies() throws IOException { parseGauges(); parseAnnotations(); @@ -74,6 +84,7 @@ public void parseWst() throws IOException { WstParser wstParser = new WstParser(); wstParser.parse(wstFile); + wst = wstParser.getWst(); } public void parseGauges() throws IOException { @@ -106,6 +117,12 @@ public void storeDependencies() { storeAnnotations(); storeGauges(); + storeWst(); + } + + public void storeWst() { + River river = getPeer(); + wst.storeDependencies(river); } public void storeAnnotations() { diff -r 88048d4f6e4d -r 3169b559ca3c flys-backend/src/main/java/de/intevation/flys/importer/ImportWst.java --- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportWst.java Wed Mar 23 10:46:58 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportWst.java Wed Mar 23 15:22:32 2011 +0000 @@ -1,17 +1,74 @@ package de.intevation.flys.importer; import de.intevation.flys.model.Wst; +import de.intevation.flys.model.River; + +import org.hibernate.Session; +import org.hibernate.Query; + +import java.util.List; +import java.util.Map; +import java.util.HashMap; public class ImportWst { + protected String description; + + protected Map columns; + protected Wst peer; public ImportWst() { + columns = new HashMap(); } - public Wst getPeer() { + public ImportWst(String description) { + this(); + this.description = description; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public ImportWstColumn getColumn(String name) { + ImportWstColumn column = columns.get(name); + if (column == null) { + column = new ImportWstColumn(this, name, null); + columns.put(name, column); + } + return column; + } + + public void storeDependencies(River river) { + Wst wst = getPeer(river); + + for (ImportWstColumn column: columns.values()) { + column.storeDependencies(river); + } + } + + public Wst getPeer(River river) { if (peer == null) { - // TODO: Implement me! + Session session = Importer.sessionHolder.get(); + Query query = session.createQuery( + "from Wst where " + + "river=:river and description=:description"); + query.setParameter("river", river); + query.setParameter("description", description); + List wsts = query.list(); + if (wsts.isEmpty()) { + peer = new Wst(river, description); + session.save(peer); + } + else { + peer = wsts.get(0); + } + } return peer; } diff -r 88048d4f6e4d -r 3169b559ca3c flys-backend/src/main/java/de/intevation/flys/importer/ImportWstColumn.java --- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportWstColumn.java Wed Mar 23 10:46:58 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportWstColumn.java Wed Mar 23 15:22:32 2011 +0000 @@ -1,17 +1,95 @@ package de.intevation.flys.importer; +import de.intevation.flys.model.Wst; import de.intevation.flys.model.WstColumn; +import de.intevation.flys.model.River; + +import org.hibernate.Session; +import org.hibernate.Query; + +import java.util.List; +import java.util.ArrayList; public class ImportWstColumn { + protected ImportWst wst; + protected String name; + protected String description; + + protected List columnQRanges; + protected WstColumn peer; public ImportWstColumn() { + columnQRanges = new ArrayList(); } - public WstColumn getPeer() { + public ImportWstColumn( + ImportWst wst, + String name, + String description + ) { + this(); + this.wst = wst; + this.name = name; + this.description = description; + } + + public ImportWst getWst() { + return wst; + } + + public void setWst(ImportWst wst) { + this.wst = wst; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public void addColumnQRange(ImportWstQRange columnQRange) { + columnQRanges.add( + new ImportWstColumnQRange(this, columnQRange)); + } + + public void storeDependencies(River river) { + WstColumn column = getPeer(river); + for (ImportWstColumnQRange columnQRange: columnQRanges) { + columnQRange.getPeer(river); + } + // TODO: Implement me! + } + + public WstColumn getPeer(River river) { if (peer == null) { - // TODO: Implement me! + Wst w = wst.getPeer(river); + Session session = Importer.sessionHolder.get(); + Query query = session.createQuery( + "from WstColumn where " + + "wst=:wst and name=:name and description=:description"); + query.setParameter("wst", w); + query.setParameter("name", name); + query.setParameter("description", description); + List columns = query.list(); + if (columns.isEmpty()) { + peer = new WstColumn(w, name, description, null); + session.save(peer); + } + else { + peer = columns.get(0); + } } return peer; } diff -r 88048d4f6e4d -r 3169b559ca3c flys-backend/src/main/java/de/intevation/flys/importer/ImportWstColumnQRange.java --- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportWstColumnQRange.java Wed Mar 23 10:46:58 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportWstColumnQRange.java Wed Mar 23 15:22:32 2011 +0000 @@ -1,17 +1,67 @@ package de.intevation.flys.importer; import de.intevation.flys.model.WstColumnQRange; +import de.intevation.flys.model.WstQRange; +import de.intevation.flys.model.WstColumn; +import de.intevation.flys.model.River; + +import org.hibernate.Session; +import org.hibernate.Query; + +import java.util.List; public class ImportWstColumnQRange { + protected ImportWstColumn wstColumn; + protected ImportWstQRange qRange; + protected WstColumnQRange peer; public ImportWstColumnQRange() { } - public WstColumnQRange getPeer() { + public ImportWstColumnQRange( + ImportWstColumn wstColumn, + ImportWstQRange qRange + ) { + this.wstColumn = wstColumn; + this.qRange = qRange; + } + + public ImportWstColumn getWstColumn() { + return wstColumn; + } + + public void setWstColumn(ImportWstColumn wstColumn) { + this.wstColumn = wstColumn; + } + + public ImportWstQRange getQRange() { + return qRange; + } + + public void setQRange(ImportWstQRange qRange) { + this.qRange = qRange; + } + + public WstColumnQRange getPeer(River river) { if (peer == null) { - // TODO: Implement me! + WstColumn c = wstColumn.getPeer(river); + WstQRange q = qRange.getPeer(river); + Session session = Importer.sessionHolder.get(); + Query query = session.createQuery( + "from WstColumnQRange where " + + "wstColumn=:c and wstQRange=:q"); + query.setParameter("c", c); + query.setParameter("q", q); + List cols = query.list(); + if (cols.isEmpty()) { + peer = new WstColumnQRange(c, q); + session.save(peer); + } + else { + peer = cols.get(0); + } } return peer; } diff -r 88048d4f6e4d -r 3169b559ca3c flys-backend/src/main/java/de/intevation/flys/importer/ImportWstQRange.java --- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportWstQRange.java Wed Mar 23 10:46:58 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportWstQRange.java Wed Mar 23 15:22:32 2011 +0000 @@ -1,17 +1,67 @@ package de.intevation.flys.importer; +import java.math.BigDecimal; + import de.intevation.flys.model.WstQRange; +import de.intevation.flys.model.River; +import de.intevation.flys.model.Range; + +import org.hibernate.Session; +import org.hibernate.Query; + +import java.util.List; public class ImportWstQRange { + protected ImportRange range; + protected BigDecimal q; + protected WstQRange peer; public ImportWstQRange() { } - public WstQRange getPeer() { + public ImportWstQRange( + ImportRange range, + BigDecimal q + ) { + this.range = range; + this.q = q; + } + + public ImportRange getRange() { + return range; + } + + public void setRange(ImportRange range) { + this.range = range; + } + + public BigDecimal getQ() { + return q; + } + + public void setQ(BigDecimal q) { + this.q = q; + } + + public WstQRange getPeer(River river) { if (peer == null) { - // TODO: Implement me! + Range r = range.getPeer(river); + Session session = Importer.sessionHolder.get(); + Query query = session.createQuery( + "from WstQRange where " + + "range=:range and q=:q"); + query.setParameter("range", r); + query.setParameter("q", q); + List wstQRanges = query.list(); + if (wstQRanges.isEmpty()) { + peer = new WstQRange(r, q); + session.save(peer); + } + else { + peer = wstQRanges.get(0); + } } return peer; } diff -r 88048d4f6e4d -r 3169b559ca3c flys-backend/src/main/java/de/intevation/flys/importer/WstParser.java --- a/flys-backend/src/main/java/de/intevation/flys/importer/WstParser.java Wed Mar 23 10:46:58 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/WstParser.java Wed Mar 23 15:22:32 2011 +0000 @@ -19,6 +19,8 @@ import java.util.regex.Pattern; import java.util.regex.Matcher; +import java.math.BigDecimal; + public class WstParser { private static Logger log = Logger.getLogger(WstParser.class); @@ -38,13 +40,25 @@ public static final Pattern UNIT = Pattern.compile("[^\\[]*\\[([^]]+)\\].*"); + protected ImportWst wst; + public WstParser() { } + public ImportWst getWst() { + return wst; + } + + public void setWst(ImportWst wst) { + this.wst = wst; + } + public void parse(File file) throws IOException { log.info("Parsing WST file '" + file + "'"); + wst = new ImportWst(file.getName()); + LineNumberReader in = null; try { in = @@ -68,7 +82,6 @@ double minKm = Double.MAX_VALUE; double maxKm = -Double.MAX_VALUE; - boolean bFirstComment = true; boolean columnHeaderChecked = false; double lastKm = Double.MAX_VALUE; @@ -280,11 +293,16 @@ // TODO: store me! } - protected static String format(double value) { + private static final NumberFormat NF = getNumberFormat(); + + private static getNumberFormat() { NumberFormat nf = NumberFormat.getInstance(); nf.setMinimumFractionDigits(2); nf.setMaximumFractionDigits(2); - return nf.format(value); + } + + protected static String format(double value) { + return NF.format(value); } protected void addInterval( @@ -292,21 +310,25 @@ double to, Map values ) { + log.debug("addInterval: " + from + " " + to); + if (values == null) { return; } - if (log.isDebugEnabled()) { - log.debug("addInterval: " + from + " " + to); - StringBuilder sb = new StringBuilder(); - for (Map.Entry entry: values.entrySet()) { - if (sb.length() > 0) { - sb.append("; "); - } - sb.append('"').append(entry.getKey()) - .append("\": ") - .append(entry.getValue()); - } - log.debug("entries: " + sb.toString()); + + if (from > to) { double t = from; from = to; to = t; } + + ImportRange range = new ImportRange( + new BigDecimal(from), + new BigDecimal(to)); + + for (Map.Entry entry: values.entrySet()) { + BigDecimal q = new BigDecimal(entry.getValue()); + ImportWstQRange wstQRange = new ImportWstQRange(range, q); + + String columnName = entry.getKey(); + ImportWstColumn column = wst.getColumn(columnName); + column.addColumnQRange(wstQRange); } } diff -r 88048d4f6e4d -r 3169b559ca3c flys-backend/src/main/java/de/intevation/flys/model/Range.java --- a/flys-backend/src/main/java/de/intevation/flys/model/Range.java Wed Mar 23 10:46:58 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/model/Range.java Wed Mar 23 15:22:32 2011 +0000 @@ -28,6 +28,12 @@ public Range() { } + public Range(BigDecimal a, BigDecimal b, River river) { + this.a = a; + this.b = b; + this.river = river; + } + @Id @SequenceGenerator( name = "SEQUENCE_RANGES_ID_SEQ", @@ -45,12 +51,6 @@ this.id = id; } - public Range(BigDecimal a, BigDecimal b, River river) { - this.a = a; - this.b = b; - this.river = river; - } - @Column(name = "a") // FIXME: type mapping needed? public BigDecimal getA() { return a; diff -r 88048d4f6e4d -r 3169b559ca3c flys-backend/src/main/java/de/intevation/flys/model/Wst.java --- a/flys-backend/src/main/java/de/intevation/flys/model/Wst.java Wed Mar 23 10:46:58 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/model/Wst.java Wed Mar 23 15:22:32 2011 +0000 @@ -24,6 +24,11 @@ public Wst() { } + public Wst(River river, String description) { + this.river = river; + this.description = description; + } + @Id @SequenceGenerator( name = "SEQUENCE_WSTS_ID_SEQ", diff -r 88048d4f6e4d -r 3169b559ca3c flys-backend/src/main/java/de/intevation/flys/model/WstColumn.java --- a/flys-backend/src/main/java/de/intevation/flys/model/WstColumn.java Wed Mar 23 10:46:58 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/model/WstColumn.java Wed Mar 23 15:22:32 2011 +0000 @@ -31,6 +31,18 @@ public WstColumn() { } + public WstColumn( + Wst wst, + String name, + String description, + TimeInterval timeInterval + ) { + this.wst = wst; + this.name = name; + this.description = description; + this.timeInterval = timeInterval; + } + @Id @SequenceGenerator( name = "SEQUENCE_WST_COLUMNS_ID_SEQ", diff -r 88048d4f6e4d -r 3169b559ca3c flys-backend/src/main/java/de/intevation/flys/model/WstColumnQRange.java --- a/flys-backend/src/main/java/de/intevation/flys/model/WstColumnQRange.java Wed Mar 23 10:46:58 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/model/WstColumnQRange.java Wed Mar 23 15:22:32 2011 +0000 @@ -24,6 +24,14 @@ public WstColumnQRange() { } + public WstColumnQRange( + WstColumn wstColumn, + WstQRange wstQRange + ) { + this.wstColumn = wstColumn; + this.wstQRange = wstQRange; + } + @Id @SequenceGenerator( name = "SEQUENCE_WST_Q_RANGES_ID_SEQ", diff -r 88048d4f6e4d -r 3169b559ca3c flys-backend/src/main/java/de/intevation/flys/model/WstQRange.java --- a/flys-backend/src/main/java/de/intevation/flys/model/WstQRange.java Wed Mar 23 10:46:58 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/model/WstQRange.java Wed Mar 23 15:22:32 2011 +0000 @@ -26,6 +26,11 @@ public WstQRange() { } + public WstQRange(Range range, BigDecimal q) { + this.range = range; + this.q = q; + } + @Id @SequenceGenerator( name = "SEQUENCE_WST_Q_RANGES_ID_SEQ",