/[projects]/android/TrainInfoService/src/dk/thoerup/traininfoservice/db/StationDAO.java
ViewVC logotype

Diff of /android/TrainInfoService/src/dk/thoerup/traininfoservice/db/StationDAO.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1511 by torben, Wed Jun 8 18:24:24 2011 UTC revision 1574 by torben, Sat Jul 9 17:18:59 2011 UTC
# Line 6  import java.sql.PreparedStatement; Line 6  import java.sql.PreparedStatement;
6  import java.sql.ResultSet;  import java.sql.ResultSet;
7  import java.sql.SQLException;  import java.sql.SQLException;
8  import java.sql.Statement;  import java.sql.Statement;
9    import java.util.Collections;
10    import java.util.Comparator;
11  import java.util.logging.Logger;  import java.util.logging.Logger;
12    
13  import dk.thoerup.android.traininfo.common.StationBean;  import dk.thoerup.android.traininfo.common.StationBean;
# Line 124  public class StationDAO { Line 126  public class StationDAO {
126                    
127                    
128          public StationBean getByNameFuzzy(final String name) throws SQLException {          public StationBean getByNameFuzzy(final String name) throws SQLException {
129                  String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0, " +                  String SQL = "SELECT * FROM (" +
130                  "levenshtein(lower(name),lower(?) ) as leven " +                                           "    SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0, " +
131                  "FROM trainstations " +                                           "    levenshtein(lower(name),lower(?) ) as leven " +
132                  "WHERE (levenshtein(lower(name),lower(?) ) <= 3) " +                                           "    FROM trainstations " +
133                  "AND  enabled = true " +                                           "    WHERE  enabled = true ) as lev2 " +
134                  "ORDER BY leven " +                                           "WHERE (leven <= 3) " +                
135                  "LIMIT 1";                                           "ORDER BY leven " +
136                                             "LIMIT 1";
137    
138                  class NameSetter implements StatementParamSetter {                  class NameSetter implements StatementParamSetter {
139                          @Override                          @Override
140                          public void setParams(PreparedStatement stmt) throws SQLException {                          public void setParams(PreparedStatement stmt) throws SQLException {
141                                  stmt.setString(1, name );                                  stmt.setString(1, name );
                                 stmt.setString(2, name );  
142                          }                                                }                      
143                  }                  }
144                                    
# Line 145  public class StationDAO { Line 147  public class StationDAO {
147                  return stations;                  return stations;
148          }          }
149                    
150          public StationBean getByName(final String name) throws SQLException {          private String removeSuffix(String str, String suffix) {
151                    if (str.endsWith(suffix)) {
152                            return str.substring(0, str.length() - suffix.length() );
153                    } else {
154                            return str;
155                    }
156            }
157            
158            public StationBean getByName(String name) throws SQLException {
159                    name = removeSuffix(name, " st.");
160                    name = removeSuffix(name, " st");
161                    name = removeSuffix(name, " station");
162                    
163                  StationBean stations = getByNameNormal(name);                  StationBean stations = getByNameNormal(name);
164                                    
165                  if (stations.entries.size() == 0) {                  if (stations.entries.size() == 0) {
                         logger.info("getByName failover: " + name);  
166                          stations = getByNameFuzzy(name);                          stations = getByNameFuzzy(name);
167    
168                            logger.info("getByName failover: " + name + "(" + (stations.entries.size() >0) + ")" );
169                  }                  }
170                  return stations;                  return stations;
171          }          }
# Line 244  public class StationDAO { Line 259  public class StationDAO {
259                  }                  }
260          }          }
261                    
262            Comparator<StationEntry> nameComparator = new Comparator<StationEntry>() {
263                    @Override
264                    public int compare(StationEntry arg0, StationEntry arg1) {
265                            return arg0.getName().compareTo( arg1.getName() );
266                    }              
267            };
268            
269          //used to create full dump in order to populate Google Appengine DB          //used to create full dump in order to populate Google Appengine DB
270          @Deprecated          //after 1.1.0 also used to populate client-side station list
271          public StationBean dumpAll() throws SQLException {          public StationBean dumpAll() throws SQLException {
272                                    
273                  String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0,aliases " +                  String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0,aliases " +
274                                  "FROM trainstations WHERE enabled = true ORDER BY id";                                  "FROM trainstations WHERE enabled = true";
275                                    
276                  Connection conn = null;                  Connection conn = null;
277                  Statement stmt = null;                  Statement stmt = null;
# Line 276  public class StationDAO { Line 298  public class StationDAO {
298                                  stations.entries.add( entry );                                  stations.entries.add( entry );
299                                                                    
300                          }                          }
301                            Collections.sort( stations.entries,nameComparator );
302                          return stations;                          return stations;
303                                                    
304    

Legend:
Removed from v.1511  
changed lines
  Added in v.1574

  ViewVC Help
Powered by ViewVC 1.1.20