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

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

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

android/TrainInfoService/src/dk/thoerup/traininfoservice/StationDAO.java revision 842 by torben, Fri Jun 11 20:50:40 2010 UTC android/TrainInfoServiceGoogle/src/dk/thoerup/traininfoservice/StationDAO.java revision 1093 by torben, Tue Sep 21 20:10:46 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 javax.jdo.PersistenceManager;
11    
12    import dk.thoerup.android.traininfo.common.StationBean;
13    import dk.thoerup.android.traininfo.common.StationBean.StationEntry;
14    import dk.thoerup.traininfoservice.jdo.JdoStationBean;
15    import dk.thoerup.traininfoservice.jdo.PMF;
16    
17  public class StationDAO {  public class StationDAO {
18          final static int LOCATION_LIMIT = 5;          final static int LOCATION_LIMIT = 8;
19            static final Logger logger = Logger.getLogger(StationDAO.class.getName());
20                    
21          private StationBean convertSingleRow(ResultSet res) throws SQLException {          
22                  StationBean station = new StationBean();          private StationEntry convertSingleRow(ResultSet res) throws SQLException {
23                    StationEntry station = new StationEntry();
24    
25                  station.setId( res.getInt(1) );                  station.setId( res.getInt(1) );
26                  station.setName( res.getString(2) );                  station.setName( res.getString(2) );
# Line 23  public class StationDAO { Line 31  public class StationDAO {
31                  station.setMetro( res.getString(7) );                  station.setMetro( res.getString(7) );
32                  station.setAddress( res.getString(8) );                  station.setAddress( res.getString(8) );
33                  station.setCalcdist( (int)res.getDouble(9) );                  station.setCalcdist( (int)res.getDouble(9) );
34                    
35                    station.setIsRegional( station.getRegional() != null );
36                    station.setIsStrain( station.getStrain() != null );
37                    station.setIsMetro( station.getMetro() != null );
38    
39                  return station;                  return station;
40          }          }
41    
42          private List<StationBean> convertResultset(ResultSet res) throws SQLException {          private StationBean convertResultset(ResultSet res) throws SQLException {
43                  List<StationBean> stations = new ArrayList<StationBean>();                  StationBean stations = new StationBean();
44                  while (res.next()) {                  while (res.next()) {
45                          stations.add( convertSingleRow(res) );                          stations.entries.add( convertSingleRow(res) );
46                  }                  }
47                  return stations;                  return stations;
48    
49          }          }
50    
51    
52          public StationBean getById(int id) throws SQLException {          public JdoStationBean getById(int id) throws SQLException {
                 String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0 " +  
                 "FROM trainstations WHERE id=" + id + " AND enabled=true";  
   
                 Connection conn = null;  
                 Statement stmt = null;  
                 ResultSet res = null;  
                 StationBean result;  
53    
54                    PersistenceManager pm = null;
55                    
56                  try {                  try {
57                          conn = DBConnection.getConnection();                          pm = PMF.get().getPersistenceManager();
58                            return pm.getObjectById(JdoStationBean.class, new Integer(id) );
                         stmt = conn.createStatement();  
                         res = stmt.executeQuery(SQL);            
                         res.next();  
                         result = convertSingleRow(res);  
59                  } finally {                  } finally {
60                          if (res != null)                          pm.close();
                                 res.close();  
                         if (stmt != null)  
                                 stmt.close();  
                         if (conn != null)  
                                 conn.close();  
61                  }                  }
   
                 return result;  
62          }          }
63    
64          /*          /*
# Line 71  public class StationDAO { Line 67  public class StationDAO {
67           *     'select $2 ilike $1' language sql strict immutable;           *     'select $2 ilike $1' language sql strict immutable;
68           *     create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~);           *     create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~);
69           */           */
70          public List<StationBean> getByName(String name) throws SQLException {          public StationBean getByName(String name) throws SQLException {
71                  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 " +
72                  "FROM trainstations " +                  "FROM trainstations " +
73                  "WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " +                  "WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " +
74                  "ORDER BY name ";                  "ORDER BY name ";
75    
76    
77                  List<StationBean> result;                  StationBean result;
78                  Connection conn = null;                  Connection conn = null;
79                  PreparedStatement stmt = null;                  PreparedStatement stmt = null;
80                  ResultSet res = null;                  ResultSet res = null;
# Line 103  public class StationDAO { Line 99  public class StationDAO {
99                  return result;                  return result;
100          }          }
101    
102          //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,
103          // in denmark 1.5dg latitude ~ 165km, 2.5dg longitude ~ 155km          // in denmark 0.4dg latitude ~ 44km, 0.75dg longitude ~ 47km
104    
105          // 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)
106          // 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
107          // calculate the distance:          // calculate the distance:
108          //     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
109    
110          public List<StationBean> getByLocationWorker(double latitude, double longitude, boolean geolimit) throws SQLException {          public StationBean getByLocationWorker(double latitude, double longitude, boolean geolimit) throws SQLException {
111                                    
112                  String limitExpression = geolimit == true ? "AND abs(latitude-?)<1.5 AND abs(longitude-?)<2.5 " : "";                  String limitExpression = (geolimit == true) ? "AND abs(latitude-?)<0.4 AND abs(longitude-?)<0.75 " : "";
113                                    
114                  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, " +
115                          "earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " +                          "earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " +
# Line 121  public class StationDAO { Line 117  public class StationDAO {
117                          "WHERE enabled = true " + limitExpression +                          "WHERE enabled = true " + limitExpression +
118                          "ORDER BY calcdist ASC " +                          "ORDER BY calcdist ASC " +
119                          "LIMIT " + LOCATION_LIMIT;                          "LIMIT " + LOCATION_LIMIT;
120    
121    
122                                    
123                  List<StationBean> result;                  StationBean result;
124                  Connection conn = null;                  Connection conn = null;
125                  PreparedStatement stmt = null;                  PreparedStatement stmt = null;
126                  ResultSet res = null;                  ResultSet res = null;
# Line 137  public class StationDAO { Line 135  public class StationDAO {
135                          }                          }
136                          res = stmt.executeQuery();                          res = stmt.executeQuery();
137                          result = convertResultset(res);                          result = convertResultset(res);
138                    
139                  } finally {                  } finally {
140                          if (res != null)                          if (res != null)
141                                  res.close();                                  res.close();
# Line 149  public class StationDAO { Line 147  public class StationDAO {
147                  return result;                  return result;
148          }          }
149                    
150          public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException {          public StationBean getByLocation(double latitude, double longitude) throws SQLException {
151                  List<StationBean> result = getByLocationWorker(latitude, longitude, true);                  StationBean result = getByLocationWorker(latitude, longitude, true);
152                                    
153                  if (result.size() < LOCATION_LIMIT) { //failover                  if (result.entries.size() < LOCATION_LIMIT) { //failover
154                            logger.info("getByLocation failover: " +latitude + "," + longitude);
155                            
156                          result = getByLocationWorker(latitude, longitude, false);                          result = getByLocationWorker(latitude, longitude, false);
157                  }                  }
158                                    
# Line 161  public class StationDAO { Line 161  public class StationDAO {
161                    
162                    
163    
164          public List<StationBean> getByList(String list) throws SQLException {          public StationBean getByList(String list) throws SQLException {
165                  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 " +
166                  "FROM trainstations " +                  "FROM trainstations " +
167                  "WHERE id IN " + list + " AND enabled = true " +                  "WHERE id IN " + list + " AND enabled = true " +
# Line 170  public class StationDAO { Line 170  public class StationDAO {
170                  Connection conn = null;                  Connection conn = null;
171                  Statement stmt = null;                  Statement stmt = null;
172                  ResultSet res = null;                  ResultSet res = null;
173                  List<StationBean> result;                  StationBean result;
174    
175                  try {                  try {
176                          conn = DBConnection.getConnection();                          conn = DBConnection.getConnection();
# Line 218  public class StationDAO { Line 218  public class StationDAO {
218                  "WHERE name = ?  AND enabled = true " +                  "WHERE name = ?  AND enabled = true " +
219                  "LIMIT 1 ";                  "LIMIT 1 ";
220    
221                  List<StationBean> result;                  StationBean result;
222                  Connection conn = null;                  Connection conn = null;
223                  PreparedStatement stmt = null;                  PreparedStatement stmt = null;
224                  ResultSet res = null;                  ResultSet res = null;
# Line 240  public class StationDAO { Line 240  public class StationDAO {
240                                  conn.close();                                  conn.close();
241                  }                  }
242    
243                  if (result.size() == 1) {                  if (result.entries.size() == 1) {
244                          return result.get(0).getId();                          return result.entries.get(0).getId();
245                  } else {                  } else {
246                          return -1;                          return -1;
247                  }                  }

Legend:
Removed from v.842  
changed lines
  Added in v.1093

  ViewVC Help
Powered by ViewVC 1.1.20