--- android/TrainInfoService/src/dk/thoerup/traininfoservice/StationDAO.java 2010/06/11 18:46:46 840 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/StationDAO.java 2010/06/11 20:13:18 841 @@ -9,6 +9,8 @@ import java.util.List; public class StationDAO { + final static int LOCATION_LIMIT = 5; + private StationBean convertSingleRow(ResultSet res) throws SQLException { StationBean station = new StationBean(); @@ -109,13 +111,17 @@ // calculate the distance: // sqrt( power(abs(latitude-?)*111320, 2) + power(abs(longitude-?)*63000,2) )::int as calcdist - public List getByLocation(double latitude, double longitude) throws SQLException { - String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address," + - " earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " + - "FROM trainstations " + - "WHERE enabled = true AND abs(latitude-?)<1.5 AND abs(longitude-?)<2.5 " + - "ORDER BY calcdist ASC " + - "LIMIT 4 "; + public List getByLocationWorker(double latitude, double longitude, boolean geolimit) throws SQLException { + + String limitExpression = geolimit == 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, " + + "earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " + + "FROM trainstations " + + "WHERE enabled = true " + limitExpression + + "ORDER BY calcdist ASC " + + "LIMIT " + LOCATION_LIMIT; + List result; Connection conn = null; PreparedStatement stmt = null; @@ -125,8 +131,10 @@ stmt = conn.prepareStatement(SQL); stmt.setDouble(1, latitude); stmt.setDouble(2, longitude); - stmt.setDouble(3, latitude); - stmt.setDouble(4, longitude); + if (geolimit == true) { + stmt.setDouble(3, latitude); + stmt.setDouble(4, longitude); + } res = stmt.executeQuery(); result = convertResultset(res); @@ -140,6 +148,18 @@ } return result; } + + public List getByLocation(double latitude, double longitude) throws SQLException { + List result = getByLocationWorker(latitude, longitude, true); + + if (result.size() < LOCATION_LIMIT) { //failover + result = getByLocationWorker(latitude, longitude, false); + } + + return result; + } + + public List getByList(String list) throws SQLException { String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro, address,0.0 " +