1 |
package dk.thoerup.traininfoservice; |
package dk.thoerup.traininfoservice.db; |
2 |
|
|
3 |
|
import java.sql.Array; |
4 |
import java.sql.Connection; |
import java.sql.Connection; |
5 |
import java.sql.PreparedStatement; |
import java.sql.PreparedStatement; |
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; |
|
import java.util.ArrayList; |
|
|
import java.util.List; |
|
9 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
10 |
|
|
11 |
|
import dk.thoerup.android.traininfo.common.StationBean; |
12 |
|
import dk.thoerup.android.traininfo.common.StationEntry; |
13 |
|
|
14 |
public class StationDAO { |
public class StationDAO { |
15 |
|
|
16 |
|
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 |
|
public static class NostationException extends Exception { |
25 |
|
private static final long serialVersionUID = 1L; |
26 |
|
} |
27 |
|
|
28 |
final static int LOCATION_LIMIT = 8; |
final static int LOCATION_LIMIT = 8; |
29 |
static final Logger logger = Logger.getLogger(StationDAO.class.getName()); |
static final Logger logger = Logger.getLogger(StationDAO.class.getName()); |
30 |
|
|
31 |
|
|
32 |
private StationBean convertSingleRow(ResultSet res) throws SQLException { |
private StationEntry convertSingleRow(ResultSet res) throws SQLException { |
33 |
StationBean station = new StationBean(); |
StationEntry station = new StationEntry(); |
34 |
|
|
35 |
station.setId( res.getInt(1) ); |
station.setId( res.getInt(1) ); |
36 |
station.setName( res.getString(2) ); |
station.setName( res.getString(2) ); |
41 |
station.setMetro( res.getString(7) ); |
station.setMetro( res.getString(7) ); |
42 |
station.setAddress( res.getString(8) ); |
station.setAddress( res.getString(8) ); |
43 |
station.setCalcdist( (int)res.getDouble(9) ); |
station.setCalcdist( (int)res.getDouble(9) ); |
44 |
|
|
45 |
|
station.setIsRegional( station.getRegional() != null ); |
46 |
|
station.setIsStrain( station.getStrain() != null ); |
47 |
|
station.setIsMetro( station.getMetro() != null ); |
48 |
|
|
49 |
return station; |
return station; |
50 |
} |
} |
51 |
|
|
52 |
private List<StationBean> convertResultset(ResultSet res) throws SQLException { |
private StationBean convertResultset(ResultSet res) throws SQLException { |
53 |
List<StationBean> stations = new ArrayList<StationBean>(); |
StationBean stations = new StationBean(); |
54 |
while (res.next()) { |
while (res.next()) { |
55 |
stations.add( convertSingleRow(res) ); |
stations.entries.add( convertSingleRow(res) ); |
56 |
} |
} |
57 |
return stations; |
return stations; |
58 |
|
|
59 |
} |
} |
60 |
|
|
61 |
|
private StationBean fetchStations(String SQL, StatementParamSetter setter) throws SQLException { |
62 |
public StationBean getById(int id) throws SQLException { |
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"; |
|
|
|
|
63 |
Connection conn = null; |
Connection conn = null; |
64 |
Statement stmt = null; |
PreparedStatement stmt = null; |
65 |
ResultSet res = null; |
ResultSet res = null; |
|
StationBean result; |
|
|
|
|
66 |
try { |
try { |
67 |
conn = DBConnection.getConnection(); |
conn = DBConnection.getConnection(); |
68 |
|
stmt = conn.prepareStatement(SQL); |
69 |
|
|
70 |
|
setter.setParams(stmt); |
71 |
|
|
72 |
|
res = stmt.executeQuery(); |
73 |
|
stations = convertResultset(res); |
74 |
|
|
|
stmt = conn.createStatement(); |
|
|
res = stmt.executeQuery(SQL); |
|
|
res.next(); |
|
|
result = convertSingleRow(res); |
|
75 |
} finally { |
} finally { |
76 |
if (res != null) |
if (res != null) |
77 |
res.close(); |
res.close(); |
78 |
if (stmt != null) |
if (stmt != null) |
79 |
stmt.close(); |
stmt.close(); |
80 |
if (conn != null) |
if (conn!= null) |
81 |
conn.close(); |
conn.close(); |
82 |
} |
} |
83 |
|
return stations; |
84 |
|
|
85 |
|
} |
86 |
|
|
87 |
return result; |
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 |
} |
} |
99 |
|
|
100 |
|
|
101 |
|
|
102 |
/* |
/* |
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) ) |
* 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) ) |
105 |
* 'select $2 ilike $1' language sql strict immutable; |
* 'select $2 ilike $1' language sql strict immutable; |
106 |
* create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~); |
* create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~); |
107 |
*/ |
*/ |
108 |
public List<StationBean> getByName(String name) throws SQLException { |
public StationBean getByNameNormal(final String name) throws SQLException { |
109 |
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 " + |
110 |
"FROM trainstations " + |
"FROM trainstations " + |
111 |
"WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " + |
"WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " + |
112 |
"ORDER BY name "; |
"ORDER BY name "; |
113 |
|
|
114 |
|
class NameSetter implements StatementParamSetter { |
115 |
List<StationBean> result; |
@Override |
116 |
Connection conn = null; |
public void setParams(PreparedStatement stmt) throws SQLException { |
117 |
PreparedStatement stmt = null; |
stmt.setString(1, name + "%"); |
118 |
ResultSet res = null; |
stmt.setString(2, name + "%"); |
119 |
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(); |
|
120 |
} |
} |
121 |
return result; |
|
122 |
|
return fetchStations(SQL, new NameSetter() ); |
123 |
} |
} |
124 |
|
|
125 |
|
|
126 |
|
public StationBean getByNameFuzzy(final String name) throws SQLException { |
127 |
|
String SQL = "SELECT * FROM (" + |
128 |
|
" SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0, " + |
129 |
|
" levenshtein(lower(name),lower(?) ) as leven " + |
130 |
|
" FROM trainstations " + |
131 |
|
" WHERE enabled = true ) as lev2 " + |
132 |
|
"WHERE (leven <= 3) " + |
133 |
|
"ORDER BY leven " + |
134 |
|
"LIMIT 1"; |
135 |
|
|
136 |
|
class NameSetter implements StatementParamSetter { |
137 |
|
@Override |
138 |
|
public void setParams(PreparedStatement stmt) throws SQLException { |
139 |
|
stmt.setString(1, name ); |
140 |
|
} |
141 |
|
} |
142 |
|
|
143 |
|
StationBean stations = fetchStations(SQL, new NameSetter() ); |
144 |
|
stations.fuzzystrmatch = true; |
145 |
|
return stations; |
146 |
|
} |
147 |
|
|
148 |
|
public StationBean getByName(String name) throws SQLException { |
149 |
|
StationBean stations = getByNameNormal(name); |
150 |
|
|
151 |
|
if (stations.entries.size() == 0) { |
152 |
|
logger.info("getByName failover: " + name); |
153 |
|
name = name.replace(".", ""); //remove any .'s before fuzzy search |
154 |
|
stations = getByNameFuzzy(name); |
155 |
|
} |
156 |
|
return stations; |
157 |
|
} |
158 |
|
|
159 |
|
|
160 |
|
|
161 |
//the "hack" with max 0.4 degrees latitude and 0.75 degrees longitude is only valid since we only service danish trains, |
//Latitude (horizonal), longitude(vertical) so |
162 |
|
// 1 degree latitude is ~ 111320 meters, since the distance between the horizonal lines is always the same |
163 |
|
// 1 degree longitude is ~111320 meters at equator but gets shorter as we get closer to the poles. |
164 |
|
// so 1 degree longitude is 64.5 km at denmarks southern point (gedser=54.55,11.95) |
165 |
|
// and is 59.4km at northern point (skagen = 57.75,10.65) |
166 |
|
// The "hack" with max 0.4 degrees latitude and 0.75 degrees longitude is only valid since we only service danish trains, |
167 |
// in denmark 0.4dg latitude ~ 44km, 0.75dg longitude ~ 47km |
// in denmark 0.4dg latitude ~ 44km, 0.75dg longitude ~ 47km |
168 |
|
|
169 |
// 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) |
171 |
// calculate the distance: |
// calculate the distance: |
172 |
// 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 |
173 |
|
|
174 |
public List<StationBean> getByLocationWorker(double latitude, double longitude, boolean geolimit) throws SQLException { |
public StationBean getByLocationWorker(final double latitude, final double longitude, final boolean geolimit) throws SQLException { |
175 |
|
|
176 |
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 " : ""; |
177 |
|
|
182 |
"ORDER BY calcdist ASC " + |
"ORDER BY calcdist ASC " + |
183 |
"LIMIT " + LOCATION_LIMIT; |
"LIMIT " + LOCATION_LIMIT; |
184 |
|
|
|
|
|
|
|
|
|
List<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); |
|
185 |
|
|
186 |
} finally { |
class LatlongSetter implements StatementParamSetter { |
187 |
if (res != null) |
@Override |
188 |
res.close(); |
public void setParams(PreparedStatement stmt) throws SQLException { |
189 |
if (stmt != null) |
stmt.setDouble(1, latitude); |
190 |
stmt.close(); |
stmt.setDouble(2, longitude); |
191 |
if (conn!= null) |
if (geolimit == true) { |
192 |
conn.close(); |
stmt.setDouble(3, latitude); |
193 |
|
stmt.setDouble(4, longitude); |
194 |
|
} |
195 |
|
} |
196 |
} |
} |
197 |
return result; |
|
198 |
|
return fetchStations(SQL, new LatlongSetter() ); |
199 |
} |
} |
200 |
|
|
201 |
public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException { |
public StationBean getByLocation(double latitude, double longitude) throws SQLException { |
202 |
List<StationBean> result = getByLocationWorker(latitude, longitude, true); |
StationBean result = getByLocationWorker(latitude, longitude, true); |
203 |
|
|
204 |
if (result.size() < LOCATION_LIMIT) { //failover |
if (result.entries.size() < LOCATION_LIMIT) { //failover |
205 |
logger.info("getByLocation failover: " +latitude + "," + longitude); |
logger.info("getByLocation failover: " +latitude + "," + longitude); |
206 |
|
|
207 |
result = getByLocationWorker(latitude, longitude, false); |
result = getByLocationWorker(latitude, longitude, false); |
212 |
|
|
213 |
|
|
214 |
|
|
215 |
public List<StationBean> getByList(String list) throws SQLException { |
public StationBean getByList(String list) throws SQLException { |
216 |
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 " + |
217 |
"FROM trainstations " + |
"FROM trainstations " + |
218 |
"WHERE id IN " + list + " AND enabled = true " + |
"WHERE id IN " + list + " AND enabled = true " + |
219 |
"ORDER BY name "; |
"ORDER BY name "; |
220 |
|
|
221 |
|
return fetchStations(SQL, new NullSetter() ); |
222 |
|
} |
223 |
|
|
224 |
|
|
225 |
|
|
226 |
|
public StationEntry getSimpleByName(final String name) throws SQLException { |
227 |
|
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " + |
228 |
|
"FROM trainstations " + |
229 |
|
"WHERE name = ? AND enabled = true " + |
230 |
|
"LIMIT 1 "; |
231 |
|
|
232 |
|
class NameSetter implements StatementParamSetter { |
233 |
|
@Override |
234 |
|
public void setParams(PreparedStatement stmt) throws SQLException { |
235 |
|
stmt.setString(1, name ); |
236 |
|
} |
237 |
|
} |
238 |
|
|
239 |
|
StationBean stations = fetchStations(SQL, new NameSetter() ); |
240 |
|
|
241 |
|
if (stations.entries.size() == 1) { |
242 |
|
return stations.entries.get(0); |
243 |
|
} else { |
244 |
|
return null; |
245 |
|
} |
246 |
|
} |
247 |
|
|
248 |
|
//used to create full dump in order to populate Google Appengine DB |
249 |
|
@Deprecated |
250 |
|
public StationBean dumpAll() throws SQLException { |
251 |
|
|
252 |
|
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0,aliases " + |
253 |
|
"FROM trainstations WHERE enabled = true ORDER BY id"; |
254 |
|
|
255 |
Connection conn = null; |
Connection conn = null; |
256 |
Statement stmt = null; |
Statement stmt = null; |
257 |
ResultSet res = null; |
ResultSet res = null; |
|
List<StationBean> result; |
|
258 |
|
|
259 |
|
|
260 |
try { |
try { |
261 |
conn = DBConnection.getConnection(); |
conn = DBConnection.getConnection(); |
262 |
|
|
263 |
stmt = conn.createStatement(); |
stmt = conn.createStatement(); |
264 |
res = stmt.executeQuery(SQL); |
res = stmt.executeQuery(SQL); |
265 |
result = convertResultset(res); |
|
266 |
|
// Does mostly the same as convertResultset() |
267 |
|
StationBean stations = new StationBean(); |
268 |
|
while (res.next()) { |
269 |
|
StationEntry entry = convertSingleRow(res); |
270 |
|
|
271 |
|
Array arr = res.getArray(10); |
272 |
|
if (arr != null) { |
273 |
|
String[] aliases = (String[]) arr.getArray(); |
274 |
|
entry.setAliases(aliases); |
275 |
|
} |
276 |
|
|
277 |
|
stations.entries.add( entry ); |
278 |
|
|
279 |
|
} |
280 |
|
return stations; |
281 |
|
|
282 |
|
|
283 |
} finally { |
} finally { |
284 |
if (res != null) |
if (res != null) |
285 |
res.close(); |
res.close(); |
286 |
if (stmt != null) |
if (stmt != null) |
287 |
stmt.close(); |
stmt.close(); |
288 |
if (conn!= null) |
if (conn != null) |
289 |
conn.close(); |
conn.close(); |
290 |
} |
} |
291 |
|
|
|
return result; |
|
|
|
|
292 |
} |
} |
293 |
|
|
294 |
|
@Deprecated |
295 |
public static String getStationName(int stationID) { |
public static String getStationName(int stationID) { |
296 |
String station = ""; |
String station = ""; |
297 |
|
|
314 |
|
|
315 |
return station; |
return station; |
316 |
} |
} |
317 |
|
|
318 |
public int getIdByName(String name) throws SQLException { |
|
319 |
|
|
320 |
|
@Deprecated |
321 |
|
public int getIdByName(final String name) throws SQLException { |
322 |
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 " + |
323 |
"FROM trainstations " + |
"FROM trainstations " + |
324 |
"WHERE name = ? AND enabled = true " + |
"WHERE name = ? AND enabled = true " + |
325 |
"LIMIT 1 "; |
"LIMIT 1 "; |
326 |
|
|
327 |
List<StationBean> result; |
class NameSetter implements StatementParamSetter { |
328 |
Connection conn = null; |
@Override |
329 |
PreparedStatement stmt = null; |
public void setParams(PreparedStatement stmt) throws SQLException { |
330 |
ResultSet res = null; |
stmt.setString(1, name ); |
331 |
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(); |
|
332 |
} |
} |
333 |
|
|
334 |
if (result.size() == 1) { |
StationBean stations = fetchStations(SQL, new NameSetter() ); |
335 |
return result.get(0).getId(); |
|
336 |
|
if (stations.entries.size() == 1) { |
337 |
|
return stations.entries.get(0).getId(); |
338 |
} else { |
} else { |
339 |
return -1; |
return -1; |
340 |
} |
} |