--- android/TrainInfoService/src/dk/thoerup/traininfoservice/db/StationDAO.java 2011/06/08 16:11:27 1507 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/db/StationDAO.java 2012/02/24 14:26:53 1692 @@ -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; @@ -124,19 +126,19 @@ public StationBean getByNameFuzzy(final String name) throws SQLException { - String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0, " + - "levenshtein(lower(name),lower(?) ) as leven " + - "FROM trainstations " + - "WHERE (levenshtein(lower(name),lower(?) ) <= 3) AND enabled = true " + - "AND enabled = true " + - "ORDER BY leven " + - "LIMIT 1"; + String SQL = "SELECT * FROM (" + + " SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0, " + + " 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 ); - stmt.setString(2, name ); } } @@ -145,12 +147,25 @@ return stations; } - public StationBean getByName(final String name) throws SQLException { + 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) { - logger.info("getByName failover: " + name); stations = getByNameFuzzy(name); + + logger.info("getByName failover: " + name + "(" + (stations.entries.size() >0) + ")" ); } return stations; } @@ -160,7 +175,9 @@ //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) @@ -242,13 +259,27 @@ } } + 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 - @Deprecated + //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 " + - "FROM trainstations WHERE enabled = true ORDER BY id"; + "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; @@ -274,6 +305,7 @@ stations.entries.add( entry ); } + Collections.sort( stations.entries,nameComparator ); return stations; @@ -284,7 +316,7 @@ stmt.close(); if (conn != null) conn.close(); - } + }*/ } @@ -312,6 +344,23 @@ 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