--- android/TrainInfoService/src/dk/thoerup/traininfoservice/StationDAO.java 2010/02/08 19:12:15 588 +++ android/TrainInfoServiceGoogle/src/dk/thoerup/traininfoservice/StationDAO.java 2010/09/20 20:11:55 1080 @@ -5,13 +5,19 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; -import java.util.ArrayList; -import java.util.List; +import java.util.logging.Logger; + +import dk.thoerup.android.traininfo.common.StationBean; +import dk.thoerup.android.traininfo.common.StationBean.StationEntry; public class StationDAO { - private StationBean convertSingleRow(ResultSet res) throws SQLException { - StationBean station = new StationBean(); - + final static int LOCATION_LIMIT = 8; + static final Logger logger = Logger.getLogger(StationDAO.class.getName()); + + + private StationEntry convertSingleRow(ResultSet res) throws SQLException { + StationEntry station = new StationEntry(); + station.setId( res.getInt(1) ); station.setName( res.getString(2) ); station.setLatitude( res.getDouble(3) ); @@ -22,114 +28,234 @@ station.setAddress( res.getString(8) ); station.setCalcdist( (int)res.getDouble(9) ); + station.setIsRegional( station.getRegional() != null ); + station.setIsStrain( station.getStrain() != null ); + station.setIsMetro( station.getMetro() != null ); + return station; } - - private List convertResultset(ResultSet res) throws SQLException { - List stations = new ArrayList(); + + private StationBean convertResultset(ResultSet res) throws SQLException { + StationBean stations = new StationBean(); while (res.next()) { - stations.add( convertSingleRow(res) ); + stations.entries.add( convertSingleRow(res) ); } return stations; - + } - - - public StationBean getById(int id) throws SQLException { + + + public StationEntry getById(int id) throws SQLException { String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0 " + - "FROM trainstations WHERE id=" + id + " AND enabled=true"; - + "FROM trainstations WHERE id=" + id + " AND enabled=true"; + Connection conn = null; - StationBean result; - + Statement stmt = null; + ResultSet res = null; + StationEntry result; + try { conn = DBConnection.getConnection(); - - Statement stmt = conn.createStatement(); - ResultSet res = stmt.executeQuery(SQL); + + stmt = conn.createStatement(); + res = stmt.executeQuery(SQL); res.next(); result = convertSingleRow(res); } finally { + if (res != null) + res.close(); + if (stmt != null) + stmt.close(); if (conn != null) conn.close(); } - + return result; } - - - public List getByName(String name) throws SQLException { + + /* + * 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) ) + * create function rlike(text,text) returns bool as + * 'select $2 ilike $1' language sql strict immutable; + * create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~); + */ + public StationBean getByName(String name) throws SQLException { String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " + "FROM trainstations " + - "WHERE name ILIKE ? AND enabled = true " + + "WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " + "ORDER BY name "; - - List result; + + StationBean result; Connection conn = null; + PreparedStatement stmt = null; + ResultSet res = null; try { conn = DBConnection.getConnection(); - PreparedStatement stmt = conn.prepareStatement(SQL); - + stmt = conn.prepareStatement(SQL); + stmt.setString(1, name + "%"); - - ResultSet rs = stmt.executeQuery(); - result = convertResultset(rs); - + stmt.setString(2, name + "%"); + + res = stmt.executeQuery(); + result = convertResultset(res); + } finally { - if (conn != null) + if (res != null) + res.close(); + if (stmt != null) + stmt.close(); + if (conn!= null) conn.close(); } return result; } - - public List getByLocation(double latitude, double longitude) throws SQLException { - String SQL = "SELECT * FROM ( "+ - " SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address," + - " earth_distance( ll_to_earth(latitude,longitude), ll_to_earth(?,?))::int AS calcdist " + - " FROM trainstations " + - " WHERE enabled = true " + - " ) AS trainstations2 " + - "ORDER BY calcdist ASC " + - "LIMIT 4 "; - List result; + + //the "hack" with max 0.4 degrees latitude and 0.75 degrees longitude is only valid since we only service danish trains, + // in denmark 0.4dg latitude ~ 44km, 0.75dg longitude ~ 47km + + // the ultra fast method (and only slightly inaccurate as long as we only cover a limited geographically area) + // is using an aproximation of the length of 1 latitude degree and 1 longitude degree and just use pythagoras to + // calculate the distance: + // sqrt( power(abs(latitude-?)*111320, 2) + power(abs(longitude-?)*63000,2) )::int as calcdist + + public StationBean getByLocationWorker(double latitude, double longitude, boolean geolimit) throws SQLException { + + String limitExpression = (geolimit == true) ? "AND abs(latitude-?)<0.4 AND abs(longitude-?)<0.75 " : ""; + + 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; + + + + StationBean result; Connection conn = null; + PreparedStatement stmt = null; + ResultSet res = null; try { conn = DBConnection.getConnection(); - PreparedStatement stmt = conn.prepareStatement(SQL); + stmt = conn.prepareStatement(SQL); stmt.setDouble(1, latitude); stmt.setDouble(2, longitude); - ResultSet rs = stmt.executeQuery(); - result = convertResultset(rs); - + if (geolimit == true) { + stmt.setDouble(3, latitude); + stmt.setDouble(4, longitude); + } + res = stmt.executeQuery(); + result = convertResultset(res); + } finally { - if (conn != null) + if (res != null) + res.close(); + if (stmt != null) + stmt.close(); + if (conn!= null) conn.close(); } 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 " + - "FROM trainstations " + - "WHERE id IN " + list + " AND enabled = true " + - "ORDER BY name "; - - Connection conn = null; - List result; + + public StationBean getByLocation(double latitude, double longitude) throws SQLException { + StationBean result = getByLocationWorker(latitude, longitude, true); + + if (result.entries.size() < LOCATION_LIMIT) { //failover + logger.info("getByLocation failover: " +latitude + "," + longitude); + result = getByLocationWorker(latitude, longitude, false); + } + + return result; + } + + + + public StationBean getByList(String list) throws SQLException { + String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro, address,0.0 " + + "FROM trainstations " + + "WHERE id IN " + list + " AND enabled = true " + + "ORDER BY name "; + + Connection conn = null; + Statement stmt = null; + ResultSet res = null; + StationBean result; + + try { + conn = DBConnection.getConnection(); + stmt = conn.createStatement(); + res = stmt.executeQuery(SQL); + result = convertResultset(res); + } finally { + if (res != null) + res.close(); + if (stmt != null) + stmt.close(); + if (conn!= null) + conn.close(); + } + + return result; + + } + public static String getStationName(int stationID) { + String station = ""; + + Connection conn = null; + try { + conn = DBConnection.getConnection(); + Statement stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT name FROM trainstations WHERE id=" + stationID); + if (rs.next()) { + station = rs.getString(1); + } + + } catch (Exception e) { + } finally { try { - conn = DBConnection.getConnection(); - Statement stmt = conn.createStatement(); - ResultSet res = stmt.executeQuery(SQL); - result = convertResultset(res); - } finally { - if (conn!= null) + if (conn != null && !conn.isClosed()) conn.close(); - } - - return result; - - } - + } catch (Exception e) {} + } + + return station; + } + + public int getIdByName(String name) throws SQLException { + String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " + + "FROM trainstations " + + "WHERE name = ? AND enabled = true " + + "LIMIT 1 "; + + StationBean result; + Connection conn = null; + PreparedStatement stmt = null; + ResultSet res = null; + try { + conn = DBConnection.getConnection(); + stmt = conn.prepareStatement(SQL); + + stmt.setString(1, name ); + + res = stmt.executeQuery(); + result = convertResultset(res); + + } finally { + if (res != null) + res.close(); + if (stmt != null) + stmt.close(); + if (conn!= null) + conn.close(); + } + + if (result.entries.size() == 1) { + return result.entries.get(0).getId(); + } else { + return -1; + } + } }