/[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 1516 by torben, Fri Jun 10 15:05: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 125  public class StationDAO { Line 135  public class StationDAO {
135                    
136          public StationBean getByNameFuzzy(final String name) throws SQLException {          public StationBean getByNameFuzzy(final String name) throws SQLException {
137                  String SQL = "SELECT * FROM (" +                  String SQL = "SELECT * FROM (" +
138                                           "    SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0, " +                                           "    SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0, aliases, " +
139                                           "    levenshtein(lower(name),lower(?) ) as leven " +                                           "    levenshtein(lower(name),lower(?) ) as leven " +
140                                           "    FROM trainstations " +                                           "    FROM trainstations " +
141                                           "    WHERE  enabled = true ) as lev2 " +                                           "    WHERE  enabled = true ) as lev2 " +
# Line 188  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 225  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 236  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 257  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 289  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 299  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 327  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.1516  
changed lines
  Added in v.1790

  ViewVC Help
Powered by ViewVC 1.1.20