1 |
torben |
1255 |
package dk.thoerup.traininfoservice.db; |
2 |
torben |
588 |
|
3 |
torben |
1082 |
import java.sql.Array; |
4 |
torben |
588 |
import java.sql.Connection; |
5 |
|
|
import java.sql.PreparedStatement; |
6 |
|
|
import java.sql.ResultSet; |
7 |
|
|
import java.sql.SQLException; |
8 |
|
|
import java.sql.Statement; |
9 |
torben |
895 |
import java.util.logging.Logger; |
10 |
torben |
588 |
|
11 |
torben |
1061 |
import dk.thoerup.android.traininfo.common.StationBean; |
12 |
torben |
1409 |
import dk.thoerup.android.traininfo.common.StationEntry; |
13 |
torben |
1060 |
|
14 |
torben |
588 |
public class StationDAO { |
15 |
torben |
1145 |
|
16 |
torben |
1504 |
private interface StatementParamSetter { |
17 |
|
|
public void setParams(PreparedStatement stmt) throws SQLException ; |
18 |
|
|
} |
19 |
|
|
private class NullSetter implements StatementParamSetter { |
20 |
|
|
@Override |
21 |
|
|
public void setParams(PreparedStatement stmt) throws SQLException {} |
22 |
|
|
} |
23 |
|
|
|
24 |
torben |
1145 |
public static class NostationException extends Exception { |
25 |
|
|
private static final long serialVersionUID = 1L; |
26 |
|
|
} |
27 |
|
|
|
28 |
torben |
997 |
final static int LOCATION_LIMIT = 8; |
29 |
torben |
895 |
static final Logger logger = Logger.getLogger(StationDAO.class.getName()); |
30 |
torben |
841 |
|
31 |
torben |
895 |
|
32 |
torben |
1060 |
private StationEntry convertSingleRow(ResultSet res) throws SQLException { |
33 |
|
|
StationEntry station = new StationEntry(); |
34 |
torben |
836 |
|
35 |
torben |
588 |
station.setId( res.getInt(1) ); |
36 |
|
|
station.setName( res.getString(2) ); |
37 |
|
|
station.setLatitude( res.getDouble(3) ); |
38 |
|
|
station.setLongitude( res.getDouble(4) ); |
39 |
|
|
station.setRegional( res.getString(5) ); |
40 |
|
|
station.setStrain( res.getString(6) ); |
41 |
|
|
station.setMetro( res.getString(7) ); |
42 |
|
|
station.setAddress( res.getString(8) ); |
43 |
|
|
station.setCalcdist( (int)res.getDouble(9) ); |
44 |
torben |
1060 |
|
45 |
|
|
station.setIsRegional( station.getRegional() != null ); |
46 |
|
|
station.setIsStrain( station.getStrain() != null ); |
47 |
|
|
station.setIsMetro( station.getMetro() != null ); |
48 |
torben |
836 |
|
49 |
torben |
588 |
return station; |
50 |
|
|
} |
51 |
torben |
836 |
|
52 |
torben |
1060 |
private StationBean convertResultset(ResultSet res) throws SQLException { |
53 |
|
|
StationBean stations = new StationBean(); |
54 |
torben |
588 |
while (res.next()) { |
55 |
torben |
1060 |
stations.entries.add( convertSingleRow(res) ); |
56 |
torben |
588 |
} |
57 |
|
|
return stations; |
58 |
torben |
836 |
|
59 |
torben |
588 |
} |
60 |
torben |
836 |
|
61 |
torben |
1504 |
private StationBean fetchStations(String SQL, StatementParamSetter setter) throws SQLException { |
62 |
|
|
StationBean stations; |
63 |
torben |
588 |
Connection conn = null; |
64 |
torben |
1504 |
PreparedStatement stmt = null; |
65 |
torben |
589 |
ResultSet res = null; |
66 |
torben |
588 |
try { |
67 |
|
|
conn = DBConnection.getConnection(); |
68 |
torben |
1504 |
stmt = conn.prepareStatement(SQL); |
69 |
|
|
|
70 |
|
|
setter.setParams(stmt); |
71 |
torben |
836 |
|
72 |
torben |
1504 |
res = stmt.executeQuery(); |
73 |
|
|
stations = convertResultset(res); |
74 |
|
|
|
75 |
torben |
588 |
} finally { |
76 |
torben |
589 |
if (res != null) |
77 |
|
|
res.close(); |
78 |
|
|
if (stmt != null) |
79 |
|
|
stmt.close(); |
80 |
torben |
1504 |
if (conn!= null) |
81 |
torben |
588 |
conn.close(); |
82 |
|
|
} |
83 |
torben |
1504 |
return stations; |
84 |
|
|
|
85 |
|
|
} |
86 |
torben |
836 |
|
87 |
torben |
1504 |
public StationEntry getById(int id) throws SQLException,NostationException { |
88 |
|
|
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0 " + |
89 |
|
|
"FROM trainstations WHERE id=" + id + " AND enabled=true"; |
90 |
|
|
|
91 |
|
|
StationBean stations = fetchStations(SQL, new NullSetter() ); |
92 |
|
|
|
93 |
|
|
if (stations.entries.size() > 0 ) { |
94 |
|
|
return stations.entries.get(0); |
95 |
|
|
} else { |
96 |
|
|
throw new NostationException(); |
97 |
|
|
} |
98 |
torben |
588 |
} |
99 |
torben |
1082 |
|
100 |
torben |
836 |
|
101 |
torben |
1082 |
|
102 |
torben |
714 |
/* |
103 |
|
|
* this code requires theses statements are run on database in order to do ILIKE searches against aliases (which is defines as array of varchar(64) ) |
104 |
|
|
* create function rlike(text,text) returns bool as |
105 |
|
|
* 'select $2 ilike $1' language sql strict immutable; |
106 |
torben |
836 |
* create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~); |
107 |
torben |
714 |
*/ |
108 |
torben |
1507 |
public StationBean getByNameNormal(final String name) throws SQLException { |
109 |
torben |
588 |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " + |
110 |
|
|
"FROM trainstations " + |
111 |
torben |
714 |
"WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " + |
112 |
torben |
588 |
"ORDER BY name "; |
113 |
|
|
|
114 |
torben |
1504 |
class NameSetter implements StatementParamSetter { |
115 |
|
|
@Override |
116 |
|
|
public void setParams(PreparedStatement stmt) throws SQLException { |
117 |
|
|
stmt.setString(1, name + "%"); |
118 |
|
|
stmt.setString(2, name + "%"); |
119 |
|
|
} |
120 |
torben |
588 |
} |
121 |
torben |
1504 |
|
122 |
|
|
return fetchStations(SQL, new NameSetter() ); |
123 |
torben |
588 |
} |
124 |
torben |
1507 |
|
125 |
|
|
|
126 |
|
|
public StationBean getByNameFuzzy(final String name) throws SQLException { |
127 |
|
|
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0, " + |
128 |
|
|
"levenshtein(lower(name),lower(?) ) as leven " + |
129 |
|
|
"FROM trainstations " + |
130 |
torben |
1510 |
"WHERE (levenshtein(lower(name),lower(?) ) <= 3) " + |
131 |
torben |
1507 |
"AND enabled = true " + |
132 |
|
|
"ORDER BY leven " + |
133 |
|
|
"LIMIT 1"; |
134 |
torben |
836 |
|
135 |
torben |
1507 |
class NameSetter implements StatementParamSetter { |
136 |
|
|
@Override |
137 |
|
|
public void setParams(PreparedStatement stmt) throws SQLException { |
138 |
|
|
stmt.setString(1, name ); |
139 |
|
|
stmt.setString(2, name ); |
140 |
|
|
} |
141 |
|
|
} |
142 |
|
|
|
143 |
|
|
StationBean stations = fetchStations(SQL, new NameSetter() ); |
144 |
|
|
stations.fuzzystrmatch = true; |
145 |
|
|
return stations; |
146 |
|
|
} |
147 |
|
|
|
148 |
|
|
public StationBean getByName(final String name) throws SQLException { |
149 |
|
|
StationBean stations = getByNameNormal(name); |
150 |
|
|
|
151 |
|
|
if (stations.entries.size() == 0) { |
152 |
|
|
logger.info("getByName failover: " + name); |
153 |
|
|
stations = getByNameFuzzy(name); |
154 |
|
|
} |
155 |
|
|
return stations; |
156 |
|
|
} |
157 |
|
|
|
158 |
|
|
|
159 |
|
|
|
160 |
torben |
1254 |
//Latitude (horizonal), longitude(vertical) so |
161 |
|
|
// 1 degree latitude is ~ 111320 meters, since the distance between the horizonal lines is always the same |
162 |
|
|
// 1 degree longitude is ~111320 meters at equator but gets shorter as we get closer to the poles. |
163 |
torben |
1511 |
// so 1 degree longitude is 64.5 km at denmarks southern point (gedser=54.55,11.95) |
164 |
|
|
// and is 59.4km at northern point (skagen = 57.75,10.65) |
165 |
|
|
// The "hack" with max 0.4 degrees latitude and 0.75 degrees longitude is only valid since we only service danish trains, |
166 |
torben |
928 |
// in denmark 0.4dg latitude ~ 44km, 0.75dg longitude ~ 47km |
167 |
torben |
836 |
|
168 |
torben |
741 |
// the ultra fast method (and only slightly inaccurate as long as we only cover a limited geographically area) |
169 |
|
|
// is using an aproximation of the length of 1 latitude degree and 1 longitude degree and just use pythagoras to |
170 |
|
|
// calculate the distance: |
171 |
torben |
836 |
// sqrt( power(abs(latitude-?)*111320, 2) + power(abs(longitude-?)*63000,2) )::int as calcdist |
172 |
|
|
|
173 |
torben |
1504 |
public StationBean getByLocationWorker(final double latitude, final double longitude, final boolean geolimit) throws SQLException { |
174 |
torben |
841 |
|
175 |
torben |
929 |
String limitExpression = (geolimit == true) ? "AND abs(latitude-?)<0.4 AND abs(longitude-?)<0.75 " : ""; |
176 |
torben |
841 |
|
177 |
|
|
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, " + |
178 |
|
|
"earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " + |
179 |
|
|
"FROM trainstations " + |
180 |
|
|
"WHERE enabled = true " + limitExpression + |
181 |
|
|
"ORDER BY calcdist ASC " + |
182 |
|
|
"LIMIT " + LOCATION_LIMIT; |
183 |
torben |
849 |
|
184 |
torben |
841 |
|
185 |
torben |
1504 |
class LatlongSetter implements StatementParamSetter { |
186 |
|
|
@Override |
187 |
|
|
public void setParams(PreparedStatement stmt) throws SQLException { |
188 |
|
|
stmt.setDouble(1, latitude); |
189 |
|
|
stmt.setDouble(2, longitude); |
190 |
|
|
if (geolimit == true) { |
191 |
|
|
stmt.setDouble(3, latitude); |
192 |
|
|
stmt.setDouble(4, longitude); |
193 |
|
|
} |
194 |
|
|
} |
195 |
|
|
} |
196 |
torben |
849 |
|
197 |
torben |
1504 |
return fetchStations(SQL, new LatlongSetter() ); |
198 |
torben |
588 |
} |
199 |
torben |
841 |
|
200 |
torben |
1060 |
public StationBean getByLocation(double latitude, double longitude) throws SQLException { |
201 |
|
|
StationBean result = getByLocationWorker(latitude, longitude, true); |
202 |
torben |
841 |
|
203 |
torben |
1060 |
if (result.entries.size() < LOCATION_LIMIT) { //failover |
204 |
torben |
894 |
logger.info("getByLocation failover: " +latitude + "," + longitude); |
205 |
|
|
|
206 |
torben |
841 |
result = getByLocationWorker(latitude, longitude, false); |
207 |
|
|
} |
208 |
|
|
|
209 |
|
|
return result; |
210 |
|
|
} |
211 |
|
|
|
212 |
|
|
|
213 |
torben |
836 |
|
214 |
torben |
1060 |
public StationBean getByList(String list) throws SQLException { |
215 |
torben |
836 |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro, address,0.0 " + |
216 |
|
|
"FROM trainstations " + |
217 |
|
|
"WHERE id IN " + list + " AND enabled = true " + |
218 |
|
|
"ORDER BY name "; |
219 |
torben |
1504 |
|
220 |
|
|
return fetchStations(SQL, new NullSetter() ); |
221 |
|
|
} |
222 |
|
|
|
223 |
torben |
836 |
|
224 |
torben |
1504 |
|
225 |
|
|
public StationEntry getSimpleByName(final String name) throws SQLException { |
226 |
|
|
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " + |
227 |
|
|
"FROM trainstations " + |
228 |
|
|
"WHERE name = ? AND enabled = true " + |
229 |
|
|
"LIMIT 1 "; |
230 |
torben |
836 |
|
231 |
torben |
1504 |
class NameSetter implements StatementParamSetter { |
232 |
|
|
@Override |
233 |
|
|
public void setParams(PreparedStatement stmt) throws SQLException { |
234 |
|
|
stmt.setString(1, name ); |
235 |
|
|
} |
236 |
torben |
836 |
} |
237 |
torben |
1504 |
|
238 |
|
|
StationBean stations = fetchStations(SQL, new NameSetter() ); |
239 |
|
|
|
240 |
|
|
if (stations.entries.size() == 1) { |
241 |
|
|
return stations.entries.get(0); |
242 |
|
|
} else { |
243 |
|
|
return null; |
244 |
|
|
} |
245 |
|
|
} |
246 |
torben |
1505 |
|
247 |
|
|
//used to create full dump in order to populate Google Appengine DB |
248 |
|
|
@Deprecated |
249 |
|
|
public StationBean dumpAll() throws SQLException { |
250 |
|
|
|
251 |
|
|
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0,aliases " + |
252 |
|
|
"FROM trainstations WHERE enabled = true ORDER BY id"; |
253 |
|
|
|
254 |
|
|
Connection conn = null; |
255 |
|
|
Statement stmt = null; |
256 |
|
|
ResultSet res = null; |
257 |
torben |
836 |
|
258 |
torben |
1505 |
|
259 |
|
|
try { |
260 |
|
|
conn = DBConnection.getConnection(); |
261 |
|
|
|
262 |
|
|
stmt = conn.createStatement(); |
263 |
|
|
res = stmt.executeQuery(SQL); |
264 |
|
|
|
265 |
|
|
// Does mostly the same as convertResultset() |
266 |
|
|
StationBean stations = new StationBean(); |
267 |
|
|
while (res.next()) { |
268 |
|
|
StationEntry entry = convertSingleRow(res); |
269 |
|
|
|
270 |
|
|
Array arr = res.getArray(10); |
271 |
|
|
if (arr != null) { |
272 |
|
|
String[] aliases = (String[]) arr.getArray(); |
273 |
|
|
entry.setAliases(aliases); |
274 |
|
|
} |
275 |
|
|
|
276 |
|
|
stations.entries.add( entry ); |
277 |
|
|
|
278 |
|
|
} |
279 |
|
|
return stations; |
280 |
|
|
|
281 |
|
|
|
282 |
|
|
} finally { |
283 |
|
|
if (res != null) |
284 |
|
|
res.close(); |
285 |
|
|
if (stmt != null) |
286 |
|
|
stmt.close(); |
287 |
|
|
if (conn != null) |
288 |
|
|
conn.close(); |
289 |
|
|
} |
290 |
|
|
|
291 |
|
|
} |
292 |
|
|
|
293 |
torben |
1417 |
@Deprecated |
294 |
torben |
836 |
public static String getStationName(int stationID) { |
295 |
|
|
String station = ""; |
296 |
|
|
|
297 |
|
|
Connection conn = null; |
298 |
|
|
try { |
299 |
|
|
conn = DBConnection.getConnection(); |
300 |
|
|
Statement stmt = conn.createStatement(); |
301 |
|
|
ResultSet rs = stmt.executeQuery("SELECT name FROM trainstations WHERE id=" + stationID); |
302 |
|
|
if (rs.next()) { |
303 |
|
|
station = rs.getString(1); |
304 |
|
|
} |
305 |
|
|
|
306 |
|
|
} catch (Exception e) { |
307 |
|
|
} finally { |
308 |
torben |
588 |
try { |
309 |
torben |
836 |
if (conn != null && !conn.isClosed()) |
310 |
torben |
588 |
conn.close(); |
311 |
torben |
836 |
} catch (Exception e) {} |
312 |
|
|
} |
313 |
torben |
650 |
|
314 |
torben |
836 |
return station; |
315 |
|
|
} |
316 |
torben |
1417 |
|
317 |
torben |
1504 |
|
318 |
|
|
|
319 |
torben |
1417 |
@Deprecated |
320 |
torben |
1504 |
public int getIdByName(final String name) throws SQLException { |
321 |
torben |
836 |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " + |
322 |
|
|
"FROM trainstations " + |
323 |
|
|
"WHERE name = ? AND enabled = true " + |
324 |
|
|
"LIMIT 1 "; |
325 |
torben |
650 |
|
326 |
torben |
1504 |
class NameSetter implements StatementParamSetter { |
327 |
|
|
@Override |
328 |
|
|
public void setParams(PreparedStatement stmt) throws SQLException { |
329 |
|
|
stmt.setString(1, name ); |
330 |
|
|
} |
331 |
torben |
836 |
} |
332 |
torben |
1504 |
|
333 |
|
|
StationBean stations = fetchStations(SQL, new NameSetter() ); |
334 |
|
|
|
335 |
|
|
if (stations.entries.size() == 1) { |
336 |
|
|
return stations.entries.get(0).getId(); |
337 |
torben |
836 |
} else { |
338 |
|
|
return -1; |
339 |
|
|
} |
340 |
|
|
} |
341 |
torben |
588 |
} |