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