/[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 894 by torben, Thu Jun 24 17:00:39 2010 UTC android/TrainInfoService/src/dk/thoerup/traininfoservice/db/StationDAO.java revision 1417 by torben, Mon May 2 16:21:37 2011 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.logging.Logger;
 import java.util.List;  
10    
11  import com.sun.istack.logging.Logger;  import dk.thoerup.android.traininfo.common.StationBean;
12    import dk.thoerup.android.traininfo.common.StationEntry;
13    
14  public class StationDAO {  public class StationDAO {
         final static int LOCATION_LIMIT = 5;  
         static final Logger logger = Logger.getLogger(StationDAO.class);  
15                    
16          private StationBean convertSingleRow(ResultSet res) throws SQLException {          public static class NostationException extends Exception {
17                  StationBean station = new StationBean();                  private static final long serialVersionUID = 1L;
18            }
19            
20            final static int LOCATION_LIMIT = 8;
21            static final Logger logger = Logger.getLogger(StationDAO.class.getName());
22            
23            
24            private StationEntry convertSingleRow(ResultSet res) throws SQLException {
25                    StationEntry station = new StationEntry();
26    
27                  station.setId( res.getInt(1) );                  station.setId( res.getInt(1) );
28                  station.setName( res.getString(2) );                  station.setName( res.getString(2) );
# Line 26  public class StationDAO { Line 33  public class StationDAO {
33                  station.setMetro( res.getString(7) );                  station.setMetro( res.getString(7) );
34                  station.setAddress( res.getString(8) );                  station.setAddress( res.getString(8) );
35                  station.setCalcdist( (int)res.getDouble(9) );                  station.setCalcdist( (int)res.getDouble(9) );
36                    
37                    station.setIsRegional( station.getRegional() != null );
38                    station.setIsStrain( station.getStrain() != null );
39                    station.setIsMetro( station.getMetro() != null );
40    
41                  return station;                  return station;
42          }          }
43    
44          private List<StationBean> convertResultset(ResultSet res) throws SQLException {          private StationBean convertResultset(ResultSet res) throws SQLException {
45                  List<StationBean> stations = new ArrayList<StationBean>();                  StationBean stations = new StationBean();
46                  while (res.next()) {                  while (res.next()) {
47                          stations.add( convertSingleRow(res) );                          stations.entries.add( convertSingleRow(res) );
48                  }                  }
49                  return stations;                  return stations;
50    
51          }          }
52    
53    
54          public StationBean getById(int id) throws SQLException {          public StationEntry getById(int id) throws SQLException,NostationException {
55                  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 " +
56                  "FROM trainstations WHERE id=" + id + " AND enabled=true";                  "FROM trainstations WHERE id=" + id + " AND enabled=true";
57    
58                  Connection conn = null;                  Connection conn = null;
59                  Statement stmt = null;                  Statement stmt = null;
60                  ResultSet res = null;                  ResultSet res = null;
61                  StationBean result;                  StationEntry result;
62    
63                  try {                  try {
64                          conn = DBConnection.getConnection();                          conn = DBConnection.getConnection();
65    
66                          stmt = conn.createStatement();                          stmt = conn.createStatement();
67                          res = stmt.executeQuery(SQL);                                    res = stmt.executeQuery(SQL);
68                          res.next();                          
69                          result = convertSingleRow(res);                          if (res.next()) {
70                                    result = convertSingleRow(res);
71                            } else {
72                                    throw new NostationException();
73                            }
74                  } finally {                  } finally {
75                          if (res != null)                          if (res != null)
76                                  res.close();                                  res.close();
# Line 67  public class StationDAO { Line 82  public class StationDAO {
82    
83                  return result;                  return result;
84          }          }
85            
86            public StationBean dumpAll() throws SQLException {
87                    
88                    String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0,aliases " +
89                                    "FROM trainstations WHERE enabled = true ORDER BY id";
90                    
91                    Connection conn = null;
92                    Statement stmt = null;
93                    ResultSet res = null;          
94    
95                    
96                    try {
97                            conn = DBConnection.getConnection();
98    
99                            stmt = conn.createStatement();
100                            res = stmt.executeQuery(SQL);          
101                                                    
102                            // Does mostly the same as convertResultset()
103                            StationBean stations = new StationBean();
104                            while (res.next()) {
105                                    StationEntry entry = convertSingleRow(res);
106                                    
107                                    Array arr = res.getArray(10);
108                                    if (arr != null) {
109                                            String[] aliases = (String[]) arr.getArray();
110                                            entry.setAliases(aliases);
111                                    }
112                                    
113                                    stations.entries.add( entry );
114                                    
115                            }
116                            return stations;
117                            
118    
119                    } finally {
120                            if (res != null)
121                                    res.close();
122                            if (stmt != null)
123                                    stmt.close();
124                            if (conn != null)
125                                    conn.close();
126                    }
127                    
128            }
129    
130          /*          /*
131           * 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 74  public class StationDAO { Line 133  public class StationDAO {
133           *     'select $2 ilike $1' language sql strict immutable;           *     'select $2 ilike $1' language sql strict immutable;
134           *     create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~);           *     create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~);
135           */           */
136          public List<StationBean> getByName(String name) throws SQLException {          public StationBean getByName(String name) throws SQLException {
137                  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 " +
138                  "FROM trainstations " +                  "FROM trainstations " +
139                  "WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " +                  "WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " +
140                  "ORDER BY name ";                  "ORDER BY name ";
141    
142    
143                  List<StationBean> result;                  StationBean result;
144                  Connection conn = null;                  Connection conn = null;
145                  PreparedStatement stmt = null;                  PreparedStatement stmt = null;
146                  ResultSet res = null;                  ResultSet res = null;
# Line 106  public class StationDAO { Line 165  public class StationDAO {
165                  return result;                  return result;
166          }          }
167    
168          //the "hack" with max 0.5 degrees latitude and 0.9 degrees longitude is only valid since we only service danish trains,          //Latitude (horizonal), longitude(vertical) so
169          // in denmark 0.5dg latitude ~ 55km, 0.9dg longitude ~ 57km          // 1 degree latitude is ~ 111320 meters, since the distance between the horizonal lines is always the same
170            // 1 degree longitude is ~111320 meters at equator but gets shorter as we get closer to the poles.
171            // the "hack" with max 0.4 degrees latitude and 0.75 degrees longitude is only valid since we only service danish trains,
172            // in denmark 0.4dg latitude ~ 44km, 0.75dg longitude ~ 47km
173    
174          // 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)
175          // 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
176          // calculate the distance:          // calculate the distance:
177          //     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
178    
179          public List<StationBean> getByLocationWorker(double latitude, double longitude, boolean geolimit) throws SQLException {          public StationBean getByLocationWorker(double latitude, double longitude, boolean geolimit) throws SQLException {
180                                    
181                  String limitExpression = geolimit == true ? "AND abs(latitude-?)<0.5 AND abs(longitude-?)<0.9 " : "";                  String limitExpression = (geolimit == true) ? "AND abs(latitude-?)<0.4 AND abs(longitude-?)<0.75 " : "";
182                                    
183                  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, " +
184                          "earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " +                          "earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " +
# Line 127  public class StationDAO { Line 189  public class StationDAO {
189    
190    
191                                    
192                  List<StationBean> result;                  StationBean result;
193                  Connection conn = null;                  Connection conn = null;
194                  PreparedStatement stmt = null;                  PreparedStatement stmt = null;
195                  ResultSet res = null;                  ResultSet res = null;
# Line 154  public class StationDAO { Line 216  public class StationDAO {
216                  return result;                  return result;
217          }          }
218                    
219          public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException {          public StationBean getByLocation(double latitude, double longitude) throws SQLException {
220                  List<StationBean> result = getByLocationWorker(latitude, longitude, true);                  StationBean result = getByLocationWorker(latitude, longitude, true);
221                                    
222                  if (result.size() < LOCATION_LIMIT) { //failover                  if (result.entries.size() < LOCATION_LIMIT) { //failover
223                          logger.info("getByLocation failover: " +latitude + "," + longitude);                          logger.info("getByLocation failover: " +latitude + "," + longitude);
224                                                    
225                          result = getByLocationWorker(latitude, longitude, false);                          result = getByLocationWorker(latitude, longitude, false);
# Line 168  public class StationDAO { Line 230  public class StationDAO {
230                    
231                    
232    
233          public List<StationBean> getByList(String list) throws SQLException {          public StationBean getByList(String list) throws SQLException {
234                  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 " +
235                  "FROM trainstations " +                  "FROM trainstations " +
236                  "WHERE id IN " + list + " AND enabled = true " +                  "WHERE id IN " + list + " AND enabled = true " +
# Line 177  public class StationDAO { Line 239  public class StationDAO {
239                  Connection conn = null;                  Connection conn = null;
240                  Statement stmt = null;                  Statement stmt = null;
241                  ResultSet res = null;                  ResultSet res = null;
242                  List<StationBean> result;                  StationBean result;
243    
244                  try {                  try {
245                          conn = DBConnection.getConnection();                          conn = DBConnection.getConnection();
# Line 196  public class StationDAO { Line 258  public class StationDAO {
258                  return result;                  return result;
259    
260          }          }
261            @Deprecated
262          public static String getStationName(int stationID) {          public static String getStationName(int stationID) {
263                  String station = "";                  String station = "";
264    
# Line 218  public class StationDAO { Line 281  public class StationDAO {
281    
282                  return station;                  return station;
283          }          }
284            
285            public StationEntry getSimpleByName(String name) throws SQLException {
286                    String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " +
287                    "FROM trainstations " +
288                    "WHERE name = ?  AND enabled = true " +
289                    "LIMIT 1 ";
290    
291                    StationBean result;
292                    Connection conn = null;
293                    PreparedStatement stmt = null;
294                    ResultSet res = null;
295                    try {
296                            conn = DBConnection.getConnection();
297                            stmt = conn.prepareStatement(SQL);
298    
299                            stmt.setString(1, name );
300    
301                            res = stmt.executeQuery();
302                            result = convertResultset(res);
303    
304                    } finally {
305                            if (res != null)
306                                    res.close();
307                            if (stmt != null)
308                                    stmt.close();
309                            if (conn!= null)
310                                    conn.close();
311                    }
312    
313                    if (result.entries.size() == 1) {
314                            return result.entries.get(0);
315                    } else {
316                            return null;
317                    }
318            }
319    
320            @Deprecated
321          public int getIdByName(String name) throws SQLException {          public int getIdByName(String name) throws SQLException {
322                  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 " +
323                  "FROM trainstations " +                  "FROM trainstations " +
324                  "WHERE name = ?  AND enabled = true " +                  "WHERE name = ?  AND enabled = true " +
325                  "LIMIT 1 ";                  "LIMIT 1 ";
326    
327                  List<StationBean> result;                  StationBean result;
328                  Connection conn = null;                  Connection conn = null;
329                  PreparedStatement stmt = null;                  PreparedStatement stmt = null;
330                  ResultSet res = null;                  ResultSet res = null;
# Line 247  public class StationDAO { Line 346  public class StationDAO {
346                                  conn.close();                                  conn.close();
347                  }                  }
348    
349                  if (result.size() == 1) {                  if (result.entries.size() == 1) {
350                          return result.get(0).getId();                          return result.entries.get(0).getId();
351                  } else {                  } else {
352                          return -1;                          return -1;
353                  }                  }

Legend:
Removed from v.894  
changed lines
  Added in v.1417

  ViewVC Help
Powered by ViewVC 1.1.20