/[projects]/android/TrainInfoServiceGoogle/src/dk/thoerup/traininfoservice/StationDAO.java
ViewVC logotype

Diff of /android/TrainInfoServiceGoogle/src/dk/thoerup/traininfoservice/StationDAO.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1105 by torben, Wed Sep 22 21:09:39 2010 UTC revision 1115 by torben, Thu Sep 23 15:08:39 2010 UTC
# Line 4  import java.io.IOException; Line 4  import java.io.IOException;
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.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;
# Line 19  import dk.thoerup.traininfoservice.jdo.P Line 29  import dk.thoerup.traininfoservice.jdo.P
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 = 30*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)  {
# Line 39  public class StationDAO { Line 65  public class StationDAO {
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                                                    
# Line 61  public class StationDAO { Line 90  public class StationDAO {
90                                          }                                          }
91                                  }                                  }
92                          }                          }
93                            
94                            Collections.sort(stationBean.entries, new Comparator<StationEntry>() {
95                                    @Override
96                                    public int compare(StationEntry arg0, StationEntry arg1) {
97                                            return arg0.getName().compareTo( arg1.getName() );
98                                    }                              
99                            });
100    
101                          return stationBean;                                              return stationBean;                    
102                  } finally {                  } finally {
103                          pm.close();                  //      pm.close();
104                  }                  }
105          }          }
106            
107            public List<JdoStationBean> getAllStations() {
108          public StationBean getByLocation(double latitude, double longitude)  {                  final String key = "allstations";
109                    List<JdoStationBean> result = (List<JdoStationBean>) cache.get(key);
110                    
111                    if (result == null) {
112                            logger.info("getAllStations Cache miss");
113                            
114                            PersistenceManager pm = null;
115                            final double LAT = 0.4;
116                            final double LNG = 0.75;
117                            
118                            try {
119                                    pm = PMF.get().getPersistenceManager();
120                                    Extent<JdoStationBean> all = pm.getExtent(JdoStationBean.class, false);
121                                    
122                                    result = new ArrayList<JdoStationBean>();
123                                    for (JdoStationBean station : all) {
124                                            result.add(station);
125                                    }
126                                    
127                                    cache.put(key, result);
128                                    
129                            } finally {
130                                    pm.close();
131                            }
132                    } else {
133                            logger.info("getAllStations Cache hit");
134                    }
135                                    
   
136                                    
137                    
138                    return result;
139                    
140            }
141            
142    
143            //String limitExpression = (geolimit == true) ? "AND abs(latitude-?)<0.4 AND abs(longitude-?)<0.75 " : "";
144            /*
145            public List<JdoStationBean> getByLocationList(double latitude, double longitude, boolean geolimit)  {
146            
147                  PersistenceManager pm = null;                  PersistenceManager pm = null;
148                    final double LAT = 0.4;
149                    final double LNG = 0.75;
150                                    
151                  try {                  try {
152                          pm = PMF.get().getPersistenceManager();                          pm = PMF.get().getPersistenceManager();
153                          List<JdoStationBean> beanList = (List<JdoStationBean>) pm.newQuery(JdoStationBean.class).execute();                          Query q = pm.newQuery(JdoStationBean.class);
154                                                    
                         StationBean stationBean = new StationBean();                      
155                                                    
156                            
157                            if (geolimit == true) {
158                                    double minLat = latitude - LAT;
159                                    double maxLat = latitude + LAT;
160                                    
161                                    //DAMN JDO implementation only allows us to compare on one parameter
162    
163                          for(JdoStationBean bean : beanList) {                                  String filter = String.format("latitude > %f && latitude < %f", minLat, maxLat);
                                 double meter = Geo.distanceKM(latitude, longitude, bean.getLatitude(), bean.getLongitude()) * 1000.0;  
164                                                                    
165                                  bean.distance = (int) meter;                                  q.setFilter( filter );
166                          }                          }
167                                                    
168                            List<JdoStationBean> beanList = (List<JdoStationBean>) q.execute();                    
169                            
170                            logger.info("beanList size " + beanList.size());
171                            
172                            return beanList;
173                    } finally {
174                            pm.close();
175                    }
176            }*/
177    
178                          Collections.sort(beanList, new Comparator<JdoStationBean>() {  
179                                  @Override          public StationBean getByLocation(double latitude, double longitude)  {
180                                  public int compare(JdoStationBean o1, JdoStationBean o2) {                  /*
181                                          if (o1.distance < o2.distance) {                  List<JdoStationBean> beanList = getByLocationList(latitude,longitude,true);
182                                                  return -1;                  
183                                          } else if (o1.distance > o2.distance) {                  if (beanList.size() < LOCATION_LIMIT ) {
184                                                  return 1;                          logger.info("getByLocation failover: " +latitude + "," + longitude);
185                                          } else {                          beanList = getByLocationList(latitude,longitude, false);
186                                                  return 0;                  }*/
187                                          }                  
188                    List<JdoStationBean> beanList = getAllStations();
189    
190                    StationBean stationBean = new StationBean();                    
191    
192    
193                    Geo location = new Geo(latitude,longitude);
194                    for(JdoStationBean bean : beanList) {
195                            double meter = Geo.distanceKM( location, new Geo(bean.getLatitude(), bean.getLongitude() )) * 1000.0;
196    
197                            bean.distance = (int) meter;
198                    }
199    
200    
201                    Collections.sort(beanList, new Comparator<JdoStationBean>() {
202                            @Override
203                            public int compare(JdoStationBean o1, JdoStationBean o2) {
204                                    if (o1.distance < o2.distance) {
205                                            return -1;
206                                    } else if (o1.distance > o2.distance) {
207                                            return 1;
208                                    } else {
209                                            return 0;
210                                  }                                  }
                         });  
                           
                         for (int i=0; i<LOCATION_LIMIT && i<beanList.size(); i++) {  
                                 stationBean.entries.add( beanList.get(i).toStationEntry() );  
211                          }                          }
212                    });
213    
214                          return stationBean;                                      for (int i=0; i<LOCATION_LIMIT && i<beanList.size(); i++) {
215                  } finally {                          stationBean.entries.add( beanList.get(i).toStationEntry() );
                         pm.close();  
216                  }                  }
217    
218                    return stationBean;                    
219          }          }
220                    
221    
# Line 171  public class StationDAO { Line 277  public class StationDAO {
277                                                    
278                          for(JdoStationBean bean : beanList) {                          for(JdoStationBean bean : beanList) {
279                                  stationBean.entries.add( bean.toStationEntry() );                                  stationBean.entries.add( bean.toStationEntry() );
280                          }                          }                      
281                            
282                                                    
283                  } finally {                  } finally {
284                          pm.close();                          pm.close();

Legend:
Removed from v.1105  
changed lines
  Added in v.1115

  ViewVC Help
Powered by ViewVC 1.1.20