--- android/TrainInfoService/src/dk/thoerup/traininfoservice/StationDAO.java 2010/09/21 05:55:31 1082 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/db/StationDAO.java 2011/05/02 16:21:37 1417 @@ -1,4 +1,4 @@ -package dk.thoerup.traininfoservice; +package dk.thoerup.traininfoservice.db; import java.sql.Array; import java.sql.Connection; @@ -9,9 +9,14 @@ import java.util.logging.Logger; import dk.thoerup.android.traininfo.common.StationBean; -import dk.thoerup.android.traininfo.common.StationBean.StationEntry; +import dk.thoerup.android.traininfo.common.StationEntry; public class StationDAO { + + 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()); @@ -46,7 +51,7 @@ } - public StationEntry 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"; @@ -59,9 +64,13 @@ 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(); @@ -156,7 +165,10 @@ return result; } - //the "hack" with max 0.4 degrees latitude and 0.75 degrees longitude is only valid since we only service danish trains, + //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) @@ -246,6 +258,7 @@ return result; } + @Deprecated public static String getStationName(int stationID) { String station = ""; @@ -268,7 +281,43 @@ return station; } + + public StationEntry getSimpleByName(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); + } else { + return null; + } + } + @Deprecated 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 " +