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

Annotation of /android/TrainInfoService/src/dk/thoerup/traininfoservice/StationDAO.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 739 - (hide annotations) (download)
Wed May 19 09:53:25 2010 UTC (14 years ago) by torben
File size: 5351 byte(s)
narrow longitude search area
1 torben 588 package dk.thoerup.traininfoservice;
2    
3     import java.sql.Connection;
4     import java.sql.PreparedStatement;
5     import java.sql.ResultSet;
6     import java.sql.SQLException;
7     import java.sql.Statement;
8     import java.util.ArrayList;
9     import java.util.List;
10    
11     public class StationDAO {
12     private StationBean convertSingleRow(ResultSet res) throws SQLException {
13     StationBean station = new StationBean();
14    
15     station.setId( res.getInt(1) );
16     station.setName( res.getString(2) );
17     station.setLatitude( res.getDouble(3) );
18     station.setLongitude( res.getDouble(4) );
19     station.setRegional( res.getString(5) );
20     station.setStrain( res.getString(6) );
21     station.setMetro( res.getString(7) );
22     station.setAddress( res.getString(8) );
23     station.setCalcdist( (int)res.getDouble(9) );
24    
25     return station;
26     }
27    
28     private List<StationBean> convertResultset(ResultSet res) throws SQLException {
29     List<StationBean> stations = new ArrayList<StationBean>();
30     while (res.next()) {
31     stations.add( convertSingleRow(res) );
32     }
33     return stations;
34    
35     }
36    
37    
38     public StationBean getById(int id) throws SQLException {
39     String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0 " +
40     "FROM trainstations WHERE id=" + id + " AND enabled=true";
41    
42     Connection conn = null;
43 torben 589 Statement stmt = null;
44     ResultSet res = null;
45 torben 588 StationBean result;
46    
47     try {
48     conn = DBConnection.getConnection();
49    
50 torben 589 stmt = conn.createStatement();
51     res = stmt.executeQuery(SQL);
52 torben 588 res.next();
53     result = convertSingleRow(res);
54     } finally {
55 torben 589 if (res != null)
56     res.close();
57     if (stmt != null)
58     stmt.close();
59 torben 588 if (conn != null)
60     conn.close();
61     }
62    
63     return result;
64     }
65    
66 torben 714 /*
67     * 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) )
68     * create function rlike(text,text) returns bool as
69     * 'select $2 ilike $1' language sql strict immutable;
70     * create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~);
71     */
72 torben 588 public List<StationBean> getByName(String name) throws SQLException {
73     String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " +
74     "FROM trainstations " +
75 torben 714 "WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " +
76 torben 588 "ORDER BY name ";
77    
78    
79     List<StationBean> result;
80     Connection conn = null;
81 torben 589 PreparedStatement stmt = null;
82     ResultSet res = null;
83 torben 588 try {
84     conn = DBConnection.getConnection();
85 torben 589 stmt = conn.prepareStatement(SQL);
86 torben 588
87     stmt.setString(1, name + "%");
88 torben 714 stmt.setString(2, name + "%");
89 torben 588
90 torben 589 res = stmt.executeQuery();
91     result = convertResultset(res);
92 torben 588
93     } finally {
94 torben 589 if (res != null)
95     res.close();
96     if (stmt != null)
97     stmt.close();
98     if (conn!= null)
99 torben 588 conn.close();
100     }
101     return result;
102     }
103    
104 torben 739 //the "hack" with max 1.5 degrees latitude and 2.5 degrees longitude is only valid since we only service danish trains
105     // in denmark 1.5dg latitude ~ 165km, 2.5dg longitude ~ 155km
106 torben 588 public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException {
107 torben 734 String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address," +
108     " earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " +
109     "FROM trainstations " +
110 torben 739 "WHERE enabled = true AND abs(latitude-?)<1.5 AND abs(longitude-?)<2.5 " +
111 torben 734 "ORDER BY calcdist ASC " +
112     "LIMIT 4 ";
113 torben 588 List<StationBean> result;
114     Connection conn = null;
115 torben 589 PreparedStatement stmt = null;
116     ResultSet res = null;
117 torben 588 try {
118     conn = DBConnection.getConnection();
119 torben 589 stmt = conn.prepareStatement(SQL);
120 torben 588 stmt.setDouble(1, latitude);
121     stmt.setDouble(2, longitude);
122 torben 737 stmt.setDouble(3, latitude);
123     stmt.setDouble(4, longitude);
124 torben 589 res = stmt.executeQuery();
125     result = convertResultset(res);
126 torben 588
127     } finally {
128 torben 589 if (res != null)
129     res.close();
130     if (stmt != null)
131     stmt.close();
132     if (conn!= null)
133 torben 588 conn.close();
134     }
135     return result;
136     }
137    
138     public List<StationBean> getByList(String list) throws SQLException {
139     String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro, address,0.0 " +
140     "FROM trainstations " +
141     "WHERE id IN " + list + " AND enabled = true " +
142     "ORDER BY name ";
143    
144     Connection conn = null;
145 torben 589 Statement stmt = null;
146     ResultSet res = null;
147 torben 588 List<StationBean> result;
148    
149     try {
150     conn = DBConnection.getConnection();
151 torben 589 stmt = conn.createStatement();
152     res = stmt.executeQuery(SQL);
153 torben 588 result = convertResultset(res);
154     } finally {
155 torben 589 if (res != null)
156     res.close();
157     if (stmt != null)
158     stmt.close();
159 torben 588 if (conn!= null)
160     conn.close();
161     }
162    
163     return result;
164    
165     }
166 torben 650 public static String getStationName(int stationID) {
167     String station = "";
168    
169     Connection conn = null;
170     try {
171     conn = DBConnection.getConnection();
172     Statement stmt = conn.createStatement();
173     ResultSet rs = stmt.executeQuery("SELECT name FROM trainstations WHERE id=" + stationID);
174     if (rs.next()) {
175     station = rs.getString(1);
176     }
177    
178     } catch (Exception e) {
179     } finally {
180     try {
181     if (conn != null && !conn.isClosed())
182     conn.close();
183     } catch (Exception e) {}
184     }
185    
186     return station;
187     }
188 torben 588 }

  ViewVC Help
Powered by ViewVC 1.1.20