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

  ViewVC Help
Powered by ViewVC 1.1.20