changeset 195:8d3533a03e10

Second part of parsing/storing main values. Should be finished now. flys-backend/trunk@1534 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 21 Mar 2011 15:06:47 +0000
parents 0f0b98ef9b04
children a33c065b95eb
files flys-backend/ChangeLog flys-backend/src/main/java/de/intevation/flys/importer/ImportGauge.java flys-backend/src/main/java/de/intevation/flys/importer/ImportMainValue.java flys-backend/src/main/java/de/intevation/flys/importer/ImportMainValueType.java flys-backend/src/main/java/de/intevation/flys/importer/ImportNamedMainValue.java flys-backend/src/main/java/de/intevation/flys/importer/StaFileParser.java flys-backend/src/main/java/de/intevation/flys/model/MainValue.java flys-backend/src/main/java/de/intevation/flys/model/NamedMainValue.java
diffstat 8 files changed, 264 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/ChangeLog	Mon Mar 21 10:52:33 2011 +0000
+++ b/flys-backend/ChangeLog	Mon Mar 21 15:06:47 2011 +0000
@@ -1,3 +1,25 @@
+2011-03-21	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	Second part of parsing/storing main values. Should be finished now.
+
+	* src/main/java/de/intevation/flys/importer/ImportNamedMainValue.java,
+	  src/main/java/de/intevation/flys/importer/ImportMainValue.java:
+	  New. Helper models for import main values,
+
+	* src/main/java/de/intevation/flys/model/MainValue.java,
+	  src/main/java/de/intevation/flys/model/NamedMainValue.java:
+	  Added convinience constructors.
+
+	* src/main/java/de/intevation/flys/importer/ImportGauge.java:
+	  Write main values to backend, too.
+
+	* src/main/java/de/intevation/flys/importer/StaFileParser.java:
+	  Build importer models for main values.
+
+	* src/main/java/de/intevation/flys/importer/ImportMainValueType.java:
+	  Data was called 'value'. Now it is 'name' to fit the
+	  schema.
+
 2011-03-21	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* doc/schema/postgresql.sql: Fixed wrong unique constraint.
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportGauge.java	Mon Mar 21 10:52:33 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportGauge.java	Mon Mar 21 15:06:47 2011 +0000
@@ -28,6 +28,10 @@
 
     protected Gauge  peer;
 
+    protected List<ImportMainValueType>  mainValueTypes;
+    protected List<ImportNamedMainValue> namedMainValues;
+    protected List<ImportMainValue>      mainValues;
+
     public ImportGauge() {
     }
 
@@ -89,14 +93,52 @@
         this.station = station;
     }
 
+    public List<ImportMainValueType> getMainValueTypes() {
+        return mainValueTypes;
+    }
+
+    public void setMainValueTypes(List<ImportMainValueType> mainValueTypes) {
+        this.mainValueTypes = mainValueTypes;
+    }
+
+    public List<ImportNamedMainValue> getNamedMainValues() {
+        return namedMainValues;
+    }
+
+    public void setNamedMainValues(List<ImportNamedMainValue> namedMainValues) {
+        this.namedMainValues = namedMainValues;
+    }
+
+    public List<ImportMainValue> getMainValues() {
+        return mainValues;
+    }
+
+    public void setMainValues(List<ImportMainValue> mainValues) {
+        this.mainValues = mainValues;
+    }
+
     public void parseDependencies() throws IOException {
         StaFileParser sfp = new StaFileParser();
         sfp.parse(this);
     }
 
     public void storeDependencies(River river) {
+
         Gauge gauge = getPeer(river);
-        // TODO: Implement me!
+
+        for (ImportMainValueType mainValueType: mainValueTypes) {
+            mainValueType.getPeer();
+        }
+
+        for (ImportNamedMainValue namedMainValue: namedMainValues) {
+            namedMainValue.getPeer();
+        }
+
+        for (ImportMainValue mainValue: mainValues) {
+            mainValue.getPeer(river);
+        }
+
+        // TODO: Implement ATs
     }
 
     public Gauge getPeer(River river) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportMainValue.java	Mon Mar 21 15:06:47 2011 +0000
