changeset 2346:f834b411ca57

Added db table, model class and importer stuff for units. flys-backend/trunk@2877 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 04 Oct 2011 06:47:00 +0000
parents c3ea91aee989
children 0acf28a3d28a
files flys-backend/ChangeLog flys-backend/doc/schema/postgresql.sql flys-backend/src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java flys-backend/src/main/java/de/intevation/flys/importer/ImportUnit.java flys-backend/src/main/java/de/intevation/flys/importer/ImportWst.java flys-backend/src/main/java/de/intevation/flys/importer/parsers/WstParser.java flys-backend/src/main/java/de/intevation/flys/model/Unit.java flys-backend/src/main/java/de/intevation/flys/model/Wst.java
diffstat 8 files changed, 180 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/ChangeLog	Tue Oct 04 05:06:10 2011 +0000
+++ b/flys-backend/ChangeLog	Tue Oct 04 06:47:00 2011 +0000
@@ -1,3 +1,25 @@
+2011-10-04  Ingo Weinzierl <ingo@intevation.de>
+
+	* doc/schema/postgresql.sql: Added a "units" table.
+
+	* src/main/java/de/intevation/flys/model/Unit.java: New. Model class that
+	  represents a unit.
+
+	* src/main/java/de/intevation/flys/importer/ImportUnit.java: New. Model
+	  class that is used to import units.
+
+	* src/main/java/de/intevation/flys/model/Wst.java: A Wst stores a Unit
+	  which references "units" table now.
+
+	* src/main/java/de/intevation/flys/importer/parsers/WstParser.java: Set
+	  units which have already been parsed on Wsts.
+
+	* src/main/java/de/intevation/flys/importer/ImportWst.java: Added setter
+	  method for Units and call storeDependencies() for ImportUnits.
+
+	* src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java:
+	  Registered new model class Unit.
+
 2011-10-04  Ingo Weinzierl <ingo@intevation.de>
 
 	flys/issue333 (W-INFO / Berechnung Wasserspiegellage, Zuordnung Bezugspegel)
--- a/flys-backend/doc/schema/postgresql.sql	Tue Oct 04 05:06:10 2011 +0000
+++ b/flys-backend/doc/schema/postgresql.sql	Tue Oct 04 06:47:00 2011 +0000
@@ -17,6 +17,13 @@
     value VARCHAR(256)    NOT NULL UNIQUE
 );
 
+CREATE SEQUENCE UNITS_ID_SEQ;
+
+CREATE TABLE units (
+    id   int PRIMARY KEY NOT NULL,
+    name VARCHAR(32)     NOT NULL UNIQUE
+);
+
 -- segments from/to at a river
 CREATE SEQUENCE RANGES_ID_SEQ;
 
@@ -162,6 +169,7 @@
     river_id    int NOT NULL REFERENCES rivers(id),
     description VARCHAR(256) NOT NULL,
     kind        int NOT NULL DEFAULT 0,
+    unit_id     int NOT NULL REFERENCES units(id),
     -- TODO: more meta infos
     UNIQUE (river_id, description)
 );
--- a/flys-backend/src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java	Tue Oct 04 05:06:10 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java	Tue Oct 04 06:47:00 2011 +0000
@@ -48,6 +48,7 @@
 import de.intevation.flys.model.River;
 import de.intevation.flys.model.RiverAxis;
 import de.intevation.flys.model.TimeInterval;
+import de.intevation.flys.model.Unit;
 import de.intevation.flys.model.WstColumn;
 import de.intevation.flys.model.WstColumnQRange;
 import de.intevation.flys.model.WstColumnValue;
@@ -232,6 +233,7 @@
         cfg.addAnnotatedClass(River.class);
         cfg.addAnnotatedClass(RiverAxis.class);
         cfg.addAnnotatedClass(TimeInterval.class);
+        cfg.addAnnotatedClass(Unit.class);
         cfg.addAnnotatedClass(WstColumn.class);
         cfg.addAnnotatedClass(WstColumnQRange.class);
         cfg.addAnnotatedClass(WstColumnValue.class);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportUnit.java	Tue Oct 04 06:47:00 2011 +0000
