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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20