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