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