4 |
import java.util.ArrayList; |
import java.util.ArrayList; |
5 |
import java.util.Collections; |
import java.util.Collections; |
6 |
import java.util.Comparator; |
import java.util.Comparator; |
7 |
import java.util.Iterator; |
import java.util.HashMap; |
8 |
import java.util.List; |
import java.util.List; |
9 |
|
import java.util.Map; |
10 |
|
import java.util.logging.Level; |
11 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
12 |
|
|
13 |
|
import javax.jdo.Extent; |
14 |
import javax.jdo.PersistenceManager; |
import javax.jdo.PersistenceManager; |
15 |
import javax.jdo.Query; |
import javax.jdo.Query; |
16 |
|
|
17 |
|
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 |
import dk.thoerup.android.traininfo.common.StationBean; |
import dk.thoerup.android.traininfo.common.StationBean; |
24 |
import dk.thoerup.android.traininfo.common.StationBean.StationEntry; |
import dk.thoerup.android.traininfo.common.StationBean.StationEntry; |
25 |
import dk.thoerup.traininfoservice.geo.Geo; |
import dk.thoerup.traininfoservice.geo.Geo; |
29 |
public class StationDAO { |
public class StationDAO { |
30 |
final static int LOCATION_LIMIT = 8; |
final static int LOCATION_LIMIT = 8; |
31 |
static final Logger logger = Logger.getLogger(StationDAO.class.getName()); |
static final Logger logger = Logger.getLogger(StationDAO.class.getName()); |
32 |
|
|
33 |
|
final int TIMEOUT_SECONDS = 10*60; |
34 |
|
|
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 |
|
|
49 |
|
|
50 |
public StationEntry getById(int id) { |
public StationEntry getById(int id) { |
65 |
PersistenceManager pm = null; |
PersistenceManager pm = null; |
66 |
|
|
67 |
try { |
try { |
68 |
pm = PMF.get().getPersistenceManager(); |
/*pm = PMF.get().getPersistenceManager(); |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
Query q = pm.newQuery(JdoStationBean.class); |
Query q = pm.newQuery(JdoStationBean.class); |
73 |
q.setOrdering("name"); |
q.setOrdering("name"); |
74 |
List<JdoStationBean> beanList = (List<JdoStationBean>) q.execute(); |
List<JdoStationBean> beanList = (List<JdoStationBean>) q.execute();*/ |
75 |
|
|
76 |
|
List<JdoStationBean> beanList = getAllStations(); |
77 |
|
|
78 |
|
|
79 |
StationBean stationBean = new StationBean(); |
StationBean stationBean = new StationBean(); |
80 |
|
|
93 |
|
|
94 |
return stationBean; |
return stationBean; |
95 |
} finally { |
} finally { |
96 |
pm.close(); |
// pm.close(); |
97 |
} |
} |
98 |
} |
} |
99 |
|
|
100 |
|
public List<JdoStationBean> getAllStations() { |
101 |
|
final String key = "allstations"; |
102 |
|
List<JdoStationBean> result = (List<JdoStationBean>) cache.get(key); |
103 |
|
|
104 |
|
if (result == null) { |
105 |
|
logger.info("getAllStations Cache miss"); |
106 |
|
|
107 |
|
PersistenceManager pm = null; |
108 |
|
final double LAT = 0.4; |
109 |
|
final double LNG = 0.75; |
110 |
|
|
111 |
|
try { |
112 |
|
pm = PMF.get().getPersistenceManager(); |
113 |
|
Extent<JdoStationBean> all = pm.getExtent(JdoStationBean.class, false); |
114 |
|
|
115 |
|
result = new ArrayList<JdoStationBean>(); |
116 |
|
for (JdoStationBean station : all) { |
117 |
|
result.add(station); |
118 |
|
} |
119 |
|
|
120 |
|
cache.put(key, result); |
121 |
|
|
122 |
|
} finally { |
123 |
|
pm.close(); |
124 |
|
} |
125 |
|
} else { |
126 |
|
logger.info("getAllStations Cache hit"); |
127 |
|
} |
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
return result; |
132 |
|
|
133 |
|
} |
134 |
|
|
135 |
|
|
136 |
//String limitExpression = (geolimit == true) ? "AND abs(latitude-?)<0.4 AND abs(longitude-?)<0.75 " : ""; |
//String limitExpression = (geolimit == true) ? "AND abs(latitude-?)<0.4 AND abs(longitude-?)<0.75 " : ""; |
137 |
|
/* |
138 |
public List<JdoStationBean> getByLocationList(double latitude, double longitude, boolean geolimit) { |
public List<JdoStationBean> getByLocationList(double latitude, double longitude, boolean geolimit) { |
139 |
|
|
140 |
PersistenceManager pm = null; |
PersistenceManager pm = null; |
166 |
} finally { |
} finally { |
167 |
pm.close(); |
pm.close(); |
168 |
} |
} |
169 |
} |
}*/ |
170 |
|
|
171 |
|
|
172 |
public StationBean getByLocation(double latitude, double longitude) { |
public StationBean getByLocation(double latitude, double longitude) { |
173 |
|
/* |
174 |
List<JdoStationBean> beanList = getByLocationList(latitude,longitude,true); |
List<JdoStationBean> beanList = getByLocationList(latitude,longitude,true); |
175 |
|
|
176 |
if (beanList.size() < LOCATION_LIMIT ) { |
if (beanList.size() < LOCATION_LIMIT ) { |
177 |
logger.info("getByLocation failover: " +latitude + "," + longitude); |
logger.info("getByLocation failover: " +latitude + "," + longitude); |
178 |
beanList = getByLocationList(latitude,longitude, false); |
beanList = getByLocationList(latitude,longitude, false); |
179 |
} |
}*/ |
180 |
|
|
181 |
|
List<JdoStationBean> beanList = getAllStations(); |
182 |
|
|
183 |
StationBean stationBean = new StationBean(); |
StationBean stationBean = new StationBean(); |
184 |
|
|