@@ -0,0 +1,83 @@
+package de.intevation.flys.importer;
+
+import java.util.List;
+
+import java.math.BigDecimal;
+
+import de.intevation.flys.model.MainValue;
+import de.intevation.flys.model.Gauge;
+import de.intevation.flys.model.River;
+import de.intevation.flys.model.NamedMainValue;
+
+import org.hibernate.Session;
+import org.hibernate.Query;
+
+public class ImportMainValue
+{
+    protected ImportGauge          gauge;
+    protected ImportNamedMainValue mainValue;
+    protected BigDecimal           value;
+
+    protected MainValue peer;
+
+    public ImportMainValue() {
+    }
+
+    public ImportMainValue(
+        ImportGauge          gauge,
+        ImportNamedMainValue mainValue,
+        BigDecimal           value
+    ) {
+        this.gauge     = gauge;
+        this.mainValue = mainValue;
+        this.value     = value;
+    }
+
+    public ImportGauge getGauge() {
+        return gauge;
+    }
+
+    public void setGauge(ImportGauge gauge) {
+        this.gauge = gauge;
+    }
+
+    public ImportNamedMainValue getMainValue() {
+        return mainValue;
+    }
+
+    public void setMainValue(ImportNamedMainValue mainValue) {
+        this.mainValue = mainValue;
+    }
+
+    public BigDecimal getValue() {
+        return value;
+    }
+
+    public void setValue(BigDecimal value) {
+        this.value = value;
+    }
+
+    public MainValue getPeer(River river) {
+        if (peer == null) {
+            Session session = Importer.sessionHolder.get();
+            Query query = session.createQuery("from MainValue where "
+                + "gauge.id=:gauge_id and mainValue.id=:name_id "
+                + "and value=:value");
+            Gauge          g = gauge.getPeer(river);
+            NamedMainValue n = mainValue.getPeer();
+            query.setParameter("gauge_id", g.getId());
+            query.setParameter("name_id",  n.getId());
+            query.setParameter("value",    value);
+            List<MainValue> values = query.list();
+            if (values.isEmpty()) {
+                peer = new MainValue(g, n, value, null);
+                session.save(peer);
+            }
+            else {
+                peer = values.get(0);
+            }
+        }
+        return peer;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportMainValueType.java	Mon Mar 21 10:52:33 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportMainValueType.java	Mon Mar 21 15:06:47 2011 +0000
@@ -10,49 +10,49 @@
 public class ImportMainValueType
 implements   Comparable<ImportMainValueType>
 {
-    protected String value;
+    protected String name;
 
     protected MainValueType peer;
 
     public ImportMainValueType() {
     }
 
-    public ImportMainValueType(String value) {
-        this.value = value;
+    public ImportMainValueType(String name) {
+        this.name = name;
     }
 
-    public String getValue() {
-        return value;
+    public String getName() {
+        return name;
     }
 
-    public void setValue(String value) {
-        this.value = value;
+    public void setName(String name) {
+        this.name = name;
     }
 
     public int compareTo(ImportMainValueType other) {
-        return value.compareTo(other.value);
+        return name.compareTo(other.name);
     }
 
     @Override
     public boolean equals(Object other) {
         if (other == this) return true;
         if (!(other instanceof ImportMainValueType)) return false;
-        return value.equals(((ImportMainValueType)other).value);
+        return name.equals(((ImportMainValueType)other).name);
     }
 
     @Override
     public int hashCode() {
-        return value.hashCode();
+        return name.hashCode();
     }
 
     public MainValueType getPeer() {
         if (peer == null) {
             Session session = Importer.sessionHolder.get();
-            Query query = session.createQuery("from MainValueType where value=:value");
-            query.setString("value", value);
+            Query query = session.createQuery("from MainValueType where name=:name");
+            query.setString("name", name);
             List<MainValueType> values = query.list();
             if (values.isEmpty()) {
-                peer = new MainValueType(value);
+                peer = new MainValueType(name);
                 session.save(peer);
             }
             else {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportNamedMainValue.java	Mon Mar 21 15:06:47 2011 +0000
@@ -0,0 +1,66 @@
+package de.intevation.flys.importer;
+
+import java.util.List;
+
+import de.intevation.flys.model.NamedMainValue;
+import de.intevation.flys.model.MainValueType;
+
+import org.hibernate.Session;
+import org.hibernate.Query;
+
+public class ImportNamedMainValue
+{
+    protected ImportMainValueType mainValueType;
+    protected String              name;
+
+    protected NamedMainValue      peer;
+
+    public ImportNamedMainValue() {
+    }
+
+    public ImportNamedMainValue(
+        ImportMainValueType mainValueType,
+        String              name
+    ) {
+        this.mainValueType = mainValueType;
+        this.name          = name;
+    }
+
+    public ImportMainValueType getMainValueType() {
+        return mainValueType;
+    }
+
+    public void setMainValueType(ImportMainValueType mainValueType) {
+        this.mainValueType = mainValueType;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public NamedMainValue getPeer() {
+        if (peer == null) {
+            MainValueType type = mainValueType.getPeer();
+            Session session = Importer.sessionHolder.get();
+            Query query = session.createQuery(
+                "from NamedMainValue where " +
+                "name=:name and type.id=:id");
+            query.setString("name", name);
+            query.setParameter("id", type.getId());
+            List<NamedMainValue> named = query.list();
+            if (named.isEmpty()) {
+                peer = new NamedMainValue(name, type);
+                session.save(peer);
+            }
+            else {
+                peer = named.get(0);
+            }
+        }
+        return peer;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-backend/src/main/java/de/intevation/flys/importer/StaFileParser.java	Mon Mar 21 10:52:33 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/StaFileParser.java	Mon Mar 21 15:06:47 2011 +0000
@@ -12,6 +12,7 @@
 import java.util.regex.Matcher;
 
 import java.util.HashMap;
+import java.util.ArrayList;
 
 import org.apache.log4j.Logger;
 
@@ -99,6 +100,12 @@
             HashMap<String, ImportMainValueType> types =
                 new HashMap<String, ImportMainValueType>();
 
+            ArrayList<ImportNamedMainValue> namedMainValues =
+                new ArrayList<ImportNamedMainValue>();
+
+            ArrayList<ImportMainValue> mainValues =
+                new ArrayList<ImportMainValue>();
+
             while ((line = in.readLine()) != null) {
                 Matcher m = QWTD_.matcher(line);
                 if (m.matches()) {
@@ -118,11 +125,24 @@
                         type = new ImportMainValueType(typeString);
                         types.put(typeString, type);
                     }
+                    String name = m.group(1);
+                    ImportNamedMainValue namedMainValue =
+                        new ImportNamedMainValue(type, name);
+                    namedMainValues.add(namedMainValue);
+
+                    ImportMainValue mainValue =
+                        new ImportMainValue(gauge, namedMainValue, value);
+
+                    mainValues.add(mainValue);
                 }
                 else {
                     // TODO: treat as a comment
                 }
             }
+            gauge.setMainValueTypes(
+                new ArrayList<ImportMainValueType>(types.values()));
+            gauge.setNamedMainValues(namedMainValues);
+            gauge.setMainValues(mainValues);
         }
         finally {
             if (in != null) {
--- a/flys-backend/src/main/java/de/intevation/flys/model/MainValue.java	Mon Mar 21 10:52:33 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/model/MainValue.java	Mon Mar 21 15:06:47 2011 +0000
@@ -32,6 +32,18 @@
     public MainValue() {
     }
 
+    public MainValue(
+        Gauge          gauge,
+        NamedMainValue mainValue,
+        BigDecimal     value,
+        TimeInterval   timeInterval
+    ) {
+        this.gauge        = gauge;
+        this.mainValue    = mainValue;
+        this.value        = value;
+        this.timeInterval = timeInterval;
+    }
+
     @Id
     @SequenceGenerator(
         name           = "SEQUENCE_MAIN_VALUES_ID_SEQ",
--- a/flys-backend/src/main/java/de/intevation/flys/model/NamedMainValue.java	Mon Mar 21 10:52:33 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/model/NamedMainValue.java	Mon Mar 21 15:06:47 2011 +0000
@@ -24,6 +24,11 @@
     public NamedMainValue() {
     }
 
+    public NamedMainValue(String name, MainValueType type) {
+        this.name = name;
+        this.type = type;
+    }
+
     @Id
     @SequenceGenerator(
         name           = "SEQUENCE_NAMED_MAIN_VALUES_ID_SEQ",

http://dive4elements.wald.intevation.org