changeset 759:9f2204ed79ed

Import edges to database. flys-backend/trunk@2107 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 14 Jun 2011 13:13:58 +0000
parents bf16268629d9
children c8a2fbc612be
files flys-backend/src/main/java/de/intevation/flys/importer/AnnotationsParser.java flys-backend/src/main/java/de/intevation/flys/importer/ImportAnnotation.java flys-backend/src/main/java/de/intevation/flys/importer/ImportEdge.java
diffstat 3 files changed, 125 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/src/main/java/de/intevation/flys/importer/AnnotationsParser.java	Tue Jun 14 12:34:08 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/AnnotationsParser.java	Tue Jun 14 13:13:58 2011 +0000
@@ -90,10 +90,42 @@
                     continue;
                 }
 
+                ImportEdge edge = null;
+
+                if (parts.length == 4) { // Only 'Unterkante'
+                    try {
+                        edge = new ImportEdge(
+                            null,
+                            new BigDecimal(parts[3].trim().replace(',', '.')));
+                    }
+                    catch (NumberFormatException nfe) {
+                        log.warn("cannot parse 'Unterkante' in line " +
+                            in.getLineNumber());
+                    }
+                }
+                else if (parts.length > 4) { // 'Unterkante' and 'Oberkante'
+                    String bottom = parts[3].trim().replace(',', '.');
+                    String top    = parts[4].trim().replace(',', '.');
+                    try {
+                        BigDecimal b = bottom.length() == 0
+                            ? null
+                            : new BigDecimal(bottom);
+                        BigDecimal t = top.length() == 0
+                            ? null
+                            : new BigDecimal(top);
+                        edge = new ImportEdge(t, b);
+                    }
+                    catch (NumberFormatException nfe) {
+                        log.warn(
+                            "cannot parse 'Unterkante' or 'Oberkante' in line "
+                            + in.getLineNumber());
+                    }
+                }
+
                 ImportRange range = new ImportRange(from, to);
 
                 ImportAnnotation annotation = new ImportAnnotation(
-                    attribute, position, range);
+                    attribute, position, range, edge);
 
                 if (!annotations.add(annotation)) {
                     log.debug("duplicated annotation in line "
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportAnnotation.java	Tue Jun 14 12:34:08 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportAnnotation.java	Tue Jun 14 13:13:58 2011 +0000
@@ -5,6 +5,7 @@
 import de.intevation.flys.model.Position;
 import de.intevation.flys.model.Attribute;
 import de.intevation.flys.model.River;
+import de.intevation.flys.model.Edge;
 
 import org.hibernate.Session;
 import org.hibernate.Query;
@@ -17,6 +18,7 @@
     protected ImportAttribute attribute;
     protected ImportPosition  position;
     protected ImportRange     range;
+    protected ImportEdge      edge;
 
     protected Annotation      peer;
 
@@ -26,11 +28,13 @@
     public ImportAnnotation(
         ImportAttribute attribute,
         ImportPosition  position,
-        ImportRange     range
+        ImportRange     range,
+        ImportEdge      edge
     ) {
         this.attribute = attribute;
         this.position  = position;
         this.range     = range;
+        this.edge      = edge;
     }
 
     public int compareTo(ImportAnnotation other) {
@@ -47,7 +51,11 @@
             return d;
         }
 
-        return 0;
+        if (edge == null && other.edge != null) return -1;
+        if (edge != null && other.edge == null) return +1;
+        if (edge == null && other.edge == null) return 0;
+
+        return edge.compareTo(other.edge);
     }
 
     public ImportAttribute getAttribute() {
@@ -79,13 +87,18 @@
             Range     r = range.getPeer(river);
             Attribute a = attribute.getPeer();
             Position  p = position.getPeer();
+            Edge      e = edge != null ? edge.getPeer() : null;
             Session session = ImporterSession.getInstance().getDatabaseSession();
             Query query = session.createQuery(
-                "from Annotation where " +
-                "range=:range and attribute=:attribute and position=:position");
+                "from Annotation where "    +
+                "range=:range and "         +
+                "attribute=:attribute and " +
+                "position=:position and "   +
+                "edge=:edge");
             query.setParameter("range",     r);
             query.setParameter("attribute", a);
             query.setParameter("position",  p);
+            query.setParameter("edge",      e);
             List<Annotation> annotations = query.list();
             if (annotations.isEmpty()) {
                 peer = new Annotation(r, a, p);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportEdge.java	Tue Jun 14 13:13:58 2011 +0000
@@ -0,0 +1,75 @@
+package de.intevation.flys.importer;
+
+import de.intevation.flys.model.Edge;
+
+import org.hibernate.Session;
+import org.hibernate.Query;
+
+import java.util.List;
+
+import java.math.BigDecimal;
+
+public class ImportEdge
+implements   Comparable<ImportEdge>
+{
+    protected BigDecimal top;
+    protected BigDecimal bottom;
+
+    protected Edge peer;
+
+    public ImportEdge() {
+    }
+
+    public ImportEdge(BigDecimal top, BigDecimal bottom) {
+        this.top    = top;
+        this.bottom = bottom;
+    }
+
+    public BigDecimal getTop() {
+        return top;
+    }
+
+    public void setTop(BigDecimal top) {
+        this.top = top;
+    }
+
+    public BigDecimal getBottom() {
+        return bottom;
+    }
+
+    public void setBottom(BigDecimal bottom) {
+        this.bottom = bottom;
+    }
+
+    private static final int compare(BigDecimal a, BigDecimal b) {
+        if (a == null && b != null) return -1;
+        if (a != null && b == null) return +1;
+        if (a == null && b == null) return  0;
+        return a.compareTo(b);
+    }
+
+    public int compareTo(ImportEdge other) {
+        int cmp = compare(top, other.top);
+        return cmp != 0 ? cmp : compare(bottom, other.bottom);
+    }
+
+    public Edge getPeer() {
+        if (peer == null) {
+            Session session = ImporterSession.getInstance().getDatabaseSession();
+            Query query = session.createQuery(
+                "from Edge where top=:top and bottom=:bottom");
+            query.setParameter("top", top);
+            query.setParameter("bottom", bottom);
+            List<Edge> edges = query.list();
+            if (edges.isEmpty()) {
+                peer = new Edge(top, bottom);
+                session.save(peer);
+            }
+            else {
+                peer = edges.get(0);
+            }
+        }
+        return peer;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org