comparison flys-aft/src/main/java/org/dive4elements/etl/aft/DischargeTable.java @ 5824:06643e440d1e

Moved directories to org.dive4elements.etl
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 11:35:06 +0200
parents flys-aft/src/main/java/de/intevation/aft/DischargeTable.java@ae3625b89cfd
children
comparison
equal deleted inserted replaced
5823:52e966cc7d35 5824:06643e440d1e
1 package de.intevation.aft;
2
3 import de.intevation.db.ConnectedStatements;
4 import de.intevation.db.SymbolicStatement;
5
6 import java.sql.ResultSet;
7 import java.sql.SQLException;
8 import java.sql.Types;
9
10 import java.util.ArrayList;
11 import java.util.Date;
12 import java.util.List;
13 import java.util.Set;
14 import java.util.TreeSet;
15
16 import org.apache.log4j.Logger;
17
18 /** A Discharge Table. */
19 public class DischargeTable
20 {
21 private static Logger log = Logger.getLogger(DischargeTable.class);
22
23 protected int id;
24 protected int gaugeId;
25 protected TimeInterval timeInterval;
26 protected String description;
27 protected String bfgId;
28 protected Set<WQ> values;
29
30 public DischargeTable() {
31 }
32
33 public DischargeTable(
34 int gaugeId,
35 TimeInterval timeInterval,
36 String description,
37 String bfgId
38 ) {
39 this.gaugeId = gaugeId;
40 this.timeInterval = timeInterval;
41 this.description = description;
42 this.bfgId = bfgId;
43 values = new TreeSet<WQ>(WQ.EPS_CMP);
44 }
45
46 public DischargeTable(
47 int id,
48 int gaugeId,
49 TimeInterval timeInterval,
50 String description,
51 String bfgId
52 ) {
53 this(gaugeId, timeInterval, description, bfgId);
54 this.id = id;
55 }
56
57 public int getId() {
58 return id;
59 }
60
61 public void setId(int id) {
62 this.id = id;
63 }
64
65 public int getGaugeId() {
66 return gaugeId;
67 }
68
69 public void setGaugeId(int gaugeId) {
70 this.gaugeId = gaugeId;
71 }
72
73 public TimeInterval getTimeInterval() {
74 return timeInterval;
75 }
76
77 public void setTimeInterval(TimeInterval timeInterval) {
78 this.timeInterval = timeInterval;
79 }
80
81 public String getDescription() {
82 return description;
83 }
84
85 public void setDescription(String description) {
86 this.description = description;
87 }
88
89 public String getBfgId() {
90 return bfgId;
91 }
92
93 public void setBfgId(String bfgId) {
94 this.bfgId = bfgId;
95 }
96
97
98 public void clearValues() {
99 values.clear();
100 }
101
102 public Set<WQ> getValues() {
103 return values;
104 }
105
106 public void setValues(Set<WQ> values) {
107 this.values = values;
108 }
109
110
111 protected void loadValues(SymbolicStatement.Instance query)
112 throws SQLException
113 {
114 ResultSet rs = query.executeQuery();
115 while (rs.next()) {
116 int id = rs.getInt("id");
117 double w = rs.getDouble("w");
118 double q = rs.getDouble("q");
119 if (!values.add(new WQ(id, w, q))) {
120 log.warn("FLYS/AFT: Value duplication w="+w+" q="+q+". -> ignore.");
121 }
122 }
123 rs.close();
124 }
125
126 public void loadAftValues(SyncContext context) throws SQLException {
127 loadValues(context.getAftStatements()
128 .getStatement("select.tafelwert")
129 .clearParameters()
130 .setInt("number", getId()));
131 }
132
133 public void loadFlysValues(SyncContext context) throws SQLException {
134 loadValues(context.getFlysStatements()
135 .getStatement("select.discharge.table.values")
136 .clearParameters()
137 .setInt("table_id", getId()));
138 }
139
140 public void storeFlysValues(
141 SyncContext context,
142 int dischargeTableId
143 )
144 throws SQLException
145 {
146 ConnectedStatements flysStatements = context.getFlysStatements();
147
148 // Create the ids.
149 SymbolicStatement.Instance nextId = flysStatements
150 .getStatement("next.discharge.table.values.id");
151
152 // Insert the values.
153 SymbolicStatement.Instance insertDTV = flysStatements
154 .getStatement("insert.discharge.table.value");
155
156 for (WQ wq: values) {
157 int wqId;
158 ResultSet rs = nextId.executeQuery();
159 try {
160 rs.next();
161 wqId = rs.getInt("discharge_table_values_id");
162 }
163 finally {
164 rs.close();
165 }
166
167 insertDTV
168 .clearParameters()
169 .setInt("id", wqId)
170 .setInt("table_id", dischargeTableId)
171 .setDouble("w", wq.getW())
172 .setDouble("q", wq.getQ())
173 .execute();
174 }
175 }
176
177 public static List<DischargeTable> loadFlysDischargeTables(
178 SyncContext context,
179 int gaugeId
180 )
181 throws SQLException
182 {
183 List<DischargeTable> dts = new ArrayList<DischargeTable>();
184
185 ResultSet rs = context
186 .getFlysStatements()
187 .getStatement("select.gauge.discharge.tables")
188 .clearParameters()
189 .setInt("gauge_id", gaugeId)
190 .executeQuery();
191 try {
192 OUTER: while (rs.next()) {
193 int id = rs.getInt("id");
194 String description = rs.getString("description");
195 String bfgId = rs.getString("bfg_id");
196 if (description == null) {
197 description = "";
198 }
199 if (bfgId == null) {
200 bfgId = "";
201 }
202 for (DischargeTable dt: dts) {
203 if (dt.getBfgId().equals(bfgId)) {
204 log.warn("FLYS: Found discharge table '" +
205 bfgId + "' with same bfg_id. -> ignore");
206 continue OUTER;
207 }
208 }
209 Date startTime = rs.getDate("start_time");
210 Date stopTime = rs.getDate("stop_time");
211 TimeInterval ti = startTime == null
212 ? null
213 : new TimeInterval(startTime, stopTime);
214
215 DischargeTable dt = new DischargeTable(
216 id, gaugeId, ti, description, bfgId);
217 dts.add(dt);
218 }
219 }
220 finally {
221 rs.close();
222 }
223
224 return dts;
225 }
226
227 public static List<DischargeTable> loadAftDischargeTables(
228 SyncContext context,
229 Long officialNumber
230 )
231 throws SQLException
232 {
233 return loadAftDischargeTables(context, officialNumber, 0);
234 }
235
236 public static List<DischargeTable> loadAftDischargeTables(
237 SyncContext context,
238 Long officialNumber,
239 int flysGaugeId
240 )
241 throws SQLException
242 {
243 List<DischargeTable> dts = new ArrayList<DischargeTable>();
244
245 ResultSet rs = context
246 .getAftStatements()
247 .getStatement("select.abflusstafel")
248 .clearParameters()
249 .setString("number", "%" + officialNumber)
250 .executeQuery();
251 try {
252 OUTER: while (rs.next()) {
253 int dtId = rs.getInt("ABFLUSSTAFEL_NR");
254 Date from = rs.getDate("GUELTIG_VON");
255 Date to = rs.getDate("GUELTIG_BIS");
256
257 if (from == null) {
258 log.warn("AFT: ABFLUSSTAFEL_NR = "
259 + dtId + ": GUELTIG_VON = NULL -> ignored.");
260 }
261
262 if (to == null) {
263 log.warn("AFT: ABFLUSSTAFEL_NR = "
264 + dtId + ": GUELTIG_BIS = NULL -> ignored.");
265 }
266
267 if (from == null || to == null) {
268 continue;
269 }
270
271 if (from.compareTo(to) > 0) {
272 log.warn("AFT: ABFLUSSTAFEL_NR = "
273 + dtId + ": " + from + " > " + to + ". -> swap");
274 Date temp = from;
275 from = to;
276 to = temp;
277 }
278
279 String description = rs.getString("ABFLUSSTAFEL_BEZ");
280 if (description == null) {
281 description = String.valueOf(officialNumber);
282 }
283
284 String bfgId = rs.getString("BFG_ID");
285 if (bfgId == null) {
286 bfgId = "";
287 }
288
289 for (DischargeTable dt: dts) {
290 if (dt.getBfgId().equals(bfgId)) {
291 log.warn("AFT: Found discharge table '" +
292 bfgId + "' with same bfg_id. -> ignore.");
293 continue OUTER;
294 }
295 }
296
297 TimeInterval timeInterval = new TimeInterval(from, to);
298
299 DischargeTable dt = new DischargeTable(
300 dtId,
301 flysGaugeId,
302 timeInterval,
303 description,
304 bfgId);
305 dts.add(dt);
306 }
307 }
308 finally {
309 rs.close();
310 }
311
312 return dts;
313 }
314
315 public void persistFlysTimeInterval(
316 SyncContext context
317 )
318 throws SQLException
319 {
320 if (timeInterval != null) {
321 timeInterval = context.fetchOrCreateFLYSTimeInterval(
322 timeInterval);
323 }
324 }
325
326 public int persistFlysDischargeTable(
327 SyncContext context,
328 int gaugeId
329 )
330 throws SQLException
331 {
332 ConnectedStatements flysStatements =
333 context.getFlysStatements();
334
335 int flysId;
336
337 ResultSet rs = flysStatements
338 .getStatement("next.discharge.id")
339 .executeQuery();
340 try {
341 rs.next();
342 flysId = rs.getInt("discharge_table_id");
343 }
344 finally {
345 rs.close();
346 }
347
348 SymbolicStatement.Instance insertDT = flysStatements
349 .getStatement("insert.dischargetable")
350 .clearParameters()
351 .setInt("id", flysId)
352 .setInt("gauge_id", gaugeId)
353 .setString("description", description)
354 .setString("bfg_id", bfgId);
355
356 if (timeInterval != null) {
357 insertDT.setInt("time_interval_id", timeInterval.getId());
358 }
359 else {
360 insertDT.setNull("time_interval_id", Types.INTEGER);
361 }
362
363 insertDT.execute();
364
365 if (log.isDebugEnabled()) {
366 log.debug("FLYS: Created discharge table id: " + id);
367 }
368
369 return flysId;
370 }
371 }
372 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org