1 |
package dk.thoerup.traininfoservice; |
package dk.thoerup.traininfoservice; |
2 |
|
|
3 |
|
import java.sql.Array; |
4 |
import java.sql.Connection; |
import java.sql.Connection; |
5 |
import java.sql.PreparedStatement; |
import java.sql.PreparedStatement; |
6 |
import java.sql.ResultSet; |
import java.sql.ResultSet; |
7 |
import java.sql.SQLException; |
import java.sql.SQLException; |
8 |
import java.sql.Statement; |
import java.sql.Statement; |
9 |
import java.util.ArrayList; |
import java.util.logging.Logger; |
10 |
import java.util.List; |
|
11 |
|
import dk.thoerup.android.traininfo.common.StationBean; |
12 |
|
import dk.thoerup.android.traininfo.common.StationBean.StationEntry; |
13 |
|
|
14 |
public class StationDAO { |
public class StationDAO { |
15 |
private StationBean convertSingleRow(ResultSet res) throws SQLException { |
final static int LOCATION_LIMIT = 8; |
16 |
StationBean station = new StationBean(); |
static final Logger logger = Logger.getLogger(StationDAO.class.getName()); |
17 |
|
|
18 |
|
|
19 |
|
private StationEntry convertSingleRow(ResultSet res) throws SQLException { |
20 |
|
StationEntry station = new StationEntry(); |
21 |
|
|
22 |
station.setId( res.getInt(1) ); |
station.setId( res.getInt(1) ); |
23 |
station.setName( res.getString(2) ); |
station.setName( res.getString(2) ); |
28 |
station.setMetro( res.getString(7) ); |
station.setMetro( res.getString(7) ); |
29 |
station.setAddress( res.getString(8) ); |
station.setAddress( res.getString(8) ); |
30 |
station.setCalcdist( (int)res.getDouble(9) ); |
station.setCalcdist( (int)res.getDouble(9) ); |
31 |
|
|
32 |
|
station.setIsRegional( station.getRegional() != null ); |
33 |
|
station.setIsStrain( station.getStrain() != null ); |
34 |
|
station.setIsMetro( station.getMetro() != null ); |
35 |
|
|
36 |
return station; |
return station; |
37 |
} |
} |
38 |
|
|
39 |
private List<StationBean> convertResultset(ResultSet res) throws SQLException { |
private StationBean convertResultset(ResultSet res) throws SQLException { |
40 |
List<StationBean> stations = new ArrayList<StationBean>(); |
StationBean stations = new StationBean(); |
41 |
while (res.next()) { |
while (res.next()) { |
42 |
stations.add( convertSingleRow(res) ); |
stations.entries.add( convertSingleRow(res) ); |
43 |
} |
} |
44 |
return stations; |
return stations; |
45 |
|
|
46 |
} |
} |
47 |
|
|
48 |
|
|
49 |
public StationBean getById(int id) throws SQLException { |
public StationEntry getById(int id) throws SQLException { |
50 |
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 " + |
51 |
"FROM trainstations WHERE id=" + id + " AND enabled=true"; |
"FROM trainstations WHERE id=" + id + " AND enabled=true"; |
52 |
|
|
53 |
Connection conn = null; |
Connection conn = null; |
54 |
Statement stmt = null; |
Statement stmt = null; |
55 |
ResultSet res = null; |
ResultSet res = null; |
56 |
StationBean result; |
StationEntry result; |
57 |
|
|
58 |
try { |
try { |
59 |
conn = DBConnection.getConnection(); |
conn = DBConnection.getConnection(); |
73 |
|
|
74 |
return result; |
return result; |
75 |
} |
} |
76 |
|
|
77 |
|
public StationBean dumpAll() throws SQLException { |
78 |
|
|
79 |
|
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0,aliases " + |
80 |
|
"FROM trainstations WHERE enabled = true ORDER BY id"; |
81 |
|
|
82 |
|
Connection conn = null; |
83 |
|
Statement stmt = null; |
84 |
|
ResultSet res = null; |
85 |
|
|
86 |
|
|
87 |
|
try { |
88 |
|
conn = DBConnection.getConnection(); |
89 |
|
|
90 |
|
stmt = conn.createStatement(); |
91 |
|
res = stmt.executeQuery(SQL); |
92 |
|
|
93 |
|
// Does mostly the same as convertResultset() |
94 |
|
StationBean stations = new StationBean(); |
95 |
|
while (res.next()) { |
96 |
|
StationEntry entry = convertSingleRow(res); |
97 |
|
|
98 |
|
Array arr = res.getArray(10); |
99 |
|
if (arr != null) { |
100 |
|
String[] aliases = (String[]) arr.getArray(); |
101 |
|
entry.setAliases(aliases); |
102 |
|
} |
103 |
|
|
104 |
|
stations.entries.add( entry ); |
105 |
|
|
106 |
|
} |
107 |
|
return stations; |
108 |
|
|
109 |
|
|
110 |
|
} finally { |
111 |
|
if (res != null) |
112 |
|
res.close(); |
113 |
|
if (stmt != null) |
114 |
|
stmt.close(); |
115 |
|
if (conn != null) |
116 |
|
conn.close(); |
117 |
|
} |
118 |
|
|
119 |
|
} |
120 |
|
|
121 |
/* |
/* |
122 |
* 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) ) |
* 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) ) |
124 |
* 'select $2 ilike $1' language sql strict immutable; |
* 'select $2 ilike $1' language sql strict immutable; |
125 |
* create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~); |
* create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~); |
126 |
*/ |
*/ |
127 |
public List<StationBean> getByName(String name) throws SQLException { |
public StationBean getByName(String name) throws SQLException { |
128 |
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 " + |
129 |
"FROM trainstations " + |
"FROM trainstations " + |
130 |
"WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " + |
"WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " + |
131 |
"ORDER BY name "; |
"ORDER BY name "; |
132 |
|
|
133 |
|
|
134 |
List<StationBean> result; |
StationBean result; |
135 |
Connection conn = null; |
Connection conn = null; |
136 |
PreparedStatement stmt = null; |
PreparedStatement stmt = null; |
137 |
ResultSet res = null; |
ResultSet res = null; |
156 |
return result; |
return result; |
157 |
} |
} |
158 |
|
|
159 |
//the "hack" with max 1.5 degrees latitude and 2.5 degrees longitude is only valid since we only service danish trains |
//the "hack" with max 0.4 degrees latitude and 0.75 degrees longitude is only valid since we only service danish trains, |
160 |
// in denmark 1.5dg latitude ~ 165km, 2.5dg longitude ~ 155km |
// in denmark 0.4dg latitude ~ 44km, 0.75dg longitude ~ 47km |
161 |
|
|
162 |
// the ultra fast method (and only slightly inaccurate as long as we only cover a limited geographically area) |
// the ultra fast method (and only slightly inaccurate as long as we only cover a limited geographically area) |
163 |
// is using an aproximation of the length of 1 latitude degree and 1 longitude degree and just use pythagoras to |
// is using an aproximation of the length of 1 latitude degree and 1 longitude degree and just use pythagoras to |
164 |
// calculate the distance: |
// calculate the distance: |
165 |
// sqrt( power(abs(latitude-?)*111320, 2) + power(abs(longitude-?)*63000,2) )::int as calcdist |
// sqrt( power(abs(latitude-?)*111320, 2) + power(abs(longitude-?)*63000,2) )::int as calcdist |
166 |
|
|
167 |
public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException { |
public StationBean getByLocationWorker(double latitude, double longitude, boolean geolimit) throws SQLException { |
168 |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address," + |
|
169 |
" earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " + |
String limitExpression = (geolimit == true) ? "AND abs(latitude-?)<0.4 AND abs(longitude-?)<0.75 " : ""; |
170 |
"FROM trainstations " + |
|
171 |
"WHERE enabled = true AND abs(latitude-?)<1.5 AND abs(longitude-?)<2.5 " + |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, " + |
172 |
"ORDER BY calcdist ASC " + |
"earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " + |
173 |
"LIMIT 4 "; |
"FROM trainstations " + |
174 |
List<StationBean> result; |
"WHERE enabled = true " + limitExpression + |
175 |
|
"ORDER BY calcdist ASC " + |
176 |
|
"LIMIT " + LOCATION_LIMIT; |
177 |
|
|
178 |
|
|
179 |
|
|
180 |
|
StationBean result; |
181 |
Connection conn = null; |
Connection conn = null; |
182 |
PreparedStatement stmt = null; |
PreparedStatement stmt = null; |
183 |
ResultSet res = null; |
ResultSet res = null; |
186 |
stmt = conn.prepareStatement(SQL); |
stmt = conn.prepareStatement(SQL); |
187 |
stmt.setDouble(1, latitude); |
stmt.setDouble(1, latitude); |
188 |
stmt.setDouble(2, longitude); |
stmt.setDouble(2, longitude); |
189 |
stmt.setDouble(3, latitude); |
if (geolimit == true) { |
190 |
stmt.setDouble(4, longitude); |
stmt.setDouble(3, latitude); |
191 |
|
stmt.setDouble(4, longitude); |
192 |
|
} |
193 |
res = stmt.executeQuery(); |
res = stmt.executeQuery(); |
194 |
result = convertResultset(res); |
result = convertResultset(res); |
195 |
|
|
196 |
} finally { |
} finally { |
197 |
if (res != null) |
if (res != null) |
198 |
res.close(); |
res.close(); |
203 |
} |
} |
204 |
return result; |
return result; |
205 |
} |
} |
206 |
|
|
207 |
|
public StationBean getByLocation(double latitude, double longitude) throws SQLException { |
208 |
|
StationBean result = getByLocationWorker(latitude, longitude, true); |
209 |
|
|
210 |
|
if (result.entries.size() < LOCATION_LIMIT) { //failover |
211 |
|
logger.info("getByLocation failover: " +latitude + "," + longitude); |
212 |
|
|
213 |
|
result = getByLocationWorker(latitude, longitude, false); |
214 |
|
} |
215 |
|
|
216 |
|
return result; |
217 |
|
} |
218 |
|
|
219 |
|
|
220 |
|
|
221 |
public List<StationBean> getByList(String list) throws SQLException { |
public StationBean getByList(String list) throws SQLException { |
222 |
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 " + |
223 |
"FROM trainstations " + |
"FROM trainstations " + |
224 |
"WHERE id IN " + list + " AND enabled = true " + |
"WHERE id IN " + list + " AND enabled = true " + |
227 |
Connection conn = null; |
Connection conn = null; |
228 |
Statement stmt = null; |
Statement stmt = null; |
229 |
ResultSet res = null; |
ResultSet res = null; |
230 |
List<StationBean> result; |
StationBean result; |
231 |
|
|
232 |
try { |
try { |
233 |
conn = DBConnection.getConnection(); |
conn = DBConnection.getConnection(); |
269 |
return station; |
return station; |
270 |
} |
} |
271 |
|
|
272 |
public int getBySpecificName(String name) throws SQLException { |
public int getIdByName(String name) throws SQLException { |
273 |
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 " + |
274 |
"FROM trainstations " + |
"FROM trainstations " + |
275 |
"WHERE name = ? AND enabled = true " + |
"WHERE name = ? AND enabled = true " + |
276 |
"LIMIT 1 "; |
"LIMIT 1 "; |
277 |
|
|
278 |
List<StationBean> result; |
StationBean result; |
279 |
Connection conn = null; |
Connection conn = null; |
280 |
PreparedStatement stmt = null; |
PreparedStatement stmt = null; |
281 |
ResultSet res = null; |
ResultSet res = null; |
297 |
conn.close(); |
conn.close(); |
298 |
} |
} |
299 |
|
|
300 |
if (result.size() == 1) { |
if (result.entries.size() == 1) { |
301 |
return result.get(0).getId(); |
return result.entries.get(0).getId(); |
302 |
} else { |
} else { |
303 |
return -1; |
return -1; |
304 |
} |
} |