changeset 188:003ac16812dd

Store annotations in backend. flys-backend/trunk@1518 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 18 Mar 2011 12:10:33 +0000
parents ecf90018563b
children bc3747a371cc
files flys-backend/ChangeLog flys-backend/src/main/java/de/intevation/flys/importer/ImportAnnotation.java flys-backend/src/main/java/de/intevation/flys/importer/ImportAttribute.java flys-backend/src/main/java/de/intevation/flys/importer/ImportPosition.java 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/Importer.java flys-backend/src/main/java/de/intevation/flys/model/Annotation.java flys-backend/src/main/java/de/intevation/flys/model/Range.java flys-backend/src/main/java/de/intevation/flys/model/River.java
diffstat 10 files changed, 186 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/ChangeLog	Thu Mar 17 18:07:13 2011 +0000
+++ b/flys-backend/ChangeLog	Fri Mar 18 12:10:33 2011 +0000
@@ -1,3 +1,29 @@
+2011-03-17	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	Store annotations in backend.
+
+	* src/main/java/de/intevation/flys/model/Annotation.java:
+	  New convinience constructor.
+
+	* src/main/java/de/intevation/flys/model/River.java:
+	  Added toString() method.
+
+	* src/main/java/de/intevation/flys/model/Range.java:
+	  Fixed nasty mistake in @OneToOne annotatation.
+	  New convinience constructors.
+
+	* src/main/java/de/intevation/flys/importer/ImportPosition.java
+	  src/main/java/de/intevation/flys/importer/ImportAnnotation.java,
+	  src/main/java/de/intevation/flys/importer/ImportRange.java
+	  src/main/java/de/intevation/flys/importer/ImportAttribute.java:
+	  Make storing to backend work. It's a bit too slow. :-/
+
+	* src/main/java/de/intevation/flys/importer/ImportRiver.java:
+	  Fetch peer from backend. Added method to store annotations.
+	* src/main/java/de/intevation/flys/importer/Importer.java:
+	  Stored annotations into backend. More eloquent SQL exception
+	  handling.
+
 2011-03-17	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/model/Attribute.java,
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportAnnotation.java	Thu Mar 17 18:07:13 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportAnnotation.java	Fri Mar 18 12:10:33 2011 +0000
@@ -1,5 +1,16 @@
 package de.intevation.flys.importer;
 
