--- android/TrainInfoService/src/dk/thoerup/traininfoservice/db/StationDAO.java 2011/06/08 15:38:11 1504 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/db/StationDAO.java 2012/08/20 21:15:37 1832 @@ -6,6 +6,8 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +import java.util.Collections; +import java.util.Comparator; import java.util.logging.Logger; import dk.thoerup.android.traininfo.common.StationBean; @@ -45,6 +47,18 @@ station.setIsRegional( station.getRegional() != null ); station.setIsStrain( station.getStrain() != null ); station.setIsMetro( station.getMetro() != null ); + + Array aliases = res.getArray( 10); + if (aliases != null) { + station.setAliases( (String[]) aliases.getArray() ); + } else { + String[] emptyArray = {}; + station.setAliases( emptyArray ); + } + + station.setTritStation( res.getInt(11) ); + if (res.wasNull()) + station.setTritStation( -1 ); return station; } @@ -85,7 +99,7 @@ } public StationEntry getById(int id) throws SQLException,NostationException { - String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0 " + + String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0,aliases,tritstation " + "FROM trainstations WHERE id=" + id + " AND enabled=true"; StationBean stations = fetchStations(SQL, new NullSetter() ); @@ -97,49 +111,7 @@ } } - 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) ) @@ -147,8 +119,8 @@ * 'select $2 ilike $1' language sql strict immutable; * create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~); */ - public StationBean getByName(final String name) throws SQLException { - String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " + + public StationBean getByNameNormal(final String name) throws SQLException { + String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0,aliases,tritstation " + "FROM trainstations " + "WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " + "ORDER BY name "; @@ -163,11 +135,61 @@ return fetchStations(SQL, new NameSetter() ); } + + + public StationBean getByNameFuzzy(final String name) throws SQLException { + String SQL = "SELECT * FROM (" + + " SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0, aliases, tritstation, " + + " levenshtein(lower(name),lower(?) ) as leven " + + " FROM trainstations " + + " WHERE enabled = true ) as lev2 " + + "WHERE (leven <= 3) " + + "ORDER BY leven " + + "LIMIT 1"; + + class NameSetter implements StatementParamSetter { + @Override + public void setParams(PreparedStatement stmt) throws SQLException { + stmt.setString(1, name ); + } + } + + StationBean stations = fetchStations(SQL, new NameSetter() ); + stations.fuzzystrmatch = true; + return stations; + } + + private String removeSuffix(String str, String suffix) { + if (str.endsWith(suffix)) { + return str.substring(0, str.length() - suffix.length() ); + } else { + return str; + } + } + + public StationBean getByName(String name) throws SQLException { + name = removeSuffix(name, " st."); + name = removeSuffix(name, " st"); + name = removeSuffix(name, " station"); + + StationBean stations = getByNameNormal(name); + + if (stations.entries.size() == 0) { + stations = getByNameFuzzy(name); + + logger.info("getByName failover: " + name + "(" + (stations.entries.size() >0) + ")" ); + } + return stations; + } + + //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, + // so 1 degree longitude is 64.5 km at denmarks southern point (gedser=54.55,11.95) + // and is 59.4km at northern point (skagen = 57.75,10.65) + // 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) @@ -180,7 +202,7 @@ 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 " + + "earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist,aliases,tritstation " + "FROM trainstations " + "WHERE enabled = true " + limitExpression + "ORDER BY calcdist ASC " + @@ -217,7 +239,7 @@ public StationBean getByList(String list) throws SQLException { - String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro, address,0.0 " + + String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro, address,0.0,aliases,tritstation " + "FROM trainstations " + "WHERE id IN " + list + " AND enabled = true " + "ORDER BY name "; @@ -228,7 +250,7 @@ public StationEntry getSimpleByName(final String name) throws SQLException { - String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " + + String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0,aliases,tritstation " + "FROM trainstations " + "WHERE name = ? AND enabled = true " + "LIMIT 1 "; @@ -248,7 +270,69 @@ return null; } } + + Comparator nameComparator = new Comparator() { + @Override + public int compare(StationEntry arg0, StationEntry arg1) { + return arg0.getName().compareTo( arg1.getName() ); + } + }; + + //used to create full dump in order to populate Google Appengine DB + //after 1.1.0 also used to populate client-side station list + public StationBean dumpAll() throws SQLException { + + String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0,aliases,tritstation " + + "FROM trainstations WHERE enabled = true"; + + + StationBean stations = fetchStations(SQL, new NullSetter() ); + Collections.sort( stations.entries,nameComparator ); + + return stations; + + /* + 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 ); + + } + Collections.sort( stations.entries,nameComparator ); + return stations; + + + } finally { + if (res != null) + res.close(); + if (stmt != null) + stmt.close(); + if (conn != null) + conn.close(); + }*/ + + } + + /* @Deprecated public static String getStationName(int stationID) { String station = ""; @@ -271,10 +355,27 @@ } return station; - } - + }*/ + public boolean hasDisabledStation(final String name) throws SQLException { + String SQL = "Select count(*) as antal from trainstations where name = '" + name + "' and enabled=false " ; + + Connection conn = DBConnection.getConnection(); + Statement stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery( SQL ); + + rs.next(); + + int antal = rs.getInt(1); + + rs.close(); + stmt.close(); + conn.close(); + + return (antal > 0); + } + /* @Deprecated public int getIdByName(final String name) throws SQLException { String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " + @@ -296,5 +397,5 @@ } else { return -1; } - } + }*/ }