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