--- android/TrainInfoService/src/dk/thoerup/traininfoservice/StationDAO.java 2010/09/16 13:32:10 1060 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/db/StationDAO.java 2011/05/02 11:59:40 1409 @@ -1,5 +1,6 @@ -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; @@ -7,9 +8,15 @@ import java.sql.Statement; import java.util.logging.Logger; -import dk.thoerup.traininfoservice.StationBean.StationEntry; +import dk.thoerup.android.traininfo.common.StationBean; +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()); @@ -44,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"; @@ -57,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(); @@ -71,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) ) @@ -110,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)