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 |
torben |
1415 |
import dk.thoerup.android.traininfo.common.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 |
torben |
1116 |
@SuppressWarnings("unchecked") |
38 |
torben |
1110 |
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 |
torben |
836 |
|
50 |
|
|
|
51 |
torben |
1105 |
public StationEntry getById(int id) { |
52 |
torben |
836 |
|
53 |
torben |
1093 |
PersistenceManager pm = null; |
54 |
|
|
|
55 |
torben |
588 |
try { |
56 |
torben |
1093 |
pm = PMF.get().getPersistenceManager(); |
57 |
torben |
1105 |
JdoStationBean bean =pm.getObjectById(JdoStationBean.class, new Integer(id) ); |
58 |
|
|
return bean.toStationEntry(); |
59 |
torben |
588 |
} finally { |
60 |
torben |
1093 |
pm.close(); |
61 |
torben |
588 |
} |
62 |
|
|
} |
63 |
torben |
836 |
|
64 |
torben |
1105 |
/* damn that JDO sucks so we to the filtering in java-code */ |
65 |
|
|
public StationBean getByName(String searchname) { |
66 |
|
|
|
67 |
torben |
1116 |
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 |
torben |
1703 |
} /*else { |
77 |
torben |
1116 |
for (String alias : bean.aliases ) { |
78 |
|
|
if (alias.toLowerCase().startsWith(searchname)) { |
79 |
|
|
stationBean.entries.add( bean.toStationEntry() ); |
80 |
torben |
1105 |
} |
81 |
|
|
} |
82 |
torben |
1703 |
}*/ |
83 |
torben |
1116 |
} |
84 |
torben |
836 |
|
85 |
torben |
1116 |
Collections.sort(stationBean.entries, new Comparator<StationEntry>() { |
86 |
|
|
@Override |
87 |
|
|
public int compare(StationEntry arg0, StationEntry arg1) { |
88 |
|
|
return arg0.getName().compareTo( arg1.getName() ); |
89 |
|
|
} |
90 |
|
|
}); |
91 |
|
|
|
92 |
|
|
return stationBean; |
93 |
|
|
|
94 |
torben |
588 |
} |
95 |
torben |
1106 |
|
96 |
torben |
1116 |
@SuppressWarnings("unchecked") |
97 |
torben |
1110 |
public List<JdoStationBean> getAllStations() { |
98 |
|
|
final String key = "allstations"; |
99 |
|
|
List<JdoStationBean> result = (List<JdoStationBean>) cache.get(key); |
100 |
|
|
|
101 |
|
|
if (result == null) { |
102 |
|
|
logger.info("getAllStations Cache miss"); |
103 |
|
|
|
104 |
|
|
PersistenceManager pm = null; |
105 |
|
|
|
106 |
|
|
try { |
107 |
|
|
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; |
127 |
|
|
|
128 |
|
|
} |
129 |
|
|
|
130 |
torben |
836 |
|
131 |
torben |
1106 |
//String limitExpression = (geolimit == true) ? "AND abs(latitude-?)<0.4 AND abs(longitude-?)<0.75 " : ""; |
132 |
torben |
1110 |
/* |
133 |
torben |
1106 |
public List<JdoStationBean> getByLocationList(double latitude, double longitude, boolean geolimit) { |
134 |
|
|
|
135 |
torben |
1105 |
PersistenceManager pm = null; |
136 |
torben |
1106 |
final double LAT = 0.4; |
137 |
|
|
final double LNG = 0.75; |
138 |
torben |
841 |
|
139 |
torben |
1105 |
try { |
140 |
|
|
pm = PMF.get().getPersistenceManager(); |
141 |
torben |
1106 |
Query q = pm.newQuery(JdoStationBean.class); |
142 |
torben |
1105 |
|
143 |
|
|
|
144 |
torben |
1106 |
|
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 |
torben |
849 |
|
151 |
torben |
1106 |
String filter = String.format("latitude > %f && latitude < %f", minLat, maxLat); |
152 |
torben |
1105 |
|
153 |
torben |
1106 |
q.setFilter( filter ); |
154 |
torben |
841 |
} |
155 |
torben |
894 |
|
156 |
torben |
1106 |
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 |
torben |
1110 |
}*/ |
165 |
torben |
836 |
|
166 |
torben |
1106 |
|
167 |
|
|
public StationBean getByLocation(double latitude, double longitude) { |
168 |
|
|
|
169 |
torben |
1110 |
List<JdoStationBean> beanList = getAllStations(); |
170 |
torben |
1106 |
|
171 |
|
|
StationBean stationBean = new StationBean(); |
172 |
|
|
|
173 |
|
|
|
174 |
|
|
Geo location = new Geo(latitude,longitude); |
175 |
|
|
for(JdoStationBean bean : beanList) { |
176 |
|
|
double meter = Geo.distanceKM( location, new Geo(bean.getLatitude(), bean.getLongitude() )) * 1000.0; |
177 |
|
|
|
178 |
|
|
bean.distance = (int) meter; |
179 |
|
|
} |
180 |
|
|
|
181 |
|
|
|
182 |
|
|
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 |
torben |
1105 |
} |
192 |
|
|
} |
193 |
torben |
1106 |
}); |
194 |
torben |
836 |
|
195 |
torben |
1106 |
for (int i=0; i<LOCATION_LIMIT && i<beanList.size(); i++) { |
196 |
|
|
stationBean.entries.add( beanList.get(i).toStationEntry() ); |
197 |
torben |
836 |
} |
198 |
torben |
1106 |
|
199 |
|
|
return stationBean; |
200 |
torben |
836 |
} |
201 |
torben |
1105 |
|
202 |
torben |
836 |
|
203 |
torben |
1116 |
@SuppressWarnings("unchecked") |
204 |
torben |
1105 |
public StationBean getByList(String list) { |
205 |
|
|
PersistenceManager pm = null; |
206 |
|
|
|
207 |
torben |
836 |
try { |
208 |
torben |
1105 |
String parts[] = list.split(","); |
209 |
|
|
|
210 |
|
|
StringBuilder filter = new StringBuilder(); |
211 |
|
|
|
212 |
|
|
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 |
torben |
836 |
} |
232 |
|
|
|
233 |
torben |
1105 |
return stationBean; |
234 |
torben |
836 |
} finally { |
235 |
torben |
1105 |
pm.close(); |
236 |
torben |
836 |
} |
237 |
torben |
650 |
|
238 |
torben |
836 |
} |
239 |
torben |
650 |
|
240 |
|
|
|
241 |
torben |
1116 |
@SuppressWarnings("unchecked") |
242 |
torben |
1105 |
public int getIdByName(String name) { |
243 |
|
|
|
244 |
|
|
List<JdoStationBean> beanList = null; |
245 |
|
|
|
246 |
|
|
PersistenceManager pm = null; |
247 |
|
|
|
248 |
torben |
836 |
try { |
249 |
torben |
1105 |
|
250 |
|
|
String filter = " name == '" + name + "'"; |
251 |
|
|
|
252 |
|
|
pm = PMF.get().getPersistenceManager(); |
253 |
|
|
Query q = pm.newQuery(JdoStationBean.class); |
254 |
|
|
q.setFilter(filter); |
255 |
|
|
|
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 |
torben |
1115 |
} |
263 |
torben |
1105 |
|
264 |
torben |
1115 |
|
265 |
torben |
836 |
} finally { |
266 |
torben |
1105 |
pm.close(); |
267 |
torben |
836 |
} |
268 |
|
|
|
269 |
torben |
1105 |
if ( beanList != null && beanList.size() == 1) { |
270 |
|
|
return (int) beanList.get(0).getId(); |
271 |
torben |
836 |
} else { |
272 |
|
|
return -1; |
273 |
|
|
} |
274 |
|
|
} |
275 |
torben |
1105 |
|
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 |
torben |
588 |
} |