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

  ViewVC Help
Powered by ViewVC 1.1.20