9 |
import java.util.List; |
import java.util.List; |
10 |
|
|
11 |
public class StationDAO { |
public class StationDAO { |
12 |
|
final static int LOCATION_LIMIT = 5; |
13 |
|
|
14 |
private StationBean convertSingleRow(ResultSet res) throws SQLException { |
private StationBean convertSingleRow(ResultSet res) throws SQLException { |
15 |
StationBean station = new StationBean(); |
StationBean station = new StationBean(); |
16 |
|
|
17 |
station.setId( res.getInt(1) ); |
station.setId( res.getInt(1) ); |
18 |
station.setName( res.getString(2) ); |
station.setName( res.getString(2) ); |
19 |
station.setLatitude( res.getDouble(3) ); |
station.setLatitude( res.getDouble(3) ); |
23 |
station.setMetro( res.getString(7) ); |
station.setMetro( res.getString(7) ); |
24 |
station.setAddress( res.getString(8) ); |
station.setAddress( res.getString(8) ); |
25 |
station.setCalcdist( (int)res.getDouble(9) ); |
station.setCalcdist( (int)res.getDouble(9) ); |
26 |
|
|
27 |
return station; |
return station; |
28 |
} |
} |
29 |
|
|
30 |
private List<StationBean> convertResultset(ResultSet res) throws SQLException { |
private List<StationBean> convertResultset(ResultSet res) throws SQLException { |
31 |
List<StationBean> stations = new ArrayList<StationBean>(); |
List<StationBean> stations = new ArrayList<StationBean>(); |
32 |
while (res.next()) { |
while (res.next()) { |
33 |
stations.add( convertSingleRow(res) ); |
stations.add( convertSingleRow(res) ); |
34 |
} |
} |
35 |
return stations; |
return stations; |
36 |
|
|
37 |
} |
} |
38 |
|
|
39 |
|
|
40 |
public StationBean getById(int id) throws SQLException { |
public StationBean getById(int id) throws SQLException { |
41 |
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 " + |
42 |
"FROM trainstations WHERE id=" + id + " AND enabled=true"; |
"FROM trainstations WHERE id=" + id + " AND enabled=true"; |
43 |
|
|
44 |
Connection conn = null; |
Connection conn = null; |
45 |
Statement stmt = null; |
Statement stmt = null; |
46 |
ResultSet res = null; |
ResultSet res = null; |
47 |
StationBean result; |
StationBean result; |
48 |
|
|
49 |
try { |
try { |
50 |
conn = DBConnection.getConnection(); |
conn = DBConnection.getConnection(); |
51 |
|
|
52 |
stmt = conn.createStatement(); |
stmt = conn.createStatement(); |
53 |
res = stmt.executeQuery(SQL); |
res = stmt.executeQuery(SQL); |
54 |
res.next(); |
res.next(); |
61 |
if (conn != null) |
if (conn != null) |
62 |
conn.close(); |
conn.close(); |
63 |
} |
} |
64 |
|
|
65 |
return result; |
return result; |
66 |
} |
} |
67 |
|
|
68 |
/* |
/* |
69 |
* 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) ) |
70 |
* create function rlike(text,text) returns bool as |
* create function rlike(text,text) returns bool as |
71 |
* 'select $2 ilike $1' language sql strict immutable; |
* 'select $2 ilike $1' language sql strict immutable; |
72 |
* create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~); |
* create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~); |
73 |
*/ |
*/ |
74 |
public List<StationBean> getByName(String name) throws SQLException { |
public List<StationBean> getByName(String name) throws SQLException { |
75 |
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 " + |
77 |
"WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " + |
"WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " + |
78 |
"ORDER BY name "; |
"ORDER BY name "; |
79 |
|
|
80 |
|
|
81 |
List<StationBean> result; |
List<StationBean> result; |
82 |
Connection conn = null; |
Connection conn = null; |
83 |
PreparedStatement stmt = null; |
PreparedStatement stmt = null; |
85 |
try { |
try { |
86 |
conn = DBConnection.getConnection(); |
conn = DBConnection.getConnection(); |
87 |
stmt = conn.prepareStatement(SQL); |
stmt = conn.prepareStatement(SQL); |
88 |
|
|
89 |
stmt.setString(1, name + "%"); |
stmt.setString(1, name + "%"); |
90 |
stmt.setString(2, name + "%"); |
stmt.setString(2, name + "%"); |
91 |
|
|
92 |
res = stmt.executeQuery(); |
res = stmt.executeQuery(); |
93 |
result = convertResultset(res); |
result = convertResultset(res); |
94 |
|
|
95 |
} finally { |
} finally { |
96 |
if (res != null) |
if (res != null) |
97 |
res.close(); |
res.close(); |
102 |
} |
} |
103 |
return result; |
return result; |
104 |
} |
} |
105 |
|
|
106 |
//the "hack" with max 2.5 degrees latitude and longitude is only valid since we only service danish trains |
//the "hack" with max 0.7 degrees latitude and 1.2 degrees longitude is only valid since we only service danish trains |
107 |
public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException { |
// in denmark 0.7dg latitude ~ 77km, 1.2dg longitude ~ 76km |
108 |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address," + |
|
109 |
" earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " + |
// the ultra fast method (and only slightly inaccurate as long as we only cover a limited geographically area) |
110 |
"FROM trainstations " + |
// is using an aproximation of the length of 1 latitude degree and 1 longitude degree and just use pythagoras to |
111 |
"WHERE enabled = true AND abs(latitude-?)<2.5 AND abs (longitude-?)<2.5 " + |
// calculate the distance: |
112 |
"ORDER BY calcdist ASC " + |
// sqrt( power(abs(latitude-?)*111320, 2) + power(abs(longitude-?)*63000,2) )::int as calcdist |
113 |
"LIMIT 4 "; |
|
114 |
|
public List<StationBean> getByLocationWorker(double latitude, double longitude, boolean geolimit) throws SQLException { |
115 |
|
|
116 |
|
String limitExpression = geolimit == true ? "AND abs(latitude-?)<0.7 AND abs(longitude-?)<1.2 " : ""; |
117 |
|
|
118 |
|
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, " + |
119 |
|
"earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " + |
120 |
|
"FROM trainstations " + |
121 |
|
"WHERE enabled = true " + limitExpression + |
122 |
|
"ORDER BY calcdist ASC " + |
123 |
|
"LIMIT " + LOCATION_LIMIT; |
124 |
|
|
125 |
|
|
126 |
|
|
127 |
List<StationBean> result; |
List<StationBean> result; |
128 |
Connection conn = null; |
Connection conn = null; |
129 |
PreparedStatement stmt = null; |
PreparedStatement stmt = null; |
133 |
stmt = conn.prepareStatement(SQL); |
stmt = conn.prepareStatement(SQL); |
134 |
stmt.setDouble(1, latitude); |
stmt.setDouble(1, latitude); |
135 |
stmt.setDouble(2, longitude); |
stmt.setDouble(2, longitude); |
136 |
stmt.setDouble(3, latitude); |
if (geolimit == true) { |
137 |
stmt.setDouble(4, longitude); |
stmt.setDouble(3, latitude); |
138 |
|
stmt.setDouble(4, longitude); |
139 |
|
} |
140 |
res = stmt.executeQuery(); |
res = stmt.executeQuery(); |
141 |
result = convertResultset(res); |
result = convertResultset(res); |
142 |
|
|
143 |
} finally { |
} finally { |
144 |
if (res != null) |
if (res != null) |
145 |
res.close(); |
res.close(); |
150 |
} |
} |
151 |
return result; |
return result; |
152 |
} |
} |
153 |
|
|
154 |
public List<StationBean> getByList(String list) throws SQLException { |
public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException { |
155 |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro, address,0.0 " + |
List<StationBean> result = getByLocationWorker(latitude, longitude, true); |
156 |
"FROM trainstations " + |
|
157 |
"WHERE id IN " + list + " AND enabled = true " + |
if (result.size() < LOCATION_LIMIT) { //failover |
158 |
"ORDER BY name "; |
result = getByLocationWorker(latitude, longitude, false); |
159 |
|
} |
160 |
Connection conn = null; |
|
161 |
Statement stmt = null; |
return result; |
162 |
ResultSet res = null; |
} |
163 |
List<StationBean> result; |
|
164 |
|
|
165 |
|
|
166 |
|
public List<StationBean> getByList(String list) throws SQLException { |
167 |
|
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro, address,0.0 " + |
168 |
|
"FROM trainstations " + |
169 |
|
"WHERE id IN " + list + " AND enabled = true " + |
170 |
|
"ORDER BY name "; |
171 |
|
|
172 |
|
Connection conn = null; |
173 |
|
Statement stmt = null; |
174 |
|
ResultSet res = null; |
175 |
|
List<StationBean> result; |
176 |
|
|
177 |
|
try { |
178 |
|
conn = DBConnection.getConnection(); |
179 |
|
stmt = conn.createStatement(); |
180 |
|
res = stmt.executeQuery(SQL); |
181 |
|
result = convertResultset(res); |
182 |
|
} finally { |
183 |
|
if (res != null) |
184 |
|
res.close(); |
185 |
|
if (stmt != null) |
186 |
|
stmt.close(); |
187 |
|
if (conn!= null) |
188 |
|
conn.close(); |
189 |
|
} |
190 |
|
|
191 |
|
return result; |
192 |
|
|
193 |
|
} |
194 |
|
public static String getStationName(int stationID) { |
195 |
|
String station = ""; |
196 |
|
|
197 |
|
Connection conn = null; |
198 |
|
try { |
199 |
|
conn = DBConnection.getConnection(); |
200 |
|
Statement stmt = conn.createStatement(); |
201 |
|
ResultSet rs = stmt.executeQuery("SELECT name FROM trainstations WHERE id=" + stationID); |
202 |
|
if (rs.next()) { |
203 |
|
station = rs.getString(1); |
204 |
|
} |
205 |
|
|
206 |
|
} catch (Exception e) { |
207 |
|
} finally { |
208 |
try { |
try { |
209 |
conn = DBConnection.getConnection(); |
if (conn != null && !conn.isClosed()) |
|
stmt = conn.createStatement(); |
|
|
res = stmt.executeQuery(SQL); |
|
|
result = convertResultset(res); |
|
|
} finally { |
|
|
if (res != null) |
|
|
res.close(); |
|
|
if (stmt != null) |
|
|
stmt.close(); |
|
|
if (conn!= null) |
|
210 |
conn.close(); |
conn.close(); |
211 |
} |
} catch (Exception e) {} |
212 |
|
} |
|
return result; |
|
|
|
|
|
} |
|
|
public static String getStationName(int stationID) { |
|
|
String station = ""; |
|
|
|
|
|
Connection conn = null; |
|
|
try { |
|
|
conn = DBConnection.getConnection(); |
|
|
Statement stmt = conn.createStatement(); |
|
|
ResultSet rs = stmt.executeQuery("SELECT name FROM trainstations WHERE id=" + stationID); |
|
|
if (rs.next()) { |
|
|
station = rs.getString(1); |
|
|
} |
|
|
|
|
|
} catch (Exception e) { |
|
|
} finally { |
|
|
try { |
|
|
if (conn != null && !conn.isClosed()) |
|
|
conn.close(); |
|
|
} catch (Exception e) {} |
|
|
} |
|
213 |
|
|
214 |
return station; |
return station; |
215 |
} |
} |
216 |
|
|
217 |
|
public int getIdByName(String name) throws SQLException { |
218 |
|
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " + |
219 |
|
"FROM trainstations " + |
220 |
|
"WHERE name = ? AND enabled = true " + |
221 |
|
"LIMIT 1 "; |
222 |
|
|
223 |
|
List<StationBean> result; |
224 |
|
Connection conn = null; |
225 |
|
PreparedStatement stmt = null; |
226 |
|
ResultSet res = null; |
227 |
|
try { |
228 |
|
conn = DBConnection.getConnection(); |
229 |
|
stmt = conn.prepareStatement(SQL); |
230 |
|
|
231 |
|
stmt.setString(1, name ); |
232 |
|
|
233 |
|
res = stmt.executeQuery(); |
234 |
|
result = convertResultset(res); |
235 |
|
|
236 |
|
} finally { |
237 |
|
if (res != null) |
238 |
|
res.close(); |
239 |
|
if (stmt != null) |
240 |
|
stmt.close(); |
241 |
|
if (conn!= null) |
242 |
|
conn.close(); |
243 |
|
} |
244 |
|
|
245 |
|
if (result.size() == 1) { |
246 |
|
return result.get(0).getId(); |
247 |
|
} else { |
248 |
|
return -1; |
249 |
|
} |
250 |
|
} |
251 |
} |
} |