/[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 836 by torben, Fri Jun 11 17:12:29 2010 UTC revision 1060 by torben, Thu Sep 16 13:32:10 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;
8  import java.util.ArrayList;  import java.util.logging.Logger;
9  import java.util.List;  
10    import dk.thoerup.traininfoservice.StationBean.StationEntry;
11    
12  public class StationDAO {  public class StationDAO {
13          private StationBean convertSingleRow(ResultSet res) throws SQLException {          final static int LOCATION_LIMIT = 8;
14                  StationBean station = new StationBean();          static final Logger logger = Logger.getLogger(StationDAO.class.getName());
15            
16            
17            private StationEntry convertSingleRow(ResultSet res) throws SQLException {
18                    StationEntry station = new StationEntry();
19    
20                  station.setId( res.getInt(1) );                  station.setId( res.getInt(1) );
21                  station.setName( res.getString(2) );                  station.setName( res.getString(2) );
# Line 21  public class StationDAO { Line 26  public class StationDAO {
26                  station.setMetro( res.getString(7) );                  station.setMetro( res.getString(7) );
27                  station.setAddress( res.getString(8) );                  station.setAddress( res.getString(8) );
28                  station.setCalcdist( (int)res.getDouble(9) );                  station.setCalcdist( (int)res.getDouble(9) );
29                    
30                    station.setIsRegional( station.getRegional() != null );
31                    station.setIsStrain( station.getStrain() != null );
32                    station.setIsMetro( station.getMetro() != null );
33    
34                  return station;                  return station;
35          }          }
36    
37          private List<StationBean> convertResultset(ResultSet res) throws SQLException {          private StationBean convertResultset(ResultSet res) throws SQLException {
38                  List<StationBean> stations = new ArrayList<StationBean>();                  StationBean stations = new StationBean();
39                  while (res.next()) {                  while (res.next()) {
40                          stations.add( convertSingleRow(res) );                          stations.entries.add( convertSingleRow(res) );
41                  }                  }
42                  return stations;                  return stations;
43    
44          }          }
45    
46    
47          public StationBean getById(int id) throws SQLException {          public StationEntry getById(int id) throws SQLException {
48                  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 " +
49                  "FROM trainstations WHERE id=" + id + " AND enabled=true";                  "FROM trainstations WHERE id=" + id + " AND enabled=true";
50    
51                  Connection conn = null;                  Connection conn = null;
52                  Statement stmt = null;                  Statement stmt = null;
53                  ResultSet res = null;                  ResultSet res = null;
54                  StationBean result;                  StationEntry result;
55    
56                  try {                  try {
57                          conn = DBConnection.getConnection();                          conn = DBConnection.getConnection();
# Line 69  public class StationDAO { Line 78  public class StationDAO {
78           *     'select $2 ilike $1' language sql strict immutable;           *     'select $2 ilike $1' language sql strict immutable;
79           *     create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~);           *     create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~);
80           */           */
81          public List<StationBean> getByName(String name) throws SQLException {          public StationBean getByName(String name) throws SQLException {
82                  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 " +
83                  "FROM trainstations " +                  "FROM trainstations " +
84                  "WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " +                  "WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " +
85                  "ORDER BY name ";                  "ORDER BY name ";
86    
87    
88                  List<StationBean> result;                  StationBean result;
89                  Connection conn = null;                  Connection conn = null;
90                  PreparedStatement stmt = null;                  PreparedStatement stmt = null;
91                  ResultSet res = null;                  ResultSet res = null;
# Line 101  public class StationDAO { Line 110  public class StationDAO {
110                  return result;                  return result;
111          }          }
112    
113          //the "hack" with max 1.5 degrees latitude and 2.5 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,
114          // in denmark 1.5dg latitude ~ 165km, 2.5dg longitude ~ 155km          // in denmark 0.4dg latitude ~ 44km, 0.75dg longitude ~ 47km
115    
116          // 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)
117          // 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
118          // calculate the distance:          // calculate the distance:
119          //     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
120    
121          public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException {          public StationBean getByLocationWorker(double latitude, double longitude, boolean geolimit) throws SQLException {
122                  String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address," +                  
123                  "       earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " +                  String limitExpression = (geolimit == true) ? "AND abs(latitude-?)<0.4 AND abs(longitude-?)<0.75 " : "";
124                  "FROM trainstations " +                  
125                  "WHERE enabled = true AND abs(latitude-?)<1.5 AND abs(longitude-?)<2.5 " +                  String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, " +
126                  "ORDER BY calcdist ASC " +                          "earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " +
127                  "LIMIT 4 ";                          "FROM trainstations " +
128                  List<StationBean> result;                          "WHERE enabled = true " + limitExpression +
129                            "ORDER BY calcdist ASC " +
130                            "LIMIT " + LOCATION_LIMIT;
131    
132    
133                    
134                    StationBean result;
135                  Connection conn = null;                  Connection conn = null;
136                  PreparedStatement stmt = null;                  PreparedStatement stmt = null;
137                  ResultSet res = null;                  ResultSet res = null;
# Line 125  public class StationDAO { Line 140  public class StationDAO {
140                          stmt = conn.prepareStatement(SQL);                          stmt = conn.prepareStatement(SQL);
141                          stmt.setDouble(1, latitude);                          stmt.setDouble(1, latitude);
142                          stmt.setDouble(2, longitude);                          stmt.setDouble(2, longitude);
143                          stmt.setDouble(3, latitude);                          if (geolimit == true) {
144                          stmt.setDouble(4, longitude);                                                    stmt.setDouble(3, latitude);
145                                    stmt.setDouble(4, longitude);
146                            }
147                          res = stmt.executeQuery();                          res = stmt.executeQuery();
148                          result = convertResultset(res);                          result = convertResultset(res);
149                    
150                  } finally {                  } finally {
151                          if (res != null)                          if (res != null)
152                                  res.close();                                  res.close();
# Line 140  public class StationDAO { Line 157  public class StationDAO {
157                  }                  }
158                  return result;                  return result;
159          }          }
160            
161            public StationBean getByLocation(double latitude, double longitude) throws SQLException {
162                    StationBean result = getByLocationWorker(latitude, longitude, true);
163                    
164                    if (result.entries.size() < LOCATION_LIMIT) { //failover
165                            logger.info("getByLocation failover: " +latitude + "," + longitude);
166                            
167                            result = getByLocationWorker(latitude, longitude, false);
168                    }
169                    
170                    return result;
171            }
172            
173            
174    
175          public List<StationBean> getByList(String list) throws SQLException {          public StationBean getByList(String list) throws SQLException {
176                  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 " +
177                  "FROM trainstations " +                  "FROM trainstations " +
178                  "WHERE id IN " + list + " AND enabled = true " +                  "WHERE id IN " + list + " AND enabled = true " +
# Line 150  public class StationDAO { Line 181  public class StationDAO {
181                  Connection conn = null;                  Connection conn = null;
182                  Statement stmt = null;                  Statement stmt = null;
183                  ResultSet res = null;                  ResultSet res = null;
184                  List<StationBean> result;                  StationBean result;
185    
186                  try {                  try {
187                          conn = DBConnection.getConnection();                          conn = DBConnection.getConnection();
# Line 192  public class StationDAO { Line 223  public class StationDAO {
223                  return station;                  return station;
224          }          }
225    
226          public int getBySpecificName(String name) throws SQLException {          public int getIdByName(String name) throws SQLException {
227                  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 " +
228                  "FROM trainstations " +                  "FROM trainstations " +
229                  "WHERE name = ?  AND enabled = true " +                  "WHERE name = ?  AND enabled = true " +
230                  "LIMIT 1 ";                  "LIMIT 1 ";
231    
232                  System.out.println(" getBySpecificName() ");                  StationBean result;
   
                 List<StationBean> result;  
233                  Connection conn = null;                  Connection conn = null;
234                  PreparedStatement stmt = null;                  PreparedStatement stmt = null;
235                  ResultSet res = null;                  ResultSet res = null;
# Line 222  public class StationDAO { Line 251  public class StationDAO {
251                                  conn.close();                                  conn.close();
252                  }                  }
253    
254                  if (result.size() == 1) {                  if (result.entries.size() == 1) {
255                          return result.get(0).getId();                          return result.entries.get(0).getId();
256                  } else {                  } else {
257                          return -1;                          return -1;
258                  }                  }

Legend:
Removed from v.836  
changed lines
  Added in v.1060

  ViewVC Help
Powered by ViewVC 1.1.20