/[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

android/TrainInfoService/src/dk/thoerup/traininfoservice/StationDAO.java revision 848 by torben, Tue Jun 15 05:33:14 2010 UTC android/TrainInfoService/src/dk/thoerup/traininfoservice/db/StationDAO.java revision 1832 by torben, Mon Aug 20 21:15:37 2012 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfoservice;  package dk.thoerup.traininfoservice.db;
2    
3    import java.sql.Array;
4  import java.sql.Connection;  import java.sql.Connection;
5  import java.sql.PreparedStatement;  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.ArrayList;  import java.util.Collections;
10  import java.util.List;  import java.util.Comparator;
11    import java.util.logging.Logger;
12    
13    import dk.thoerup.android.traininfo.common.StationBean;
14    import dk.thoerup.android.traininfo.common.StationEntry;
15    
16  public class StationDAO {  public class StationDAO {
         final static int LOCATION_LIMIT = 5;  
17                    
18          private StationBean convertSingleRow(ResultSet res) throws SQLException {          private interface StatementParamSetter {
19                  StationBean station = new StationBean();                  public void setParams(PreparedStatement stmt) throws SQLException ;
20            }
21            private class NullSetter implements StatementParamSetter {
22                    @Override
23                    public void setParams(PreparedStatement stmt) throws SQLException {}
24            }
25            
26            public static class NostationException extends Exception {
27                    private static final long serialVersionUID = 1L;
28            }
29            
30            final static int LOCATION_LIMIT = 8;
31            static final Logger logger = Logger.getLogger(StationDAO.class.getName());
32            
33            
34            private StationEntry convertSingleRow(ResultSet res) throws SQLException {
35                    StationEntry station = new StationEntry();
36    
37                  station.setId( res.getInt(1) );                  station.setId( res.getInt(1) );
38                  station.setName( res.getString(2) );                  station.setName( res.getString(2) );
# Line 23  public class StationDAO { Line 43  public class StationDAO {
43                  station.setMetro( res.getString(7) );                  station.setMetro( res.getString(7) );
44                  station.setAddress( res.getString(8) );                  station.setAddress( res.getString(8) );
45                  station.setCalcdist( (int)res.getDouble(9) );                  station.setCalcdist( (int)res.getDouble(9) );
46                    
47                    station.setIsRegional( station.getRegional() != null );
48                    station.setIsStrain( station.getStrain() != null );
49                    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                    station.setTritStation( res.getInt(11) );
60                    if (res.wasNull())
61                            station.setTritStation( -1 );
62    
63                  return station;                  return station;
64          }          }
65    
66          private List<StationBean> convertResultset(ResultSet res) throws SQLException {          private StationBean convertResultset(ResultSet res) throws SQLException {
67                  List<StationBean> stations = new ArrayList<StationBean>();                  StationBean stations = new StationBean();
68                  while (res.next()) {                  while (res.next()) {
69                          stations.add( convertSingleRow(res) );                          stations.entries.add( convertSingleRow(res) );
70                  }                  }
71                  return stations;                  return stations;
72    
73          }          }
74    
75            private StationBean fetchStations(String SQL, StatementParamSetter setter) throws SQLException {
76          public StationBean getById(int id) throws SQLException {                  StationBean stations;
                 String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0 " +  
                 "FROM trainstations WHERE id=" + id + " AND enabled=true";  
   
77                  Connection conn = null;                  Connection conn = null;
78                  Statement stmt = null;                  PreparedStatement stmt = null;
79                  ResultSet res = null;                  ResultSet res = null;
                 StationBean result;  
   
80                  try {                  try {
81                          conn = DBConnection.getConnection();                          conn = DBConnection.getConnection();
82                            stmt = conn.prepareStatement(SQL);
83                            
84                            setter.setParams(stmt);
85    
86                            res = stmt.executeQuery();
87                            stations = convertResultset(res);
88    
                         stmt = conn.createStatement();  
                         res = stmt.executeQuery(SQL);            
                         res.next();  
                         result = convertSingleRow(res);  
89                  } finally {                  } finally {
90                          if (res != null)                          if (res != null)
91                                  res.close();                                  res.close();
92                          if (stmt != null)                          if (stmt != null)
93                                  stmt.close();                                  stmt.close();
94                          if (conn != null)                          if (conn!= null)
95                                  conn.close();                                  conn.close();
96                  }                  }
97                    return stations;
98                    
99            }
100    
101                  return result;          public StationEntry getById(int id) throws SQLException,NostationException {
102                    String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0,aliases,tritstation " +
103                    "FROM trainstations WHERE id=" + id + " AND enabled=true";
104                                    
105                    StationBean stations =  fetchStations(SQL, new NullSetter() );
106                    
107                    if (stations.entries.size() > 0 ) {
108                            return stations.entries.get(0);
109                    } else {
110                            throw new NostationException();
111                    }
112          }          }
113            
114    
115    
116          /*          /*
117           * 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 71  public class StationDAO { Line 119  public class StationDAO {
119           *     'select $2 ilike $1' language sql strict immutable;           *     'select $2 ilike $1' language sql strict immutable;
120           *     create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~);           *     create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~);
121           */           */
122          public List<StationBean> getByName(String name) throws SQLException {          public StationBean getByNameNormal(final String name) throws SQLException {
123                  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,tritstation " +
124                  "FROM trainstations " +                  "FROM trainstations " +
125                  "WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " +                  "WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " +
126                  "ORDER BY name ";                  "ORDER BY name ";
127    
128                    class NameSetter implements StatementParamSetter {
129                            @Override
130                            public void setParams(PreparedStatement stmt) throws SQLException {
131                                    stmt.setString(1, name + "%");
132                                    stmt.setString(2, name + "%");                          
133                            }                      
134                    }
135                    
136                    return fetchStations(SQL, new NameSetter() );
137            }
138            
139            
140            public StationBean getByNameFuzzy(final String name) throws SQLException {
141                    String SQL = "SELECT * FROM (" +
142                                             "    SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0, aliases, tritstation, " +
143                                             "    levenshtein(lower(name),lower(?) ) as leven " +
144                                             "    FROM trainstations " +
145                                             "    WHERE  enabled = true ) as lev2 " +
146                                             "WHERE (leven <= 3) " +                
147                                             "ORDER BY leven " +
148                                             "LIMIT 1";
149    
150                    class NameSetter implements StatementParamSetter {
151                            @Override
152                            public void setParams(PreparedStatement stmt) throws SQLException {
153                                    stmt.setString(1, name );
154                            }                      
155                    }
156                    
157                    StationBean stations = fetchStations(SQL, new NameSetter() );
158                    stations.fuzzystrmatch = true;
159                    return stations;
160            }
161            
162            private String removeSuffix(String str, String suffix) {
163                    if (str.endsWith(suffix)) {
164                            return str.substring(0, str.length() - suffix.length() );
165                    } else {
166                            return str;
167                    }
168            }
169            
170            public StationBean getByName(String name) throws SQLException {
171                    name = removeSuffix(name, " st.");
172                    name = removeSuffix(name, " st");
173                    name = removeSuffix(name, " station");
174                    
175                    StationBean stations = getByNameNormal(name);
176                    
177                    if (stations.entries.size() == 0) {
178                            stations = getByNameFuzzy(name);
179    
180                  List<StationBean> result;                          logger.info("getByName failover: " + name + "(" + (stations.entries.size() >0) + ")" );
                 Connection conn = null;  
                 PreparedStatement stmt = null;  
                 ResultSet res = null;  
                 try {  
                         conn = DBConnection.getConnection();  
                         stmt = conn.prepareStatement(SQL);  
   
                         stmt.setString(1, name + "%");  
                         stmt.setString(2, name + "%");  
   
                         res = stmt.executeQuery();  
                         result = convertResultset(res);  
   
                 } finally {  
                         if (res != null)  
                                 res.close();  
                         if (stmt != null)  
                                 stmt.close();  
                         if (conn!= null)  
                                 conn.close();  
181                  }                  }
182                  return result;                  return stations;
183          }          }
184            
185            
186    
187          //the "hack" with max 1.0 degrees latitude and 2.0 degrees longitude is only valid since we only service danish trains          //Latitude (horizonal), longitude(vertical) so
188          // in denmark 1.0dg latitude ~ 110km, 2.0dg longitude ~ 120km          // 1 degree latitude is ~ 111320 meters, since the distance between the horizonal lines is always the same
189            // 1 degree longitude is ~111320 meters at equator but gets shorter as we get closer to the poles.
190            // so 1 degree longitude is 64.5 km at denmarks southern point (gedser=54.55,11.95)
191            // and is 59.4km at northern point (skagen = 57.75,10.65)
192            // The "hack" with max 0.4 degrees latitude and 0.75 degrees longitude is only valid since we only service danish trains,
193            // in denmark 0.4dg latitude ~ 44km, 0.75dg longitude ~ 47km
194    
195          // 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)
196          // is using an aproximation of the length of 1 latitude degree and 1 longitude degree and just use pythagoras to          // is using an aproximation of the length of 1 latitude degree and 1 longitude degree and just use pythagoras to
197          // calculate the distance:          // calculate the distance:
198          //     sqrt( power(abs(latitude-?)*111320, 2) + power(abs(longitude-?)*63000,2) )::int as calcdist          //     sqrt( power(abs(latitude-?)*111320, 2) + power(abs(longitude-?)*63000,2) )::int as calcdist
199    
200          public List<StationBean> getByLocationWorker(double latitude, double longitude, boolean geolimit) throws SQLException {          public StationBean getByLocationWorker(final double latitude, final double longitude, final boolean geolimit) throws SQLException {
201                                    
202                  String limitExpression = geolimit == true ? "AND abs(latitude-?)<1.0 AND abs(longitude-?)<2.0 " : "";                  String limitExpression = (geolimit == true) ? "AND abs(latitude-?)<0.4 AND abs(longitude-?)<0.75 " : "";
203                                    
204                  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, " +
205                          "earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " +                          "earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist,aliases,tritstation " +
206                          "FROM trainstations " +                          "FROM trainstations " +
207                          "WHERE enabled = true " + limitExpression +                          "WHERE enabled = true " + limitExpression +
208                          "ORDER BY calcdist ASC " +                          "ORDER BY calcdist ASC " +
209                          "LIMIT " + LOCATION_LIMIT;                          "LIMIT " + LOCATION_LIMIT;
                   
                 List<StationBean> result;  
                 Connection conn = null;  
                 PreparedStatement stmt = null;  
                 ResultSet res = null;  
                 try {  
                         conn = DBConnection.getConnection();  
                         stmt = conn.prepareStatement(SQL);  
                         stmt.setDouble(1, latitude);  
                         stmt.setDouble(2, longitude);  
                         if (geolimit == true) {  
                                 stmt.setDouble(3, latitude);  
                                 stmt.setDouble(4, longitude);  
                         }  
                         res = stmt.executeQuery();  
                         result = convertResultset(res);  
210    
211                  } finally {                  
212                          if (res != null)                  class LatlongSetter implements StatementParamSetter {
213                                  res.close();                          @Override
214                          if (stmt != null)                          public void setParams(PreparedStatement stmt) throws SQLException {
215                                  stmt.close();                                  stmt.setDouble(1, latitude);
216                          if (conn!= null)                                  stmt.setDouble(2, longitude);
217                                  conn.close();                                  if (geolimit == true) {
218                                            stmt.setDouble(3, latitude);
219                                            stmt.setDouble(4, longitude);
220                                    }                              
221                            }                      
222                  }                  }
223                  return result;                  
224                    return fetchStations(SQL, new LatlongSetter() );
225          }          }
226                    
227          public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException {          public StationBean getByLocation(double latitude, double longitude) throws SQLException {
228                  List<StationBean> result = getByLocationWorker(latitude, longitude, true);                  StationBean result = getByLocationWorker(latitude, longitude, true);
229                                    
230                  if (result.size() < LOCATION_LIMIT) { //failover                  if (result.entries.size() < LOCATION_LIMIT) { //failover
231                            logger.info("getByLocation failover: " +latitude + "," + longitude);
232                            
233                          result = getByLocationWorker(latitude, longitude, false);                          result = getByLocationWorker(latitude, longitude, false);
234                  }                  }
235                                    
# Line 161  public class StationDAO { Line 238  public class StationDAO {
238                    
239                    
240    
241          public List<StationBean> getByList(String list) throws SQLException {          public StationBean getByList(String list) throws SQLException {
242                  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,tritstation " +
243                  "FROM trainstations " +                  "FROM trainstations " +
244                  "WHERE id IN " + list + " AND enabled = true " +                  "WHERE id IN " + list + " AND enabled = true " +
245                  "ORDER BY name ";                  "ORDER BY name ";
246                    
247                    return fetchStations(SQL, new NullSetter() );
248            }
249            
250    
251            
252            public StationEntry getSimpleByName(final String name) throws SQLException {
253                    String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0,aliases,tritstation " +
254                    "FROM trainstations " +
255                    "WHERE name = ?  AND enabled = true " +
256                    "LIMIT 1 ";
257    
258                    class NameSetter implements StatementParamSetter {
259                            @Override
260                            public void setParams(PreparedStatement stmt) throws SQLException {
261                                    stmt.setString(1, name );
262                            }                      
263                    }
264                    
265                    StationBean stations = fetchStations(SQL, new NameSetter() );
266                    
267                    if (stations.entries.size() == 1) {
268                            return stations.entries.get(0);
269                    } else {
270                            return null;
271                    }
272            }
273            
274            Comparator<StationEntry> nameComparator = new Comparator<StationEntry>() {
275                    @Override
276                    public int compare(StationEntry arg0, StationEntry arg1) {
277                            return arg0.getName().compareTo( arg1.getName() );
278                    }              
279            };
280            
281            //used to create full dump in order to populate Google Appengine DB
282            //after 1.1.0 also used to populate client-side station list
283            public StationBean dumpAll() throws SQLException {
284                    
285                    String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0,aliases,tritstation " +
286                                    "FROM trainstations WHERE enabled = true";
287                    
288                    
289                    StationBean stations = fetchStations(SQL, new NullSetter() );
290                    Collections.sort( stations.entries,nameComparator );
291                    
292                    return stations;
293                    
294                    /*
295                  Connection conn = null;                  Connection conn = null;
296                  Statement stmt = null;                  Statement stmt = null;
297                  ResultSet res = null;                  ResultSet res = null;          
                 List<StationBean> result;  
298    
299                    
300                  try {                  try {
301                          conn = DBConnection.getConnection();                          conn = DBConnection.getConnection();
302    
303                          stmt = conn.createStatement();                          stmt = conn.createStatement();
304                          res = stmt.executeQuery(SQL);                          res = stmt.executeQuery(SQL);          
305                          result = convertResultset(res);                                                  
306                            // Does mostly the same as convertResultset()
307                            StationBean stations = new StationBean();
308                            while (res.next()) {
309                                    StationEntry entry = convertSingleRow(res);
310                                    
311                                    Array arr = res.getArray(10);
312                                    if (arr != null) {
313                                            String[] aliases = (String[]) arr.getArray();
314                                            entry.setAliases(aliases);
315                                    }
316                                    
317                                    stations.entries.add( entry );
318                                    
319                            }
320                            Collections.sort( stations.entries,nameComparator );
321                            return stations;
322                            
323    
324                  } finally {                  } finally {
325                          if (res != null)                          if (res != null)
326                                  res.close();                                  res.close();
327                          if (stmt != null)                          if (stmt != null)
328                                  stmt.close();                                  stmt.close();
329                          if (conn!= null)                          if (conn != null)
330                                  conn.close();                                  conn.close();
331                  }                  }*/
332                    
                 return result;  
   
333          }          }
334    
335            /*
336            @Deprecated
337          public static String getStationName(int stationID) {          public static String getStationName(int stationID) {
338                  String station = "";                  String station = "";
339    
# Line 210  public class StationDAO { Line 355  public class StationDAO {
355                  }                  }
356    
357                  return station;                  return station;
358          }          }*/
359            
360            public boolean hasDisabledStation(final String name) throws SQLException {
361                    String SQL = "Select count(*) as antal from trainstations where name = '" + name + "'  and enabled=false " ;
362    
363          public int getIdByName(String name) throws SQLException {                  Connection conn = DBConnection.getConnection();
364                    Statement stmt = conn.createStatement();
365                    ResultSet rs = stmt.executeQuery( SQL );
366                    
367                    rs.next();
368    
369                    int antal = rs.getInt(1);
370                    
371                    rs.close();
372                    stmt.close();
373                    conn.close();
374                    
375                    return (antal > 0);
376            }
377            
378            /*
379            @Deprecated
380            public int getIdByName(final String name) throws SQLException {
381                  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 " +
382                  "FROM trainstations " +                  "FROM trainstations " +
383                  "WHERE name = ?  AND enabled = true " +                  "WHERE name = ?  AND enabled = true " +
384                  "LIMIT 1 ";                  "LIMIT 1 ";
385    
386                  List<StationBean> result;                  class NameSetter implements StatementParamSetter {
387                  Connection conn = null;                          @Override
388                  PreparedStatement stmt = null;                          public void setParams(PreparedStatement stmt) throws SQLException {
389                  ResultSet res = null;                                  stmt.setString(1, name );
390                  try {                          }                      
                         conn = DBConnection.getConnection();  
                         stmt = conn.prepareStatement(SQL);  
   
                         stmt.setString(1, name );  
   
                         res = stmt.executeQuery();  
                         result = convertResultset(res);  
   
                 } finally {  
                         if (res != null)  
                                 res.close();  
                         if (stmt != null)  
                                 stmt.close();  
                         if (conn!= null)  
                                 conn.close();  
391                  }                  }
392                    
393                  if (result.size() == 1) {                  StationBean stations = fetchStations(SQL, new NameSetter() );
394                          return result.get(0).getId();                  
395                    if (stations.entries.size() == 1) {
396                            return stations.entries.get(0).getId();
397                  } else {                  } else {
398                          return -1;                          return -1;
399                  }                  }
400          }          }*/
401  }  }

Legend:
Removed from v.848  
changed lines
  Added in v.1832

  ViewVC Help
Powered by ViewVC 1.1.20