/[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 1790 by torben, Wed Apr 11 10:11:38 2012 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 45  public class StationDAO { Line 47  public class StationDAO {
47                  station.setIsRegional( station.getRegional() != null );                  station.setIsRegional( station.getRegional() != null );
48                  station.setIsStrain( station.getStrain() != null );                  station.setIsStrain( station.getStrain() != null );
49                  station.setIsMetro( station.getMetro() != null );                  station.setIsMetro( station.getMetro() != null );
50                    
51                    Array aliases = res.getArray( 10);
52                    if (aliases != null) {
53                            station.setAliases( (String[]) aliases.getArray() );
54                    } else {
55                            String[] emptyArray = {};
56                            station.setAliases( emptyArray );
57                    }
58    
59                  return station;                  return station;
60          }          }
# Line 85  public class StationDAO { Line 95  public class StationDAO {
95          }          }
96    
97          public StationEntry getById(int id) throws SQLException,NostationException {          public StationEntry getById(int id) throws SQLException,NostationException {
98                  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 " +
99                  "FROM trainstations WHERE id=" + id + " AND enabled=true";                  "FROM trainstations WHERE id=" + id + " AND enabled=true";
100                                                                    
101                  StationBean stations =  fetchStations(SQL, new NullSetter() );                  StationBean stations =  fetchStations(SQL, new NullSetter() );
# Line 106  public class StationDAO { Line 116  public class StationDAO {
116           *     create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~);           *     create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~);
117           */           */
118          public StationBean getByNameNormal(final String name) throws SQLException {          public StationBean getByNameNormal(final String name) throws SQLException {
119                  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 " +
120                  "FROM trainstations " +                  "FROM trainstations " +
121                  "WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " +                  "WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " +
122                  "ORDER BY name ";                  "ORDER BY name ";
# Line 124  public class StationDAO { Line 134  public class StationDAO {
134                    
135                    
136          public StationBean getByNameFuzzy(final String name) throws SQLException {          public StationBean getByNameFuzzy(final String name) throws SQLException {
137                  String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0, " +                  String SQL = "SELECT * FROM (" +
138                  "levenshtein(lower(name),lower(?) ) as leven " +                                           "    SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0, aliases, " +
139                  "FROM trainstations " +                                           "    levenshtein(lower(name),lower(?) ) as leven " +
140                  "WHERE (levenshtein(lower(name),lower(?) ) <= 3) " +                                           "    FROM trainstations " +
141                  "AND  enabled = true " +                                           "    WHERE  enabled = true ) as lev2 " +
142                  "ORDER BY leven " +                                           "WHERE (leven <= 3) " +                
143                  "LIMIT 1";                                           "ORDER BY leven " +
144                                             "LIMIT 1";
145    
146                  class NameSetter implements StatementParamSetter {                  class NameSetter implements StatementParamSetter {
147                          @Override                          @Override
148                          public void setParams(PreparedStatement stmt) throws SQLException {                          public void setParams(PreparedStatement stmt) throws SQLException {
149                                  stmt.setString(1, name );                                  stmt.setString(1, name );
                                 stmt.setString(2, name );  
150                          }                                                }                      
151                  }                  }
152                                    
# Line 145  public class StationDAO { Line 155  public class StationDAO {
155                  return stations;                  return stations;
156          }          }
157                    
158          public StationBean getByName(final String name) throws SQLException {          private String removeSuffix(String str, String suffix) {
159                    if (str.endsWith(suffix)) {
160                            return str.substring(0, str.length() - suffix.length() );
161                    } else {
162                            return str;
163                    }
164            }
165            
166            public StationBean getByName(String name) throws SQLException {
167                    name = removeSuffix(name, " st.");
168                    name = removeSuffix(name, " st");
169                    name = removeSuffix(name, " station");
170                    
171                  StationBean stations = getByNameNormal(name);                  StationBean stations = getByNameNormal(name);
172                                    
173                  if (stations.entries.size() == 0) {                  if (stations.entries.size() == 0) {
                         logger.info("getByName failover: " + name);  
174                          stations = getByNameFuzzy(name);                          stations = getByNameFuzzy(name);
175    
176                            logger.info("getByName failover: " + name + "(" + (stations.entries.size() >0) + ")" );
177                  }                  }
178                  return stations;                  return stations;
179          }          }
# Line 175  public class StationDAO { Line 198  public class StationDAO {
198                  String limitExpression = (geolimit == true) ? "AND abs(latitude-?)<0.4 AND abs(longitude-?)<0.75 " : "";                  String limitExpression = (geolimit == true) ? "AND abs(latitude-?)<0.4 AND abs(longitude-?)<0.75 " : "";
199                                    
200                  String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, " +                  String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, " +
201                          "earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " +                          "earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist,aliases " +
202                          "FROM trainstations " +                          "FROM trainstations " +
203                          "WHERE enabled = true " + limitExpression +                          "WHERE enabled = true " + limitExpression +
204                          "ORDER BY calcdist ASC " +                          "ORDER BY calcdist ASC " +
# Line 212  public class StationDAO { Line 235  public class StationDAO {
235                    
236    
237          public StationBean getByList(String list) throws SQLException {          public StationBean getByList(String list) throws SQLException {
238                  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 " +
239                  "FROM trainstations " +                  "FROM trainstations " +
240                  "WHERE id IN " + list + " AND enabled = true " +                  "WHERE id IN " + list + " AND enabled = true " +
241                  "ORDER BY name ";                  "ORDER BY name ";
# Line 223  public class StationDAO { Line 246  public class StationDAO {
246    
247                    
248          public StationEntry getSimpleByName(final String name) throws SQLException {          public StationEntry getSimpleByName(final String name) throws SQLException {
249                  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 " +
250                  "FROM trainstations " +                  "FROM trainstations " +
251                  "WHERE name = ?  AND enabled = true " +                  "WHERE name = ?  AND enabled = true " +
252                  "LIMIT 1 ";                  "LIMIT 1 ";
# Line 244  public class StationDAO { Line 267  public class StationDAO {
267                  }                  }
268          }          }
269                    
270            Comparator<StationEntry> nameComparator = new Comparator<StationEntry>() {
271                    @Override
272                    public int compare(StationEntry arg0, StationEntry arg1) {
273                            return arg0.getName().compareTo( arg1.getName() );
274                    }              
275            };
276            
277          //used to create full dump in order to populate Google Appengine DB          //used to create full dump in order to populate Google Appengine DB
278          @Deprecated          //after 1.1.0 also used to populate client-side station list
279          public StationBean dumpAll() throws SQLException {          public StationBean dumpAll() throws SQLException {
280                                    
281                  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 " +
282                                  "FROM trainstations WHERE enabled = true ORDER BY id";                                  "FROM trainstations WHERE enabled = true";
283                    
284                    
285                    StationBean stations = fetchStations(SQL, new NullSetter() );
286                    Collections.sort( stations.entries,nameComparator );
287                                    
288                    return stations;
289                    
290                    /*
291                  Connection conn = null;                  Connection conn = null;
292                  Statement stmt = null;                  Statement stmt = null;
293                  ResultSet res = null;                            ResultSet res = null;          
# Line 276  public class StationDAO { Line 313  public class StationDAO {
313                                  stations.entries.add( entry );                                  stations.entries.add( entry );
314                                                                    
315                          }                          }
316                            Collections.sort( stations.entries,nameComparator );
317                          return stations;                          return stations;
318                                                    
319    
# Line 286  public class StationDAO { Line 324  public class StationDAO {
324                                  stmt.close();                                  stmt.close();
325                          if (conn != null)                          if (conn != null)
326                                  conn.close();                                  conn.close();
327                  }                  }*/
328                                    
329          }          }
330    
# Line 314  public class StationDAO { Line 352  public class StationDAO {
352                  return station;                  return station;
353          }          }
354                    
355            public boolean hasDisabledStation(final String name) throws SQLException {
356                    String SQL = "Select count(*) as antal from trainstations where name = '" + name + "'  and enabled=false " ;
357    
358                    Connection conn = DBConnection.getConnection();
359                    Statement stmt = conn.createStatement();
360                    ResultSet rs = stmt.executeQuery( SQL );
361                    
362                    rs.next();
363    
364                    int antal = rs.getInt(1);
365                    
366                    rs.close();
367                    stmt.close();
368                    conn.close();
369                    
370                    return (antal > 0);
371            }
372                    
373                    
374          @Deprecated          @Deprecated

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

  ViewVC Help
Powered by ViewVC 1.1.20