--- android/TrainInfoService/src/dk/thoerup/traininfoservice/StationDAO.java 2010/06/11 20:13:18 841 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/db/StationDAO.java 2011/05/02 11:59:40 1409 @@ -1,18 +1,28 @@ -package dk.thoerup.traininfoservice; +package dk.thoerup.traininfoservice.db; +import java.sql.Array; import java.sql.Connection; import java.sql.PreparedStatement; 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.StationEntry; public class StationDAO { - final static int LOCATION_LIMIT = 5; - private StationBean convertSingleRow(ResultSet res) throws SQLException { - StationBean station = new StationBean(); + public static class NostationException extends Exception { + private static final long serialVersionUID = 1L; + } + + 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) ); @@ -23,36 +33,44 @@ station.setMetro( res.getString(7) ); 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,NostationException { String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0 " + "FROM trainstations WHERE id=" + id + " AND enabled=true"; Connection conn = null; Statement stmt = null; ResultSet res = null; - StationBean result; + StationEntry result; try { conn = DBConnection.getConnection(); stmt = conn.createStatement(); - res = stmt.executeQuery(SQL); - res.next(); - result = convertSingleRow(res); + res = stmt.executeQuery(SQL); + + if (res.next()) { + result = convertSingleRow(res); + } else { + throw new NostationException(); + } } finally { if (res != null) res.close(); @@ -64,6 +82,50 @@ return result; } + + public StationBean dumpAll() throws SQLException { + + String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0,aliases " + + "FROM trainstations WHERE enabled = true ORDER BY id"; + + Connection conn = null; + Statement stmt = null; + ResultSet res = null; + + + try { + conn = DBConnection.getConnection(); + + stmt = conn.createStatement(); + res = stmt.executeQuery(SQL); + + // Does mostly the same as convertResultset() + StationBean stations = new StationBean(); + while (res.next()) { + StationEntry entry = convertSingleRow(res); + + Array arr = res.getArray(10); + if (arr != null) { + String[] aliases = (String[]) arr.getArray(); + entry.setAliases(aliases); + } + + stations.entries.add( entry ); + + } + return stations; + + + } finally { + if (res != null) + res.close(); + if (stmt != null) + stmt.close(); + if (conn != null) + conn.close(); + } + + } /* * 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) ) @@ -71,14 +133,14 @@ * 'select $2 ilike $1' language sql strict immutable; * create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~); */ - public List getByName(String name) throws SQLException { + 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 ? OR ? ~~~ ANY(aliases)) AND enabled = true " + "ORDER BY name "; - List result; + StationBean result; Connection conn = null; PreparedStatement stmt = null; ResultSet res = null; @@ -103,17 +165,20 @@ return result; } - //the "hack" with max 1.5 degrees latitude and 2.5 degrees longitude is only valid since we only service danish trains - // in denmark 1.5dg latitude ~ 165km, 2.5dg longitude ~ 155km + //Latitude (horizonal), longitude(vertical) so + // 1 degree latitude is ~ 111320 meters, since the distance between the horizonal lines is always the same + // 1 degree longitude is ~111320 meters at equator but gets shorter as we get closer to the poles. + // 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 List getByLocationWorker(double latitude, double longitude, boolean geolimit) throws SQLException { + public StationBean getByLocationWorker(double latitude, double longitude, boolean geolimit) throws SQLException { - String limitExpression = geolimit == true ? "AND abs(latitude-?)<1.5 AND abs(longitude-?)<2.5 " : ""; + 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 " + @@ -121,8 +186,10 @@ "WHERE enabled = true " + limitExpression + "ORDER BY calcdist ASC " + "LIMIT " + LOCATION_LIMIT; + + - List result; + StationBean result; Connection conn = null; PreparedStatement stmt = null; ResultSet res = null; @@ -137,7 +204,7 @@ } res = stmt.executeQuery(); result = convertResultset(res); - + } finally { if (res != null) res.close(); @@ -149,10 +216,12 @@ return result; } - public List getByLocation(double latitude, double longitude) throws SQLException { - List result = getByLocationWorker(latitude, longitude, true); + public StationBean getByLocation(double latitude, double longitude) throws SQLException { + StationBean result = getByLocationWorker(latitude, longitude, true); - if (result.size() < LOCATION_LIMIT) { //failover + if (result.entries.size() < LOCATION_LIMIT) { //failover + logger.info("getByLocation failover: " +latitude + "," + longitude); + result = getByLocationWorker(latitude, longitude, false); } @@ -161,7 +230,7 @@ - public List getByList(String list) throws SQLException { + 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 " + @@ -170,7 +239,7 @@ Connection conn = null; Statement stmt = null; ResultSet res = null; - List result; + StationBean result; try { conn = DBConnection.getConnection(); @@ -212,13 +281,13 @@ return station; } - public int getBySpecificName(String name) throws SQLException { + 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 "; - List result; + StationBean result; Connection conn = null; PreparedStatement stmt = null; ResultSet res = null; @@ -240,8 +309,8 @@ conn.close(); } - if (result.size() == 1) { - return result.get(0).getId(); + if (result.entries.size() == 1) { + return result.entries.get(0).getId(); } else { return -1; }