diff flys-aft/src/main/java/de/intevation/aft/River.java @ 4090:d556e29592f5

Create new discharge tables if needed. flys-aft/trunk@3590 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 04 Jan 2012 17:59:26 +0000
parents 52cde7fe742a
children 7bddd4601707
line wrap: on
line diff
--- a/flys-aft/src/main/java/de/intevation/aft/River.java	Tue Jan 03 12:25:06 2012 +0000
+++ b/flys-aft/src/main/java/de/intevation/aft/River.java	Wed Jan 04 17:59:26 2012 +0000
@@ -8,6 +8,7 @@
 
 import java.sql.ResultSet;
 import java.sql.SQLException;
+import java.sql.Types;
 
 import org.apache.log4j.Logger;
 
@@ -195,6 +196,8 @@
         ConnectedStatements flysStatements = context.getFlysStatements();
         ConnectedStatements aftStatements  = context.getAftStatements();
 
+        List<DischargeTable> dts = new ArrayList<DischargeTable>();
+
         ResultSet rs = null;
         try {
             rs = aftStatements.getStatement("select.abflusstafel")
@@ -203,24 +206,89 @@
                 executeQuery();
 
             while (rs.next()) {
-                int dtId  = rs.getInt("ABFLUSSTAFEL_NR");
-                Date from = rs.getDate("GUELTIG_VON");
-                Date to   = rs.getDate("GUELTIG_BIS");
+                int    dtId        = rs.getInt("ABFLUSSTAFEL_NR");
+                Date   from        = rs.getDate("GUELTIG_VON");
+                Date   to          = rs.getDate("GUELTIG_BIS");
+                String description = rs.getString("ABFLUSSTAFEL_BEZ");
+                if (description == null) {
+                    description = String.valueOf(officialNumber);
+                }
 
                 double datumValue = rs.getDouble("PEGELNULLPUNKT");
                 Double datum = rs.wasNull() ? null : datumValue;
 
                 if (debug) {
-                    log.debug("id:         " + dtId);
-                    log.debug("valid from: " + from);
-                    log.debug("valid to:   " + to);
-                    log.debug("datum:      " + datum);
+                    log.debug("id:          " + dtId);
+                    log.debug("valid from:  " + from);
+                    log.debug("valid to:    " + to);
+                    log.debug("datum:       " + datum);
+                    log.debug("description: " + description);
+                }
+
+                TimeInterval timeInterval = from == null
+                    ? null
+                    : new TimeInterval(from, to);
+
+                DischargeTable dt = new DischargeTable(
+                    gauge.getFlysId(),
+                    timeInterval,
+                    description);
+                dts.add(dt);
+            }
+        }
+        finally {
+            if (rs != null) {
+                rs.close();
+                rs = null;
+            }
+        }
+
+        // Persist the time intervals.
+        for (DischargeTable dt: dts) {
+            TimeInterval timeInterval = dt.getTimeInterval();
+            if (timeInterval != null) {
+                dt.setTimeInterval(
+                    context.fetchOrCreateFLYSTimeInterval(timeInterval));
+            }
+        }
+
+        // Persist the discharge tables
+        try {
+            SymbolicStatement.Instance nextId =
+                flysStatements.getStatement("next.discharge.id");
+
+            SymbolicStatement.Instance insertDT =
+                flysStatements.getStatement("insert.dischargetable");
+
+            for (DischargeTable dt: dts) {
+                rs = nextId.executeQuery();
+                rs.next();
+                int id = rs.getInt("discharge_table_id");
+                rs.close(); rs = null;
+
+                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;
             }
         }
     }

http://dive4elements.wald.intevation.org