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