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; |
9 |
import java.util.ArrayList; |
import java.util.logging.Logger; |
10 |
import java.util.List; |
|
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 |
private StationBean convertSingleRow(ResultSet res) throws SQLException { |
|
16 |
StationBean station = new StationBean(); |
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; |
29 |
|
static final Logger logger = Logger.getLogger(StationDAO.class.getName()); |
30 |
|
|
31 |
|
|
32 |
|
private StationEntry convertSingleRow(ResultSet res) throws SQLException { |
33 |
|
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 id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0, " + |
128 |
|
"levenshtein(lower(name),lower(?) ) as leven " + |
129 |
|
"FROM trainstations " + |
130 |
|
"WHERE (levenshtein(lower(name),lower(?) ) <= 3) " + |
131 |
|
"AND enabled = true " + |
132 |
|
"ORDER BY leven " + |
133 |
|
"LIMIT 1"; |
134 |
|
|
135 |
|
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 |
//the "hack" with max 1.5 degrees latitude and 2.5 degrees longitude is only valid since we only service danish trains |
//Latitude (horizonal), longitude(vertical) so |
161 |
// in denmark 1.5dg latitude ~ 165km, 2.5dg longitude ~ 155km |
// 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 |
|
// 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 |
|
// in denmark 0.4dg latitude ~ 44km, 0.75dg longitude ~ 47km |
167 |
|
|
168 |
// 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) |
169 |
// is using an aproximation of the length of 1 latitude degree and 1 longitude degree and just use pythagoras to |
// is using an aproximation of the length of 1 latitude degree and 1 longitude degree and just use pythagoras to |
170 |
// calculate the distance: |
// calculate the distance: |
171 |
// 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 |
172 |
|
|
173 |
public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException { |
public StationBean getByLocationWorker(final double latitude, final double longitude, final boolean geolimit) throws SQLException { |
174 |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address," + |
|
175 |
" earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " + |
String limitExpression = (geolimit == true) ? "AND abs(latitude-?)<0.4 AND abs(longitude-?)<0.75 " : ""; |
176 |
"FROM trainstations " + |
|
177 |
"WHERE enabled = true AND abs(latitude-?)<1.5 AND abs(longitude-?)<2.5 " + |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, " + |
178 |
"ORDER BY calcdist ASC " + |
"earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " + |
179 |
"LIMIT 4 "; |
"FROM trainstations " + |
180 |
List<StationBean> result; |
"WHERE enabled = true " + limitExpression + |
181 |
Connection conn = null; |
"ORDER BY calcdist ASC " + |
182 |
PreparedStatement stmt = null; |
"LIMIT " + LOCATION_LIMIT; |
183 |
ResultSet res = null; |
|
184 |
try { |
|
185 |
conn = DBConnection.getConnection(); |
class LatlongSetter implements StatementParamSetter { |
186 |
stmt = conn.prepareStatement(SQL); |
@Override |
187 |
stmt.setDouble(1, latitude); |
public void setParams(PreparedStatement stmt) throws SQLException { |
188 |
stmt.setDouble(2, longitude); |
stmt.setDouble(1, latitude); |
189 |
stmt.setDouble(3, latitude); |
stmt.setDouble(2, longitude); |
190 |
stmt.setDouble(4, longitude); |
if (geolimit == true) { |
191 |
res = stmt.executeQuery(); |
stmt.setDouble(3, latitude); |
192 |
result = convertResultset(res); |
stmt.setDouble(4, longitude); |
193 |
|
} |
194 |
} finally { |
} |
195 |
if (res != null) |
} |
196 |
res.close(); |
|
197 |
if (stmt != null) |
return fetchStations(SQL, new LatlongSetter() ); |
198 |
stmt.close(); |
} |
199 |
if (conn!= null) |
|
200 |
conn.close(); |
public StationBean getByLocation(double latitude, double longitude) throws SQLException { |
201 |
|
StationBean result = getByLocationWorker(latitude, longitude, true); |
202 |
|
|
203 |
|
if (result.entries.size() < LOCATION_LIMIT) { //failover |
204 |
|
logger.info("getByLocation failover: " +latitude + "," + longitude); |
205 |
|
|
206 |
|
result = getByLocationWorker(latitude, longitude, false); |
207 |
} |
} |
208 |
|
|
209 |
return result; |
return result; |
210 |
} |
} |
211 |
|
|
212 |
|
|
213 |
|
|
214 |
public List<StationBean> getByList(String list) throws SQLException { |
public StationBean getByList(String list) throws SQLException { |
215 |
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 " + |
216 |
"FROM trainstations " + |
"FROM trainstations " + |
217 |
"WHERE id IN " + list + " AND enabled = true " + |
"WHERE id IN " + list + " AND enabled = true " + |
218 |
"ORDER BY name "; |
"ORDER BY name "; |
219 |
|
|
220 |
|
return fetchStations(SQL, new NullSetter() ); |
221 |
|
} |
222 |
|
|
223 |
|
|
224 |
|
|
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 |
|
|
231 |
|
class NameSetter implements StatementParamSetter { |
232 |
|
@Override |
233 |
|
public void setParams(PreparedStatement stmt) throws SQLException { |
234 |
|
stmt.setString(1, name ); |
235 |
|
} |
236 |
|
} |
237 |
|
|
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 |
|
|
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; |
Connection conn = null; |
255 |
Statement stmt = null; |
Statement stmt = null; |
256 |
ResultSet res = null; |
ResultSet res = null; |
|
List<StationBean> result; |
|
257 |
|
|
258 |
|
|
259 |
try { |
try { |
260 |
conn = DBConnection.getConnection(); |
conn = DBConnection.getConnection(); |
261 |
|
|
262 |
stmt = conn.createStatement(); |
stmt = conn.createStatement(); |
263 |
res = stmt.executeQuery(SQL); |
res = stmt.executeQuery(SQL); |
264 |
result = convertResultset(res); |
|
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 { |
} finally { |
283 |
if (res != null) |
if (res != null) |
284 |
res.close(); |
res.close(); |
285 |
if (stmt != null) |
if (stmt != null) |
286 |
stmt.close(); |
stmt.close(); |
287 |
if (conn!= null) |
if (conn != null) |
288 |
conn.close(); |
conn.close(); |
289 |
} |
} |
290 |
|
|
|
return result; |
|
|
|
|
291 |
} |
} |
292 |
|
|
293 |
|
@Deprecated |
294 |
public static String getStationName(int stationID) { |
public static String getStationName(int stationID) { |
295 |
String station = ""; |
String station = ""; |
296 |
|
|
313 |
|
|
314 |
return station; |
return station; |
315 |
} |
} |
316 |
|
|
317 |
public int getBySpecificName(String name) throws SQLException { |
|
318 |
|
|
319 |
|
@Deprecated |
320 |
|
public int getIdByName(final String name) throws SQLException { |
321 |
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 " + |
322 |
"FROM trainstations " + |
"FROM trainstations " + |
323 |
"WHERE name = ? AND enabled = true " + |
"WHERE name = ? AND enabled = true " + |
324 |
"LIMIT 1 "; |
"LIMIT 1 "; |
325 |
|
|
326 |
System.out.println(" getBySpecificName() "); |
class NameSetter implements StatementParamSetter { |
327 |
|
@Override |
328 |
List<StationBean> result; |
public void setParams(PreparedStatement stmt) throws SQLException { |
329 |
Connection conn = null; |
stmt.setString(1, name ); |
330 |
PreparedStatement stmt = null; |
} |
|
ResultSet res = null; |
|
|
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(); |
|
331 |
} |
} |
332 |
|
|
333 |
if (result.size() == 1) { |
StationBean stations = fetchStations(SQL, new NameSetter() ); |
334 |
return result.get(0).getId(); |
|
335 |
|
if (stations.entries.size() == 1) { |
336 |
|
return stations.entries.get(0).getId(); |
337 |
} else { |
} else { |
338 |
return -1; |
return -1; |
339 |
} |
} |