9 |
import java.util.List; |
import java.util.List; |
10 |
|
|
11 |
public class StationDAO { |
public class StationDAO { |
12 |
|
final static int LOCATION_LIMIT = 5; |
13 |
|
|
14 |
private StationBean convertSingleRow(ResultSet res) throws SQLException { |
private StationBean convertSingleRow(ResultSet res) throws SQLException { |
15 |
StationBean station = new StationBean(); |
StationBean station = new StationBean(); |
16 |
|
|
111 |
// calculate the distance: |
// calculate the distance: |
112 |
// 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 |
113 |
|
|
114 |
public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException { |
public List<StationBean> getByLocationWorker(double latitude, double longitude, boolean geolimit) throws SQLException { |
115 |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address," + |
|
116 |
" earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " + |
String limitExpression = geolimit == true ? "AND abs(latitude-?)<1.5 AND abs(longitude-?)<2.5 " : ""; |
117 |
"FROM trainstations " + |
|
118 |
"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, " + |
119 |
"ORDER BY calcdist ASC " + |
"earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " + |
120 |
"LIMIT 4 "; |
"FROM trainstations " + |
121 |
|
"WHERE enabled = true " + limitExpression + |
122 |
|
"ORDER BY calcdist ASC " + |
123 |
|
"LIMIT " + LOCATION_LIMIT; |
124 |
|
|
125 |
List<StationBean> result; |
List<StationBean> result; |
126 |
Connection conn = null; |
Connection conn = null; |
127 |
PreparedStatement stmt = null; |
PreparedStatement stmt = null; |
131 |
stmt = conn.prepareStatement(SQL); |
stmt = conn.prepareStatement(SQL); |
132 |
stmt.setDouble(1, latitude); |
stmt.setDouble(1, latitude); |
133 |
stmt.setDouble(2, longitude); |
stmt.setDouble(2, longitude); |
134 |
stmt.setDouble(3, latitude); |
if (geolimit == true) { |
135 |
stmt.setDouble(4, longitude); |
stmt.setDouble(3, latitude); |
136 |
|
stmt.setDouble(4, longitude); |
137 |
|
} |
138 |
res = stmt.executeQuery(); |
res = stmt.executeQuery(); |
139 |
result = convertResultset(res); |
result = convertResultset(res); |
140 |
|
|
148 |
} |
} |
149 |
return result; |
return result; |
150 |
} |
} |
151 |
|
|
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 |
|
|
164 |
public List<StationBean> getByList(String list) throws SQLException { |
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 " + |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro, address,0.0 " + |