changeset 4102:e8967ee1cb05

Fixed logic bug when writing discharge tables of an gauge existing in both dbs. flys-aft/trunk@3631 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 09 Jan 2012 16:43:14 +0000
parents b6a18d706cbe
children 2305731f563c
files flys-aft/ChangeLog flys-aft/src/main/java/de/intevation/aft/DischargeTable.java flys-aft/src/main/java/de/intevation/aft/River.java
diffstat 3 files changed, 89 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
--- a/flys-aft/ChangeLog	Mon Jan 09 15:34:19 2012 +0000
+++ b/flys-aft/ChangeLog	Mon Jan 09 16:43:14 2012 +0000
@@ -1,3 +1,11 @@
+2012-01-09	Sascha L. Teichmann	<sascha.teichmann@inteavtion.de>
+
+	* src/main/java/de/intevation/aft/River.java: Fixed logic bug
+	  when writing discharge tables of an gauge existing in both dbs.
+
+	* src/main/java/de/intevation/aft/DischargeTable.java: Moved
+	  some SQL code from River here to simplify the persistence.
+
 2012-01-09	Sascha L. Teichmann	<sascha.teichmann@inteavtion.de>
 
 	* src/main/java/de/intevation/aft/TimeInterval.java(toString): Added
--- a/flys-aft/src/main/java/de/intevation/aft/DischargeTable.java	Mon Jan 09 15:34:19 2012 +0000
+++ b/flys-aft/src/main/java/de/intevation/aft/DischargeTable.java	Mon Jan 09 16:43:14 2012 +0000
@@ -6,6 +6,7 @@
 
 import java.sql.SQLException;
 import java.sql.ResultSet;
+import java.sql.Types;
 
 import de.intevation.db.SymbolicStatement;
 import de.intevation.db.ConnectedStatements;
@@ -248,5 +249,56 @@
 
         return dts;
     }
+
+    public void persistFlysTimeInterval(
+        SyncContext context
+    )
+    throws SQLException
+    {
+        if (timeInterval != null) {
+            timeInterval = context.fetchOrCreateFLYSTimeInterval(
+                timeInterval);
+        }
+    }
+
+    public int persistFlysDischargeTable(
+        SyncContext context,
+        int         gaugeId
+    )
+    throws SQLException
+    {
+        ConnectedStatements flysStatements =
+            context.getFlysStatements();
+
+        ResultSet rs = flysStatements
+            .getStatement("next.discharge.id")
+            .executeQuery();
+
+        rs.next();
+        int flysId = rs.getInt("discharge_table_id");
+        rs.close();
+
+        SymbolicStatement.Instance insertDT = flysStatements
+            .getStatement("insert.dischargetable")
+            .clearParameters()
+            .setInt("id", flysId)
+            .setInt("gauge_id", gaugeId)
+            .setString("description", description);
+
+        if (timeInterval != null) {
+            insertDT.setInt("time_interval_id", timeInterval.getId());
+        }
+        else {
+            insertDT.setNull("time_interval_id", Types.INTEGER);
+        }
+
+        insertDT.execute();
+
+        if (log.isDebugEnabled()) {
+            log.debug("FLYS: Created discharge table id: " + id);
+        }
+
+        return flysId;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-aft/src/main/java/de/intevation/aft/River.java	Mon Jan 09 15:34:19 2012 +0000
+++ b/flys-aft/src/main/java/de/intevation/aft/River.java	Mon Jan 09 16:43:14 2012 +0000
@@ -7,7 +7,6 @@
 
 import java.sql.ResultSet;
 import java.sql.SQLException;
-import java.sql.Types;
 
 import org.apache.log4j.Logger;
 
@@ -139,6 +138,7 @@
     )
     throws SQLException
     {
+        log.info("FLYS: Updating gauge '" + gauge.getAftName() + "'.");
         // We need to load all discharge tables from both database
         // of the gauge and do some pairing based on their descriptions.
 
@@ -189,9 +189,12 @@
                 + "' found in FLYS but not in AFT. -> ignore");
         }
 
