Mercurial > dive4elements > river
comparison flys-aft/src/main/java/org/dive4elements/etl/aft/SyncContext.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/SyncContext.java@2b7f44c80857 |
children |
comparison
equal
deleted
inserted
replaced
5823:52e966cc7d35 | 5824:06643e440d1e |
---|---|
1 package de.intevation.aft; | |
2 | |
3 import de.intevation.db.ConnectedStatements; | |
4 | |
5 import java.sql.ResultSet; | |
6 import java.sql.SQLException; | |
7 | |
8 import java.util.Date; | |
9 import java.util.HashMap; | |
10 import java.util.Map; | |
11 import java.util.TreeMap; | |
12 | |
13 import org.apache.log4j.Logger; | |
14 | |
15 import org.w3c.dom.Document; | |
16 import org.w3c.dom.Element; | |
17 import org.w3c.dom.NodeList; | |
18 | |
19 public class SyncContext | |
20 { | |
21 private static Logger log = Logger.getLogger(SyncContext.class); | |
22 | |
23 protected ConnectedStatements aftStatements; | |
24 protected ConnectedStatements flysStatements; | |
25 protected Document dips; | |
26 | |
27 protected Map<Long, DIPSGauge> numberToGauge; | |
28 protected Map<TimeInterval, TimeInterval> flysTimeIntervals; | |
29 | |
30 public SyncContext() { | |
31 } | |
32 | |
33 public SyncContext( | |
34 ConnectedStatements aftStatements, | |
35 ConnectedStatements flysStatements, | |
36 Document dips | |
37 ) { | |
38 this.aftStatements = aftStatements; | |
39 this.flysStatements = flysStatements; | |
40 this.dips = dips; | |
41 } | |
42 | |
43 public void init() throws SQLException { | |
44 numberToGauge = indexByNumber(dips); | |
45 flysTimeIntervals = loadTimeIntervals(); | |
46 } | |
47 | |
48 public ConnectedStatements getAftStatements() { | |
49 return aftStatements; | |
50 } | |
51 | |
52 public void setAftStatements(ConnectedStatements aftStatements) { | |
53 this.aftStatements = aftStatements; | |
54 } | |
55 | |
56 public ConnectedStatements getFlysStatements() { | |
57 return flysStatements; | |
58 } | |
59 | |
60 public void setFlysStatements(ConnectedStatements flysStatements) { | |
61 this.flysStatements = flysStatements; | |
62 } | |
63 | |
64 public Document getDips() { | |
65 return dips; | |
66 } | |
67 | |
68 public void setDips(Document dips) { | |
69 this.dips = dips; | |
70 } | |
71 | |
72 void close() { | |
73 aftStatements.close(); | |
74 flysStatements.close(); | |
75 } | |
76 | |
77 public static Long numberToLong(String s) { | |
78 try { | |
79 return Long.valueOf(s.trim()); | |
80 } | |
81 catch (NumberFormatException nfe) { | |
82 } | |
83 return null; | |
84 } | |
85 | |
86 public Map<Long, DIPSGauge> getDIPSGauges() { | |
87 return numberToGauge; | |
88 } | |
89 | |
90 public Map<Long, DIPSGauge> getDIPSGauges( | |
91 String riverName, | |
92 double from, | |
93 double to | |
94 ) { | |
95 if (from > to) { | |
96 double t = from; | |
97 from = to; | |
98 to = t; | |
99 } | |
100 | |
101 riverName = riverName.toLowerCase(); | |
102 | |
103 Map<Long, DIPSGauge> result = new HashMap<Long, DIPSGauge>(); | |
104 | |
105 for (Map.Entry<Long, DIPSGauge> entry: numberToGauge.entrySet()) { | |
106 DIPSGauge gauge = entry.getValue(); | |
107 // XXX: Maybe a bit too sloppy. | |
108 if (!riverName.contains(gauge.getRiverName().toLowerCase())) { | |
109 continue; | |
110 } | |
111 double station = gauge.getStation(); | |
112 if (station >= from && station <= to) { | |
113 result.put(entry.getKey(), gauge); | |
114 } | |
115 } | |
116 | |
117 return result; | |
118 } | |
119 | |
120 protected static Map<Long, DIPSGauge> indexByNumber(Document document) { | |
121 Map<Long, DIPSGauge> map = new HashMap<Long, DIPSGauge>(); | |
122 NodeList nodes = document.getElementsByTagName("PEGELSTATION"); | |
123 for (int i = nodes.getLength()-1; i >= 0; --i) { | |
124 Element element = (Element)nodes.item(i); | |
125 String numberString = element.getAttribute("NUMMER"); | |
126 Long number = numberToLong(numberString); | |
127 if (number != null) { | |
128 DIPSGauge newG = new DIPSGauge(element); | |
129 DIPSGauge oldG = map.put(number, newG); | |
130 if (oldG != null) { | |
131 log.warn("DIPS: '" + newG.getName() + | |
132 "' collides with '" + oldG.getName() + | |
133 "' on gauge number " + number + "."); | |
134 } | |
135 } | |
136 else { | |
137 log.warn("DIPS: Gauge '" + element.getAttribute("NAME") + | |
138 "' has invalid gauge number '" + numberString + "'."); | |
139 } | |
140 } | |
141 return map; | |
142 } | |
143 | |
144 protected Map<TimeInterval, TimeInterval> loadTimeIntervals() | |
145 throws SQLException { | |
146 | |
147 boolean debug = log.isDebugEnabled(); | |
148 | |
149 Map<TimeInterval, TimeInterval> intervals = | |
150 new TreeMap<TimeInterval, TimeInterval>(); | |
151 | |
152 ResultSet rs = flysStatements | |
153 .getStatement("select.timeintervals") | |
154 .executeQuery(); | |
155 | |
156 try { | |
157 while (rs.next()) { | |
158 int id = rs.getInt("id"); | |
159 Date start = rs.getDate("start_time"); | |
160 Date stop = rs.getDate("stop_time"); | |
161 | |
162 if (debug) { | |
163 log.debug("id: " + id); | |
164 log.debug("start: " + start); | |
165 log.debug("stop: " + stop); | |
166 } | |
167 | |
168 TimeInterval ti = new TimeInterval(id, start, stop); | |
169 intervals.put(ti, ti); | |
170 } | |
171 } | |
172 finally { | |
173 rs.close(); | |
174 } | |
175 | |
176 if (debug) { | |
177 log.debug("loaded time intervals: " + intervals.size()); | |
178 } | |
179 | |
180 return intervals; | |
181 } | |
182 | |
183 public TimeInterval fetchOrCreateFLYSTimeInterval(TimeInterval key) | |
184 throws SQLException | |
185 { | |
186 TimeInterval old = flysTimeIntervals.get(key); | |
187 if (old != null) { | |
188 return old; | |
189 } | |
190 | |
191 ResultSet rs = flysStatements | |
192 .getStatement("next.timeinterval.id") | |
193 .executeQuery(); | |
194 | |
195 try { | |
196 rs.next(); | |
197 key.setId(rs.getInt("time_interval_id")); | |
198 } | |
199 finally { | |
200 rs.close(); | |
201 } | |
202 | |
203 if (log.isDebugEnabled()) { | |
204 log.debug("FLYS: Created time interval id: " + key.getId()); | |
205 log.debug("FLYS: " + key); | |
206 } | |
207 | |
208 flysStatements.getStatement("insert.timeinterval") | |
209 .clearParameters() | |
210 .setInt("id", key.getId()) | |
211 .setObject("start_time", key.getStart()) | |
212 .setObject("stop_time", key.getStop()) | |
213 .execute(); | |
214 | |
215 flysTimeIntervals.put(key, key); | |
216 | |
217 return key; | |
218 } | |
219 } | |
220 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |