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