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