1 |
torben |
588 |
package dk.thoerup.traininfoservice; |
2 |
|
|
|
3 |
torben |
1105 |
import java.io.IOException; |
4 |
|
|
import java.util.ArrayList; |
5 |
|
|
import java.util.Collections; |
6 |
|
|
import java.util.Comparator; |
7 |
torben |
1110 |
import java.util.HashMap; |
8 |
torben |
1105 |
import java.util.List; |
9 |
torben |
1110 |
import java.util.Map; |
10 |
|
|
import java.util.logging.Level; |
11 |
torben |
895 |
import java.util.logging.Logger; |
12 |
torben |
588 |
|
13 |
torben |
1110 |
import javax.jdo.Extent; |
14 |
torben |
1093 |
import javax.jdo.PersistenceManager; |
15 |
torben |
1105 |
import javax.jdo.Query; |
16 |
torben |
1093 |
|
17 |
torben |
1110 |
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 |
torben |
1061 |
import dk.thoerup.android.traininfo.common.StationBean; |
24 |
|
|
import dk.thoerup.android.traininfo.common.StationBean.StationEntry; |
25 |
torben |
1105 |
import dk.thoerup.traininfoservice.geo.Geo; |
26 |
torben |
1093 |
import dk.thoerup.traininfoservice.jdo.JdoStationBean; |
27 |
|
|
import dk.thoerup.traininfoservice.jdo.PMF; |
28 |
torben |
1060 |
|
29 |
torben |
588 |
public class StationDAO { |
30 |
torben |
997 |
final static int LOCATION_LIMIT = 8; |
31 |
torben |
895 |
static final Logger logger = Logger.getLogger(StationDAO.class.getName()); |
32 |
torben |
1110 |
|
33 |
torben |
1113 |
final int TIMEOUT_SECONDS = 30*60; |
34 |
torben |
1110 |
|
35 |
|
|
Cache cache; |
36 |
|
|
|
37 |
|
|
public StationDAO() { |
38 |
|
|
Map props = new HashMap(); |
39 |
|
|
props.put(GCacheFactory.EXPIRATION_DELTA, TIMEOUT_SECONDS); |
40 |
|
|
|
41 |
|
|
try { |
42 |
|
|
cache = CacheManager.getInstance().getCacheFactory().createCache(props); |
43 |
|
|
} catch (CacheException e) { |
44 |
|
|
logger.log(Level.WARNING, "error creating cache", e); |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
} |
48 |
torben |
836 |
|
49 |
|
|
|
50 |
torben |
1105 |
public StationEntry getById(int id) { |
51 |
torben |
836 |
|
52 |
torben |
1093 |
PersistenceManager pm = null; |
53 |
|
|
|
54 |
torben |
588 |
try { |
55 |
torben |
1093 |
pm = PMF.get().getPersistenceManager(); |
56 |
torben |
1105 |
JdoStationBean bean =pm.getObjectById(JdoStationBean.class, new Integer(id) ); |
57 |
|
|
return bean.toStationEntry(); |
58 |
torben |
588 |
} finally { |
59 |
torben |
1093 |
pm.close(); |
60 |
torben |
588 |
} |
61 |
|
|
} |
62 |
torben |
836 |
|
63 |
torben |
1105 |
/* damn that JDO sucks so we to the filtering in java-code */ |
64 |
|
|
public StationBean getByName(String searchname) { |
65 |
|
|
PersistenceManager pm = null; |
66 |
|
|
|
67 |
torben |
588 |
try { |
68 |
torben |
1110 |
/*pm = PMF.get().getPersistenceManager(); |
69 |
torben |
1105 |
|
70 |
|
|
|
71 |
|
|
|
72 |
|
|
Query q = pm.newQuery(JdoStationBean.class); |
73 |
|
|
q.setOrdering("name"); |
74 |
torben |
1110 |
List<JdoStationBean> beanList = (List<JdoStationBean>) q.execute();*/ |
75 |
torben |
1105 |
|
76 |
torben |
1110 |
List<JdoStationBean> beanList = getAllStations(); |
77 |
|
|
|
78 |
|
|
|
79 |
torben |
1105 |
StationBean stationBean = new StationBean(); |
80 |
|
|
|
81 |
|
|
searchname = searchname.toLowerCase(); |
82 |
|
|
for(JdoStationBean bean : beanList) { |
83 |
|
|
if (bean.getName().toLowerCase().startsWith(searchname)) { |
84 |
|
|
stationBean.entries.add( bean.toStationEntry() ); |
85 |
|
|
} else { |
86 |
|
|
for (String alias : bean.aliases ) { |
87 |
|
|
if (alias.toLowerCase().startsWith(searchname)) { |
88 |
|
|
stationBean.entries.add( bean.toStationEntry() ); |
89 |
|
|
} |
90 |
|
|
} |
91 |
|
|
} |
92 |
|
|
} |
93 |
torben |
1115 |
|
94 |
|
|
Collections.sort(stationBean.entries, new Comparator<StationEntry>() { |
95 |
|
|
@Override |
96 |
|
|
public int compare(StationEntry arg0, StationEntry arg1) { |
97 |
|
|
return arg0.getName().compareTo( arg1.getName() ); |
98 |
|
|
} |
99 |
|
|
}); |
100 |
torben |
836 |
|
101 |
torben |
1105 |
return stationBean; |
102 |
torben |
588 |
} finally { |
103 |
torben |
1110 |
// pm.close(); |
104 |
torben |
588 |
} |
105 |
|
|
} |
106 |
torben |
1106 |
|
107 |
torben |
1110 |
public List<JdoStationBean> getAllStations() { |
108 |
|
|
final String key = "allstations"; |
109 |
|
|
List<JdoStationBean> result = (List<JdoStationBean>) cache.get(key); |
110 |
|
|
|
111 |
|
|
if (result == null) { |
112 |
|
|
logger.info("getAllStations Cache miss"); |
113 |
|
|
|
114 |
|
|
PersistenceManager pm = null; |
115 |
|
|
final double LAT = 0.4; |
116 |
|
|
final double LNG = 0.75; |
117 |
|
|
|
118 |
|
|
try { |
119 |
|
|
pm = PMF.get().getPersistenceManager(); |
120 |
|
|
Extent<JdoStationBean> all = pm.getExtent(JdoStationBean.class, false); |
121 |
|
|
|
122 |
|
|
result = new ArrayList<JdoStationBean>(); |
123 |
|
|
for (JdoStationBean station : all) { |
124 |
|
|
result.add(station); |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
cache.put(key, result); |
128 |
|
|
|
129 |
|
|
} finally { |
130 |
|
|
pm.close(); |
131 |
|
|
} |
132 |
|
|
} else { |
133 |
|
|
logger.info("getAllStations Cache hit"); |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
|
137 |
|
|
|
138 |
|
|
return result; |
139 |
|
|
|
140 |
|
|
} |
141 |
|
|
|
142 |
torben |
836 |
|
143 |
torben |
1106 |
//String limitExpression = (geolimit == true) ? "AND abs(latitude-?)<0.4 AND abs(longitude-?)<0.75 " : ""; |
144 |
torben |
1110 |
/* |
145 |
torben |
1106 |
public List<JdoStationBean> getByLocationList(double latitude, double longitude, boolean geolimit) { |
146 |
|
|
|
147 |
torben |
1105 |
PersistenceManager pm = null; |
148 |
torben |
1106 |
final double LAT = 0.4; |
149 |
|
|
final double LNG = 0.75; |
150 |
torben |
841 |
|
151 |
torben |
1105 |
try { |
152 |
|
|
pm = PMF.get().getPersistenceManager(); |
153 |
torben |
1106 |
Query q = pm.newQuery(JdoStationBean.class); |
154 |
torben |
1105 |
|
155 |
|
|
|
156 |
torben |
1106 |
|
157 |
|
|
if (geolimit == true) { |
158 |
|
|
double minLat = latitude - LAT; |
159 |
|
|
double maxLat = latitude + LAT; |
160 |
|
|
|
161 |
|
|
//DAMN JDO implementation only allows us to compare on one parameter |
162 |
torben |
849 |
|
163 |
torben |
1106 |
String filter = String.format("latitude > %f && latitude < %f", minLat, maxLat); |
164 |
torben |
1105 |
|
165 |
torben |
1106 |
q.setFilter( filter ); |
166 |
torben |
841 |
} |
167 |
torben |
894 |
|
168 |
torben |
1106 |
List<JdoStationBean> beanList = (List<JdoStationBean>) q.execute(); |
169 |
|
|
|
170 |
|
|
logger.info("beanList size " + beanList.size()); |
171 |
|
|
|
172 |
|
|
return beanList; |
173 |
|
|
} finally { |
174 |
|
|
pm.close(); |
175 |
|
|
} |
176 |
torben |
1110 |
}*/ |
177 |
torben |
836 |
|
178 |
torben |
1106 |
|
179 |
|
|
public StationBean getByLocation(double latitude, double longitude) { |
180 |
torben |
1110 |
/* |
181 |
torben |
1106 |
List<JdoStationBean> beanList = getByLocationList(latitude,longitude,true); |
182 |
|
|
|
183 |
|
|
if (beanList.size() < LOCATION_LIMIT ) { |
184 |
|
|
logger.info("getByLocation failover: " +latitude + "," + longitude); |
185 |
|
|
beanList = getByLocationList(latitude,longitude, false); |
186 |
torben |
1110 |
}*/ |
187 |
|
|
|
188 |
|
|
List<JdoStationBean> beanList = getAllStations(); |
189 |
torben |
1106 |
|
190 |
|
|
StationBean stationBean = new StationBean(); |
191 |
|
|
|
192 |
|
|
|
193 |
|
|
Geo location = new Geo(latitude,longitude); |
194 |
|
|
for(JdoStationBean bean : beanList) { |
195 |
|
|
double meter = Geo.distanceKM( location, new Geo(bean.getLatitude(), bean.getLongitude() )) * 1000.0; |
196 |
|
|
|
197 |
|
|
bean.distance = (int) meter; |
198 |
|
|
} |
199 |
|
|
|
200 |
|
|
|
201 |
|
|
Collections.sort(beanList, new Comparator<JdoStationBean>() { |
202 |
|
|
@Override |
203 |
|
|
public int compare(JdoStationBean o1, JdoStationBean o2) { |
204 |
|
|
if (o1.distance < o2.distance) { |
205 |
|
|
return -1; |
206 |
|
|
} else if (o1.distance > o2.distance) { |
207 |
|
|
return 1; |
208 |
|
|
} else { |
209 |
|
|
return 0; |
210 |
torben |
1105 |
} |
211 |
|
|
} |
212 |
torben |
1106 |
}); |
213 |
torben |
836 |
|
214 |
torben |
1106 |
for (int i=0; i<LOCATION_LIMIT && i<beanList.size(); i++) { |
215 |
|
|
stationBean.entries.add( beanList.get(i).toStationEntry() ); |
216 |
torben |
836 |
} |
217 |
torben |
1106 |
|
218 |
|
|
return stationBean; |
219 |
torben |
836 |
} |
220 |
torben |
1105 |
|
221 |
torben |
836 |
|
222 |
torben |
1105 |
public StationBean getByList(String list) { |
223 |
|
|
PersistenceManager pm = null; |
224 |
|
|
|
225 |
torben |
836 |
try { |
226 |
torben |
1105 |
String parts[] = list.split(","); |
227 |
|
|
|
228 |
|
|
StringBuilder filter = new StringBuilder(); |
229 |
|
|
|
230 |
|
|
for(String part : parts) { |
231 |
|
|
if (filter.length() > 0) { |
232 |
|
|
filter.append( " || " ); |
233 |
|
|
} |
234 |
|
|
filter.append("id == ").append(part); |
235 |
|
|
} |
236 |
|
|
|
237 |
|
|
//String filter = "id == 10 || id == 82"; //TODO: build filter |
238 |
|
|
|
239 |
|
|
pm = PMF.get().getPersistenceManager(); |
240 |
|
|
Query q = pm.newQuery(JdoStationBean.class); |
241 |
|
|
q.setFilter( filter.toString() ); |
242 |
|
|
q.setOrdering("name"); |
243 |
|
|
|
244 |
|
|
List<JdoStationBean> beanList = (List<JdoStationBean>) q.execute(); |
245 |
|
|
|
246 |
|
|
StationBean stationBean = new StationBean(); |
247 |
|
|
|
248 |
|
|
for(JdoStationBean bean : beanList) { |
249 |
|
|
stationBean.entries.add( bean.toStationEntry() ); |
250 |
torben |
836 |
} |
251 |
|
|
|
252 |
torben |
1105 |
return stationBean; |
253 |
torben |
836 |
} finally { |
254 |
torben |
1105 |
pm.close(); |
255 |
torben |
836 |
} |
256 |
torben |
650 |
|
257 |
torben |
836 |
} |
258 |
torben |
650 |
|
259 |
|
|
|
260 |
torben |
1105 |
public int getIdByName(String name) { |
261 |
|
|
|
262 |
|
|
List<JdoStationBean> beanList = null; |
263 |
|
|
|
264 |
|
|
PersistenceManager pm = null; |
265 |
|
|
|
266 |
torben |
836 |
try { |
267 |
torben |
1105 |
|
268 |
|
|
String filter = " name == '" + name + "'"; |
269 |
|
|
|
270 |
|
|
pm = PMF.get().getPersistenceManager(); |
271 |
|
|
Query q = pm.newQuery(JdoStationBean.class); |
272 |
|
|
q.setFilter(filter); |
273 |
|
|
|
274 |
|
|
beanList = (List<JdoStationBean>) q.execute(); |
275 |
|
|
|
276 |
|
|
StationBean stationBean = new StationBean(); |
277 |
|
|
|
278 |
|
|
for(JdoStationBean bean : beanList) { |
279 |
|
|
stationBean.entries.add( bean.toStationEntry() ); |
280 |
torben |
1115 |
} |
281 |
torben |
1105 |
|
282 |
torben |
1115 |
|
283 |
torben |
836 |
} finally { |
284 |
torben |
1105 |
pm.close(); |
285 |
torben |
836 |
} |
286 |
|
|
|
287 |
torben |
1105 |
if ( beanList != null && beanList.size() == 1) { |
288 |
|
|
return (int) beanList.get(0).getId(); |
289 |
torben |
836 |
} else { |
290 |
|
|
return -1; |
291 |
|
|
} |
292 |
|
|
} |
293 |
torben |
1105 |
|
294 |
|
|
@SuppressWarnings("unchecked") |
295 |
|
|
public int saveStations(StationBean stationBean) throws IOException { |
296 |
|
|
PersistenceManager pm = null; |
297 |
|
|
|
298 |
|
|
try { |
299 |
|
|
pm = PMF.get().getPersistenceManager(); |
300 |
|
|
|
301 |
|
|
List oldEntries = (List) pm.newQuery(JdoStationBean.class).execute(); |
302 |
|
|
pm.deletePersistentAll(oldEntries); |
303 |
|
|
|
304 |
|
|
List<JdoStationBean> jdoList = new ArrayList<JdoStationBean>(); |
305 |
|
|
for (StationEntry station : stationBean.entries) { |
306 |
|
|
JdoStationBean jdoBean = JdoStationBean.fromStationEntry(station); |
307 |
|
|
|
308 |
|
|
jdoList.add(jdoBean); |
309 |
|
|
|
310 |
|
|
} |
311 |
|
|
pm.makePersistentAll(jdoList); |
312 |
|
|
|
313 |
|
|
return jdoList.size(); |
314 |
|
|
|
315 |
|
|
} finally { |
316 |
|
|
pm.close(); |
317 |
|
|
} |
318 |
|
|
} |
319 |
|
|
|
320 |
torben |
588 |
} |