diff flys-aft/src/main/java/de/intevation/aft/DischargeTable.java @ 4775:d9f1202cef78

Do the pairing of FLYS and AFT discharge tables by the field 'ABFLUSSTAFEL.BFG_ID' in AFT and 'discharge_table.bfg_id' in FLYS. TODO: Create new column in discharge_tabel.
author Sascha L. Teichmann <teichmann@intevation.de>
date Sat, 05 Jan 2013 14:52:55 +0100
parents f939e1e6cfa4
children ae3625b89cfd
line wrap: on
line diff
--- a/flys-aft/src/main/java/de/intevation/aft/DischargeTable.java	Sat Jan 05 13:46:27 2013 +0100
+++ b/flys-aft/src/main/java/de/intevation/aft/DischargeTable.java	Sat Jan 05 14:52:55 2013 +0100
@@ -23,6 +23,7 @@
     protected int          gaugeId;
     protected TimeInterval timeInterval;
     protected String       description;
+    protected String       bfgId;
     protected Set<WQ>      values;
 
     public DischargeTable() {
@@ -31,11 +32,13 @@
     public DischargeTable(
         int          gaugeId,
         TimeInterval timeInterval,
-        String       description
+        String       description,
+        String       bfgId
     ) {
         this.gaugeId      = gaugeId;
         this.timeInterval = timeInterval;
         this.description  = description;
+        this.bfgId        = bfgId;
         values = new TreeSet<WQ>(WQ.EPS_CMP);
     }
 
@@ -43,9 +46,10 @@
         int          id,
         int          gaugeId,
         TimeInterval timeInterval,
-        String       description
+        String       description,
+        String       bfgId
     ) {
-        this(gaugeId, timeInterval, description);
+        this(gaugeId, timeInterval, description, bfgId);
         this.id = id;
     }
 
@@ -81,6 +85,15 @@
         this.description = description;
     }
 
+    public String getBfgId() {
+        return bfgId;
+    }
+
+    public void setBfgId(String bfgId) {
+        this.bfgId = bfgId;
+    }
+
+
     public void clearValues() {
         values.clear();
     }
