/[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 1145 - (hide annotations) (download)
Fri Oct 1 05:30:32 2010 UTC (13 years, 8 months ago) by torben
File size: 8490 byte(s)
Handle invalid station ID's in request
1 torben 588 package dk.thoerup.traininfoservice;
2    
3 torben 1082 import java.sql.Array;
4 torben 588 import java.sql.Connection;
5     import java.sql.PreparedStatement;
6     import java.sql.ResultSet;
7     import java.sql.SQLException;
8     import java.sql.Statement;
9 torben 895 import java.util.logging.Logger;
10 torben 588
11 torben 1061 import dk.thoerup.android.traininfo.common.StationBean;
12     import dk.thoerup.android.traininfo.common.StationBean.StationEntry;
13 torben 1060
14 torben 588 public class StationDAO {
15 torben 1145
16     public static class NostationException extends Exception {
17     private static final long serialVersionUID = 1L;
18     }
19    
20 torben 997 final static int LOCATION_LIMIT = 8;
21 torben 895 static final Logger logger = Logger.getLogger(StationDAO.class.getName());
22 torben 841
23 torben 895
24 torben 1060 private StationEntry convertSingleRow(ResultSet res) throws SQLException {
25     StationEntry station = new StationEntry();
26 torben 836
27 torben 588 station.setId( res.getInt(1) );
28     station.setName( res.getString(2) );
29     station.setLatitude( res.getDouble(3) );
30     station.setLongitude( res.getDouble(4) );
31     station.setRegional( res.getString(5) );
32     station.setStrain( res.getString(6) );
33     station.setMetro( res.getString(7) );
34     station.setAddress( res.getString(8) );
35     station.setCalcdist( (int)res.getDouble(9) );
36 torben 1060
37     station.setIsRegional( station.getRegional() != null );
38     station.setIsStrain( station.getStrain() != null );
39     station.setIsMetro( station.getMetro() != null );
40 torben 836
41 torben 588 return station;
42     }
43 torben 836
44 torben 1060 private StationBean convertResultset(ResultSet res) throws SQLException {
45     StationBean stations = new StationBean();
46 torben 588 while (res.next()) {
47 torben 1060 stations.entries.add( convertSingleRow(res) );
48 torben 588 }
49     return stations;
50 torben 836
51 torben 588 }
52 torben 836
53    
54 torben 1145 public StationEntry getById(int id) throws SQLException,NostationException {
55 torben 588 String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0 " +
56 torben 836 "FROM trainstations WHERE id=" + id + " AND enabled=true";
57    
58 torben 588 Connection conn = null;
59 torben 589 Statement stmt = null;
60     ResultSet res = null;
61 torben 1060 StationEntry result;
62 torben 836
63 torben 588 try {
64     conn = DBConnection.getConnection();
65 torben 836
66 torben 589 stmt = conn.createStatement();
67 torben 1145 res = stmt.executeQuery(SQL);
68    
69     if (res.next()) {
70     result = convertSingleRow(res);
71     } else {
72     throw new NostationException();
73     }
74 torben 588 } finally {
75 torben 589 if (res != null)
76     res.close();
77     if (stmt != null)
78     stmt.close();
79 torben 588 if (conn != null)
80     conn.close();
81     }
82 torben 836
83 torben 588 return result;
84     }
85 torben 1082
86     public StationBean dumpAll() throws SQLException {
87    
88     String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0,aliases " +
89     "FROM trainstations WHERE enabled = true ORDER BY id";
90    
91     Connection conn = null;
92     Statement stmt = null;
93     ResultSet res = null;
94 torben 836
95 torben 1082
96     try {
97     conn = DBConnection.getConnection();
98    
99     stmt = conn.createStatement();
100     res = stmt.executeQuery(SQL);
101    
102     // Does mostly the same as convertResultset()
103     StationBean stations = new StationBean();
104     while (res.next()) {
105     StationEntry entry = convertSingleRow(res);
106    
107     Array arr = res.getArray(10);
108     if (arr != null) {
109     String[] aliases = (String[]) arr.getArray();
110     entry.setAliases(aliases);
111     }
112    
113     stations.entries.add( entry );
114    
115     }
116     return stations;
117    
118    
119     } finally {
120     if (res != null)
121     res.close();
122     if (stmt != null)
123     stmt.close();
124     if (conn != null)
125     conn.close();
126     }
127    
128     }
129    
130 torben 714 /*
131     * 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) )
132     * create function rlike(text,text) returns bool as
133     * 'select $2 ilike $1' language sql strict immutable;
134 torben 836 * create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~);
135 torben 714 */
136 torben 1060 public StationBean getByName(String name) throws SQLException {
137 torben 588 String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " +
138     "FROM trainstations " +
139 torben 714 "WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " +
140 torben 588 "ORDER BY name ";
141    
142 torben 836
143 torben 1060 StationBean result;
144 torben 588 Connection conn = null;
145 torben 589 PreparedStatement stmt = null;
146     ResultSet res = null;
147 torben 588 try {
148     conn = DBConnection.getConnection();
149 torben 589 stmt = conn.prepareStatement(SQL);
150 torben 836
151 torben 588 stmt.setString(1, name + "%");
152 torben 714 stmt.setString(2, name + "%");
153 torben 836
154 torben 589 res = stmt.executeQuery();
155     result = convertResultset(res);
156 torben 836
157 torben 588 } finally {
158 torben 589 if (res != null)
159     res.close();
160     if (stmt != null)
161     stmt.close();
162     if (conn!= null)
163 torben 588 conn.close();
164     }
165     return result;
166     }
167 torben 836
168 torben 928 //the "hack" with max 0.4 degrees latitude and 0.75 degrees longitude is only valid since we only service danish trains,
169     // in denmark 0.4dg latitude ~ 44km, 0.75dg longitude ~ 47km
170 torben 836
171 torben 741 // the ultra fast method (and only slightly inaccurate as long as we only cover a limited geographically area)
172     // is using an aproximation of the length of 1 latitude degree and 1 longitude degree and just use pythagoras to
173     // calculate the distance:
174 torben 836 // sqrt( power(abs(latitude-?)*111320, 2) + power(abs(longitude-?)*63000,2) )::int as calcdist
175    
176 torben 1060 public StationBean getByLocationWorker(double latitude, double longitude, boolean geolimit) throws SQLException {
177 torben 841
178 torben 929 String limitExpression = (geolimit == true) ? "AND abs(latitude-?)<0.4 AND abs(longitude-?)<0.75 " : "";
179 torben 841
180     String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, " +
181     "earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " +
182     "FROM trainstations " +
183     "WHERE enabled = true " + limitExpression +
184     "ORDER BY calcdist ASC " +
185     "LIMIT " + LOCATION_LIMIT;
186 torben 849
187    
188 torben 841
189 torben 1060 StationBean result;
190 torben 588 Connection conn = null;
191 torben 589 PreparedStatement stmt = null;
192     ResultSet res = null;
193 torben 588 try {
194     conn = DBConnection.getConnection();
195 torben 589 stmt = conn.prepareStatement(SQL);
196 torben 588 stmt.setDouble(1, latitude);
197     stmt.setDouble(2, longitude);
198 torben 841 if (geolimit == true) {
199     stmt.setDouble(3, latitude);
200     stmt.setDouble(4, longitude);
201     }
202 torben 589 res = stmt.executeQuery();
203     result = convertResultset(res);
204 torben 849
205 torben 588 } finally {
206 torben 589 if (res != null)
207     res.close();
208     if (stmt != null)
209     stmt.close();
210     if (conn!= null)
211 torben 588 conn.close();
212     }
213     return result;
214     }
215 torben 841
216 torben 1060 public StationBean getByLocation(double latitude, double longitude) throws SQLException {
217     StationBean result = getByLocationWorker(latitude, longitude, true);
218 torben 841
219 torben 1060 if (result.entries.size() < LOCATION_LIMIT) { //failover
220 torben 894 logger.info("getByLocation failover: " +latitude + "," + longitude);
221    
222 torben 841 result = getByLocationWorker(latitude, longitude, false);
223     }
224    
225     return result;
226     }
227    
228    
229 torben 836
230 torben 1060 public StationBean getByList(String list) throws SQLException {
231 torben 836 String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro, address,0.0 " +
232     "FROM trainstations " +
233     "WHERE id IN " + list + " AND enabled = true " +
234     "ORDER BY name ";
235    
236     Connection conn = null;
237     Statement stmt = null;
238     ResultSet res = null;
239 torben 1060 StationBean result;
240 torben 836
241     try {
242     conn = DBConnection.getConnection();
243     stmt = conn.createStatement();
244     res = stmt.executeQuery(SQL);
245     result = convertResultset(res);
246     } finally {
247     if (res != null)
248     res.close();
249     if (stmt != null)
250     stmt.close();
251     if (conn!= null)
252     conn.close();
253     }
254    
255     return result;
256    
257     }
258     public static String getStationName(int stationID) {
259     String station = "";
260    
261     Connection conn = null;
262     try {
263     conn = DBConnection.getConnection();
264     Statement stmt = conn.createStatement();
265     ResultSet rs = stmt.executeQuery("SELECT name FROM trainstations WHERE id=" + stationID);
266     if (rs.next()) {
267     station = rs.getString(1);
268     }
269    
270     } catch (Exception e) {
271     } finally {
272 torben 588 try {
273 torben 836 if (conn != null && !conn.isClosed())
274 torben 588 conn.close();
275 torben 836 } catch (Exception e) {}
276     }
277 torben 650
278 torben 836 return station;
279     }
280 torben 650
281 torben 842 public int getIdByName(String name) throws SQLException {
282 torben 836 String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " +
283     "FROM trainstations " +
284     "WHERE name = ? AND enabled = true " +
285     "LIMIT 1 ";
286 torben 650
287 torben 1060 StationBean result;
288 torben 836 Connection conn = null;
289     PreparedStatement stmt = null;
290     ResultSet res = null;
291     try {
292     conn = DBConnection.getConnection();
293     stmt = conn.prepareStatement(SQL);
294    
295     stmt.setString(1, name );
296    
297     res = stmt.executeQuery();
298     result = convertResultset(res);
299    
300     } finally {
301     if (res != null)
302     res.close();
303     if (stmt != null)
304     stmt.close();
305     if (conn!= null)
306     conn.close();
307     }
308    
309 torben 1060 if (result.entries.size() == 1) {
310     return result.entries.get(0).getId();
311 torben 836 } else {
312     return -1;
313     }
314     }
315 torben 588 }

  ViewVC Help
Powered by ViewVC 1.1.20