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 |
1106 |
import java.util.Iterator; |
8 |
torben |
1105 |
import java.util.List; |
9 |
torben |
895 |
import java.util.logging.Logger; |
10 |
torben |
588 |
|
11 |
torben |
1093 |
import javax.jdo.PersistenceManager; |
12 |
torben |
1105 |
import javax.jdo.Query; |
13 |
torben |
1093 |
|
14 |
torben |
1061 |
import dk.thoerup.android.traininfo.common.StationBean; |
15 |
|
|
import dk.thoerup.android.traininfo.common.StationBean.StationEntry; |
16 |
torben |
1105 |
import dk.thoerup.traininfoservice.geo.Geo; |
17 |
torben |
1093 |
import dk.thoerup.traininfoservice.jdo.JdoStationBean; |
18 |
|
|
import dk.thoerup.traininfoservice.jdo.PMF; |
19 |
torben |
1060 |
|
20 |
torben |
588 |
public class StationDAO { |
21 |
torben |
997 |
final static int LOCATION_LIMIT = 8; |
22 |
torben |
895 |
static final Logger logger = Logger.getLogger(StationDAO.class.getName()); |
23 |
torben |
836 |
|
24 |
|
|
|
25 |
torben |
1105 |
public StationEntry getById(int id) { |
26 |
torben |
836 |
|
27 |
torben |
1093 |
PersistenceManager pm = null; |
28 |
|
|
|
29 |
torben |
588 |
try { |
30 |
torben |
1093 |
pm = PMF.get().getPersistenceManager(); |
31 |
torben |
1105 |
JdoStationBean bean =pm.getObjectById(JdoStationBean.class, new Integer(id) ); |
32 |
|
|
return bean.toStationEntry(); |
33 |
torben |
588 |
} finally { |
34 |
torben |
1093 |
pm.close(); |
35 |
torben |
588 |
} |
36 |
|
|
} |
37 |
torben |
836 |
|
38 |
torben |
1105 |
/* damn that JDO sucks so we to the filtering in java-code */ |
39 |
|
|
public StationBean getByName(String searchname) { |
40 |
|
|
PersistenceManager pm = null; |
41 |
|
|
|
42 |
torben |
588 |
try { |
43 |
torben |
1105 |
pm = PMF.get().getPersistenceManager(); |
44 |
|
|
|
45 |
|
|
|
46 |
|
|
|
47 |
|
|
Query q = pm.newQuery(JdoStationBean.class); |
48 |
|
|
q.setOrdering("name"); |
49 |
|
|
List<JdoStationBean> beanList = (List<JdoStationBean>) q.execute(); |
50 |
|
|
|
51 |
|
|
StationBean stationBean = new StationBean(); |
52 |
|
|
|
53 |
|
|
searchname = searchname.toLowerCase(); |
54 |
|
|
for(JdoStationBean bean : beanList) { |
55 |
|
|
if (bean.getName().toLowerCase().startsWith(searchname)) { |
56 |
|
|
stationBean.entries.add( bean.toStationEntry() ); |
57 |
|
|
} else { |
58 |
|
|
for (String alias : bean.aliases ) { |
59 |
|
|
if (alias.toLowerCase().startsWith(searchname)) { |
60 |
|
|
stationBean.entries.add( bean.toStationEntry() ); |
61 |
|
|
} |
62 |
|
|
} |
63 |
|
|
} |
64 |
|
|
} |
65 |
torben |
836 |
|
66 |
torben |
1105 |
return stationBean; |
67 |
torben |
588 |
} finally { |
68 |
torben |
1105 |
pm.close(); |
69 |
torben |
588 |
} |
70 |
|
|
} |
71 |
torben |
1106 |
|
72 |
torben |
836 |
|
73 |
torben |
1106 |
//String limitExpression = (geolimit == true) ? "AND abs(latitude-?)<0.4 AND abs(longitude-?)<0.75 " : ""; |
74 |
|
|
public List<JdoStationBean> getByLocationList(double latitude, double longitude, boolean geolimit) { |
75 |
|
|
|
76 |
torben |
1105 |
PersistenceManager pm = null; |
77 |
torben |
1106 |
final double LAT = 0.4; |
78 |
|
|
final double LNG = 0.75; |
79 |
torben |
841 |
|
80 |
torben |
1105 |
try { |
81 |
|
|
pm = PMF.get().getPersistenceManager(); |
82 |
torben |
1106 |
Query q = pm.newQuery(JdoStationBean.class); |
83 |
torben |
1105 |
|
84 |
|
|
|
85 |
torben |
1106 |
|
86 |
|
|
if (geolimit == true) { |
87 |
|
|
double minLat = latitude - LAT; |
88 |
|
|
double maxLat = latitude + LAT; |
89 |
|
|
|
90 |
|
|
//DAMN JDO implementation only allows us to compare on one parameter |
91 |
torben |
849 |
|
92 |
torben |
1106 |
String filter = String.format("latitude > %f && latitude < %f", minLat, maxLat); |
93 |
torben |
1105 |
|
94 |
torben |
1106 |
q.setFilter( filter ); |
95 |
torben |
841 |
} |
96 |
torben |
894 |
|
97 |
torben |
1106 |
List<JdoStationBean> beanList = (List<JdoStationBean>) q.execute(); |
98 |
|
|
|
99 |
|
|
logger.info("beanList size " + beanList.size()); |
100 |
|
|
|
101 |
|
|
return beanList; |
102 |
|
|
} finally { |
103 |
|
|
pm.close(); |
104 |
|
|
} |
105 |
|
|
} |
106 |
torben |
836 |
|
107 |
torben |
1106 |
|
108 |
|
|
public StationBean getByLocation(double latitude, double longitude) { |
109 |
|
|
|
110 |
|
|
List<JdoStationBean> beanList = getByLocationList(latitude,longitude,true); |
111 |
|
|
|
112 |
|
|
if (beanList.size() < LOCATION_LIMIT ) { |
113 |
|
|
logger.info("getByLocation failover: " +latitude + "," + longitude); |
114 |
|
|
beanList = getByLocationList(latitude,longitude, false); |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
StationBean stationBean = new StationBean(); |
118 |
|
|
|
119 |
|
|
|
120 |
|
|
Geo location = new Geo(latitude,longitude); |
121 |
|
|
for(JdoStationBean bean : beanList) { |
122 |
|
|
double meter = Geo.distanceKM( location, new Geo(bean.getLatitude(), bean.getLongitude() )) * 1000.0; |
123 |
|
|
|
124 |
|
|
bean.distance = (int) meter; |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
|
128 |
|
|
Collections.sort(beanList, new Comparator<JdoStationBean>() { |
129 |
|
|
@Override |
130 |
|
|
public int compare(JdoStationBean o1, JdoStationBean o2) { |
131 |
|
|
if (o1.distance < o2.distance) { |
132 |
|
|
return -1; |
133 |
|
|
} else if (o1.distance > o2.distance) { |
134 |
|
|
return 1; |
135 |
|
|
} else { |
136 |
|
|
return 0; |
137 |
torben |
1105 |
} |
138 |
|
|
} |
139 |
torben |
1106 |
}); |
140 |
torben |
836 |
|
141 |
torben |
1106 |
for (int i=0; i<LOCATION_LIMIT && i<beanList.size(); i++) { |
142 |
|
|
stationBean.entries.add( beanList.get(i).toStationEntry() ); |
143 |
torben |
836 |
} |
144 |
torben |
1106 |
|
145 |
|
|
return stationBean; |
146 |
torben |
836 |
} |
147 |
torben |
1105 |
|
148 |
torben |
836 |
|
149 |
torben |
1105 |
public StationBean getByList(String list) { |
150 |
|
|
PersistenceManager pm = null; |
151 |
|
|
|
152 |
torben |
836 |
try { |
153 |
torben |
1105 |
String parts[] = list.split(","); |
154 |
|
|
|
155 |
|
|
StringBuilder filter = new StringBuilder(); |
156 |
|
|
|
157 |
|
|
for(String part : parts) { |
158 |
|
|
if (filter.length() > 0) { |
159 |
|
|
filter.append( " || " ); |
160 |
|
|
} |
161 |
|
|
filter.append("id == ").append(part); |
162 |
|
|
} |
163 |
|
|
|
164 |
|
|
//String filter = "id == 10 || id == 82"; //TODO: build filter |
165 |
|
|
|
166 |
|
|
pm = PMF.get().getPersistenceManager(); |
167 |
|
|
Query q = pm.newQuery(JdoStationBean.class); |
168 |
|
|
q.setFilter( filter.toString() ); |
169 |
|
|
q.setOrdering("name"); |
170 |
|
|
|
171 |
|
|
List<JdoStationBean> beanList = (List<JdoStationBean>) q.execute(); |
172 |
|
|
|
173 |
|
|
StationBean stationBean = new StationBean(); |
174 |
|
|
|
175 |
|
|
for(JdoStationBean bean : beanList) { |
176 |
|
|
stationBean.entries.add( bean.toStationEntry() ); |
177 |
torben |
836 |
} |
178 |
|
|
|
179 |
torben |
1105 |
return stationBean; |
180 |
torben |
836 |
} finally { |
181 |
torben |
1105 |
pm.close(); |
182 |
torben |
836 |
} |
183 |
torben |
650 |
|
184 |
torben |
836 |
} |
185 |
torben |
650 |
|
186 |
|
|
|
187 |
torben |
1105 |
public int getIdByName(String name) { |
188 |
|
|
|
189 |
|
|
List<JdoStationBean> beanList = null; |
190 |
|
|
|
191 |
|
|
PersistenceManager pm = null; |
192 |
|
|
|
193 |
torben |
836 |
try { |
194 |
torben |
1105 |
|
195 |
|
|
String filter = " name == '" + name + "'"; |
196 |
|
|
|
197 |
|
|
pm = PMF.get().getPersistenceManager(); |
198 |
|
|
Query q = pm.newQuery(JdoStationBean.class); |
199 |
|
|
q.setFilter(filter); |
200 |
|
|
|
201 |
|
|
beanList = (List<JdoStationBean>) q.execute(); |
202 |
|
|
|
203 |
|
|
StationBean stationBean = new StationBean(); |
204 |
|
|
|
205 |
|
|
for(JdoStationBean bean : beanList) { |
206 |
|
|
stationBean.entries.add( bean.toStationEntry() ); |
207 |
|
|
} |
208 |
|
|
|
209 |
torben |
836 |
} finally { |
210 |
torben |
1105 |
pm.close(); |
211 |
torben |
836 |
} |
212 |
|
|
|
213 |
torben |
1105 |
if ( beanList != null && beanList.size() == 1) { |
214 |
|
|
return (int) beanList.get(0).getId(); |
215 |
torben |
836 |
} else { |
216 |
|
|
return -1; |
217 |
|
|
} |
218 |
|
|
} |
219 |
torben |
1105 |
|
220 |
|
|
@SuppressWarnings("unchecked") |
221 |
|
|
public int saveStations(StationBean stationBean) throws IOException { |
222 |
|
|
PersistenceManager pm = null; |
223 |
|
|
|
224 |
|
|
try { |
225 |
|
|
pm = PMF.get().getPersistenceManager(); |
226 |
|
|
|
227 |
|
|
List oldEntries = (List) pm.newQuery(JdoStationBean.class).execute(); |
228 |
|
|
pm.deletePersistentAll(oldEntries); |
229 |
|
|
|
230 |
|
|
List<JdoStationBean> jdoList = new ArrayList<JdoStationBean>(); |
231 |
|
|
for (StationEntry station : stationBean.entries) { |
232 |
|
|
JdoStationBean jdoBean = JdoStationBean.fromStationEntry(station); |
233 |
|
|
|
234 |
|
|
jdoList.add(jdoBean); |
235 |
|
|
|
236 |
|
|
} |
237 |
|
|
pm.makePersistentAll(jdoList); |
238 |
|
|
|
239 |
|
|
return jdoList.size(); |
240 |
|
|
|
241 |
|
|
} finally { |
242 |
|
|
pm.close(); |
243 |
|
|
} |
244 |
|
|
} |
245 |
|
|
|
246 |
torben |
588 |
} |