@@ -0,0 +1,57 @@
+package de.intevation.flys.importer;
+
+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.Unit;
+
+
+public class ImportUnit
+{
+    private static final Logger log = Logger.getLogger(ImportUnit.class);
+
+    protected String name;
+
+    protected Unit peer;
+
+
+    public ImportUnit(String name) {
+        this.name = name;
+    }
+
+
+    public String getName() {
+        return name;
+    }
+
+
+    public void storeDependencies(River river) {
+        log.info("store '" + name + "'");
+        getPeer(river);
+    }
+
+    public Unit getPeer(River river) {
+        if (peer == null) {
+            Session session = ImporterSession.getInstance().getDatabaseSession();
+            Query query = session.createQuery("from Unit where name=:name");
+            query.setParameter("name", name);
+
+            List<Unit> units = query.list();
+            if (units.isEmpty()) {
+                peer = new Unit(name);
+                session.save(peer);
+            }
+            else {
+                peer = units.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/ImportWst.java	Tue Oct 04 05:06:10 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportWst.java	Tue Oct 04 06:47:00 2011 +0000
@@ -2,6 +2,7 @@
 
 import de.intevation.flys.model.Wst;
 import de.intevation.flys.model.River;
+import de.intevation.flys.model.Unit;
 
 import org.apache.log4j.Logger;
 
@@ -21,6 +22,8 @@
 
     protected List<ImportWstColumn> columns;
 
+    protected ImportUnit unit;
+
     protected Wst peer;
 
     public ImportWst() {
@@ -64,6 +67,10 @@
         return columns.get(index);
     }
 
+    public void setUnit(ImportUnit unit) {
+        this.unit = unit;
+    }
+
     public void storeDependencies(River river) {
 
         log.info("store '" + description + "'");
@@ -72,12 +79,16 @@
         for (ImportWstColumn column: columns) {
             column.storeDependencies(river);
         }
+
+        unit.storeDependencies(river);
+
         Session session = ImporterSession.getInstance().getDatabaseSession();
         session.flush();
     }
 
     public Wst getPeer(River river) {
         if (peer == null) {
+            Unit u = unit.getPeer(river);
             Session session = ImporterSession.getInstance().getDatabaseSession();
             Query query = session.createQuery(
                 "from Wst where " +
@@ -87,7 +98,7 @@
             query.setParameter("kind",        kind);
             List<Wst> wsts = query.list();
             if (wsts.isEmpty()) {
-                peer = new Wst(river, description, kind);
+                peer = new Wst(river, description, kind, u);
                 session.save(peer);
             }
             else {
--- a/flys-backend/src/main/java/de/intevation/flys/importer/parsers/WstParser.java	Tue Oct 04 05:06:10 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/parsers/WstParser.java	Tue Oct 04 06:47:00 2011 +0000
@@ -23,6 +23,7 @@
 import de.intevation.flys.importer.ImportWstQRange;
 import de.intevation.flys.importer.ImportWstColumn;
 import de.intevation.flys.importer.ImportRange;
+import de.intevation.flys.importer.ImportUnit;
 import de.intevation.flys.importer.ImportWst;
 
 public class WstParser
@@ -303,6 +304,9 @@
                 }
 
             }
+
+            wst.setUnit(new ImportUnit(einheit));
+
             addInterval(minKm, maxKm, aktAbfluesse);
         }
         finally {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/model/Unit.java	Tue Oct 04 06:47:00 2011 +0000
@@ -0,0 +1,60 @@
+package de.intevation.flys.model;
+
+import java.io.Serializable;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.SequenceGenerator;
+import javax.persistence.Table;
+
+
+@Entity
+@Table(name = "units")
+public class Unit
+implements   Serializable
+{
+    protected Integer id;
+    protected String  name;
+
+
+    public Unit() {
+    }
+
+
+    public Unit(String name) {
+        this.name = name;
+    }
+
+
+    @Id
+    @SequenceGenerator(
+        name           = "SEQUENCE_UNITS_ID_SEQ",
+        sequenceName   = "UNITS_ID_SEQ",
+        allocationSize = 1)
+    @GeneratedValue(
+        strategy  = GenerationType.SEQUENCE,
+        generator = "SEQUENCE_UNITS_ID_SEQ")
+    @Column(name = "id")
+    public Integer getId() {
+        return id;
+    }
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+
+    @Column(name = "name")
+    public String getName() {
+        return name;
+    }
+
+
+    public void setName(String name) {
+        this.name = name;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-backend/src/main/java/de/intevation/flys/model/Wst.java	Tue Oct 04 05:06:10 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/model/Wst.java	Tue Oct 04 06:47:00 2011 +0000
@@ -34,6 +34,7 @@
     private River   river;
     private String  description;
     private Integer kind;
+    private Unit    unit;
 
     private List<WstColumn> columns;
 
@@ -41,13 +42,14 @@
     }
 
     public Wst(River river, String description) {
-        this(river, description, 0);
+        this(river, description, 0, null);
     }
 
-    public Wst(River river, String description, Integer kind) {
+    public Wst(River river, String description, Integer kind, Unit unit) {
         this.river       = river;
         this.description = description;
         this.kind        = kind;
+        this.unit        = unit;
     }
 
     @Id
@@ -105,6 +107,17 @@
         this.columns = columns;
     }
 
+
+    @OneToOne
+    @JoinColumn(name = "unit_id" )
+    public Unit getUnit() {
+        return unit;
+    }
+
+    public void setUnit(Unit unit) {
+        this.unit = unit;
+    }
+
     /**
      * Determines the min and max Q values of this WST. The min value is placed
      * in the first field of the resulting array - the max value is placed in

http://dive4elements.wald.intevation.org