/[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 894 by torben, Thu Jun 24 17:00:39 2010 UTC
# Line 8  import java.sql.Statement; Line 8  import java.sql.Statement;
8  import java.util.ArrayList;  import java.util.ArrayList;
9  import java.util.List;  import java.util.List;
10    
11    import com.sun.istack.logging.Logger;
12    
13  public class StationDAO {  public class StationDAO {
14            final static int LOCATION_LIMIT = 5;
15            static final Logger logger = Logger.getLogger(StationDAO.class);
16            
17          private StationBean convertSingleRow(ResultSet res) throws SQLException {          private StationBean convertSingleRow(ResultSet res) throws SQLException {
18                  StationBean station = new StationBean();                  StationBean station = new StationBean();
19    
# Line 101  public class StationDAO { Line 106  public class StationDAO {
106                  return result;                  return result;
107          }          }
108    
109          //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.5 degrees latitude and 0.9 degrees longitude is only valid since we only service danish trains,
110          // in denmark 1.5dg latitude ~ 165km, 2.5dg longitude ~ 155km          // in denmark 0.5dg latitude ~ 55km, 0.9dg longitude ~ 57km
111    
112          // 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)
113          // 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
114          // calculate the distance:          // calculate the distance:
115          //     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
116    
117          public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException {          public List<StationBean> getByLocationWorker(double latitude, double longitude, boolean geolimit) throws SQLException {
118                  String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address," +                  
119                  "       earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " +                  String limitExpression = geolimit == true ? "AND abs(latitude-?)<0.5 AND abs(longitude-?)<0.9 " : "";
120                  "FROM trainstations " +                  
121                  "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, " +
122                  "ORDER BY calcdist ASC " +                          "earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " +
123                  "LIMIT 4 ";                          "FROM trainstations " +
124                            "WHERE enabled = true " + limitExpression +
125                            "ORDER BY calcdist ASC " +
126                            "LIMIT " + LOCATION_LIMIT;
127    
128    
129                    
130                  List<StationBean> result;                  List<StationBean> result;
131                  Connection conn = null;                  Connection conn = null;
132                  PreparedStatement stmt = null;                  PreparedStatement stmt = null;
# Line 125  public class StationDAO { Line 136  public class StationDAO {
136                          stmt = conn.prepareStatement(SQL);                          stmt = conn.prepareStatement(SQL);
137                          stmt.setDouble(1, latitude);                          stmt.setDouble(1, latitude);
138                          stmt.setDouble(2, longitude);                          stmt.setDouble(2, longitude);
139                          stmt.setDouble(3, latitude);                          if (geolimit == true) {
140                          stmt.setDouble(4, longitude);                                                    stmt.setDouble(3, latitude);
141                                    stmt.setDouble(4, longitude);
142                            }
143                          res = stmt.executeQuery();                          res = stmt.executeQuery();
144                          result = convertResultset(res);                          result = convertResultset(res);
145                    
146                  } finally {                  } finally {
147                          if (res != null)                          if (res != null)
148                                  res.close();                                  res.close();
# Line 140  public class StationDAO { Line 153  public class StationDAO {
153                  }                  }
154                  return result;                  return result;
155          }          }
156            
157            public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException {
158                    List<StationBean> result = getByLocationWorker(latitude, longitude, true);
159                    
160                    if (result.size() < LOCATION_LIMIT) { //failover
161                            logger.info("getByLocation failover: " +latitude + "," + longitude);
162                            
163                            result = getByLocationWorker(latitude, longitude, false);
164                    }
165                    
166                    return result;
167            }
168            
169            
170    
171          public List<StationBean> getByList(String list) throws SQLException {          public List<StationBean> getByList(String list) throws SQLException {
172                  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 " +
# Line 192  public class StationDAO { Line 219  public class StationDAO {
219                  return station;                  return station;
220          }          }
221    
222          public int getBySpecificName(String name) throws SQLException {          public int getIdByName(String name) throws SQLException {
223                  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 " +
224                  "FROM trainstations " +                  "FROM trainstations " +
225                  "WHERE name = ?  AND enabled = true " +                  "WHERE name = ?  AND enabled = true " +
226                  "LIMIT 1 ";                  "LIMIT 1 ";
227    
                 System.out.println(" getBySpecificName() ");  
   
228                  List<StationBean> result;                  List<StationBean> result;
229                  Connection conn = null;                  Connection conn = null;
230                  PreparedStatement stmt = null;                  PreparedStatement stmt = null;

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

  ViewVC Help
Powered by ViewVC 1.1.20