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

Annotation of /android/TrainInfoService/src/dk/thoerup/traininfoservice/db/StationDAO.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1790 - (hide annotations) (download)
Wed Apr 11 10:11:38 2012 UTC (12 years, 1 month ago) by torben
File size: 11878 byte(s)
Also extract aliases from DB to XML response
1 torben 1255 package dk.thoerup.traininfoservice.db;
2 torben 588
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 1574 import java.util.Collections;
10     import java.util.Comparator;
11 torben 895 import java.util.logging.Logger;
12 torben 588
13 torben 1061 import dk.thoerup.android.traininfo.common.StationBean;
14 torben 1409 import dk.thoerup.android.traininfo.common.StationEntry;
15 torben 1060
16 torben 588 public class StationDAO {
17 torben 1145
18 torben 1504 private interface StatementParamSetter {
19     public void setParams(PreparedStatement stmt) throws SQLException ;
20     }
21     private class NullSetter implements StatementParamSetter {
22     @Override
23     public void setParams(PreparedStatement stmt) throws SQLException {}
24     }
25    
26 torben 1145 public static class NostationException extends Exception {
27     private static final long serialVersionUID = 1L;
28     }
29    
30 torben 997 final static int LOCATION_LIMIT = 8;
31 torben 895 static final Logger logger = Logger.getLogger(StationDAO.class.getName());
32 torben 841
33 torben 895
34 torben 1060 private StationEntry convertSingleRow(ResultSet res) throws SQLException {
35     StationEntry station = new StationEntry();
36 torben 836
37 torben 588 station.setId( res.getInt(1) );
38     station.setName( res.getString(2) );
39     station.setLatitude( res.getDouble(3) );
40     station.setLongitude( res.getDouble(4) );
41     station.setRegional( res.getString(5) );
42     station.setStrain( res.getString(6) );
43     station.setMetro( res.getString(7) );
44     station.setAddress( res.getString(8) );
45     station.setCalcdist( (int)res.getDouble(9) );
46 torben 1060
47     station.setIsRegional( station.getRegional() != null );
48     station.setIsStrain( station.getStrain() != null );
49     station.setIsMetro( station.getMetro() != null );
50 torben 1790
51     Array aliases = res.getArray( 10);
52     if (aliases != null) {
53     station.setAliases( (String[]) aliases.getArray() );
54     } else {
55     String[] emptyArray = {};
56     station.setAliases( emptyArray );
57     }
58 torben 836
59 torben 588 return station;
60     }
61 torben 836
62 torben 1060 private StationBean convertResultset(ResultSet res) throws SQLException {
63     StationBean stations = new StationBean();
64 torben 588 while (res.next()) {
65 torben 1060 stations.entries.add( convertSingleRow(res) );
66 torben 588 }
67     return stations;
68 torben 836
69 torben 588 }
70 torben 836
71 torben 1504 private StationBean fetchStations(String SQL, StatementParamSetter setter) throws SQLException {
72     StationBean stations;
73 torben 588 Connection conn = null;
74 torben 1504 PreparedStatement stmt = null;
75 torben 589 ResultSet res = null;
76 torben 588 try {
77     conn = DBConnection.getConnection();
78 torben 1504 stmt = conn.prepareStatement(SQL);
79    
80     setter.setParams(stmt);
81 torben 836
82 torben 1504 res = stmt.executeQuery();
83     stations = convertResultset(res);
84    
85 torben 588 } finally {
86 torben 589 if (res != null)
87     res.close();
88     if (stmt != null)
89     stmt.close();
90 torben 1504 if (conn!= null)
91 torben 588 conn.close();
92     }
93 torben 1504 return stations;
94    
95     }
96 torben 836
97 torben 1504 public StationEntry getById(int id) throws SQLException,NostationException {
98 torben 1790 String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0,aliases " +
99 torben 1504 "FROM trainstations WHERE id=" + id + " AND enabled=true";
100    
101     StationBean stations = fetchStations(SQL, new NullSetter() );
102    
103     if (stations.entries.size() > 0 ) {
104     return stations.entries.get(0);
105     } else {
106     throw new NostationException();
107     }
108 torben 588 }
109 torben 1082
110 torben 836
111 torben 1082
112 torben 714 /*
113     * 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) )
114     * create function rlike(text,text) returns bool as
115     * 'select $2 ilike $1' language sql strict immutable;
116 torben 836 * create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~);
117 torben 714 */
118 torben 1507 public StationBean getByNameNormal(final String name) throws SQLException {
119 torben 1790 String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0,aliases " +
120 torben 588 "FROM trainstations " +
121 torben 714 "WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " +
122 torben 588 "ORDER BY name ";
123    
124 torben 1504 class NameSetter implements StatementParamSetter {
125     @Override
126     public void setParams(PreparedStatement stmt) throws SQLException {
127     stmt.setString(1, name + "%");
128     stmt.setString(2, name + "%");
129     }
130 torben 588 }
131 torben 1504
132     return fetchStations(SQL, new NameSetter() );
133 torben 588 }
134 torben 1507
135    
136     public StationBean getByNameFuzzy(final String name) throws SQLException {
137 torben 1512 String SQL = "SELECT * FROM (" +
138 torben 1790 " SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0, aliases, " +
139 torben 1512 " levenshtein(lower(name),lower(?) ) as leven " +
140     " FROM trainstations " +
141     " WHERE enabled = true ) as lev2 " +
142     "WHERE (leven <= 3) " +
143     "ORDER BY leven " +
144     "LIMIT 1";
145 torben 836
146 torben 1507 class NameSetter implements StatementParamSetter {
147     @Override
148     public void setParams(PreparedStatement stmt) throws SQLException {
149     stmt.setString(1, name );
150     }
151     }
152    
153     StationBean stations = fetchStations(SQL, new NameSetter() );
154     stations.fuzzystrmatch = true;
155     return stations;
156     }
157    
158 torben 1516 private String removeSuffix(String str, String suffix) {
159     if (str.endsWith(suffix)) {
160     return str.substring(0, str.length() - suffix.length() );
161     } else {
162     return str;
163     }
164     }
165    
166 torben 1513 public StationBean getByName(String name) throws SQLException {
167 torben 1516 name = removeSuffix(name, " st.");
168     name = removeSuffix(name, " st");
169     name = removeSuffix(name, " station");
170    
171 torben 1507 StationBean stations = getByNameNormal(name);
172    
173     if (stations.entries.size() == 0) {
174     stations = getByNameFuzzy(name);
175 torben 1515
176     logger.info("getByName failover: " + name + "(" + (stations.entries.size() >0) + ")" );
177 torben 1507 }
178     return stations;
179     }
180    
181    
182    
183 torben 1254 //Latitude (horizonal), longitude(vertical) so
184     // 1 degree latitude is ~ 111320 meters, since the distance between the horizonal lines is always the same
185     // 1 degree longitude is ~111320 meters at equator but gets shorter as we get closer to the poles.
186 torben 1511 // so 1 degree longitude is 64.5 km at denmarks southern point (gedser=54.55,11.95)
187     // and is 59.4km at northern point (skagen = 57.75,10.65)
188     // The "hack" with max 0.4 degrees latitude and 0.75 degrees longitude is only valid since we only service danish trains,
189 torben 928 // in denmark 0.4dg latitude ~ 44km, 0.75dg longitude ~ 47km
190 torben 836
191 torben 741 // the ultra fast method (and only slightly inaccurate as long as we only cover a limited geographically area)
192     // is using an aproximation of the length of 1 latitude degree and 1 longitude degree and just use pythagoras to
193     // calculate the distance:
194 torben 836 // sqrt( power(abs(latitude-?)*111320, 2) + power(abs(longitude-?)*63000,2) )::int as calcdist
195    
196 torben 1504 public StationBean getByLocationWorker(final double latitude, final double longitude, final boolean geolimit) throws SQLException {
197 torben 841
198 torben 929 String limitExpression = (geolimit == true) ? "AND abs(latitude-?)<0.4 AND abs(longitude-?)<0.75 " : "";
199 torben 841
200     String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, " +
201 torben 1790 "earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist,aliases " +
202 torben 841 "FROM trainstations " +
203     "WHERE enabled = true " + limitExpression +
204     "ORDER BY calcdist ASC " +
205     "LIMIT " + LOCATION_LIMIT;
206 torben 849
207 torben 841
208 torben 1504 class LatlongSetter implements StatementParamSetter {
209     @Override
210     public void setParams(PreparedStatement stmt) throws SQLException {
211     stmt.setDouble(1, latitude);
212     stmt.setDouble(2, longitude);
213     if (geolimit == true) {
214     stmt.setDouble(3, latitude);
215     stmt.setDouble(4, longitude);
216     }
217     }
218     }
219 torben 849
220 torben 1504 return fetchStations(SQL, new LatlongSetter() );
221 torben 588 }
222 torben 841
223 torben 1060 public StationBean getByLocation(double latitude, double longitude) throws SQLException {
224     StationBean result = getByLocationWorker(latitude, longitude, true);
225 torben 841
226 torben 1060 if (result.entries.size() < LOCATION_LIMIT) { //failover
227 torben 894 logger.info("getByLocation failover: " +latitude + "," + longitude);
228    
229 torben 841 result = getByLocationWorker(latitude, longitude, false);
230     }
231    
232     return result;
233     }
234    
235    
236 torben 836
237 torben 1060 public StationBean getByList(String list) throws SQLException {
238 torben 1790 String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro, address,0.0,aliases " +
239 torben 836 "FROM trainstations " +
240     "WHERE id IN " + list + " AND enabled = true " +
241     "ORDER BY name ";
242 torben 1504
243     return fetchStations(SQL, new NullSetter() );
244     }
245    
246 torben 836
247 torben 1504
248     public StationEntry getSimpleByName(final String name) throws SQLException {
249 torben 1790 String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0,aliases " +
250 torben 1504 "FROM trainstations " +
251     "WHERE name = ? AND enabled = true " +
252     "LIMIT 1 ";
253 torben 836
254 torben 1504 class NameSetter implements StatementParamSetter {
255     @Override
256     public void setParams(PreparedStatement stmt) throws SQLException {
257     stmt.setString(1, name );
258     }
259 torben 836 }
260 torben 1504
261     StationBean stations = fetchStations(SQL, new NameSetter() );
262    
263     if (stations.entries.size() == 1) {
264     return stations.entries.get(0);
265     } else {
266     return null;
267     }
268     }
269 torben 1505
270 torben 1574 Comparator<StationEntry> nameComparator = new Comparator<StationEntry>() {
271     @Override
272     public int compare(StationEntry arg0, StationEntry arg1) {
273     return arg0.getName().compareTo( arg1.getName() );
274     }
275     };
276    
277 torben 1505 //used to create full dump in order to populate Google Appengine DB
278 torben 1569 //after 1.1.0 also used to populate client-side station list
279 torben 1505 public StationBean dumpAll() throws SQLException {
280    
281     String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0,aliases " +
282 torben 1574 "FROM trainstations WHERE enabled = true";
283 torben 1505
284 torben 1575
285     StationBean stations = fetchStations(SQL, new NullSetter() );
286     Collections.sort( stations.entries,nameComparator );
287    
288     return stations;
289    
290     /*
291 torben 1505 Connection conn = null;
292     Statement stmt = null;
293     ResultSet res = null;
294 torben 836
295 torben 1505
296     try {
297     conn = DBConnection.getConnection();
298    
299     stmt = conn.createStatement();
300     res = stmt.executeQuery(SQL);
301    
302     // Does mostly the same as convertResultset()
303     StationBean stations = new StationBean();
304     while (res.next()) {
305     StationEntry entry = convertSingleRow(res);
306    
307     Array arr = res.getArray(10);
308     if (arr != null) {
309     String[] aliases = (String[]) arr.getArray();
310     entry.setAliases(aliases);
311     }
312    
313     stations.entries.add( entry );
314    
315     }
316 torben 1574 Collections.sort( stations.entries,nameComparator );
317 torben 1505 return stations;
318    
319    
320     } finally {
321     if (res != null)
322     res.close();
323     if (stmt != null)
324     stmt.close();
325     if (conn != null)
326     conn.close();
327 torben 1575 }*/
328 torben 1505
329     }
330    
331 torben 1417 @Deprecated
332 torben 836 public static String getStationName(int stationID) {
333     String station = "";
334    
335     Connection conn = null;
336     try {
337     conn = DBConnection.getConnection();
338     Statement stmt = conn.createStatement();
339     ResultSet rs = stmt.executeQuery("SELECT name FROM trainstations WHERE id=" + stationID);
340     if (rs.next()) {
341     station = rs.getString(1);
342     }
343    
344     } catch (Exception e) {
345     } finally {
346 torben 588 try {
347 torben 836 if (conn != null && !conn.isClosed())
348 torben 588 conn.close();
349 torben 836 } catch (Exception e) {}
350     }
351 torben 650
352 torben 836 return station;
353     }
354 torben 1417
355 torben 1692 public boolean hasDisabledStation(final String name) throws SQLException {
356     String SQL = "Select count(*) as antal from trainstations where name = '" + name + "' and enabled=false " ;
357    
358     Connection conn = DBConnection.getConnection();
359     Statement stmt = conn.createStatement();
360     ResultSet rs = stmt.executeQuery( SQL );
361    
362     rs.next();
363    
364     int antal = rs.getInt(1);
365    
366     rs.close();
367     stmt.close();
368     conn.close();
369    
370     return (antal > 0);
371     }
372 torben 1504
373    
374 torben 1417 @Deprecated
375 torben 1504 public int getIdByName(final String name) throws SQLException {
376 torben 836 String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " +
377     "FROM trainstations " +
378     "WHERE name = ? AND enabled = true " +
379     "LIMIT 1 ";
380 torben 650
381 torben 1504 class NameSetter implements StatementParamSetter {
382     @Override
383     public void setParams(PreparedStatement stmt) throws SQLException {
384     stmt.setString(1, name );
385     }
386 torben 836 }
387 torben 1504
388     StationBean stations = fetchStations(SQL, new NameSetter() );
389    
390     if (stations.entries.size() == 1) {
391     return stations.entries.get(0).getId();
392 torben 836 } else {
393     return -1;
394     }
395     }
396 torben 588 }

  ViewVC Help
Powered by ViewVC 1.1.20