changeset 201:3169b559ca3c

Build models for wsts, wst columns and q ranges and store them in the backend. flys-backend/trunk@1549 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 23 Mar 2011 15:22:32 +0000
parents 88048d4f6e4d
children 29a408f80a89
files flys-backend/ChangeLog flys-backend/src/main/java/de/intevation/flys/importer/ImportRange.java flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java flys-backend/src/main/java/de/intevation/flys/importer/ImportWst.java flys-backend/src/main/java/de/intevation/flys/importer/ImportWstColumn.java flys-backend/src/main/java/de/intevation/flys/importer/ImportWstColumnQRange.java flys-backend/src/main/java/de/intevation/flys/importer/ImportWstQRange.java flys-backend/src/main/java/de/intevation/flys/importer/WstParser.java flys-backend/src/main/java/de/intevation/flys/model/Range.java flys-backend/src/main/java/de/intevation/flys/model/Wst.java flys-backend/src/main/java/de/intevation/flys/model/WstColumn.java flys-backend/src/main/java/de/intevation/flys/model/WstColumnQRange.java flys-backend/src/main/java/de/intevation/flys/model/WstQRange.java
diffstat 13 files changed, 357 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- 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	<sascha.teichmann@intevation.de>
+
+	* 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	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/importer/WstParser.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<Range> ranges = query.list();
             if (ranges.isEmpty()) {
                 peer = new Range(a, b, river);
--- 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<ImportAnnotation> 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() {
--- 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<String, ImportWstColumn> columns;
+
     protected Wst peer;
 
     public ImportWst() {
+        columns = new HashMap<String, ImportWstColumn>();
     }
 
-    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<Wst> wsts = query.list();
+            if (wsts.isEmpty()) {
+                peer = new Wst(river, description);
+                session.save(peer);
+            }
+            else {
+                peer = wsts.get(0);
+            }
+
         }
         return peer;
     }
--- 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<ImportWstColumnQRange> columnQRanges;
+
     protected WstColumn peer;
 
     public ImportWstColumn() {
+        columnQRanges = new ArrayList<ImportWstColumnQRange>();
     }
 
-    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<WstColumn> columns = query.list();
+            if (columns.isEmpty()) {
+                peer = new WstColumn(w, name, description, null);
+                session.save(peer);
+            }
+            else {
+                peer = columns.get(0);
+            }
         }
         return peer;
     }
--- 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<WstColumnQRange> cols = query.list();
+            if (cols.isEmpty()) {
+                peer = new WstColumnQRange(c, q);
+                session.save(peer);
+            }
+            else {
+                peer = cols.get(0);
+            }
         }
         return peer;
     }
--- 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<WstQRange> wstQRanges = query.list();
+            if (wstQRanges.isEmpty()) {
+                peer = new WstQRange(r, q);
+                session.save(peer);
+            }
+            else {
+                peer = wstQRanges.get(0);
+            }
         }
         return peer;
     }
--- 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<String, Double> 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<String, Double> 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<String, Double> 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);
         }
     }
 
--- 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;
--- 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",
--- 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",
--- 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",
--- 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",

http://dive4elements.wald.intevation.org