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

  ViewVC Help
Powered by ViewVC 1.1.20