changeset 4093:7bddd4601707

Copy over W/Q values from AFT to FLYS for new discharge tables. flys-aft/trunk@3603 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 05 Jan 2012 17:46:18 +0000
parents b3fc044f75ba
children b20b710aa86f
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, 75 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/flys-aft/ChangeLog	Thu Jan 05 17:06:29 2012 +0000
+++ b/flys-aft/ChangeLog	Thu Jan 05 17:46:18 2012 +0000
@@ -1,4 +1,4 @@
-2012-01-04	Sascha L. Teichmann	<sascha.teichmann@inteavtion.de>
+2012-01-05	Sascha L. Teichmann	<sascha.teichmann@inteavtion.de>
 
 	* src/main/java/de/intevation/aft/DischargeTable.java: Store
 	  W/Q values to FLYS.
@@ -6,7 +6,7 @@
 	* src/main/resources/sql/flys-common.properties: Added statements
 	  to store W/Q values into FLYS database.
 
-2012-01-04	Sascha L. Teichmann	<sascha.teichmann@inteavtion.de>
+2012-01-05	Sascha L. Teichmann	<sascha.teichmann@inteavtion.de>
 
 	* src/main/java/de/intevation/aft/WQ.java: New. W/Q model used
 	  for AFT and FLYS.
--- a/flys-aft/src/main/java/de/intevation/aft/DischargeTable.java	Thu Jan 05 17:06:29 2012 +0000
+++ b/flys-aft/src/main/java/de/intevation/aft/DischargeTable.java	Thu Jan 05 17:46:18 2012 +0000
@@ -73,6 +73,10 @@
         this.description = description;
     }
 
+    public void clearValues() {
+        values.clear();
+    }
+
     protected void loadValues(SymbolicStatement.Instance query) 
     throws SQLException
     {
--- a/flys-aft/src/main/java/de/intevation/aft/River.java	Thu Jan 05 17:06:29 2012 +0000
+++ b/flys-aft/src/main/java/de/intevation/aft/River.java	Thu Jan 05 17:46:18 2012 +0000
@@ -191,16 +191,36 @@
     {
         log.info("create discharge tables");
 
-        boolean debug = log.isDebugEnabled();
+        // Load the discharge tables from AFT.
+        List<DischargeTable> dts = loadAftDischargeTables(
+            context, officialNumber, gauge);
 
-        ConnectedStatements flysStatements = context.getFlysStatements();
-        ConnectedStatements aftStatements  = context.getAftStatements();
+        // Persist the time intervals.
+        persistFlysTimeIntervals(context, dts);
+
+        // Persist the discharge tables
+        int [] flysDTIds = persistFlysDischargeTables(context, dts);
+
+        // Copy over the W/Q values
+        copyWQsFromAftToFlys(context, dts, flysDTIds);
+    }
+
+    protected List<DischargeTable> loadAftDischargeTables(
+        SyncContext context,
+        Long        officialNumber,
+        DIPSGauge   gauge
+    )
+    throws SQLException
+    {
+        boolean debug = log.isDebugEnabled();
 
         List<DischargeTable> dts = new ArrayList<DischargeTable>();
 
         ResultSet rs = null;
         try {
-            rs = aftStatements.getStatement("select.abflusstafel")
+            rs = context
+                .getAftStatements()
+                .getStatement("select.abflusstafel")
                 .clearParameters()
                 .setString("number", "%" + officialNumber).
                 executeQuery();
@@ -230,6 +250,7 @@
                     : new TimeInterval(from, to);
 
                 DischargeTable dt = new DischargeTable(
+                    dtId,
                     gauge.getFlysId(),
                     timeInterval,
                     description);
@@ -243,7 +264,15 @@
             }
         }
 
-        // Persist the time intervals.
+        return dts;
+    }
+
+    protected void persistFlysTimeIntervals(
+        SyncContext          context,
+        List<DischargeTable> dts
+    )
+    throws SQLException
+    {
         for (DischargeTable dt: dts) {
             TimeInterval timeInterval = dt.getTimeInterval();
             if (timeInterval != null) {
@@ -251,21 +280,37 @@
                     context.fetchOrCreateFLYSTimeInterval(timeInterval));
             }
         }
+    }
 
-        // Persist the discharge tables
+    protected int [] persistFlysDischargeTables(
+        SyncContext          context,
+        List<DischargeTable> dts
+    )
+    throws SQLException
+    {
+        boolean debug = log.isDebugEnabled();
+
+        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 (DischargeTable dt: dts) {
+            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())
@@ -291,6 +336,23 @@
                 rs = null;
             }
         }
+
+        return flysDTIds;
+    }
+
+    protected void copyWQsFromAftToFlys(
+        SyncContext          context,
+        List<DischargeTable> dts,
+        int []               flysDTIds
+    )
+    throws SQLException
+    {
+        for (int i = 0; i < flysDTIds.length; ++i) {
+            DischargeTable dt = dts.get(i);
+            dt.loadAftValues(context);
+            dt.storeFlysValues(context, flysDTIds[i]);
+            dt.clearValues(); // To save memory.
+        }
     }
 
     public String toString() {

http://dive4elements.wald.intevation.org