comparison flys-aft/src/main/java/de/intevation/aft/River.java @ 5379:61bf64b102bc mapgenfix

Merge with default branch
author Christian Lins <christian.lins@intevation.de>
date Fri, 22 Mar 2013 11:25:54 +0100
parents da1e897c7224
children 7a68967ca72a
comparison
equal deleted inserted replaced
5175:cfc5540a4eec 5379:61bf64b102bc
63 } 63 }
64 64
65 public boolean sync(SyncContext context) throws SQLException { 65 public boolean sync(SyncContext context) throws SQLException {
66 log.info("sync river: " + this); 66 log.info("sync river: " + this);
67 67
68 Map<Long, DIPSGauge> dipsGauges = context.getDIPSGauges(); 68 // Only take relevant gauges into account.
69 Map<Long, DIPSGauge> dipsGauges = context.getDIPSGauges(name, from, to);
69 70
70 ConnectedStatements flysStatements = context.getFlysStatements(); 71 ConnectedStatements flysStatements = context.getFlysStatements();
71 ConnectedStatements aftStatements = context.getAftStatements(); 72 ConnectedStatements aftStatements = context.getAftStatements();
72 73
73 String riverName = getName(); 74 String riverName = getName();
76 77
77 ResultSet messstellenRs = aftStatements 78 ResultSet messstellenRs = aftStatements
78 .getStatement("select.messstelle") 79 .getStatement("select.messstelle")
79 .clearParameters() 80 .clearParameters()
80 .setInt("GEWAESSER_NR", id2) 81 .setInt("GEWAESSER_NR", id2)
81 .setDouble("START_KM", from)
82 .setDouble("END_KM", to)
83 .executeQuery(); 82 .executeQuery();
84 83
85 try { 84 try {
86 while (messstellenRs.next()) { 85 while (messstellenRs.next()) {
87 String name = messstellenRs.getString("NAME"); 86 String name = messstellenRs.getString("NAME");
88 String num = messstellenRs.getString("MESSSTELLE_NR"); 87 String num = messstellenRs.getString("MESSSTELLE_NR");
88 double station = messstellenRs.getDouble("STATIONIERUNG");
89
90 if (!messstellenRs.wasNull() && !inside(station)) {
91 log.warn("Station found in AFT but in not range: " + station);
92 continue;
93 }
89 94
90 Long number = SyncContext.numberToLong(num); 95 Long number = SyncContext.numberToLong(num);
91 if (number == null) { 96 if (number == null) {
92 log.warn("AFT: Invalid MESSSTELLE_NR for MESSSTELLE '"+name+"'"); 97 log.warn("AFT: Invalid MESSSTELLE_NR for MESSSTELLE '"+name+"'");
93 continue; 98 continue;
164 throws SQLException 169 throws SQLException
165 { 170 {
166 boolean modified = false; 171 boolean modified = false;
167 172
168 for (DIPSGauge gauge: gauges) { 173 for (DIPSGauge gauge: gauges) {
174 modified |= updateBfGIdOnMasterDischargeTable(context, gauge);
169 modified |= updateGauge(context, gauge); 175 modified |= updateGauge(context, gauge);
170 } 176 }
171 177
172 return modified; 178 return modified;
179 }
180
181 protected boolean updateBfGIdOnMasterDischargeTable(
182 SyncContext context,
183 DIPSGauge gauge
184 ) throws SQLException {
185 log.info(
186 "FLYS: Updating master discharge table bfg_id for '" +
187 gauge.getAftName() + "'");
188 ConnectedStatements flysStatements = context.getFlysStatements();
189
190 ResultSet rs = flysStatements
191 .getStatement("select.gauge.master.discharge.table")
192 .clearParameters()
193 .setInt("gauge_id", gauge.getFlysId())
194 .executeQuery();
195
196 int flysId;
197
198 try {
199 if (rs.next()) {
200 log.error(
201 "FLYS: No master discharge table found for gauge '" +
202 gauge.getAftName() + "'");
203 return false;
204 }
205 String bfgId = rs.getString("bfg_id");
206 if (!rs.wasNull()) { // already has BFG_ID
207 return false;
208 }
209 flysId = rs.getInt("id");
210 } finally {
211 rs.close();
212 }
213
214 // We need to find out the BFG_ID of the current discharge table
215 // for this gauge in AFT.
216
217 ConnectedStatements aftStatements = context.getAftStatements();
218
219 rs = aftStatements
220 .getStatement("select.bfg.id.current")
221 .clearParameters()
222 .setString("number", "%" + gauge.getOfficialNumber())
223 .executeQuery();
224
225 String bfgId = null;
226
227 try {
228 if (rs.next()) {
229 bfgId = rs.getString("BFG_ID");
230 }
231 } finally {
232 rs.close();
233 }
234
235 if (bfgId == null) {
236 log.warn(
237 "No BFG_ID found for current discharge table of gauge '" +
238 gauge + "'");
239 return false;
240 }
241
242 // Set the BFG_ID in FLYS.
243 flysStatements.beginTransaction();
244 try {
245 flysStatements
246 .getStatement("update.bfg.id.discharge.table")
247 .clearParameters()
248 .setInt("id", flysId)
249 .setString("bfg_id", bfgId)
250 .executeUpdate();
251 flysStatements.commitTransaction();
252 } catch (SQLException sqle) {
253 flysStatements.rollbackTransaction();
254 log.error(sqle, sqle);
255 return false;
256 }
257
258 return true;
173 } 259 }
174 260
175 protected boolean updateGauge( 261 protected boolean updateGauge(
176 SyncContext context, 262 SyncContext context,
177 DIPSGauge gauge 263 DIPSGauge gauge

http://dive4elements.wald.intevation.org