/[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 1059 by torben, Sat Jul 17 06:01:09 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;
 import java.util.ArrayList;  
 import java.util.List;  
8  import java.util.logging.Logger;  import java.util.logging.Logger;
9    
10    import dk.thoerup.traininfoservice.StationBean.StationEntry;
11    
12  public class StationDAO {  public class StationDAO {
13          final static int LOCATION_LIMIT = 8;          final static int LOCATION_LIMIT = 8;
14          static final Logger logger = Logger.getLogger(StationDAO.class.getName());          static final Logger logger = Logger.getLogger(StationDAO.class.getName());
15                    
16                    
17          private StationBean convertSingleRow(ResultSet res) throws SQLException {          private StationEntry convertSingleRow(ResultSet res) throws SQLException {
18                  StationBean station = new StationBean();                  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 26  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 74  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 114  public class StationDAO { Line 118  public class StationDAO {
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> getByLocationWorker(double latitude, double longitude, boolean geolimit) throws SQLException {          public StationBean getByLocationWorker(double latitude, double longitude, boolean geolimit) throws SQLException {
122                                    
123                  String limitExpression = (geolimit == true) ? "AND abs(latitude-?)<0.4 AND abs(longitude-?)<0.75 " : "";                  String limitExpression = (geolimit == true) ? "AND abs(latitude-?)<0.4 AND abs(longitude-?)<0.75 " : "";
124                                    
# Line 127  public class StationDAO { Line 131  public class StationDAO {
131    
132    
133                                    
134                  List<StationBean> result;                  StationBean result;
135                  Connection conn = null;                  Connection conn = null;
136                  PreparedStatement stmt = null;                  PreparedStatement stmt = null;
137                  ResultSet res = null;                  ResultSet res = null;
# Line 154  public class StationDAO { Line 158  public class StationDAO {
158                  return result;                  return result;
159          }          }
160                    
161          public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException {          public StationBean getByLocation(double latitude, double longitude) throws SQLException {
162                  List<StationBean> result = getByLocationWorker(latitude, longitude, true);                  StationBean result = getByLocationWorker(latitude, longitude, true);
163                                    
164                  if (result.size() < LOCATION_LIMIT) { //failover                  if (result.entries.size() < LOCATION_LIMIT) { //failover
165                          logger.info("getByLocation failover: " +latitude + "," + longitude);                          logger.info("getByLocation failover: " +latitude + "," + longitude);
166                                                    
167                          result = getByLocationWorker(latitude, longitude, false);                          result = getByLocationWorker(latitude, longitude, false);
# Line 168  public class StationDAO { Line 172  public class StationDAO {
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 177  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 225  public class StationDAO { Line 229  public class StationDAO {
229                  "WHERE name = ?  AND enabled = true " +                  "WHERE name = ?  AND enabled = true " +
230                  "LIMIT 1 ";                  "LIMIT 1 ";
231    
232                  List<StationBean> result;                  StationBean result;
233                  Connection conn = null;                  Connection conn = null;
234                  PreparedStatement stmt = null;                  PreparedStatement stmt = null;
235                  ResultSet res = null;                  ResultSet res = null;
# Line 247  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.1059  
changed lines
  Added in v.1060

  ViewVC Help
Powered by ViewVC 1.1.20