comparison flys-aft/src/main/java/de/intevation/aft/SyncContext.java @ 4090:d556e29592f5

Create new discharge tables if needed. flys-aft/trunk@3590 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 04 Jan 2012 17:59:26 +0000
parents 859b4781554a
children b6a18d706cbe
comparison
equal deleted inserted replaced
4089:859b4781554a 4090:d556e29592f5
1 package de.intevation.aft; 1 package de.intevation.aft;
2 2
3 import java.util.Map; 3 import java.util.Map;
4 import java.util.Date; 4 import java.util.Date;
5 import java.util.HashMap; 5 import java.util.HashMap;
6 import java.util.Set; 6 import java.util.TreeMap;
7 import java.util.TreeSet;
8 7
9 import java.sql.SQLException; 8 import java.sql.SQLException;
10 import java.sql.ResultSet; 9 import java.sql.ResultSet;
11 10
12 import org.w3c.dom.Document; 11 import org.w3c.dom.Document;
20 19
21 public class SyncContext 20 public class SyncContext
22 { 21 {
23 private static Logger log = Logger.getLogger(SyncContext.class); 22 private static Logger log = Logger.getLogger(SyncContext.class);
24 23
25 protected ConnectedStatements aftStatements; 24 protected ConnectedStatements aftStatements;
26 protected ConnectedStatements flysStatements; 25 protected ConnectedStatements flysStatements;
27 protected Document dips; 26 protected Document dips;
28 protected Map<Long, DIPSGauge> numberToGauge; 27
29 protected Set<TimeInterval> flysTimeIntervals; 28 protected Map<Long, DIPSGauge> numberToGauge;
29 protected Map<TimeInterval, TimeInterval> flysTimeIntervals;
30 30
31 public SyncContext() { 31 public SyncContext() {
32 } 32 }
33 33
34 public SyncContext( 34 public SyncContext(
110 } 110 }
111 } 111 }
112 return map; 112 return map;
113 } 113 }
114 114
115 protected Set<TimeInterval> loadTimeIntervals() throws SQLException { 115 protected Map<TimeInterval, TimeInterval> loadTimeIntervals()
116 throws SQLException {
116 117
117 boolean debug = log.isDebugEnabled(); 118 boolean debug = log.isDebugEnabled();
118 119
119 Set<TimeInterval> intervals = new TreeSet<TimeInterval>(); 120 Map<TimeInterval, TimeInterval> intervals =
121 new TreeMap<TimeInterval, TimeInterval>();
122
120 ResultSet rs = null; 123 ResultSet rs = null;
121 124
122 try { 125 try {
123 rs = flysStatements 126 rs = flysStatements
124 .getStatement("select.timeintervals") 127 .getStatement("select.timeintervals")
134 log.debug("start: " + start); 137 log.debug("start: " + start);
135 log.debug("stop: " + stop); 138 log.debug("stop: " + stop);
136 } 139 }
137 140
138 TimeInterval ti = new TimeInterval(id, start, stop); 141 TimeInterval ti = new TimeInterval(id, start, stop);
139 intervals.add(ti); 142 intervals.put(ti, ti);
140 } 143 }
141 } 144 }
142 finally { 145 finally {
143 if (rs != null) { 146 if (rs != null) {
144 rs.close(); 147 rs.close();
150 } 153 }
151 154
152 return intervals; 155 return intervals;
153 } 156 }
154 157
158 public TimeInterval fetchOrCreateFLYSTimeInterval(TimeInterval key)
159 throws SQLException
160 {
161 TimeInterval old = flysTimeIntervals.get(key);
162 if (old != null) {
163 return old;
164 }
165
166 ResultSet rs = null;
167 try {
168 rs = flysStatements.getStatement("next.timeinterval.id")
169 .executeQuery();
170 rs.next();
171 key.setId(rs.getInt("time_interval_id"));
172 rs.close(); rs = null;
173
174 flysStatements.getStatement("insert.timeinterval")
175 .clearParameters()
176 .setInt("id", key.getId())
177 .setObject("start_time", key.getStart())
178 .setObject("stop_time", key.getStop())
179 .execute();
180
181 if (log.isDebugEnabled()) {
182 log.debug("FLYS: Created time interval id: " + key.getId());
183 }
184 }
185 finally {
186 if (rs != null) {
187 rs.close();
188 }
189 }
190
191 flysTimeIntervals.put(key, key);
192
193 return key;
194 }
195
155 } 196 }
156 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 197 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
157 198

http://dive4elements.wald.intevation.org