7 |
import java.sql.Statement; |
import java.sql.Statement; |
8 |
import java.util.ArrayList; |
import java.util.ArrayList; |
9 |
import java.util.List; |
import java.util.List; |
10 |
|
import java.util.logging.Logger; |
11 |
|
|
12 |
public class StationDAO { |
public class StationDAO { |
13 |
final static int LOCATION_LIMIT = 5; |
final static int LOCATION_LIMIT = 5; |
14 |
|
static final Logger logger = Logger.getLogger(StationDAO.class.getName()); |
15 |
|
|
16 |
|
|
17 |
private StationBean convertSingleRow(ResultSet res) throws SQLException { |
private StationBean convertSingleRow(ResultSet res) throws SQLException { |
18 |
StationBean station = new StationBean(); |
StationBean station = new StationBean(); |
106 |
return result; |
return result; |
107 |
} |
} |
108 |
|
|
109 |
//the "hack" with max 1.0 degrees latitude and 2.0 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, |
110 |
// in denmark 1.0dg latitude ~ 110km, 2.0dg longitude ~ 120km |
// in denmark 0.4dg latitude ~ 44km, 0.75dg longitude ~ 47km |
111 |
|
|
112 |
// 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) |
113 |
// 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 |
116 |
|
|
117 |
public List<StationBean> getByLocationWorker(double latitude, double longitude, boolean geolimit) throws SQLException { |
public List<StationBean> getByLocationWorker(double latitude, double longitude, boolean geolimit) throws SQLException { |
118 |
|
|
119 |
String limitExpression = geolimit == true ? "AND abs(latitude-?)<1.0 AND abs(longitude-?)<2.0 " : ""; |
String limitExpression = (geolimit == true) ? "AND abs(latitude-?)<0.4 AND abs(longitude-?)<0.75 " : ""; |
120 |
|
|
121 |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, " + |
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 " + |
"earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " + |
124 |
"WHERE enabled = true " + limitExpression + |
"WHERE enabled = true " + limitExpression + |
125 |
"ORDER BY calcdist ASC " + |
"ORDER BY calcdist ASC " + |
126 |
"LIMIT " + LOCATION_LIMIT; |
"LIMIT " + LOCATION_LIMIT; |
127 |
|
|
128 |
|
|
129 |
|
|
130 |
List<StationBean> result; |
List<StationBean> result; |
131 |
Connection conn = null; |
Connection conn = null; |
142 |
} |
} |
143 |
res = stmt.executeQuery(); |
res = stmt.executeQuery(); |
144 |
result = convertResultset(res); |
result = convertResultset(res); |
145 |
|
|
146 |
} finally { |
} finally { |
147 |
if (res != null) |
if (res != null) |
148 |
res.close(); |
res.close(); |
158 |
List<StationBean> result = getByLocationWorker(latitude, longitude, true); |
List<StationBean> result = getByLocationWorker(latitude, longitude, true); |
159 |
|
|
160 |
if (result.size() < LOCATION_LIMIT) { //failover |
if (result.size() < LOCATION_LIMIT) { //failover |
161 |
|
logger.info("getByLocation failover: " +latitude + "," + longitude); |
162 |
|
|
163 |
result = getByLocationWorker(latitude, longitude, false); |
result = getByLocationWorker(latitude, longitude, false); |
164 |
} |
} |
165 |
|
|