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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1060 - (hide annotations) (download)
Thu Sep 16 13:32:10 2010 UTC (13 years, 8 months ago) by torben
Original Path: android/TrainInfoService/src/dk/thoerup/traininfoservice/StationDAO.java
File size: 7222 byte(s)
Experimental: use Simple (simple.sourceforge.net) for XML serialization
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 torben 895 import java.util.logging.Logger;
9 torben 588
10 torben 1060 import dk.thoerup.traininfoservice.StationBean.StationEntry;
11    
12 torben 588 public class StationDAO {
13 torben 997 final static int LOCATION_LIMIT = 8;
14 torben 895 static final Logger logger = Logger.getLogger(StationDAO.class.getName());
15 torben 841
16 torben 895
17 torben 1060 private StationEntry convertSingleRow(ResultSet res) throws SQLException {
18     StationEntry station = new StationEntry();
19 torben 836
20 torben 588 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 torben 1060
30     station.setIsRegional( station.getRegional() != null );
31     station.setIsStrain( station.getStrain() != null );
32     station.setIsMetro( station.getMetro() != null );
33 torben 836
34 torben 588 return station;
35     }
36 torben 836
37 torben 1060 private StationBean convertResultset(ResultSet res) throws SQLException {
38     StationBean stations = new StationBean();
39 torben 588 while (res.next()) {
40 torben 1060 stations.entries.add( convertSingleRow(res) );
41 torben 588 }
42     return stations;
43 torben 836
44 torben 588 }
45 torben 836
46    
47 torben 1060 public StationEntry getById(int id) throws SQLException {
48 torben 588 String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0 " +
49 torben 836 "FROM trainstations WHERE id=" + id + " AND enabled=true";
50    
51 torben 588 Connection conn = null;
52 torben 589 Statement stmt = null;
53     ResultSet res = null;
54 torben 1060 StationEntry result;
55 torben 836
56 torben 588 try {
57     conn = DBConnection.getConnection();
58 torben 836
59 torben 589 stmt = conn.createStatement();
60     res = stmt.executeQuery(SQL);
61 torben 588 res.next();
62     result = convertSingleRow(res);
63     } finally {
64 torben 589 if (res != null)
65     res.close();
66     if (stmt != null)
67     stmt.close();
68 torben 588 if (conn != null)
69     conn.close();
70     }
71 torben 836
72 torben 588 return result;
73     }
74 torben 836
75 torben 714 /*
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 torben 836 * create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~);
80 torben 714 */
81 torben 1060 public StationBean getByName(String name) throws SQLException {
82 torben 588 String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " +
83     "FROM trainstations " +
84 torben 714 "WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " +
85 torben 588 "ORDER BY name ";
86    
87 torben 836
88 torben 1060 StationBean result;
89 torben 588 Connection conn = null;
90 torben 589 PreparedStatement stmt = null;
91     ResultSet res = null;
92 torben 588 try {
93     conn = DBConnection.getConnection();
94 torben 589 stmt = conn.prepareStatement(SQL);
95 torben 836
96 torben 588 stmt.setString(1, name + "%");
97 torben 714 stmt.setString(2, name + "%");
98 torben 836
99 torben 589 res = stmt.executeQuery();
100     result = convertResultset(res);
101 torben 836
102 torben 588 } finally {
103 torben 589 if (res != null)
104     res.close();
105     if (stmt != null)
106     stmt.close();
107     if (conn!= null)
108 torben 588 conn.close();
109     }
110     return result;
111     }
112 torben 836
113 torben 928 //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 torben 836
116 torben 741 // 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 torben 836 // sqrt( power(abs(latitude-?)*111320, 2) + power(abs(longitude-?)*63000,2) )::int as calcdist
120    
121 torben 1060 public StationBean getByLocationWorker(double latitude, double longitude, boolean geolimit) throws SQLException {
122 torben 841
123 torben 929 String limitExpression = (geolimit == true) ? "AND abs(latitude-?)<0.4 AND abs(longitude-?)<0.75 " : "";
124 torben 841
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 torben 849
132    
133 torben 841
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 588 stmt.setDouble(1, latitude);
142     stmt.setDouble(2, longitude);
143 torben 841 if (geolimit == true) {
144     stmt.setDouble(3, latitude);
145     stmt.setDouble(4, longitude);
146     }
147 torben 589 res = stmt.executeQuery();
148     result = convertResultset(res);
149 torben 849
150 torben 588 } finally {
151 torben 589 if (res != null)
152     res.close();
153     if (stmt != null)
154     stmt.close();
155     if (conn!= null)
156 torben 588 conn.close();
157     }
158     return result;
159     }
160 torben 841
161 torben 1060 public StationBean getByLocation(double latitude, double longitude) throws SQLException {
162     StationBean result = getByLocationWorker(latitude, longitude, true);
163 torben 841
164 torben 1060 if (result.entries.size() < LOCATION_LIMIT) { //failover
165 torben 894 logger.info("getByLocation failover: " +latitude + "," + longitude);
166    
167 torben 841 result = getByLocationWorker(latitude, longitude, false);
168     }
169    
170     return result;
171     }
172    
173    
174 torben 836
175 torben 1060 public StationBean getByList(String list) throws SQLException {
176 torben 836 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 torben 1060 StationBean result;
185 torben 836
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 torben 588 try {
218 torben 836 if (conn != null && !conn.isClosed())
219 torben 588 conn.close();
220 torben 836 } catch (Exception e) {}
221     }
222 torben 650
223 torben 836 return station;
224     }
225 torben 650
226 torben 842 public int getIdByName(String name) throws SQLException {
227 torben 836 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 torben 650
232 torben 1060 StationBean result;
233 torben 836 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 torben 1060 if (result.entries.size() == 1) {
255     return result.entries.get(0).getId();
256 torben 836 } else {
257     return -1;
258     }
259     }
260 torben 588 }

  ViewVC Help
Powered by ViewVC 1.1.20