/[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 1575 by torben, Sat Jul 9 17:34:55 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 97  public class StationDAO { Line 99  public class StationDAO {
99                  }                  }
100          }          }
101                    
         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;  
                           
102    
                 } finally {  
                         if (res != null)  
                                 res.close();  
                         if (stmt != null)  
                                 stmt.close();  
                         if (conn != null)  
                                 conn.close();  
                 }  
                   
         }  
103    
104          /*          /*
105           * 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 107  public class StationDAO {
107           *     'select $2 ilike $1' language sql strict immutable;           *     'select $2 ilike $1' language sql strict immutable;
108           *     create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~);           *     create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~);
109           */           */
110          public StationBean getByName(final String name) throws SQLException {          public StationBean getByNameNormal(final String name) throws SQLException {
111                  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 " +
112                  "FROM trainstations " +                  "FROM trainstations " +
113                  "WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " +                  "WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " +
# Line 163  public class StationDAO { Line 123  public class StationDAO {
123                                    
124                  return fetchStations(SQL, new NameSetter() );                  return fetchStations(SQL, new NameSetter() );
125          }          }
126            
127            
128            public StationBean getByNameFuzzy(final String name) throws SQLException {
129                    String SQL = "SELECT * FROM (" +
130                                             "    SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0, " +
131                                             "    levenshtein(lower(name),lower(?) ) as leven " +
132                                             "    FROM trainstations " +
133                                             "    WHERE  enabled = true ) as lev2 " +
134                                             "WHERE (leven <= 3) " +                
135                                             "ORDER BY leven " +
136                                             "LIMIT 1";
137    
138                    class NameSetter implements StatementParamSetter {
139                            @Override
140                            public void setParams(PreparedStatement stmt) throws SQLException {
141                                    stmt.setString(1, name );
142                            }                      
143                    }
144                    
145                    StationBean stations = fetchStations(SQL, new NameSetter() );
146                    stations.fuzzystrmatch = true;
147                    return stations;
148            }
149            
150            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);
164                    
165                    if (stations.entries.size() == 0) {
166                            stations = getByNameFuzzy(name);
167    
168                            logger.info("getByName failover: " + name + "(" + (stations.entries.size() >0) + ")" );
169                    }
170                    return stations;
171            }
172            
173            
174    
175          //Latitude (horizonal), longitude(vertical) so          //Latitude (horizonal), longitude(vertical) so
176          // 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
177          // 1 degree longitude is ~111320 meters at equator but gets shorter as we get closer to the poles.          // 1 degree longitude is ~111320 meters at equator but gets shorter as we get closer to the poles.
178          // 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)
179            // and is 59.4km at northern point (skagen = 57.75,10.65)
180            // The "hack" with max 0.4 degrees latitude and 0.75 degrees longitude is only valid since we only service danish trains,
181          // in denmark 0.4dg latitude ~ 44km, 0.75dg longitude ~ 47km          // in denmark 0.4dg latitude ~ 44km, 0.75dg longitude ~ 47km
182    
183          // the ultra fast method  (and only slightly inaccurate as long as we only cover a limited geographically area)          // the ultra fast method  (and only slightly inaccurate as long as we only cover a limited geographically area)
# Line 248  public class StationDAO { Line 258  public class StationDAO {
258                          return null;                          return null;
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
270            //after 1.1.0 also used to populate client-side station list
271            public StationBean dumpAll() throws SQLException {
272                    
273                    String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0,aliases " +
274                                    "FROM trainstations WHERE enabled = true";
275                    
276                    
277                    StationBean stations = fetchStations(SQL, new NullSetter() );
278                    Collections.sort( stations.entries,nameComparator );
279                    
280                    return stations;
281                    
282                    /*
283                    Connection conn = null;
284                    Statement stmt = null;
285                    ResultSet res = null;          
286    
287                    
288                    try {
289                            conn = DBConnection.getConnection();
290    
291                            stmt = conn.createStatement();
292                            res = stmt.executeQuery(SQL);          
293                                                    
294                            // Does mostly the same as convertResultset()
295                            StationBean stations = new StationBean();
296                            while (res.next()) {
297                                    StationEntry entry = convertSingleRow(res);
298                                    
299                                    Array arr = res.getArray(10);
300                                    if (arr != null) {
301                                            String[] aliases = (String[]) arr.getArray();
302                                            entry.setAliases(aliases);
303                                    }
304                                    
305                                    stations.entries.add( entry );
306                                    
307                            }
308                            Collections.sort( stations.entries,nameComparator );
309                            return stations;
310                            
311    
312                    } finally {
313                            if (res != null)
314                                    res.close();
315                            if (stmt != null)
316                                    stmt.close();
317                            if (conn != null)
318                                    conn.close();
319                    }*/
320                    
321            }
322    
323          @Deprecated          @Deprecated
324          public static String getStationName(int stationID) {          public static String getStationName(int stationID) {

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

  ViewVC Help
Powered by ViewVC 1.1.20