/[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 928 by torben, Sun Jun 27 16:55:36 2010 UTC revision 1082 by torben, Tue Sep 21 05:55:31 2010 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfoservice;  package dk.thoerup.traininfoservice;
2    
3    import java.sql.Array;
4  import java.sql.Connection;  import java.sql.Connection;
5  import java.sql.PreparedStatement;  import java.sql.PreparedStatement;
6  import java.sql.ResultSet;  import java.sql.ResultSet;
7  import java.sql.SQLException;  import java.sql.SQLException;
8  import java.sql.Statement;  import java.sql.Statement;
 import java.util.ArrayList;  
 import java.util.List;  
9  import java.util.logging.Logger;  import java.util.logging.Logger;
10    
11    import dk.thoerup.android.traininfo.common.StationBean;
12    import dk.thoerup.android.traininfo.common.StationBean.StationEntry;
13    
14  public class StationDAO {  public class StationDAO {
15          final static int LOCATION_LIMIT = 5;          final static int LOCATION_LIMIT = 8;
16          static final Logger logger = Logger.getLogger(StationDAO.class.getName());          static final Logger logger = Logger.getLogger(StationDAO.class.getName());
17                    
18                    
19          private StationBean convertSingleRow(ResultSet res) throws SQLException {          private StationEntry convertSingleRow(ResultSet res) throws SQLException {
20                  StationBean station = new StationBean();                  StationEntry station = new StationEntry();
21    
22                  station.setId( res.getInt(1) );                  station.setId( res.getInt(1) );
23                  station.setName( res.getString(2) );                  station.setName( res.getString(2) );
# Line 26  public class StationDAO { Line 28  public class StationDAO {
28                  station.setMetro( res.getString(7) );                  station.setMetro( res.getString(7) );
29                  station.setAddress( res.getString(8) );                  station.setAddress( res.getString(8) );
30                  station.setCalcdist( (int)res.getDouble(9) );                  station.setCalcdist( (int)res.getDouble(9) );
31                    
32                    station.setIsRegional( station.getRegional() != null );
33                    station.setIsStrain( station.getStrain() != null );
34                    station.setIsMetro( station.getMetro() != null );
35    
36                  return station;                  return station;
37          }          }
38    
39          private List<StationBean> convertResultset(ResultSet res) throws SQLException {          private StationBean convertResultset(ResultSet res) throws SQLException {
40                  List<StationBean> stations = new ArrayList<StationBean>();                  StationBean stations = new StationBean();
41                  while (res.next()) {                  while (res.next()) {
42                          stations.add( convertSingleRow(res) );                          stations.entries.add( convertSingleRow(res) );
43                  }                  }
44                  return stations;                  return stations;
45    
46          }          }
47    
48    
49          public StationBean getById(int id) throws SQLException {          public StationEntry getById(int id) throws SQLException {
50                  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 " +
51                  "FROM trainstations WHERE id=" + id + " AND enabled=true";                  "FROM trainstations WHERE id=" + id + " AND enabled=true";
52    
53                  Connection conn = null;                  Connection conn = null;
54                  Statement stmt = null;                  Statement stmt = null;
55                  ResultSet res = null;                  ResultSet res = null;
56                  StationBean result;                  StationEntry result;
57    
58                  try {                  try {
59                          conn = DBConnection.getConnection();                          conn = DBConnection.getConnection();
# Line 67  public class StationDAO { Line 73  public class StationDAO {
73    
74                  return result;                  return result;
75          }          }
76            
77            public StationBean dumpAll() throws SQLException {
78                    
79                    String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0,aliases " +
80                                    "FROM trainstations WHERE enabled = true ORDER BY id";
81                    
82                    Connection conn = null;
83                    Statement stmt = null;
84                    ResultSet res = null;          
85    
86                    
87                    try {
88                            conn = DBConnection.getConnection();
89    
90                            stmt = conn.createStatement();
91                            res = stmt.executeQuery(SQL);          
92                                                    
93                            // Does mostly the same as convertResultset()
94                            StationBean stations = new StationBean();
95                            while (res.next()) {
96                                    StationEntry entry = convertSingleRow(res);
97                                    
98                                    Array arr = res.getArray(10);
99                                    if (arr != null) {
100                                            String[] aliases = (String[]) arr.getArray();
101                                            entry.setAliases(aliases);
102                                    }
103                                    
104                                    stations.entries.add( entry );
105                                    
106                            }
107                            return stations;
108                            
109    
110                    } finally {
111                            if (res != null)
112                                    res.close();
113                            if (stmt != null)
114                                    stmt.close();
115                            if (conn != null)
116                                    conn.close();
117                    }
118                    
119            }
120    
121          /*          /*
122           * this code requires theses statements are run on database in order to do ILIKE searches against aliases (which is defines as array of varchar(64) )           * this code requires theses statements are run on database in order to do ILIKE searches against aliases (which is defines as array of varchar(64) )
# Line 74  public class StationDAO { Line 124  public class StationDAO {
124           *     'select $2 ilike $1' language sql strict immutable;           *     'select $2 ilike $1' language sql strict immutable;
125           *     create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~);           *     create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~);
126           */           */
127          public List<StationBean> getByName(String name) throws SQLException {          public StationBean getByName(String name) throws SQLException {
128                  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 " +
129                  "FROM trainstations " +                  "FROM trainstations " +
130                  "WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " +                  "WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " +
131                  "ORDER BY name ";                  "ORDER BY name ";
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 114  public class StationDAO { Line 164  public class StationDAO {
164          // calculate the distance:          // calculate the distance:
165          //     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
166    
167          public List<StationBean> getByLocationWorker(double latitude, double longitude, boolean geolimit) throws SQLException {          public StationBean getByLocationWorker(double latitude, double longitude, boolean geolimit) throws SQLException {
168                                    
169                  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 " : "";
170                                    
171                  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, " +
172                          "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 177  public class StationDAO {
177    
178    
179                                    
180                  List<StationBean> result;                  StationBean result;
181                  Connection conn = null;                  Connection conn = null;
182                  PreparedStatement stmt = null;                  PreparedStatement stmt = null;
183                  ResultSet res = null;                  ResultSet res = null;
# Line 154  public class StationDAO { Line 204  public class StationDAO {
204                  return result;                  return result;
205          }          }
206                    
207          public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException {          public StationBean getByLocation(double latitude, double longitude) throws SQLException {
208                  List<StationBean> result = getByLocationWorker(latitude, longitude, true);                  StationBean result = getByLocationWorker(latitude, longitude, true);
209                                    
210                  if (result.size() < LOCATION_LIMIT) { //failover                  if (result.entries.size() < LOCATION_LIMIT) { //failover
211                          logger.info("getByLocation failover: " +latitude + "," + longitude);                          logger.info("getByLocation failover: " +latitude + "," + longitude);
212                                                    
213                          result = getByLocationWorker(latitude, longitude, false);                          result = getByLocationWorker(latitude, longitude, false);
# Line 168  public class StationDAO { Line 218  public class StationDAO {
218                    
219                    
220    
221          public List<StationBean> getByList(String list) throws SQLException {          public StationBean getByList(String list) throws SQLException {
222                  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 " +
223                  "FROM trainstations " +                  "FROM trainstations " +
224                  "WHERE id IN " + list + " AND enabled = true " +                  "WHERE id IN " + list + " AND enabled = true " +
# Line 177  public class StationDAO { Line 227  public class StationDAO {
227                  Connection conn = null;                  Connection conn = null;
228                  Statement stmt = null;                  Statement stmt = null;
229                  ResultSet res = null;                  ResultSet res = null;
230                  List<StationBean> result;                  StationBean result;
231    
232                  try {                  try {
233                          conn = DBConnection.getConnection();                          conn = DBConnection.getConnection();
# Line 225  public class StationDAO { Line 275  public class StationDAO {
275                  "WHERE name = ?  AND enabled = true " +                  "WHERE name = ?  AND enabled = true " +
276                  "LIMIT 1 ";                  "LIMIT 1 ";
277    
278                  List<StationBean> result;                  StationBean result;
279                  Connection conn = null;                  Connection conn = null;
280                  PreparedStatement stmt = null;                  PreparedStatement stmt = null;
281                  ResultSet res = null;                  ResultSet res = null;
# Line 247  public class StationDAO { Line 297  public class StationDAO {
297                                  conn.close();                                  conn.close();
298                  }                  }
299    
300                  if (result.size() == 1) {                  if (result.entries.size() == 1) {
301                          return result.get(0).getId();                          return result.entries.get(0).getId();
302                  } else {                  } else {
303                          return -1;                          return -1;
304                  }                  }

Legend:
Removed from v.928  
changed lines
  Added in v.1082

  ViewVC Help
Powered by ViewVC 1.1.20