1 |
package dk.thoerup.traininfoservice; |
package dk.thoerup.traininfoservice; |
2 |
|
|
3 |
import java.sql.Connection; |
import java.io.IOException; |
|
import java.sql.PreparedStatement; |
|
|
import java.sql.ResultSet; |
|
|
import java.sql.SQLException; |
|
|
import java.sql.Statement; |
|
4 |
import java.util.ArrayList; |
import java.util.ArrayList; |
5 |
|
import java.util.Collections; |
6 |
|
import java.util.Comparator; |
7 |
|
import java.util.HashMap; |
8 |
import java.util.List; |
import java.util.List; |
9 |
|
import java.util.Map; |
10 |
|
import java.util.logging.Level; |
11 |
|
import java.util.logging.Logger; |
12 |
|
|
13 |
|
import javax.jdo.Extent; |
14 |
|
import javax.jdo.PersistenceManager; |
15 |
|
import javax.jdo.Query; |
16 |
|
|
17 |
|
import net.sf.jsr107cache.Cache; |
18 |
|
import net.sf.jsr107cache.CacheException; |
19 |
|
import net.sf.jsr107cache.CacheManager; |
20 |
|
|
21 |
|
import com.google.appengine.api.memcache.jsr107cache.GCacheFactory; |
22 |
|
|
23 |
|
import dk.thoerup.android.traininfo.common.StationBean; |
24 |
|
import dk.thoerup.android.traininfo.common.StationEntry; |
25 |
|
import dk.thoerup.traininfoservice.geo.Geo; |
26 |
|
import dk.thoerup.traininfoservice.jdo.JdoStationBean; |
27 |
|
import dk.thoerup.traininfoservice.jdo.PMF; |
28 |
|
|
29 |
public class StationDAO { |
public class StationDAO { |
30 |
private StationBean convertSingleRow(ResultSet res) throws SQLException { |
final static int LOCATION_LIMIT = 8; |
31 |
StationBean station = new StationBean(); |
static final Logger logger = Logger.getLogger(StationDAO.class.getName()); |
32 |
|
|
33 |
|
final int TIMEOUT_SECONDS = 30*60; |
34 |
|
|
35 |
|
Cache cache; |
36 |
|
|
37 |
|
@SuppressWarnings("unchecked") |
38 |
|
public StationDAO() { |
39 |
|
Map props = new HashMap(); |
40 |
|
props.put(GCacheFactory.EXPIRATION_DELTA, TIMEOUT_SECONDS); |
41 |
|
|
42 |
|
try { |
43 |
|
cache = CacheManager.getInstance().getCacheFactory().createCache(props); |
44 |
|
} catch (CacheException e) { |
45 |
|
logger.log(Level.WARNING, "error creating cache", e); |
46 |
|
} |
47 |
|
|
48 |
|
} |
49 |
|
|
|
station.setId( res.getInt(1) ); |
|
|
station.setName( res.getString(2) ); |
|
|
station.setLatitude( res.getDouble(3) ); |
|
|
station.setLongitude( res.getDouble(4) ); |
|
|
station.setRegional( res.getString(5) ); |
|
|
station.setStrain( res.getString(6) ); |
|
|
station.setMetro( res.getString(7) ); |
|
|
station.setAddress( res.getString(8) ); |
|
|
station.setCalcdist( (int)res.getDouble(9) ); |
|
50 |
|
|
51 |
return station; |
public StationEntry getById(int id) { |
|
} |
|
52 |
|
|
53 |
private List<StationBean> convertResultset(ResultSet res) throws SQLException { |
PersistenceManager pm = null; |
54 |
List<StationBean> stations = new ArrayList<StationBean>(); |
|
55 |
while (res.next()) { |
try { |
56 |
stations.add( convertSingleRow(res) ); |
pm = PMF.get().getPersistenceManager(); |
57 |
|
JdoStationBean bean =pm.getObjectById(JdoStationBean.class, new Integer(id) ); |
58 |
|
return bean.toStationEntry(); |
59 |
|
} finally { |
60 |
|
pm.close(); |
61 |
} |
} |
|
return stations; |
|
|
|
|
62 |
} |
} |
63 |
|
|
64 |
|
/* damn that JDO sucks so we to the filtering in java-code */ |
65 |
|
public StationBean getByName(String searchname) { |
66 |
|
|
67 |
|
List<JdoStationBean> beanList = getAllStations(); |
68 |
|
|
69 |
|
|
70 |
|
StationBean stationBean = new StationBean(); |
71 |
|
|
72 |
|
searchname = searchname.toLowerCase(); |
73 |
|
for(JdoStationBean bean : beanList) { |
74 |
|
if (bean.getName().toLowerCase().startsWith(searchname)) { |
75 |
|
stationBean.entries.add( bean.toStationEntry() ); |
76 |
|
} /*else { |
77 |
|
for (String alias : bean.aliases ) { |
78 |
|
if (alias.toLowerCase().startsWith(searchname)) { |
79 |
|
stationBean.entries.add( bean.toStationEntry() ); |
80 |
|
} |
81 |
|
} |
82 |
|
}*/ |
83 |
|
} |
84 |
|
|
85 |
public StationBean getById(int id) throws SQLException { |
Collections.sort(stationBean.entries, new Comparator<StationEntry>() { |
86 |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0 " + |
@Override |
87 |
"FROM trainstations WHERE id=" + id + " AND enabled=true"; |
public int compare(StationEntry arg0, StationEntry arg1) { |
88 |
|
return arg0.getName().compareTo( arg1.getName() ); |
89 |
|
} |
90 |
|
}); |
91 |
|
|
92 |
Connection conn = null; |
return stationBean; |
|
Statement stmt = null; |
|
|
ResultSet res = null; |
|
|
StationBean result; |
|
93 |
|
|
94 |
try { |
} |
95 |
conn = DBConnection.getConnection(); |
|
96 |
|
@SuppressWarnings("unchecked") |
97 |
stmt = conn.createStatement(); |
public List<JdoStationBean> getAllStations() { |
98 |
res = stmt.executeQuery(SQL); |
final String key = "allstations"; |
99 |
res.next(); |
List<JdoStationBean> result = (List<JdoStationBean>) cache.get(key); |
100 |
result = convertSingleRow(res); |
|
101 |
} finally { |
if (result == null) { |
102 |
if (res != null) |
logger.info("getAllStations Cache miss"); |
103 |
res.close(); |
|
104 |
if (stmt != null) |
PersistenceManager pm = null; |
105 |
stmt.close(); |
|
106 |
if (conn != null) |
try { |
107 |
conn.close(); |
pm = PMF.get().getPersistenceManager(); |
108 |
|
Extent<JdoStationBean> all = pm.getExtent(JdoStationBean.class, false); |
109 |
|
|
110 |
|
result = new ArrayList<JdoStationBean>(); |
111 |
|
for (JdoStationBean station : all) { |
112 |
|
result.add(station); |
113 |
|
} |
114 |
|
|
115 |
|
cache.put(key, result); |
116 |
|
|
117 |
|
} finally { |
118 |
|
pm.close(); |
119 |
|
} |
120 |
|
} else { |
121 |
|
logger.info("getAllStations Cache hit"); |
122 |
} |
} |
123 |
|
|
124 |
|
|
125 |
|
|
126 |
return result; |
return result; |
127 |
|
|
128 |
} |
} |
129 |
|
|
130 |
|
|
131 |
|
//String limitExpression = (geolimit == true) ? "AND abs(latitude-?)<0.4 AND abs(longitude-?)<0.75 " : ""; |
132 |
/* |
/* |
133 |
* 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) ) |
public List<JdoStationBean> getByLocationList(double latitude, double longitude, boolean geolimit) { |
134 |
* create function rlike(text,text) returns bool as |
|
135 |
* 'select $2 ilike $1' language sql strict immutable; |
PersistenceManager pm = null; |
136 |
* create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~); |
final double LAT = 0.4; |
137 |
*/ |
final double LNG = 0.75; |
138 |
public List<StationBean> getByName(String name) throws SQLException { |
|
|
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " + |
|
|
"FROM trainstations " + |
|
|
"WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " + |
|
|
"ORDER BY name "; |
|
|
|
|
|
|
|
|
List<StationBean> result; |
|
|
Connection conn = null; |
|
|
PreparedStatement stmt = null; |
|
|
ResultSet res = null; |
|
139 |
try { |
try { |
140 |
conn = DBConnection.getConnection(); |
pm = PMF.get().getPersistenceManager(); |
141 |
stmt = conn.prepareStatement(SQL); |
Query q = pm.newQuery(JdoStationBean.class); |
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
if (geolimit == true) { |
146 |
|
double minLat = latitude - LAT; |
147 |
|
double maxLat = latitude + LAT; |
148 |
|
|
149 |
|
//DAMN JDO implementation only allows us to compare on one parameter |
150 |
|
|
151 |
|
String filter = String.format("latitude > %f && latitude < %f", minLat, maxLat); |
152 |
|
|
153 |
|
q.setFilter( filter ); |
154 |
|
} |
155 |
|
|
156 |
|
List<JdoStationBean> beanList = (List<JdoStationBean>) q.execute(); |
157 |
|
|
158 |
|
logger.info("beanList size " + beanList.size()); |
159 |
|
|
160 |
|
return beanList; |
161 |
|
} finally { |
162 |
|
pm.close(); |
163 |
|
} |
164 |
|
}*/ |
165 |
|
|
|
stmt.setString(1, name + "%"); |
|
|
stmt.setString(2, name + "%"); |
|
166 |
|
|
167 |
res = stmt.executeQuery(); |
public StationBean getByLocation(double latitude, double longitude) { |
168 |
result = convertResultset(res); |
|
169 |
|
List<JdoStationBean> beanList = getAllStations(); |
170 |
|
|
171 |
} finally { |
StationBean stationBean = new StationBean(); |
|
if (res != null) |
|
|
res.close(); |
|
|
if (stmt != null) |
|
|
stmt.close(); |
|
|
if (conn!= null) |
|
|
conn.close(); |
|
|
} |
|
|
return result; |
|
|
} |
|
172 |
|
|
|
//the "hack" with max 1.5 degrees latitude and 2.5 degrees longitude is only valid since we only service danish trains |
|
|
// in denmark 1.5dg latitude ~ 165km, 2.5dg longitude ~ 155km |
|
173 |
|
|
174 |
// the ultra fast method (and only slightly inaccurate as long as we only cover a limited geographically area) |
Geo location = new Geo(latitude,longitude); |
175 |
// is using an aproximation of the length of 1 latitude degree and 1 longitude degree and just use pythagoras to |
for(JdoStationBean bean : beanList) { |
176 |
// calculate the distance: |
double meter = Geo.distanceKM( location, new Geo(bean.getLatitude(), bean.getLongitude() )) * 1000.0; |
|
// sqrt( power(abs(latitude-?)*111320, 2) + power(abs(longitude-?)*63000,2) )::int as calcdist |
|
|
|
|
|
public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException { |
|
|
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address," + |
|
|
" earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " + |
|
|
"FROM trainstations " + |
|
|
"WHERE enabled = true AND abs(latitude-?)<1.5 AND abs(longitude-?)<2.5 " + |
|
|
"ORDER BY calcdist ASC " + |
|
|
"LIMIT 4 "; |
|
|
List<StationBean> result; |
|
|
Connection conn = null; |
|
|
PreparedStatement stmt = null; |
|
|
ResultSet res = null; |
|
|
try { |
|
|
conn = DBConnection.getConnection(); |
|
|
stmt = conn.prepareStatement(SQL); |
|
|
stmt.setDouble(1, latitude); |
|
|
stmt.setDouble(2, longitude); |
|
|
stmt.setDouble(3, latitude); |
|
|
stmt.setDouble(4, longitude); |
|
|
res = stmt.executeQuery(); |
|
|
result = convertResultset(res); |
|
177 |
|
|
178 |
} finally { |
bean.distance = (int) meter; |
|
if (res != null) |
|
|
res.close(); |
|
|
if (stmt != null) |
|
|
stmt.close(); |
|
|
if (conn!= null) |
|
|
conn.close(); |
|
179 |
} |
} |
|
return result; |
|
|
} |
|
180 |
|
|
|
public List<StationBean> getByList(String list) throws SQLException { |
|
|
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro, address,0.0 " + |
|
|
"FROM trainstations " + |
|
|
"WHERE id IN " + list + " AND enabled = true " + |
|
|
"ORDER BY name "; |
|
|
|
|
|
Connection conn = null; |
|
|
Statement stmt = null; |
|
|
ResultSet res = null; |
|
|
List<StationBean> result; |
|
|
|
|
|
try { |
|
|
conn = DBConnection.getConnection(); |
|
|
stmt = conn.createStatement(); |
|
|
res = stmt.executeQuery(SQL); |
|
|
result = convertResultset(res); |
|
|
} finally { |
|
|
if (res != null) |
|
|
res.close(); |
|
|
if (stmt != null) |
|
|
stmt.close(); |
|
|
if (conn!= null) |
|
|
conn.close(); |
|
|
} |
|
181 |
|
|
182 |
return result; |
Collections.sort(beanList, new Comparator<JdoStationBean>() { |
183 |
|
@Override |
184 |
|
public int compare(JdoStationBean o1, JdoStationBean o2) { |
185 |
|
if (o1.distance < o2.distance) { |
186 |
|
return -1; |
187 |
|
} else if (o1.distance > o2.distance) { |
188 |
|
return 1; |
189 |
|
} else { |
190 |
|
return 0; |
191 |
|
} |
192 |
|
} |
193 |
|
}); |
194 |
|
|
195 |
|
for (int i=0; i<LOCATION_LIMIT && i<beanList.size(); i++) { |
196 |
|
stationBean.entries.add( beanList.get(i).toStationEntry() ); |
197 |
|
} |
198 |
|
|
199 |
|
return stationBean; |
200 |
} |
} |
201 |
public static String getStationName(int stationID) { |
|
|
String station = ""; |
|
202 |
|
|
203 |
Connection conn = null; |
@SuppressWarnings("unchecked") |
204 |
|
public StationBean getByList(String list) { |
205 |
|
PersistenceManager pm = null; |
206 |
|
|
207 |
try { |
try { |
208 |
conn = DBConnection.getConnection(); |
String parts[] = list.split(","); |
209 |
Statement stmt = conn.createStatement(); |
|
210 |
ResultSet rs = stmt.executeQuery("SELECT name FROM trainstations WHERE id=" + stationID); |
StringBuilder filter = new StringBuilder(); |
211 |
if (rs.next()) { |
|
212 |
station = rs.getString(1); |
for(String part : parts) { |
213 |
|
if (filter.length() > 0) { |
214 |
|
filter.append( " || " ); |
215 |
|
} |
216 |
|
filter.append("id == ").append(part); |
217 |
|
} |
218 |
|
|
219 |
|
|
220 |
|
pm = PMF.get().getPersistenceManager(); |
221 |
|
Query q = pm.newQuery(JdoStationBean.class); |
222 |
|
q.setFilter( filter.toString() ); |
223 |
|
q.setOrdering("name"); |
224 |
|
|
225 |
|
List<JdoStationBean> beanList = (List<JdoStationBean>) q.execute(); |
226 |
|
|
227 |
|
StationBean stationBean = new StationBean(); |
228 |
|
|
229 |
|
for(JdoStationBean bean : beanList) { |
230 |
|
stationBean.entries.add( bean.toStationEntry() ); |
231 |
} |
} |
232 |
|
|
233 |
} catch (Exception e) { |
return stationBean; |
234 |
} finally { |
} finally { |
235 |
try { |
pm.close(); |
|
if (conn != null && !conn.isClosed()) |
|
|
conn.close(); |
|
|
} catch (Exception e) {} |
|
236 |
} |
} |
237 |
|
|
|
return station; |
|
238 |
} |
} |
239 |
|
|
|
public int getBySpecificName(String name) throws SQLException { |
|
|
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " + |
|
|
"FROM trainstations " + |
|
|
"WHERE name = ? AND enabled = true " + |
|
|
"LIMIT 1 "; |
|
240 |
|
|
241 |
List<StationBean> result; |
@SuppressWarnings("unchecked") |
242 |
Connection conn = null; |
public int getIdByName(String name) { |
243 |
PreparedStatement stmt = null; |
|
244 |
ResultSet res = null; |
List<JdoStationBean> beanList = null; |
245 |
|
|
246 |
|
PersistenceManager pm = null; |
247 |
|
|
248 |
try { |
try { |
249 |
conn = DBConnection.getConnection(); |
|
250 |
stmt = conn.prepareStatement(SQL); |
String filter = " name == '" + name + "'"; |
251 |
|
|
252 |
stmt.setString(1, name ); |
pm = PMF.get().getPersistenceManager(); |
253 |
|
Query q = pm.newQuery(JdoStationBean.class); |
254 |
res = stmt.executeQuery(); |
q.setFilter(filter); |
255 |
result = convertResultset(res); |
|
256 |
|
beanList = (List<JdoStationBean>) q.execute(); |
257 |
|
|
258 |
|
StationBean stationBean = new StationBean(); |
259 |
|
|
260 |
|
for(JdoStationBean bean : beanList) { |
261 |
|
stationBean.entries.add( bean.toStationEntry() ); |
262 |
|
} |
263 |
|
|
264 |
|
|
265 |
} finally { |
} finally { |
266 |
if (res != null) |
pm.close(); |
|
res.close(); |
|
|
if (stmt != null) |
|
|
stmt.close(); |
|
|
if (conn!= null) |
|
|
conn.close(); |
|
267 |
} |
} |
268 |
|
|
269 |
if (result.size() == 1) { |
if ( beanList != null && beanList.size() == 1) { |
270 |
return result.get(0).getId(); |
return (int) beanList.get(0).getId(); |
271 |
} else { |
} else { |
272 |
return -1; |
return -1; |
273 |
} |
} |
274 |
} |
} |
275 |
|
|
276 |
|
@SuppressWarnings("unchecked") |
277 |
|
public int saveStations(StationBean stationBean) throws IOException { |
278 |
|
PersistenceManager pm = null; |
279 |
|
|
280 |
|
try { |
281 |
|
pm = PMF.get().getPersistenceManager(); |
282 |
|
|
283 |
|
List oldEntries = (List) pm.newQuery(JdoStationBean.class).execute(); |
284 |
|
pm.deletePersistentAll(oldEntries); |
285 |
|
|
286 |
|
List<JdoStationBean> jdoList = new ArrayList<JdoStationBean>(); |
287 |
|
for (StationEntry station : stationBean.entries) { |
288 |
|
JdoStationBean jdoBean = JdoStationBean.fromStationEntry(station); |
289 |
|
|
290 |
|
jdoList.add(jdoBean); |
291 |
|
|
292 |
|
} |
293 |
|
pm.makePersistentAll(jdoList); |
294 |
|
|
295 |
|
return jdoList.size(); |
296 |
|
|
297 |
|
} finally { |
298 |
|
pm.close(); |
299 |
|
} |
300 |
|
} |
301 |
|
|
302 |
} |
} |