diff flys-aft/src/main/java/de/intevation/aft/River.java @ 4087:aad1886ea226

Store new gauges in FLYS db. flys-aft/trunk@3568 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 02 Jan 2012 17:45:25 +0000
parents 067341e86375
children 52cde7fe742a
line wrap: on
line diff
--- a/flys-aft/src/main/java/de/intevation/aft/River.java	Mon Jan 02 15:13:58 2012 +0000
+++ b/flys-aft/src/main/java/de/intevation/aft/River.java	Mon Jan 02 17:45:25 2012 +0000
@@ -11,6 +11,7 @@
 import org.apache.log4j.Logger;
 
 import de.intevation.db.ConnectedStatements;
+import de.intevation.db.SymbolicStatement;
 
 public class River
 extends      IdPair
@@ -108,11 +109,71 @@
             updateGauges.add(aftDIPSGauge);
         }
 
+        createGauges(context, aftDIPSGauges);
+
         gaugesRs.close();
+    }
 
-        for (DIPSGauge gauge: aftDIPSGauges.values()) {
+    protected void createGauges(
+        SyncContext          context,
+        Map<Long, DIPSGauge> gauges
+    )
+    throws SQLException
+    {
+        ConnectedStatements flysStatements = context.getFlysStatements();
+
+        SymbolicStatement.Instance nextId =
+            flysStatements.getStatement("next.gauge.id");
+
+        SymbolicStatement.Instance insertStmnt =
+            flysStatements.getStatement("insert.gauge");
+
+        for (Map.Entry<Long, DIPSGauge> entry: gauges.entrySet()) {
+            Long      officialNumber = entry.getKey();
+            DIPSGauge gauge          = entry.getValue();
+
             log.info("Gauge '" + gauge.getAftName() +
                 "' not in FLYS but in AFT/DIPS. -> Create");
+
+            if (!gauge.hasDatums()) {
+                log.warn("FLYS: Gauge '" + 
+                    gauge.getAftName() + "' has no datum. Ignored.");
+                continue;
+            }
+
+            ResultSet rs = null;
+            flysStatements.beginTransaction();
+            try {
+                (rs = nextId.executeQuery()).next();
+                int gaugeId = rs.getInt("gauge_id");
+                rs.close(); rs = null;
+
+                insertStmnt
+                    .clearParameters()
+                    .setInt("id", gaugeId)
+                    .setString("name", gauge.getAftName())
+                    .setInt("river_id", id1)
+                    .setDouble("station", gauge.getStation())
+                    .setDouble("aeo", gauge.getAeo())
+                    .setDouble("official_number", officialNumber)
+                    .setDouble("datum", gauge.getLatestDatum().getValue());
+
+                insertStmnt.execute();
+
+                log.info("FLYS: Created gauge '" + gauge.getAftName() + 
+                    "' with id " + gaugeId + ".");
+
+                flysStatements.commitTransaction();
+            }
+            catch (SQLException sqle) {
+                flysStatements.rollbackTransaction();
+                throw sqle;
+            }
+            finally {
+                if (rs != null) {
+                    rs.close();
+                }
+            }
         }
     }
 

http://dive4elements.wald.intevation.org