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 |
|
|
import java.util.List; |
8 |
torben |
895 |
import java.util.logging.Logger; |
9 |
torben |
588 |
|
10 |
torben |
1093 |
import javax.jdo.PersistenceManager; |
11 |
torben |
1105 |
import javax.jdo.Query; |
12 |
torben |
1093 |
|
13 |
torben |
1061 |
import dk.thoerup.android.traininfo.common.StationBean; |
14 |
|
|
import dk.thoerup.android.traininfo.common.StationBean.StationEntry; |
15 |
torben |
1105 |
import dk.thoerup.traininfoservice.geo.Geo; |
16 |
torben |
1093 |
import dk.thoerup.traininfoservice.jdo.JdoStationBean; |
17 |
|
|
import dk.thoerup.traininfoservice.jdo.PMF; |
18 |
torben |
1060 |
|
19 |
torben |
588 |
public class StationDAO { |
20 |
torben |
997 |
final static int LOCATION_LIMIT = 8; |
21 |
torben |
895 |
static final Logger logger = Logger.getLogger(StationDAO.class.getName()); |
22 |
torben |
836 |
|
23 |
|
|
|
24 |
torben |
1105 |
public StationEntry getById(int id) { |
25 |
torben |
836 |
|
26 |
torben |
1093 |
PersistenceManager pm = null; |
27 |
|
|
|
28 |
torben |
588 |
try { |
29 |
torben |
1093 |
pm = PMF.get().getPersistenceManager(); |
30 |
torben |
1105 |
JdoStationBean bean =pm.getObjectById(JdoStationBean.class, new Integer(id) ); |
31 |
|
|
return bean.toStationEntry(); |
32 |
torben |
588 |
} finally { |
33 |
torben |
1093 |
pm.close(); |
34 |
torben |
588 |
} |
35 |
|
|
} |
36 |
torben |
836 |
|
37 |
torben |
1105 |
/* damn that JDO sucks so we to the filtering in java-code */ |
38 |
|
|
public StationBean getByName(String searchname) { |
39 |
|
|
PersistenceManager pm = null; |
40 |
|
|
|
41 |
torben |
588 |
try { |
42 |
torben |
1105 |
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 |
torben |
836 |
|
65 |
torben |
1105 |
return stationBean; |
66 |
torben |
588 |
} finally { |
67 |
torben |
1105 |
pm.close(); |
68 |
torben |
588 |
} |
69 |
|
|
} |
70 |
torben |
836 |
|
71 |
|
|
|
72 |
torben |
1105 |
public StationBean getByLocation(double latitude, double longitude) { |
73 |
|
|
|
74 |
torben |
836 |
|
75 |
torben |
841 |
|
76 |
torben |
1105 |
PersistenceManager pm = null; |
77 |
torben |
841 |
|
78 |
torben |
1105 |
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 |
torben |
849 |
|
85 |
torben |
1105 |
for(JdoStationBean bean : beanList) { |
86 |
|
|
double meter = Geo.distanceKM(latitude, longitude, bean.getLatitude(), bean.getLongitude()) * 1000.0; |
87 |
|
|
|
88 |
|
|
bean.distance = (int) meter; |
89 |
torben |
841 |
} |
90 |
torben |
894 |
|
91 |
torben |
836 |
|
92 |
torben |
1105 |
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 |
torben |
836 |
|
109 |
torben |
1105 |
return stationBean; |
110 |
torben |
836 |
} finally { |
111 |
torben |
1105 |
pm.close(); |
112 |
torben |
836 |
} |
113 |
|
|
} |
114 |
torben |
1105 |
|
115 |
torben |
836 |
|
116 |
torben |
1105 |
public StationBean getByList(String list) { |
117 |
|
|
PersistenceManager pm = null; |
118 |
|
|
|
119 |
torben |
836 |
try { |
120 |
torben |
1105 |
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 |
torben |
836 |
} |
145 |
|
|
|
146 |
torben |
1105 |
return stationBean; |
147 |
torben |
836 |
} finally { |
148 |
torben |
1105 |
pm.close(); |
149 |
torben |
836 |
} |
150 |
torben |
650 |
|
151 |
torben |
836 |
} |
152 |
torben |
650 |
|
153 |
|
|
|
154 |
torben |
1105 |
public int getIdByName(String name) { |
155 |
|
|
|
156 |
|
|
List<JdoStationBean> beanList = null; |
157 |
|
|
|
158 |
|
|
PersistenceManager pm = null; |
159 |
|
|
|
160 |
torben |
836 |
try { |
161 |
torben |
1105 |
|
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 |
torben |
836 |
} finally { |
177 |
torben |
1105 |
pm.close(); |
178 |
torben |
836 |
} |
179 |
|
|
|
180 |
torben |
1105 |
if ( beanList != null && beanList.size() == 1) { |
181 |
|
|
return (int) beanList.get(0).getId(); |
182 |
torben |
836 |
} else { |
183 |
|
|
return -1; |
184 |
|
|
} |
185 |
|
|
} |
186 |
torben |
1105 |
|
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 |
torben |
588 |
} |