--- android/TrainInfoService/src/dk/thoerup/traininfoservice/StationDAO.java 2010/09/16 09:11:23 1059 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/StationDAO.java 2010/09/16 13:32:10 1060 @@ -5,17 +5,17 @@ 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.traininfoservice.StationBean.StationEntry; + public class StationDAO { final static int LOCATION_LIMIT = 8; static final Logger logger = Logger.getLogger(StationDAO.class.getName()); - private StationBean convertSingleRow(ResultSet res) throws SQLException { - StationBean station = new StationBean(); + private StationEntry convertSingleRow(ResultSet res) throws SQLException { + StationEntry station = new StationEntry(); station.setId( res.getInt(1) ); station.setName( res.getString(2) ); @@ -26,28 +26,32 @@ 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 { 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(); @@ -74,14 +78,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; @@ -114,7 +118,7 @@ // 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-?)<0.4 AND abs(longitude-?)<0.75 " : ""; @@ -127,7 +131,7 @@ - List result; + StationBean result; Connection conn = null; PreparedStatement stmt = null; ResultSet res = null; @@ -154,10 +158,10 @@ 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); @@ -168,7 +172,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 " + @@ -177,7 +181,7 @@ Connection conn = null; Statement stmt = null; ResultSet res = null; - List result; + StationBean result; try { conn = DBConnection.getConnection(); @@ -225,7 +229,7 @@ "WHERE name = ? AND enabled = true " + "LIMIT 1 "; - List result; + StationBean result; Connection conn = null; PreparedStatement stmt = null; ResultSet res = null; @@ -247,8 +251,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; }