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

  ViewVC Help
Powered by ViewVC 1.1.20