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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1060 - (show annotations) (download)
Thu Sep 16 13:32:10 2010 UTC (13 years, 8 months ago) by torben
File size: 7222 byte(s)
Experimental: use Simple (simple.sourceforge.net) for XML serialization
1 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.logging.Logger;
9
10 import dk.thoerup.traininfoservice.StationBean.StationEntry;
11
12 public class StationDAO {
13 final static int LOCATION_LIMIT = 8;
14 static final Logger logger = Logger.getLogger(StationDAO.class.getName());
15
16
17 private StationEntry convertSingleRow(ResultSet res) throws SQLException {
18 StationEntry station = new StationEntry();
19
20 station.setId( res.getInt(1) );
21 station.setName( res.getString(2) );
22 station.setLatitude( res.getDouble(3) );
23 station.setLongitude( res.getDouble(4) );
24 station.setRegional( res.getString(5) );
25 station.setStrain( res.getString(6) );
26 station.setMetro( res.getString(7) );
27 station.setAddress( res.getString(8) );
28 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;
35 }
36
37 private StationBean convertResultset(ResultSet res) throws SQLException {
38 StationBean stations = new StationBean();
39 while (res.next()) {
40 stations.entries.add( convertSingleRow(res) );
41 }
42 return stations;
43
44 }
45
46
47 public StationEntry getById(int id) throws SQLException {
48 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";
50
51 Connection conn = null;
52 Statement stmt = null;
53 ResultSet res = null;
54 StationEntry result;
55
56 try {
57 conn = DBConnection.getConnection();
58
59 stmt = conn.createStatement();
60 res = stmt.executeQuery(SQL);
61 res.next();
62 result = convertSingleRow(res);
63 } finally {
64 if (res != null)
65 res.close();
66 if (stmt != null)
67 stmt.close();
68 if (conn != null)
69 conn.close();
70 }
71
72 return result;
73 }
74
75 /*
76 * 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) )
77 * create function rlike(text,text) returns bool as
78 * 'select $2 ilike $1' language sql strict immutable;
79 * create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~);
80 */
81 public StationBean getByName(String name) throws SQLException {
82 String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " +
83 "FROM trainstations " +
84 "WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " +
85 "ORDER BY name ";
86
87
88 StationBean result;
89 Connection conn = null;
90 PreparedStatement stmt = null;
91 ResultSet res = null;
92 try {
93 conn = DBConnection.getConnection();
94 stmt = conn.prepareStatement(SQL);
95
96 stmt.setString(1, name + "%");
97 stmt.setString(2, name + "%");
98
99 res = stmt.executeQuery();
100 result = convertResultset(res);
101
102 } finally {
103 if (res != null)
104 res.close();
105 if (stmt != null)
106 stmt.close();
107 if (conn!= null)
108 conn.close();
109 }
110 return result;
111 }
112
113 //the "hack" with max 0.4 degrees latitude and 0.75 degrees longitude is only valid since we only service danish trains,
114 // in denmark 0.4dg latitude ~ 44km, 0.75dg longitude ~ 47km
115
116 // the ultra fast method (and only slightly inaccurate as long as we only cover a limited geographically area)
117 // is using an aproximation of the length of 1 latitude degree and 1 longitude degree and just use pythagoras to
118 // calculate the distance:
119 // sqrt( power(abs(latitude-?)*111320, 2) + power(abs(longitude-?)*63000,2) )::int as calcdist
120
121 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 " : "";
124
125 String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, " +
126 "earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " +
127 "FROM trainstations " +
128 "WHERE enabled = true " + limitExpression +
129 "ORDER BY calcdist ASC " +
130 "LIMIT " + LOCATION_LIMIT;
131
132
133
134 StationBean result;
135 Connection conn = null;
136 PreparedStatement stmt = null;
137 ResultSet res = null;
138 try {
139 conn = DBConnection.getConnection();
140 stmt = conn.prepareStatement(SQL);
141 stmt.setDouble(1, latitude);
142 stmt.setDouble(2, longitude);
143 if (geolimit == true) {
144 stmt.setDouble(3, latitude);
145 stmt.setDouble(4, longitude);
146 }
147 res = stmt.executeQuery();
148 result = convertResultset(res);
149
150 } finally {
151 if (res != null)
152 res.close();
153 if (stmt != null)
154 stmt.close();
155 if (conn!= null)
156 conn.close();
157 }
158 return result;
159 }
160
161 public StationBean getByLocation(double latitude, double longitude) throws SQLException {
162 StationBean result = getByLocationWorker(latitude, longitude, true);
163
164 if (result.entries.size() < LOCATION_LIMIT) { //failover
165 logger.info("getByLocation failover: " +latitude + "," + longitude);
166
167 result = getByLocationWorker(latitude, longitude, false);
168 }
169
170 return result;
171 }
172
173
174
175 public StationBean getByList(String list) throws SQLException {
176 String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro, address,0.0 " +
177 "FROM trainstations " +
178 "WHERE id IN " + list + " AND enabled = true " +
179 "ORDER BY name ";
180
181 Connection conn = null;
182 Statement stmt = null;
183 ResultSet res = null;
184 StationBean result;
185
186 try {
187 conn = DBConnection.getConnection();
188 stmt = conn.createStatement();
189 res = stmt.executeQuery(SQL);
190 result = convertResultset(res);
191 } finally {
192 if (res != null)
193 res.close();
194 if (stmt != null)
195 stmt.close();
196 if (conn!= null)
197 conn.close();
198 }
199
200 return result;
201
202 }
203 public static String getStationName(int stationID) {
204 String station = "";
205
206 Connection conn = null;
207 try {
208 conn = DBConnection.getConnection();
209 Statement stmt = conn.createStatement();
210 ResultSet rs = stmt.executeQuery("SELECT name FROM trainstations WHERE id=" + stationID);
211 if (rs.next()) {
212 station = rs.getString(1);
213 }
214
215 } catch (Exception e) {
216 } finally {
217 try {
218 if (conn != null && !conn.isClosed())
219 conn.close();
220 } catch (Exception e) {}
221 }
222
223 return station;
224 }
225
226 public int getIdByName(String name) throws SQLException {
227 String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " +
228 "FROM trainstations " +
229 "WHERE name = ? AND enabled = true " +
230 "LIMIT 1 ";
231
232 StationBean result;
233 Connection conn = null;
234 PreparedStatement stmt = null;
235 ResultSet res = null;
236 try {
237 conn = DBConnection.getConnection();
238 stmt = conn.prepareStatement(SQL);
239
240 stmt.setString(1, name );
241
242 res = stmt.executeQuery();
243 result = convertResultset(res);
244
245 } finally {
246 if (res != null)
247 res.close();
248 if (stmt != null)
249 stmt.close();
250 if (conn!= null)
251 conn.close();
252 }
253
254 if (result.entries.size() == 1) {
255 return result.entries.get(0).getId();
256 } else {
257 return -1;
258 }
259 }
260 }

  ViewVC Help
Powered by ViewVC 1.1.20