comparison 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
comparison
equal deleted inserted replaced
4774:9dcc32fc5167 4775:d9f1202cef78
21 21
22 protected int id; 22 protected int id;
23 protected int gaugeId; 23 protected int gaugeId;
24 protected TimeInterval timeInterval; 24 protected TimeInterval timeInterval;
25 protected String description; 25 protected String description;
26 protected String bfgId;
26 protected Set<WQ> values; 27 protected Set<WQ> values;
27 28
28 public DischargeTable() { 29 public DischargeTable() {
29 } 30 }
30 31
31 public DischargeTable( 32 public DischargeTable(
32 int gaugeId, 33 int gaugeId,
33 TimeInterval timeInterval, 34 TimeInterval timeInterval,
34 String description 35 String description,
36 String bfgId
35 ) { 37 ) {
36 this.gaugeId = gaugeId; 38 this.gaugeId = gaugeId;
37 this.timeInterval = timeInterval; 39 this.timeInterval = timeInterval;
38 this.description = description; 40 this.description = description;
41 this.bfgId = bfgId;
39 values = new TreeSet<WQ>(WQ.EPS_CMP); 42 values = new TreeSet<WQ>(WQ.EPS_CMP);
40 } 43 }
41 44
42 public DischargeTable( 45 public DischargeTable(
43 int id, 46 int id,
44 int gaugeId, 47 int gaugeId,
45 TimeInterval timeInterval, 48 TimeInterval timeInterval,
46 String description 49 String description,
50 String bfgId
47 ) { 51 ) {
48 this(gaugeId, timeInterval, description); 52 this(gaugeId, timeInterval, description, bfgId);
49 this.id = id; 53 this.id = id;
50 } 54 }
51 55
52 public int getId() { 56 public int getId() {
53 return id; 57 return id;
78 } 82 }
79 83
80 public void setDescription(String description) { 84 public void setDescription(String description) {
81 this.description = description; 85 this.description = description;
82 } 86 }
87
88 public String getBfgId() {
89 return bfgId;
90 }
91
92 public void setBfgId(String bfgId) {
93 this.bfgId = bfgId;
94 }
95
83 96
84 public void clearValues() { 97 public void clearValues() {
85 values.clear(); 98 values.clear();
86 } 99 }
87 100
138 // Insert the values. 151 // Insert the values.
139 SymbolicStatement.Instance insertDTV = flysStatements 152 SymbolicStatement.Instance insertDTV = flysStatements
140 .getStatement("insert.discharge.table.value"); 153 .getStatement("insert.discharge.table.value");
141 154
142 for (WQ wq: values) { 155 for (WQ wq: values) {
156 int wqId;
143 ResultSet rs = nextId.executeQuery(); 157 ResultSet rs = nextId.executeQuery();
144 rs.next(); 158 try {
145 int wqId = rs.getInt("discharge_table_values_id"); 159 rs.next();
146 rs.close(); 160 wqId = rs.getInt("discharge_table_values_id");
161 }
162 finally {
163 rs.close();
164 }
147 165
148 insertDTV 166 insertDTV
149 .clearParameters() 167 .clearParameters()
150 .setInt("id", wqId) 168 .setInt("id", wqId)
151 .setInt("table_id", dischargeTableId) 169 .setInt("table_id", dischargeTableId)
167 .getFlysStatements() 185 .getFlysStatements()
168 .getStatement("select.gauge.discharge.tables") 186 .getStatement("select.gauge.discharge.tables")
169 .clearParameters() 187 .clearParameters()
170 .setInt("gauge_id", gaugeId) 188 .setInt("gauge_id", gaugeId)
171 .executeQuery(); 189 .executeQuery();
172 190 try {
173 OUTER: while (rs.next()) { 191 OUTER: while (rs.next()) {
174 int id = rs.getInt("id"); 192 int id = rs.getInt("id");
175 String description = rs.getString("description"); 193 String description = rs.getString("description");
176 if (description == null) { 194 String bfgId = rs.getString("bfg_id");
177 description = ""; 195 if (description == null) {
178 } 196 description = "";
179 for (DischargeTable dt: dts) { 197 }
180 if (dt.getDescription().equals(description)) { 198 if (bfgId == null) {
181 log.warn("FLYS: Found discharge table '" + 199 bfgId = "";
182 description + "' with same description. -> ignore"); 200 }
183 continue OUTER; 201 for (DischargeTable dt: dts) {
184 } 202 if (dt.getBfgId().equals(bfgId)) {
185 } 203 log.warn("FLYS: Found discharge table '" +
186 Date startTime = rs.getDate("start_time"); 204 bfgId + "' with same bfg_id. -> ignore");
187 Date stopTime = rs.getDate("stop_time"); 205 continue OUTER;
188 TimeInterval ti = startTime == null 206 }
189 ? null 207 }
190 : new TimeInterval(startTime, stopTime); 208 Date startTime = rs.getDate("start_time");
191 209 Date stopTime = rs.getDate("stop_time");
192 DischargeTable dt = new DischargeTable( 210 TimeInterval ti = startTime == null
193 id, gaugeId, ti, description); 211 ? null
194 dts.add(dt); 212 : new TimeInterval(startTime, stopTime);
195 } 213
196 rs.close(); 214 DischargeTable dt = new DischargeTable(
215 id, gaugeId, ti, description, bfgId);
216 dts.add(dt);
217 }
218 }
219 finally {
220 rs.close();
221 }
197 222
198 return dts; 223 return dts;
199 } 224 }
200 225
201 public static List<DischargeTable> loadAftDischargeTables( 226 public static List<DischargeTable> loadAftDischargeTables(
220 .getAftStatements() 245 .getAftStatements()
221 .getStatement("select.abflusstafel") 246 .getStatement("select.abflusstafel")
222 .clearParameters() 247 .clearParameters()
223 .setString("number", "%" + officialNumber) 248 .setString("number", "%" + officialNumber)
224 .executeQuery(); 249 .executeQuery();
225 250 try {
226 OUTER: while (rs.next()) { 251 OUTER: while (rs.next()) {
227 int dtId = rs.getInt("ABFLUSSTAFEL_NR"); 252 int dtId = rs.getInt("ABFLUSSTAFEL_NR");
228 Date from = rs.getDate("GUELTIG_VON"); 253 Date from = rs.getDate("GUELTIG_VON");
229 Date to = rs.getDate("GUELTIG_BIS"); 254 Date to = rs.getDate("GUELTIG_BIS");
230 255
231 if (from == null) { 256 if (from == null) {
232 log.warn("AFT: ABFLUSSTAFEL_NR = "
233 + dtId + ": GUELTIG_VON = NULL -> ignored.");
234 }
235
236 if (to == null) {
237 log.warn("AFT: ABFLUSSTAFEL_NR = "
238 + dtId + ": GUELTIG_BIS = NULL -> ignored.");
239 }
240
241 if (from == null || to == null) {
242 continue;
243 }
244
245 if (from.compareTo(to) > 0) {
246 log.warn("AFT: ABFLUSSTAFEL_NR = " 257 log.warn("AFT: ABFLUSSTAFEL_NR = "
247 + dtId + ": " + from + " > " + to + ". -> swap"); 258 + dtId + ": GUELTIG_VON = NULL -> ignored.");
248 Date temp = from; 259 }
249 from = to; 260
250 to = temp; 261 if (to == null) {
251 } 262 log.warn("AFT: ABFLUSSTAFEL_NR = "
252 263 + dtId + ": GUELTIG_BIS = NULL -> ignored.");
253 String description = rs.getString("ABFLUSSTAFEL_BEZ"); 264 }
254 if (description == null) { 265
255 description = String.valueOf(officialNumber); 266 if (from == null || to == null) {
256 } 267 continue;
257 268 }
258 for (DischargeTable dt: dts) { 269
259 if (dt.getDescription().equals(description)) { 270 if (from.compareTo(to) > 0) {
260 log.warn("AFT: Found discharge table '" + 271 log.warn("AFT: ABFLUSSTAFEL_NR = "
261 description + "' with same description. -> ignore."); 272 + dtId + ": " + from + " > " + to + ". -> swap");
262 continue OUTER; 273 Date temp = from;
263 } 274 from = to;
264 } 275 to = temp;
265 276 }
266 TimeInterval timeInterval = new TimeInterval(from, to); 277
267 278 String description = rs.getString("ABFLUSSTAFEL_BEZ");
268 DischargeTable dt = new DischargeTable( 279 if (description == null) {
269 dtId, 280 description = String.valueOf(officialNumber);
270 flysGaugeId, 281 }
271 timeInterval, 282
272 description); 283 String bfgId = rs.getString("BFG_ID");
273 dts.add(dt); 284 if (bfgId == null) {
274 } 285 bfgId = "";
275 rs.close(); 286 }
287
288 for (DischargeTable dt: dts) {
289 if (dt.getBfgId().equals(bfgId)) {
290 log.warn("AFT: Found discharge table '" +
291 bfgId + "' with same bfg_id. -> ignore.");
292 continue OUTER;
293 }
294 }
295
296 TimeInterval timeInterval = new TimeInterval(from, to);
297
298 DischargeTable dt = new DischargeTable(
299 dtId,
300 flysGaugeId,
301 timeInterval,
302 description,
303 bfgId);
304 dts.add(dt);
305 }
306 }
307 finally {
308 rs.close();
309 }
276 310
277 return dts; 311 return dts;
278 } 312 }
279 313
280 public void persistFlysTimeInterval( 314 public void persistFlysTimeInterval(
295 throws SQLException 329 throws SQLException
296 { 330 {
297 ConnectedStatements flysStatements = 331 ConnectedStatements flysStatements =
298 context.getFlysStatements(); 332 context.getFlysStatements();
299 333
334 int flysId;
335
300 ResultSet rs = flysStatements 336 ResultSet rs = flysStatements
301 .getStatement("next.discharge.id") 337 .getStatement("next.discharge.id")
302 .executeQuery(); 338 .executeQuery();
303 339 try {
304 rs.next(); 340 rs.next();
305 int flysId = rs.getInt("discharge_table_id"); 341 flysId = rs.getInt("discharge_table_id");
306 rs.close(); 342 }
343 finally {
344 rs.close();
345 }
307 346
308 SymbolicStatement.Instance insertDT = flysStatements 347 SymbolicStatement.Instance insertDT = flysStatements
309 .getStatement("insert.dischargetable") 348 .getStatement("insert.dischargetable")
310 .clearParameters() 349 .clearParameters()
311 .setInt("id", flysId) 350 .setInt("id", flysId)
312 .setInt("gauge_id", gaugeId) 351 .setInt("gauge_id", gaugeId)
313 .setString("description", description); 352 .setString("description", description)
353 .setString("bfg_id", bfgId);
314 354
315 if (timeInterval != null) { 355 if (timeInterval != null) {
316 insertDT.setInt("time_interval_id", timeInterval.getId()); 356 insertDT.setInt("time_interval_id", timeInterval.getId());
317 } 357 }
318 else { 358 else {

http://dive4elements.wald.intevation.org