@@ -140,10 +153,15 @@
             .getStatement("insert.discharge.table.value");
 
         for (WQ wq: values) {
+            int wqId;
             ResultSet rs = nextId.executeQuery();
-            rs.next();
-            int wqId = rs.getInt("discharge_table_values_id");
-            rs.close();
+            try {
+                rs.next();
+                wqId = rs.getInt("discharge_table_values_id");
+            }
+            finally {
+                rs.close();
+            }
 
             insertDTV
                 .clearParameters()
@@ -169,31 +187,38 @@
             .clearParameters()
             .setInt("gauge_id", gaugeId)
             .executeQuery();
+        try {
+            OUTER: while (rs.next()) {
+                int    id          = rs.getInt("id");
+                String description = rs.getString("description");
+                String bfgId       = rs.getString("bfg_id");
+                if (description == null) {
+                    description = "";
+                }
+                if (bfgId == null) {
+                    bfgId = "";
+                }
+                for (DischargeTable dt: dts) {
+                    if (dt.getBfgId().equals(bfgId)) {
+                        log.warn("FLYS: Found discharge table '" +
+                            bfgId + "' with same bfg_id. -> ignore");
+                        continue OUTER;
+                    }
+                }
+                Date startTime = rs.getDate("start_time");
+                Date stopTime  = rs.getDate("stop_time");
+                TimeInterval ti = startTime == null
+                    ? null
+                    : new TimeInterval(startTime, stopTime);
 
-        OUTER: while (rs.next()) {
-            int    id          = rs.getInt("id");
-            String description = rs.getString("description");
-            if (description == null) {
-                description = "";
+                DischargeTable dt = new DischargeTable(
+                    id, gaugeId, ti, description, bfgId);
+                dts.add(dt);
             }
-            for (DischargeTable dt: dts) {
-                if (dt.getDescription().equals(description)) {
-                    log.warn("FLYS: Found discharge table '" +
-                        description + "' with same description. -> ignore");
-                    continue OUTER;
-                }
-            }
-            Date startTime = rs.getDate("start_time");
-            Date stopTime  = rs.getDate("stop_time");
-            TimeInterval ti = startTime == null
-                ? null
-                : new TimeInterval(startTime, stopTime);
-
-            DischargeTable dt = new DischargeTable(
-                id, gaugeId, ti, description);
-            dts.add(dt);
         }
-        rs.close();
+        finally {
+            rs.close();
+        }
 
         return dts;
     }
@@ -222,57 +247,66 @@
             .clearParameters()
             .setString("number", "%" + officialNumber)
             .executeQuery();
-
-        OUTER: while (rs.next()) {
-            int  dtId = rs.getInt("ABFLUSSTAFEL_NR");
-            Date from = rs.getDate("GUELTIG_VON");
-            Date to   = rs.getDate("GUELTIG_BIS");
+        try {
+            OUTER: while (rs.next()) {
+                int  dtId = rs.getInt("ABFLUSSTAFEL_NR");
+                Date from = rs.getDate("GUELTIG_VON");
+                Date to   = rs.getDate("GUELTIG_BIS");
 
-            if (from == null) {
-                log.warn("AFT: ABFLUSSTAFEL_NR = "
-                    + dtId + ": GUELTIG_VON = NULL -> ignored.");
-            }
-
-            if (to == null) {
-                log.warn("AFT: ABFLUSSTAFEL_NR = "
-                    + dtId + ": GUELTIG_BIS = NULL -> ignored.");
-            }
-
-            if (from == null || to == null) {
-                continue;
-            }
-
-            if (from.compareTo(to) > 0) {
+                if (from == null) {
                     log.warn("AFT: ABFLUSSTAFEL_NR = "
-                    + dtId + ": " + from + " > " + to + ". -> swap");
-                Date temp = from;
-                from = to;
-                to = temp;
-            }
-
-            String description = rs.getString("ABFLUSSTAFEL_BEZ");
-            if (description == null) {
-                description = String.valueOf(officialNumber);
-            }
+                        + dtId + ": GUELTIG_VON = NULL -> ignored.");
+                }
 
-            for (DischargeTable dt: dts) {
-                if (dt.getDescription().equals(description)) {
-                    log.warn("AFT: Found discharge table '" +
-                        description + "' with same description. -> ignore.");
-                    continue OUTER;
+                if (to == null) {
+                    log.warn("AFT: ABFLUSSTAFEL_NR = "
+                        + dtId + ": GUELTIG_BIS = NULL -> ignored.");
                 }
-            }
 
-            TimeInterval timeInterval = new TimeInterval(from, to);
+                if (from == null || to == null) {
+                    continue;
+                }
 
-            DischargeTable dt = new DischargeTable(
-                dtId,
-                flysGaugeId,
-                timeInterval,
-                description);
-            dts.add(dt);
+                if (from.compareTo(to) > 0) {
+                        log.warn("AFT: ABFLUSSTAFEL_NR = "
+                        + dtId + ": " + from + " > " + to + ". -> swap");
+                    Date temp = from;
+                    from = to;
+                    to = temp;
+                }
+
+                String description = rs.getString("ABFLUSSTAFEL_BEZ");
+                if (description == null) {
+                    description = String.valueOf(officialNumber);
+                }
+
+                String bfgId = rs.getString("BFG_ID");
+                if (bfgId == null) {
+                    bfgId = "";
+                }
+
+                for (DischargeTable dt: dts) {
+                    if (dt.getBfgId().equals(bfgId)) {
+                        log.warn("AFT: Found discharge table '" +
+                            bfgId + "' with same bfg_id. -> ignore.");
+                        continue OUTER;
+                    }
+                }
+
+                TimeInterval timeInterval = new TimeInterval(from, to);
+
+                DischargeTable dt = new DischargeTable(
+                    dtId,
+                    flysGaugeId,
+                    timeInterval,
+                    description,
+                    bfgId);
+                dts.add(dt);
+            }
         }
-        rs.close();
+        finally {
+            rs.close();
+        }
 
         return dts;
     }
@@ -297,20 +331,26 @@
         ConnectedStatements flysStatements =
             context.getFlysStatements();
 
+        int flysId;
+
         ResultSet rs = flysStatements
             .getStatement("next.discharge.id")
             .executeQuery();
-
-        rs.next();
-        int flysId = rs.getInt("discharge_table_id");
-        rs.close();
+        try {
+            rs.next();
+            flysId = rs.getInt("discharge_table_id");
+        }
+        finally {
+            rs.close();
+        }
 
         SymbolicStatement.Instance insertDT = flysStatements
             .getStatement("insert.dischargetable")
             .clearParameters()
             .setInt("id", flysId)
             .setInt("gauge_id", gaugeId)
-            .setString("description", description);
+            .setString("description", description)
+            .setString("bfg_id", bfgId);
 
         if (timeInterval != null) {
             insertDT.setInt("time_interval_id", timeInterval.getId());

http://dive4elements.wald.intevation.org