1 |
package dk.thoerup.traininfoservice; |
package dk.thoerup.traininfoservice.db; |
2 |
|
|
3 |
import java.sql.Array; |
import java.sql.Array; |
4 |
import java.sql.Connection; |
import java.sql.Connection; |
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 |
} |
} |
60 |
|
|
61 |
} |
} |
62 |
|
|
63 |
|
private StationBean fetchStations(String SQL, StatementParamSetter setter) throws SQLException { |
64 |
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"; |
|
|
|
|
65 |
Connection conn = null; |
Connection conn = null; |
66 |
Statement stmt = null; |
PreparedStatement stmt = null; |
67 |
ResultSet res = null; |
ResultSet res = null; |
|
StationEntry result; |
|
|
|
|
68 |
try { |
try { |
69 |
conn = DBConnection.getConnection(); |
conn = DBConnection.getConnection(); |
70 |
|
stmt = conn.prepareStatement(SQL); |
|
stmt = conn.createStatement(); |
|
|
res = stmt.executeQuery(SQL); |
|
71 |
|
|
72 |
if (res.next()) { |
setter.setParams(stmt); |
73 |
result = convertSingleRow(res); |
|
74 |
} else { |
res = stmt.executeQuery(); |
75 |
throw new NostationException(); |
stations = convertResultset(res); |
76 |
} |
|
77 |
} finally { |
} finally { |
78 |
if (res != null) |
if (res != null) |
79 |
res.close(); |
res.close(); |
80 |
if (stmt != null) |
if (stmt != null) |
81 |
stmt.close(); |
stmt.close(); |
82 |
if (conn != null) |
if (conn!= null) |
83 |
conn.close(); |
conn.close(); |
84 |
} |
} |
85 |
|
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; |
|
|
|
|
86 |
|
|
87 |
try { |
} |
|
conn = DBConnection.getConnection(); |
|
88 |
|
|
89 |
stmt = conn.createStatement(); |
public StationEntry getById(int id) throws SQLException,NostationException { |
90 |
res = stmt.executeQuery(SQL); |
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"; |
|
// 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 ); |
|
92 |
|
|
93 |
} |
StationBean stations = fetchStations(SQL, new NullSetter() ); |
|
return stations; |
|
|
|
|
|
|
|
|
} finally { |
|
|
if (res != null) |
|
|
res.close(); |
|
|
if (stmt != null) |
|
|
stmt.close(); |
|
|
if (conn != null) |
|
|
conn.close(); |
|
|
} |
|
94 |
|
|
95 |
|
if (stations.entries.size() > 0 ) { |
96 |
|
return stations.entries.get(0); |
97 |
|
} else { |
98 |
|
throw new NostationException(); |
99 |
|
} |
100 |
} |
} |
101 |
|
|
102 |
|
|
103 |
|
|
104 |
/* |
/* |
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) ) |
* 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) ) |
107 |
* 'select $2 ilike $1' language sql strict immutable; |
* 'select $2 ilike $1' language sql strict immutable; |
108 |
* create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~); |
* create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~); |
109 |
*/ |
*/ |
110 |
public StationBean getByName(String name) throws SQLException { |
public StationBean getByNameNormal(final String name) throws SQLException { |
111 |
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 " + |
112 |
"FROM trainstations " + |
"FROM trainstations " + |
113 |
"WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " + |
"WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " + |
114 |
"ORDER BY name "; |
"ORDER BY name "; |
115 |
|
|
116 |
|
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 |
|
} |
123 |
|
|
124 |
|
return fetchStations(SQL, new NameSetter() ); |
125 |
|
} |
126 |
|
|
127 |
|
|
128 |
|
public StationBean getByNameFuzzy(final String name) throws SQLException { |
129 |
|
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 |
|
|
138 |
|
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 |
|
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 |
|
public StationBean getByName(String name) throws SQLException { |
159 |
|
name = removeSuffix(name, " st."); |
160 |
|
name = removeSuffix(name, " st"); |
161 |
|
name = removeSuffix(name, " station"); |
162 |
|
|
163 |
|
StationBean stations = getByNameNormal(name); |
164 |
|
|
165 |
|
if (stations.entries.size() == 0) { |
166 |
|
stations = getByNameFuzzy(name); |
167 |
|
|
168 |
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(); |
|
169 |
} |
} |
170 |
return result; |
return stations; |
171 |
} |
} |
172 |
|
|
173 |
|
|
174 |
|
|
175 |
//Latitude (horizonal), longitude(vertical) so |
//Latitude (horizonal), longitude(vertical) so |
176 |
// 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 |
177 |
// 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. |
178 |
// 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) |
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 |
// in denmark 0.4dg latitude ~ 44km, 0.75dg longitude ~ 47km |
// in denmark 0.4dg latitude ~ 44km, 0.75dg longitude ~ 47km |
182 |
|
|
183 |
// 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) |
185 |
// calculate the distance: |
// calculate the distance: |
186 |
// 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 |
187 |
|
|
188 |
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 { |
189 |
|
|
190 |
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 " : ""; |
191 |
|
|
196 |
"ORDER BY calcdist ASC " + |
"ORDER BY calcdist ASC " + |
197 |
"LIMIT " + LOCATION_LIMIT; |
"LIMIT " + LOCATION_LIMIT; |
198 |
|
|
|
|
|
|
|
|
|
StationBean result; |
|
|
Connection conn = null; |
|
|
PreparedStatement stmt = null; |
|
|
ResultSet res = null; |
|
|
try { |
|
|
conn = DBConnection.getConnection(); |
|
|
stmt = conn.prepareStatement(SQL); |
|
|
stmt.setDouble(1, latitude); |
|
|
stmt.setDouble(2, longitude); |
|
|
if (geolimit == true) { |
|
|
stmt.setDouble(3, latitude); |
|
|
stmt.setDouble(4, longitude); |
|
|
} |
|
|
res = stmt.executeQuery(); |
|
|
result = convertResultset(res); |
|
199 |
|
|
200 |
} finally { |
class LatlongSetter implements StatementParamSetter { |
201 |
if (res != null) |
@Override |
202 |
res.close(); |
public void setParams(PreparedStatement stmt) throws SQLException { |
203 |
if (stmt != null) |
stmt.setDouble(1, latitude); |
204 |
stmt.close(); |
stmt.setDouble(2, longitude); |
205 |
if (conn!= null) |
if (geolimit == true) { |
206 |
conn.close(); |
stmt.setDouble(3, latitude); |
207 |
|
stmt.setDouble(4, longitude); |
208 |
|
} |
209 |
|
} |
210 |
} |
} |
211 |
return result; |
|
212 |
|
return fetchStations(SQL, new LatlongSetter() ); |
213 |
} |
} |
214 |
|
|
215 |
public StationBean getByLocation(double latitude, double longitude) throws SQLException { |
public StationBean getByLocation(double latitude, double longitude) throws SQLException { |
231 |
"FROM trainstations " + |
"FROM trainstations " + |
232 |
"WHERE id IN " + list + " AND enabled = true " + |
"WHERE id IN " + list + " AND enabled = true " + |
233 |
"ORDER BY name "; |
"ORDER BY name "; |
234 |
|
|
235 |
|
return fetchStations(SQL, new NullSetter() ); |
236 |
|
} |
237 |
|
|
238 |
|
|
239 |
|
|
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 |
|
|
246 |
|
class NameSetter implements StatementParamSetter { |
247 |
|
@Override |
248 |
|
public void setParams(PreparedStatement stmt) throws SQLException { |
249 |
|
stmt.setString(1, name ); |
250 |
|
} |
251 |
|
} |
252 |
|
|
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 |
|
|
262 |
|
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 |
|
//used to create full dump in order to populate Google Appengine DB |
270 |
|
//after 1.1.0 also used to populate client-side station list |
271 |
|
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 |
|
"FROM trainstations WHERE enabled = true"; |
275 |
|
|
276 |
Connection conn = null; |
Connection conn = null; |
277 |
Statement stmt = null; |
Statement stmt = null; |
278 |
ResultSet res = null; |
ResultSet res = null; |
|
StationBean result; |
|
279 |
|
|
280 |
|
|
281 |
try { |
try { |
282 |
conn = DBConnection.getConnection(); |
conn = DBConnection.getConnection(); |
283 |
|
|
284 |
stmt = conn.createStatement(); |
stmt = conn.createStatement(); |
285 |
res = stmt.executeQuery(SQL); |
res = stmt.executeQuery(SQL); |
286 |
result = convertResultset(res); |
|
287 |
|
// Does mostly the same as convertResultset() |
288 |
|
StationBean stations = new StationBean(); |
289 |
|
while (res.next()) { |
290 |
|
StationEntry entry = convertSingleRow(res); |
291 |
|
|
292 |
|
Array arr = res.getArray(10); |
293 |
|
if (arr != null) { |
294 |
|
String[] aliases = (String[]) arr.getArray(); |
295 |
|
entry.setAliases(aliases); |
296 |
|
} |
297 |
|
|
298 |
|
stations.entries.add( entry ); |
299 |
|
|
300 |
|
} |
301 |
|
Collections.sort( stations.entries,nameComparator ); |
302 |
|
return stations; |
303 |
|
|
304 |
|
|
305 |
} finally { |
} finally { |
306 |
if (res != null) |
if (res != null) |
307 |
res.close(); |
res.close(); |
308 |
if (stmt != null) |
if (stmt != null) |
309 |
stmt.close(); |
stmt.close(); |
310 |
if (conn!= null) |
if (conn != null) |
311 |
conn.close(); |
conn.close(); |
312 |
} |
} |
313 |
|
|
|
return result; |
|
|
|
|
314 |
} |
} |
315 |
|
|
316 |
|
@Deprecated |
317 |
public static String getStationName(int stationID) { |
public static String getStationName(int stationID) { |
318 |
String station = ""; |
String station = ""; |
319 |
|
|
336 |
|
|
337 |
return station; |
return station; |
338 |
} |
} |
339 |
|
|
340 |
public int getIdByName(String name) throws SQLException { |
|
341 |
|
|
342 |
|
@Deprecated |
343 |
|
public int getIdByName(final String name) throws SQLException { |
344 |
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 " + |
345 |
"FROM trainstations " + |
"FROM trainstations " + |
346 |
"WHERE name = ? AND enabled = true " + |
"WHERE name = ? AND enabled = true " + |
347 |
"LIMIT 1 "; |
"LIMIT 1 "; |
348 |
|
|
349 |
StationBean result; |
class NameSetter implements StatementParamSetter { |
350 |
Connection conn = null; |
@Override |
351 |
PreparedStatement stmt = null; |
public void setParams(PreparedStatement stmt) throws SQLException { |
352 |
ResultSet res = null; |
stmt.setString(1, name ); |
353 |
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(); |
|
354 |
} |
} |
355 |
|
|
356 |
if (result.entries.size() == 1) { |
StationBean stations = fetchStations(SQL, new NameSetter() ); |
357 |
return result.entries.get(0).getId(); |
|
358 |
|
if (stations.entries.size() == 1) { |
359 |
|
return stations.entries.get(0).getId(); |
360 |
} else { |
} else { |
361 |
return -1; |
return -1; |
362 |
} |
} |