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