/[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 1504 by torben, Wed Jun 8 15:38:11 2011 UTC revision 1507 by torben, Wed Jun 8 16:11:27 2011 UTC
# Line 97  public class StationDAO { Line 97  public class StationDAO {
97                  }                  }
98          }          }
99                    
         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;  
                           
100    
                 } finally {  
                         if (res != null)  
                                 res.close();  
                         if (stmt != null)  
                                 stmt.close();  
                         if (conn != null)  
                                 conn.close();  
                 }  
                   
         }  
101    
102          /*          /*
103           * 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) )           * 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) )
# Line 147  public class StationDAO { Line 105  public class StationDAO {
105           *     'select $2 ilike $1' language sql strict immutable;           *     'select $2 ilike $1' language sql strict immutable;
106           *     create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~);           *     create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~);
107           */           */
108          public StationBean getByName(final String name) throws SQLException {          public StationBean getByNameNormal(final String name) throws SQLException {
109                  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 " +
110                  "FROM trainstations " +                  "FROM trainstations " +
111                  "WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " +                  "WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " +
# Line 163  public class StationDAO { Line 121  public class StationDAO {
121                                    
122                  return fetchStations(SQL, new NameSetter() );                  return fetchStations(SQL, new NameSetter() );
123          }          }
124            
125            
126            public StationBean getByNameFuzzy(final String name) throws SQLException {
127                    String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0, " +
128                    "levenshtein(lower(name),lower(?) ) as leven " +
129                    "FROM trainstations " +
130                    "WHERE (levenshtein(lower(name),lower(?) ) <= 3) AND enabled = true " +
131                    "AND  enabled = true " +
132                    "ORDER BY leven " +
133                    "LIMIT 1";
134    
135                    class NameSetter implements StatementParamSetter {
136                            @Override
137                            public void setParams(PreparedStatement stmt) throws SQLException {
138                                    stmt.setString(1, name );
139                                    stmt.setString(2, name );
140                            }                      
141                    }
142                    
143                    StationBean stations = fetchStations(SQL, new NameSetter() );
144                    stations.fuzzystrmatch = true;
145                    return stations;
146            }
147            
148            public StationBean getByName(final String name) throws SQLException {
149                    StationBean stations = getByNameNormal(name);
150                    
151                    if (stations.entries.size() == 0) {
152                            logger.info("getByName failover: " + name);
153                            stations = getByNameFuzzy(name);
154                    }
155                    return stations;
156            }
157            
158            
159    
160          //Latitude (horizonal), longitude(vertical) so          //Latitude (horizonal), longitude(vertical) so
161          // 1 degree latitude is ~ 111320 meters, since the distance between the horizonal lines is always the same          // 1 degree latitude is ~ 111320 meters, since the distance between the horizonal lines is always the same
# Line 248  public class StationDAO { Line 241  public class StationDAO {
241                          return null;                          return null;
242                  }                  }
243          }          }
244            
245            //used to create full dump in order to populate Google Appengine DB
246            @Deprecated
247            public StationBean dumpAll() throws SQLException {
248                    
249                    String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0,aliases " +
250                                    "FROM trainstations WHERE enabled = true ORDER BY id";
251                    
252                    Connection conn = null;
253                    Statement stmt = null;
254                    ResultSet res = null;          
255    
256                    
257                    try {
258                            conn = DBConnection.getConnection();
259    
260                            stmt = conn.createStatement();
261                            res = stmt.executeQuery(SQL);          
262                                                    
263                            // Does mostly the same as convertResultset()
264                            StationBean stations = new StationBean();
265                            while (res.next()) {
266                                    StationEntry entry = convertSingleRow(res);
267                                    
268                                    Array arr = res.getArray(10);
269                                    if (arr != null) {
270                                            String[] aliases = (String[]) arr.getArray();
271                                            entry.setAliases(aliases);
272                                    }
273                                    
274                                    stations.entries.add( entry );
275                                    
276                            }
277                            return stations;
278                            
279    
280                    } finally {
281                            if (res != null)
282                                    res.close();
283                            if (stmt != null)
284                                    stmt.close();
285                            if (conn != null)
286                                    conn.close();
287                    }
288                    
289            }
290    
291          @Deprecated          @Deprecated
292          public static String getStationName(int stationID) {          public static String getStationName(int stationID) {

Legend:
Removed from v.1504  
changed lines
  Added in v.1507

  ViewVC Help
Powered by ViewVC 1.1.20