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