+        log.info("FLYS: Copy " + createDTs.size() +
+            " discharge tables over from AFT.");
+
         // Create the new discharge tables.
         for (DischargeTable aftDT: createDTs) {
-            createDischargeTables(context, gauge);
+            createDischargeTable(context, aftDT, gauge.getFlysId());
             modified = true;
         }
 
@@ -285,13 +288,28 @@
         return modified;
     }
 
+    protected void createDischargeTable(
+        SyncContext    context,
+        DischargeTable aftDT,
+        int            flysGaugeId
+    )
+    throws SQLException
+    {
+        aftDT.persistFlysTimeInterval(context);
+        int flysId = aftDT.persistFlysDischargeTable(context, flysGaugeId);
+
+        aftDT.loadAftValues(context);
+        aftDT.storeFlysValues(context, flysId);
+    }
+
     protected void createDischargeTables(
         SyncContext context,
         DIPSGauge   gauge
     )
     throws SQLException
     {
-        log.info("create discharge tables");
+        log.info("FLYS: Create discharge tables for '" +
+            gauge.getAftName() + "'.");
 
         // Load the discharge tables from AFT.
         List<DischargeTable> dts = loadAftDischargeTables(
@@ -301,7 +319,8 @@
         persistFlysTimeIntervals(context, dts);
 
         // Persist the discharge tables
-        int [] flysDTIds = persistFlysDischargeTables(context, dts);
+        int [] flysDTIds = persistFlysDischargeTables(
+            context, dts, gauge.getFlysId());
 
         // Copy over the W/Q values
         copyWQsFromAftToFlys(context, dts, flysDTIds);
@@ -324,17 +343,14 @@
     throws SQLException
     {
         for (DischargeTable dt: dts) {
-            TimeInterval timeInterval = dt.getTimeInterval();
-            if (timeInterval != null) {
-                dt.setTimeInterval(
-                    context.fetchOrCreateFLYSTimeInterval(timeInterval));
-            }
+            dt.persistFlysTimeInterval(context);
         }
     }
 
     protected int [] persistFlysDischargeTables(
         SyncContext          context,
-        List<DischargeTable> dts
+        List<DischargeTable> dts,
+        int                  flysGaugeId
     )
     throws SQLException
     {
@@ -342,49 +358,9 @@
 
         int [] flysDTIds = new int[dts.size()];
 
-        ResultSet rs = null;
-        try {
-            ConnectedStatements flysStatements =
-                context.getFlysStatements();
-
-            SymbolicStatement.Instance nextId =
-                flysStatements.getStatement("next.discharge.id");
-
-            SymbolicStatement.Instance insertDT =
-                flysStatements.getStatement("insert.dischargetable");
-
-            for (int i = 0; i < flysDTIds.length; ++i) {
-                rs = nextId.executeQuery();
-                rs.next();
-                int id = rs.getInt("discharge_table_id");
-                flysDTIds[i] = id;
-                rs.close(); rs = null;
-
-                DischargeTable dt = dts.get(i);
-                insertDT.clearParameters()
-                    .setInt("id", id)
-                    .setInt("gauge_id", dt.getGaugeId())
-                    .setString("description", dt.getDescription());
-
-                TimeInterval timeInterval = dt.getTimeInterval();
-                if (timeInterval != null) {
-                    insertDT.setInt("time_interval_id", timeInterval.getId());
-                }
-                else {
-                    insertDT.setNull("time_interval_id", Types.INTEGER);
-                }
-
-                insertDT.execute();
-                if (debug) {
-                    log.debug("FLYS: Created discharge table id: " + id);
-                }
-            }
-        }
-        finally {
-            if (rs != null) {
-                rs.close();
-                rs = null;
-            }
+        for (int i = 0; i < flysDTIds.length; ++i) {
+            flysDTIds[i] = dts.get(i)
+                .persistFlysDischargeTable(context, flysGaugeId);
         }
 
         return flysDTIds;
@@ -408,6 +384,5 @@
     public String toString() {
         return "[River: name=" + name + ", " + super.toString() + "]";
     }
-
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org