+import de.intevation.flys.model.Annotation;
+import de.intevation.flys.model.Range;
+import de.intevation.flys.model.Position;
+import de.intevation.flys.model.Attribute;
+import de.intevation.flys.model.River;
+
+import org.hibernate.Session;
+import org.hibernate.Query;
+
+import java.util.List;
+
 public class ImportAnnotation
 implements   Comparable<ImportAnnotation>
 {
@@ -7,6 +18,8 @@
     protected ImportPosition  position;
     protected ImportRange     range;
 
+    protected Annotation      peer;
+
     public ImportAnnotation() {
     }
 
@@ -60,5 +73,29 @@
     public void setRange(ImportRange range) {
         this.range = range;
     }
+
+    public Annotation getPeer(River river) {
+        if (peer == null) {
+            Range     r = range.getPeer(river);
+            Attribute a = attribute.getPeer();
+            Position  p = position.getPeer();
+            Session session = Importer.sessionHolder.get();
+            Query query = session.createQuery(
+                "from Annotation where " +
+                "range=:range and attribute=:attribute and position=:position");
+            query.setParameter("range",     r);
+            query.setParameter("attribute", a);
+            query.setParameter("position",  p);
+            List<Annotation> annotations = query.list();
+            if (annotations.isEmpty()) {
+                peer = new Annotation(r, a, p);
+                session.save(peer);
+            }
+            else {
+                peer = annotations.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/ImportAttribute.java	Thu Mar 17 18:07:13 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportAttribute.java	Fri Mar 18 12:10:33 2011 +0000
@@ -46,7 +46,7 @@
     }
 
     public Attribute getPeer() {
-        if (peer != null) {
+        if (peer == null) {
             Session session = Importer.sessionHolder.get();
             Query query = session.createQuery("from Attribute where value=:value");
             query.setString("value", value);
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportPosition.java	Thu Mar 17 18:07:13 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportPosition.java	Fri Mar 18 12:10:33 2011 +0000
@@ -34,7 +34,7 @@
     }
 
     public Position getPeer() {
-        if (peer != null) {
+        if (peer == null) {
             Session session = Importer.sessionHolder.get();
             Query query = session.createQuery("from Position where value=:value");
             query.setString("value", value);
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportRange.java	Thu Mar 17 18:07:13 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportRange.java	Fri Mar 18 12:10:33 2011 +0000
@@ -1,10 +1,26 @@
 package de.intevation.flys.importer;
 
+import de.intevation.flys.model.Range;
+import de.intevation.flys.model.River;
+
+import org.hibernate.Session;
+import org.hibernate.Query;
+
+import java.util.List;
+
+import java.math.BigDecimal;
+
+import org.apache.log4j.Logger;
+
 public class ImportRange
 implements   Comparable<ImportRange>
 {
-    public Double from;
-    public Double to;
+    private static Logger log = Logger.getLogger(ImportRange.class);
+
+    protected Double from;
+    protected Double to;
+
+    protected Range peer;
 
     public ImportRange() {
     }
@@ -48,5 +64,27 @@
     public void setTo(Double to) {
         this.to = to;
     }
+
+    public Range getPeer(River river) {
+        if (peer == null) {
+            Session session = Importer.sessionHolder.get();
+            Query query = session.createQuery(
+                "from Range where a=:a and b=:b and river.id=:river");
+            BigDecimal a = from != null ? BigDecimal.valueOf(from) : null;
+            BigDecimal b = to   != null ? BigDecimal.valueOf(to) : null;
+            query.setParameter("a", a);
+            query.setParameter("b", b);
+            query.setParameter("river", river.getId());
+            List<Range> ranges = query.list();
+            if (ranges.isEmpty()) {
+                peer = new Range(a, b, river);
+                session.save(peer);
+            }
+            else {
+                peer = ranges.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/ImportRiver.java	Thu Mar 17 18:07:13 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java	Fri Mar 18 12:10:33 2011 +0000
@@ -7,8 +7,13 @@
 
 import org.apache.log4j.Logger;
 
+import de.intevation.flys.model.River;
+
 import de.intevation.flys.utils.FileTools;
 
+import org.hibernate.Session;
+import org.hibernate.Query;
+
 public class ImportRiver
 {
     private static Logger log = Logger.getLogger(ImportRiver.class);
@@ -25,6 +30,8 @@
 
     protected List<ImportAnnotation> annotations;
 
+    protected River peer;
+
     public ImportRiver() {
     }
 
@@ -85,5 +92,29 @@
 
         annotations = aparser.getAnnotations();
     }
+
+    public void storeAnnotations() {
+        River river = getPeer();
+        for (ImportAnnotation annotation: annotations) {
+            annotation.getPeer(river);
+        }
+    }
+
+    public River getPeer() {
+        if (peer == null) {
+            Session session = Importer.sessionHolder.get();
+            Query query = session.createQuery("from River where name=:name");
+            query.setString("name", name);
+            List<River> rivers = query.list();
+            if (rivers.isEmpty()) {
+                peer = new River(name);
+                session.save(peer);
+            }
+            else {
+                peer = rivers.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/Importer.java	Thu Mar 17 18:07:13 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/Importer.java	Fri Mar 18 12:10:33 2011 +0000
@@ -5,12 +5,15 @@
 
 import java.util.List;
 
+import java.sql.SQLException;
+
 import org.apache.log4j.Logger;
 
 import org.hibernate.SessionFactory;
 import org.hibernate.Session;
 import org.hibernate.Transaction;
 import org.hibernate.Query;
+import org.hibernate.HibernateException;
 
 import de.intevation.flys.backend.SessionFactoryProvider;
 
@@ -42,18 +45,15 @@
 
     public void writeRivers() {
         log.debug("write rivers started");
+
         Session session = sessionHolder.get();
 
-        Query query = session.createQuery("from River where name=:name");
-        for (ImportRiver iriver: rivers) {
-            log.debug("writing river '" + iriver.getName() + "'");
-            query.setString("name", iriver.getName());
-            List<River> dbrivers = (List<River>)query.list();
-            if (dbrivers.isEmpty()) {
-                River dbriver = new River(iriver.getName());
-                session.save(dbriver);
-            }
+        for (ImportRiver river: rivers) {
+            log.debug("writing river '" + river.getName() + "'");
+            river.storeAnnotations();
+            session.flush();
         }
+
         log.debug("write rivers finished");
     }
 
@@ -70,7 +70,18 @@
         try {
             tx = session.beginTransaction();
 
-            writeRivers();
+            try {
+                writeRivers();
+            }
+            catch (HibernateException he) {
+                Throwable t = he.getCause();
+                while (t instanceof SQLException) {
+                    SQLException sqle = (SQLException)t;
+                    log.error("SQL exeception chain:", sqle);
+                    t = sqle.getNextException();
+                }
+                throw he;
+            }
 
             tx.commit();
         }
--- a/flys-backend/src/main/java/de/intevation/flys/model/Annotation.java	Thu Mar 17 18:07:13 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/model/Annotation.java	Fri Mar 18 12:10:33 2011 +0000
@@ -25,6 +25,12 @@
     public Annotation() {
     }
 
+    public Annotation(Range range, Attribute attribute, Position position) {
+        this.range     = range;
+        this.attribute = attribute;
+        this.position  = position;
+    }
+
     @Id
     @SequenceGenerator(
         name           = "SEQUENCE_ANNOTATIONS_ID_SEQ",
--- a/flys-backend/src/main/java/de/intevation/flys/model/Range.java	Thu Mar 17 18:07:13 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/model/Range.java	Fri Mar 18 12:10:33 2011 +0000
@@ -45,6 +45,19 @@
         this.id = id;
     }
 
+    public Range(Double a, Double b, River river) {
+        this(
+            a != null ? BigDecimal.valueOf(a) : null,
+            b != null ? BigDecimal.valueOf(b) : null,
+            river);
+    }
+
+    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;
@@ -63,14 +76,14 @@
         this.b = b;
     }
 
+    @OneToOne
+    @JoinColumn(name = "river_id")
+    public River getRiver() {
+        return river;
+    }
+
     public void setRiver(River river) {
         this.river = river;
     }
-
-    @OneToOne
-    @JoinColumn(name = "id")
-    public River getRiver() {
-        return river;
-    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-backend/src/main/java/de/intevation/flys/model/River.java	Thu Mar 17 18:07:13 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/model/River.java	Fri Mar 18 12:10:33 2011 +0000
@@ -67,5 +67,9 @@
     public void setGauges(List<Gauge> gauges) {
         this.gauges = gauges;
     }
+
+    public String toString() {
+        return name != null ? name : "";
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org