comparison flys-aft/src/main/java/de/intevation/aft/River.java @ 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 981de0b77c6b
children cdcf98245e36
comparison
equal deleted inserted replaced
4101:b6a18d706cbe 4102:e8967ee1cb05
5 import java.util.HashMap; 5 import java.util.HashMap;
6 import java.util.Map; 6 import java.util.Map;
7 7
8 import java.sql.ResultSet; 8 import java.sql.ResultSet;
9 import java.sql.SQLException; 9 import java.sql.SQLException;
10 import java.sql.Types;
11 10
12 import org.apache.log4j.Logger; 11 import org.apache.log4j.Logger;
13 12
14 import de.intevation.db.ConnectedStatements; 13 import de.intevation.db.ConnectedStatements;
15 import de.intevation.db.SymbolicStatement; 14 import de.intevation.db.SymbolicStatement;
137 SyncContext context, 136 SyncContext context,
138 DIPSGauge gauge 137 DIPSGauge gauge
139 ) 138 )
140 throws SQLException 139 throws SQLException
141 { 140 {
141 log.info("FLYS: Updating gauge '" + gauge.getAftName() + "'.");
142 // We need to load all discharge tables from both database 142 // We need to load all discharge tables from both database
143 // of the gauge and do some pairing based on their descriptions. 143 // of the gauge and do some pairing based on their descriptions.
144 144
145 boolean modified = false; 145 boolean modified = false;
146 146
187 for (String description: desc2FlysDT.keySet()) { 187 for (String description: desc2FlysDT.keySet()) {
188 log.info("FLYS: Discharge table '" + description 188 log.info("FLYS: Discharge table '" + description
189 + "' found in FLYS but not in AFT. -> ignore"); 189 + "' found in FLYS but not in AFT. -> ignore");
190 } 190 }
191 191
192 log.info("FLYS: Copy " + createDTs.size() +
193 " discharge tables over from AFT.");
194
192 // Create the new discharge tables. 195 // Create the new discharge tables.
193 for (DischargeTable aftDT: createDTs) { 196 for (DischargeTable aftDT: createDTs) {
194 createDischargeTables(context, gauge); 197 createDischargeTable(context, aftDT, gauge.getFlysId());
195 modified = true; 198 modified = true;
196 } 199 }
197 200
198 return modified; 201 return modified;
199 } 202 }
283 } 286 }
284 287
285 return modified; 288 return modified;
286 } 289 }
287 290
291 protected void createDischargeTable(
292 SyncContext context,
293 DischargeTable aftDT,
294 int flysGaugeId
295 )
296 throws SQLException
297 {
298 aftDT.persistFlysTimeInterval(context);
299 int flysId = aftDT.persistFlysDischargeTable(context, flysGaugeId);
300
301 aftDT.loadAftValues(context);
302 aftDT.storeFlysValues(context, flysId);
303 }
304
288 protected void createDischargeTables( 305 protected void createDischargeTables(
289 SyncContext context, 306 SyncContext context,
290 DIPSGauge gauge 307 DIPSGauge gauge
291 ) 308 )
292 throws SQLException 309 throws SQLException
293 { 310 {
294 log.info("create discharge tables"); 311 log.info("FLYS: Create discharge tables for '" +
312 gauge.getAftName() + "'.");
295 313
296 // Load the discharge tables from AFT. 314 // Load the discharge tables from AFT.
297 List<DischargeTable> dts = loadAftDischargeTables( 315 List<DischargeTable> dts = loadAftDischargeTables(
298 context, gauge); 316 context, gauge);
299 317
300 // Persist the time intervals. 318 // Persist the time intervals.
301 persistFlysTimeIntervals(context, dts); 319 persistFlysTimeIntervals(context, dts);
302 320
303 // Persist the discharge tables 321 // Persist the discharge tables
304 int [] flysDTIds = persistFlysDischargeTables(context, dts); 322 int [] flysDTIds = persistFlysDischargeTables(
323 context, dts, gauge.getFlysId());
305 324
306 // Copy over the W/Q values 325 // Copy over the W/Q values
307 copyWQsFromAftToFlys(context, dts, flysDTIds); 326 copyWQsFromAftToFlys(context, dts, flysDTIds);
308 } 327 }
309 328
322 List<DischargeTable> dts 341 List<DischargeTable> dts
323 ) 342 )
324 throws SQLException 343 throws SQLException
325 { 344 {
326 for (DischargeTable dt: dts) { 345 for (DischargeTable dt: dts) {
327 TimeInterval timeInterval = dt.getTimeInterval(); 346 dt.persistFlysTimeInterval(context);
328 if (timeInterval != null) {
329 dt.setTimeInterval(
330 context.fetchOrCreateFLYSTimeInterval(timeInterval));
331 }
332 } 347 }
333 } 348 }
334 349
335 protected int [] persistFlysDischargeTables( 350 protected int [] persistFlysDischargeTables(
336 SyncContext context, 351 SyncContext context,
337 List<DischargeTable> dts 352 List<DischargeTable> dts,
353 int flysGaugeId
338 ) 354 )
339 throws SQLException 355 throws SQLException
340 { 356 {
341 boolean debug = log.isDebugEnabled(); 357 boolean debug = log.isDebugEnabled();
342 358
343 int [] flysDTIds = new int[dts.size()]; 359 int [] flysDTIds = new int[dts.size()];
344 360
345 ResultSet rs = null; 361 for (int i = 0; i < flysDTIds.length; ++i) {
346 try { 362 flysDTIds[i] = dts.get(i)
347 ConnectedStatements flysStatements = 363 .persistFlysDischargeTable(context, flysGaugeId);
348 context.getFlysStatements();
349
350 SymbolicStatement.Instance nextId =
351 flysStatements.getStatement("next.discharge.id");
352
353 SymbolicStatement.Instance insertDT =
354 flysStatements.getStatement("insert.dischargetable");
355
356 for (int i = 0; i < flysDTIds.length; ++i) {
357 rs = nextId.executeQuery();
358 rs.next();
359 int id = rs.getInt("discharge_table_id");
360 flysDTIds[i] = id;
361 rs.close(); rs = null;
362
363 DischargeTable dt = dts.get(i);
364 insertDT.clearParameters()
365 .setInt("id", id)
366 .setInt("gauge_id", dt.getGaugeId())
367 .setString("description", dt.getDescription());
368
369 TimeInterval timeInterval = dt.getTimeInterval();
370 if (timeInterval != null) {
371 insertDT.setInt("time_interval_id", timeInterval.getId());
372 }
373 else {
374 insertDT.setNull("time_interval_id", Types.INTEGER);
375 }
376
377 insertDT.execute();
378 if (debug) {
379 log.debug("FLYS: Created discharge table id: " + id);
380 }
381 }
382 }
383 finally {
384 if (rs != null) {
385 rs.close();
386 rs = null;
387 }
388 } 364 }
389 365
390 return flysDTIds; 366 return flysDTIds;
391 } 367 }
392 368
406 } 382 }
407 383
408 public String toString() { 384 public String toString() {
409 return "[River: name=" + name + ", " + super.toString() + "]"; 385 return "[River: name=" + name + ", " + super.toString() + "]";
410 } 386 }
411
412 } 387 }
413 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 388 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org