/[projects]/android/TrainInfoService/src/dk/thoerup/traininfoservice/StationDAO.java
ViewVC logotype

Diff of /android/TrainInfoService/src/dk/thoerup/traininfoservice/StationDAO.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 895 by torben, Thu Jun 24 17:17:45 2010 UTC revision 1061 by torben, Thu Sep 16 14:04:28 2010 UTC
# Line 5  import java.sql.PreparedStatement; Line 5  import java.sql.PreparedStatement;
5  import java.sql.ResultSet;  import java.sql.ResultSet;
6  import java.sql.SQLException;  import java.sql.SQLException;
7  import java.sql.Statement;  import java.sql.Statement;
 import java.util.ArrayList;  
 import java.util.List;  
8  import java.util.logging.Logger;  import java.util.logging.Logger;
9    
10    import dk.thoerup.android.traininfo.common.StationBean;
11    import dk.thoerup.android.traininfo.common.StationBean.StationEntry;
12    
13  public class StationDAO {  public class StationDAO {
14          final static int LOCATION_LIMIT = 5;          final static int LOCATION_LIMIT = 8;
15          static final Logger logger = Logger.getLogger(StationDAO.class.getName());          static final Logger logger = Logger.getLogger(StationDAO.class.getName());
16                    
17                    
18          private StationBean convertSingleRow(ResultSet res) throws SQLException {          private StationEntry convertSingleRow(ResultSet res) throws SQLException {
19                  StationBean station = new StationBean();                  StationEntry station = new StationEntry();
20    
21                  station.setId( res.getInt(1) );                  station.setId( res.getInt(1) );
22                  station.setName( res.getString(2) );                  station.setName( res.getString(2) );
# Line 26  public class StationDAO { Line 27  public class StationDAO {
27                  station.setMetro( res.getString(7) );                  station.setMetro( res.getString(7) );
28                  station.setAddress( res.getString(8) );                  station.setAddress( res.getString(8) );
29                  station.setCalcdist( (int)res.getDouble(9) );                  station.setCalcdist( (int)res.getDouble(9) );
30                    
31                    station.setIsRegional( station.getRegional() != null );
32                    station.setIsStrain( station.getStrain() != null );
33                    station.setIsMetro( station.getMetro() != null );
34    
35                  return station;                  return station;
36          }          }
37    
38          private List<StationBean> convertResultset(ResultSet res) throws SQLException {          private StationBean convertResultset(ResultSet res) throws SQLException {
39                  List<StationBean> stations = new ArrayList<StationBean>();                  StationBean stations = new StationBean();
40                  while (res.next()) {                  while (res.next()) {
41                          stations.add( convertSingleRow(res) );                          stations.entries.add( convertSingleRow(res) );
42                  }                  }
43                  return stations;                  return stations;
44    
45          }          }
46    
47    
48          public StationBean getById(int id) throws SQLException {          public StationEntry getById(int id) throws SQLException {
49                  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 " +
50                  "FROM trainstations WHERE id=" + id + " AND enabled=true";                  "FROM trainstations WHERE id=" + id + " AND enabled=true";
51    
52                  Connection conn = null;                  Connection conn = null;
53                  Statement stmt = null;                  Statement stmt = null;
54                  ResultSet res = null;                  ResultSet res = null;
55                  StationBean result;                  StationEntry result;
56    
57                  try {                  try {
58                          conn = DBConnection.getConnection();                          conn = DBConnection.getConnection();
# Line 74  public class StationDAO { Line 79  public class StationDAO {
79           *     'select $2 ilike $1' language sql strict immutable;           *     'select $2 ilike $1' language sql strict immutable;
80           *     create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~);           *     create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~);
81           */           */
82          public List<StationBean> getByName(String name) throws SQLException {          public StationBean getByName(String name) throws SQLException {
83                  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 " +
84                  "FROM trainstations " +                  "FROM trainstations " +
85                  "WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " +                  "WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " +
86                  "ORDER BY name ";                  "ORDER BY name ";
87    
88    
89                  List<StationBean> result;                  StationBean result;
90                  Connection conn = null;                  Connection conn = null;
91                  PreparedStatement stmt = null;                  PreparedStatement stmt = null;
92                  ResultSet res = null;                  ResultSet res = null;
# Line 106  public class StationDAO { Line 111  public class StationDAO {
111                  return result;                  return result;
112          }          }
113    
114          //the "hack" with max 0.5 degrees latitude and 0.9 degrees longitude is only valid since we only service danish trains,          //the "hack" with max 0.4 degrees latitude and 0.75 degrees longitude is only valid since we only service danish trains,
115          // in denmark 0.5dg latitude ~ 55km, 0.9dg longitude ~ 57km          // in denmark 0.4dg latitude ~ 44km, 0.75dg longitude ~ 47km
116    
117          // 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)
118          // 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
119          // calculate the distance:          // calculate the distance:
120          //     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
121    
122          public List<StationBean> getByLocationWorker(double latitude, double longitude, boolean geolimit) throws SQLException {          public StationBean getByLocationWorker(double latitude, double longitude, boolean geolimit) throws SQLException {
123                                    
124                  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 " : "";
125                                    
126                  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, " +
127                          "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 132  public class StationDAO {
132    
133    
134                                    
135                  List<StationBean> result;                  StationBean result;
136                  Connection conn = null;                  Connection conn = null;
137                  PreparedStatement stmt = null;                  PreparedStatement stmt = null;
138                  ResultSet res = null;                  ResultSet res = null;
# Line 154  public class StationDAO { Line 159  public class StationDAO {
159                  return result;                  return result;
160          }          }
161                    
162          public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException {          public StationBean getByLocation(double latitude, double longitude) throws SQLException {
163                  List<StationBean> result = getByLocationWorker(latitude, longitude, true);                  StationBean result = getByLocationWorker(latitude, longitude, true);
164                                    
165                  if (result.size() < LOCATION_LIMIT) { //failover                  if (result.entries.size() < LOCATION_LIMIT) { //failover
166                          logger.info("getByLocation failover: " +latitude + "," + longitude);                          logger.info("getByLocation failover: " +latitude + "," + longitude);
167                                                    
168                          result = getByLocationWorker(latitude, longitude, false);                          result = getByLocationWorker(latitude, longitude, false);
# Line 168  public class StationDAO { Line 173  public class StationDAO {
173                    
174                    
175    
176          public List<StationBean> getByList(String list) throws SQLException {          public StationBean getByList(String list) throws SQLException {
177                  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 " +
178                  "FROM trainstations " +                  "FROM trainstations " +
179                  "WHERE id IN " + list + " AND enabled = true " +                  "WHERE id IN " + list + " AND enabled = true " +
# Line 177  public class StationDAO { Line 182  public class StationDAO {
182                  Connection conn = null;                  Connection conn = null;
183                  Statement stmt = null;                  Statement stmt = null;
184                  ResultSet res = null;                  ResultSet res = null;
185                  List<StationBean> result;                  StationBean result;
186    
187                  try {                  try {
188                          conn = DBConnection.getConnection();                          conn = DBConnection.getConnection();
# Line 225  public class StationDAO { Line 230  public class StationDAO {
230                  "WHERE name = ?  AND enabled = true " +                  "WHERE name = ?  AND enabled = true " +
231                  "LIMIT 1 ";                  "LIMIT 1 ";
232    
233                  List<StationBean> result;                  StationBean result;
234                  Connection conn = null;                  Connection conn = null;
235                  PreparedStatement stmt = null;                  PreparedStatement stmt = null;
236                  ResultSet res = null;                  ResultSet res = null;
# Line 247  public class StationDAO { Line 252  public class StationDAO {
252                                  conn.close();                                  conn.close();
253                  }                  }
254    
255                  if (result.size() == 1) {                  if (result.entries.size() == 1) {
256                          return result.get(0).getId();                          return result.entries.get(0).getId();
257                  } else {                  } else {
258                          return -1;                          return -1;
259                  }                  }

Legend:
Removed from v.895  
changed lines
  Added in v.1061

  ViewVC Help
Powered by ViewVC 1.1.20