diff flys-backend/src/main/java/de/intevation/flys/importer/ImportCrossSectionLine.java @ 1204:22858e7cca79

Integrated PRF parsing into importer. Needs testing! flys-backend/trunk@2309 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 07 Jul 2011 22:25:38 +0000
parents
children 5f1506fc7636
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportCrossSectionLine.java	Thu Jul 07 22:25:38 2011 +0000
@@ -0,0 +1,117 @@
+package de.intevation.flys.importer;
+
+import de.intevation.flys.model.CrossSection;
+import de.intevation.flys.model.CrossSectionPoint;
+import de.intevation.flys.model.CrossSectionLine;
+
+import org.hibernate.Session;
+import org.hibernate.Query;
+
+import java.math.BigDecimal;
+
+import java.util.List;
+import java.util.Comparator;
+import java.util.Map;
+import java.util.TreeMap;
+
+public class ImportCrossSectionLine
+{
+    public static final Comparator<CrossSectionPoint> INDEX_CMP =
+        new Comparator<CrossSectionPoint>() {
+            public int compare(CrossSectionPoint a, CrossSectionPoint b) {
+                return a.getColPos().compareTo(b.getColPos());
+            }
+        };
+
+    protected BigDecimal         km;
+    protected ImportCrossSection crossSection;
+    protected List<XY>           points;
+
+    protected CrossSectionLine peer;
+
+    public ImportCrossSectionLine() {
+    }
+
+    public ImportCrossSectionLine(BigDecimal km, List<XY> points) {
+        this.km     = km;
+        this.points = points;
+    }
+
+    public ImportCrossSection getCrossSection() {
+        return crossSection;
+    }
+
+    public void setCrossSection(ImportCrossSection crossSection) {
+        this.crossSection = crossSection;
+    }
+
+    public BigDecimal getKm() {
+        return km;
+    }
+
+    public void setKm(BigDecimal km) {
+        this.km = km;
+    }
+
+    public void storeDependencies() {
+        storePoints();
+    }
+
+    protected void storePoints() {
+        CrossSectionLine csl = getPeer();
+
+        Map<CrossSectionPoint, CrossSectionPoint> map =
+            new TreeMap<CrossSectionPoint, CrossSectionPoint>(INDEX_CMP);
+
+        // build index for faster collision lookup
+        for (CrossSectionPoint point: csl.getPoints()) {
+            map.put(point, point);
+        }
+
+        Session session =
+            ImporterSession.getInstance().getDatabaseSession();
+
+        CrossSectionPoint key = new CrossSectionPoint();
+
+        for (XY xy: points) {
+            key.setColPos(xy.getIndex());
+            CrossSectionPoint csp = map.get(key);
+            if (csp == null) { // create new
+                csp = new CrossSectionPoint(
+                    csl, key.getColPos(),
+                    new BigDecimal(xy.getX()),
+                    new BigDecimal(xy.getY()));
+            }
+            else { // update old
+                csp.setX(new BigDecimal(xy.getX()));
+                csp.setY(new BigDecimal(xy.getY()));
+            }
+            session.save(csp);
+        }
+    }
+
+    public CrossSectionLine getPeer() {
+        if (peer == null) {
+            CrossSection cs = crossSection.getPeer();
+
+            Session session =
+                ImporterSession.getInstance().getDatabaseSession();
+
+            Query query = session.createQuery(
+                "from CrossSectionLine where crossSection=:cs and km=:km");
+            query.setParameter("cs", cs);
+            query.setParameter("km", km);
+
+            List<CrossSectionLine> lines = query.list();
+            if (lines.isEmpty()) {
+                peer = new CrossSectionLine(cs, km);
+                session.save(peer);
+            }
+            else {
+                peer = lines.get(0);
+            }
+        }
+        return peer;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org