diff flys-aft/src/main/java/de/intevation/aft/River.java @ 5379:61bf64b102bc mapgenfix

Merge with default branch
author Christian Lins <christian.lins@intevation.de>
date Fri, 22 Mar 2013 11:25:54 +0100
parents da1e897c7224
children 7a68967ca72a
line wrap: on
line diff
--- a/flys-aft/src/main/java/de/intevation/aft/River.java	Wed Mar 06 14:14:15 2013 +0100
+++ b/flys-aft/src/main/java/de/intevation/aft/River.java	Fri Mar 22 11:25:54 2013 +0100
@@ -65,7 +65,8 @@
     public boolean sync(SyncContext context) throws SQLException {
         log.info("sync river: " + this);
 
-        Map<Long, DIPSGauge> dipsGauges = context.getDIPSGauges();
+        // Only take relevant gauges into account.
+        Map<Long, DIPSGauge> dipsGauges = context.getDIPSGauges(name, from, to);
 
         ConnectedStatements flysStatements = context.getFlysStatements();
         ConnectedStatements aftStatements  = context.getAftStatements();
@@ -78,14 +79,18 @@
             .getStatement("select.messstelle")
             .clearParameters()
             .setInt("GEWAESSER_NR", id2)
-            .setDouble("START_KM", from)
-            .setDouble("END_KM", to)
             .executeQuery();
 
         try {
             while (messstellenRs.next()) {
                 String name = messstellenRs.getString("NAME");
                 String num  = messstellenRs.getString("MESSSTELLE_NR");
+                double station = messstellenRs.getDouble("STATIONIERUNG");
+
+                if (!messstellenRs.wasNull() && !inside(station)) {
+                    log.warn("Station found in AFT but in not range: " + station);
+                    continue;
+                }
 
                 Long number = SyncContext.numberToLong(num);
                 if (number == null) {
@@ -166,12 +171,93 @@
         boolean modified = false;
 
         for (DIPSGauge gauge: gauges) {
+            modified |= updateBfGIdOnMasterDischargeTable(context, gauge);
             modified |= updateGauge(context, gauge);
         }
 
         return modified;
     }
 
+    protected boolean updateBfGIdOnMasterDischargeTable(
+        SyncContext context,
+        DIPSGauge   gauge
+    ) throws SQLException {
+        log.info(
+            "FLYS: Updating master discharge table bfg_id for '" +
+            gauge.getAftName() + "'");
+        ConnectedStatements flysStatements = context.getFlysStatements();
+
+        ResultSet rs = flysStatements
+            .getStatement("select.gauge.master.discharge.table")
+            .clearParameters()
+            .setInt("gauge_id", gauge.getFlysId())
+            .executeQuery();
+
+        int flysId;
+
+        try {
+            if (rs.next()) {
+                log.error(
+                    "FLYS: No master discharge table found for gauge '" +
+                    gauge.getAftName() + "'");
+                return false;
+            }
+            String bfgId = rs.getString("bfg_id");
+            if (!rs.wasNull()) { // already has BFG_ID
+                return false;
+            }
+            flysId = rs.getInt("id");
+        } finally {
+            rs.close();
+        }
+
+        // We need to find out the BFG_ID of the current discharge table
+        // for this gauge in AFT.
+
+        ConnectedStatements aftStatements = context.getAftStatements();
+
+        rs = aftStatements
+            .getStatement("select.bfg.id.current")
+            .clearParameters()
+            .setString("number", "%" + gauge.getOfficialNumber())
+            .executeQuery();
+
+        String bfgId = null;
+
+        try {
+            if (rs.next()) {
+                bfgId = rs.getString("BFG_ID");
+            }
+        } finally {
+            rs.close();
+        }
+
+        if (bfgId == null) {
+            log.warn(
+                "No BFG_ID found for current discharge table of gauge '" +
+                gauge + "'");
+            return false;
+        }
+
+        // Set the BFG_ID in FLYS.
+        flysStatements.beginTransaction();
+        try {
+            flysStatements
+                .getStatement("update.bfg.id.discharge.table")
+                .clearParameters()
+                .setInt("id", flysId)
+                .setString("bfg_id", bfgId)
+                .executeUpdate();
+            flysStatements.commitTransaction();
+        } catch (SQLException sqle) {
+            flysStatements.rollbackTransaction();
+            log.error(sqle, sqle);
+            return false;
+        }
+
+        return true;
+    }
+
     protected boolean updateGauge(
         SyncContext context,
         DIPSGauge   gauge

http://dive4elements.wald.intevation.org