/[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 1116 by torben, Thu Sep 23 15:11:52 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            @SuppressWarnings("unchecked")
38            public StationDAO() {
39            Map props = new HashMap();
40            props.put(GCacheFactory.EXPIRATION_DELTA, TIMEOUT_SECONDS);        
41                    
42                    try {
43                cache = CacheManager.getInstance().getCacheFactory().createCache(props);            
44            } catch (CacheException e) {
45                    logger.log(Level.WARNING, "error creating cache", e);
46            }
47            
48            }
49    
50    
51          public StationEntry getById(int id)  {          public StationEntry getById(int id)  {
# Line 36  public class StationDAO { Line 63  public class StationDAO {
63    
64          /* damn that JDO sucks so we to the filtering in java-code */          /* damn that JDO sucks so we to the filtering in java-code */
65          public StationBean getByName(String searchname) {          public StationBean getByName(String searchname) {
                 PersistenceManager pm = null;  
                   
                 try {  
                         pm = PMF.get().getPersistenceManager();  
                           
                           
                           
                         Query q = pm.newQuery(JdoStationBean.class);  
                         q.setOrdering("name");                    
                         List<JdoStationBean> beanList = (List<JdoStationBean>) q.execute();  
                           
                         StationBean stationBean = new StationBean();  
66                                                    
67                          searchname = searchname.toLowerCase();                  List<JdoStationBean> beanList = getAllStations();
68                          for(JdoStationBean bean : beanList) {  
69                                  if (bean.getName().toLowerCase().startsWith(searchname)) {  
70                                          stationBean.entries.add( bean.toStationEntry() );                  StationBean stationBean = new StationBean();
71                                  } else {  
72                                          for (String alias : bean.aliases ) {                  searchname = searchname.toLowerCase();
73                                                  if (alias.toLowerCase().startsWith(searchname)) {                  for(JdoStationBean bean : beanList) {
74                                                          stationBean.entries.add( bean.toStationEntry() );                          if (bean.getName().toLowerCase().startsWith(searchname)) {
75                                                  }                                  stationBean.entries.add( bean.toStationEntry() );
76                            } else {
77                                    for (String alias : bean.aliases ) {
78                                            if (alias.toLowerCase().startsWith(searchname)) {
79                                                    stationBean.entries.add( bean.toStationEntry() );
80                                          }                                          }
81                                  }                                  }
82                          }                          }
   
                         return stationBean;                      
                 } finally {  
                         pm.close();  
83                  }                  }
         }  
84    
85                    Collections.sort(stationBean.entries, new Comparator<StationEntry>() {
86                            @Override
87                            public int compare(StationEntry arg0, StationEntry arg1) {
88                                    return arg0.getName().compareTo( arg1.getName() );
89                            }                              
90                    });
91    
92          public StationBean getByLocation(double latitude, double longitude)  {                  return stationBean;                    
                   
93    
94            }
95            
96            @SuppressWarnings("unchecked")
97            public List<JdoStationBean> getAllStations() {
98                    final String key = "allstations";
99                    List<JdoStationBean> result = (List<JdoStationBean>) cache.get(key);
100                                    
101                    if (result == null) {
102                            logger.info("getAllStations Cache miss");
103                            
104                            PersistenceManager pm = null;
105                            
106                            try {
107                                    pm = PMF.get().getPersistenceManager();
108                                    Extent<JdoStationBean> all = pm.getExtent(JdoStationBean.class, false);
109                                    
110                                    result = new ArrayList<JdoStationBean>();
111                                    for (JdoStationBean station : all) {
112                                            result.add(station);
113                                    }
114                                    
115                                    cache.put(key, result);
116                                    
117                            } finally {
118                                    pm.close();
119                            }
120                    } else {
121                            logger.info("getAllStations Cache hit");
122                    }
123                    
124                    
125                    
126                    return result;
127                    
128            }
129            
130    
131            //String limitExpression = (geolimit == true) ? "AND abs(latitude-?)<0.4 AND abs(longitude-?)<0.75 " : "";
132            /*
133            public List<JdoStationBean> getByLocationList(double latitude, double longitude, boolean geolimit)  {
134            
135                  PersistenceManager pm = null;                  PersistenceManager pm = null;
136                    final double LAT = 0.4;
137                    final double LNG = 0.75;
138                                    
139                  try {                  try {
140                          pm = PMF.get().getPersistenceManager();                          pm = PMF.get().getPersistenceManager();
141                          List<JdoStationBean> beanList = (List<JdoStationBean>) pm.newQuery(JdoStationBean.class).execute();                          Query q = pm.newQuery(JdoStationBean.class);
142                            
143                                                    
                         StationBean stationBean = new StationBean();                      
144                                                    
145                            if (geolimit == true) {
146                                    double minLat = latitude - LAT;
147                                    double maxLat = latitude + LAT;
148                                    
149                                    //DAMN JDO implementation only allows us to compare on one parameter
150    
151                          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;  
152                                                                    
153                                  bean.distance = (int) meter;                                  q.setFilter( filter );
154                          }                          }
155                                                    
156                            List<JdoStationBean> beanList = (List<JdoStationBean>) q.execute();                    
157                            
158                            logger.info("beanList size " + beanList.size());
159                            
160                            return beanList;
161                    } finally {
162                            pm.close();
163                    }
164            }*/
165    
166                          Collections.sort(beanList, new Comparator<JdoStationBean>() {  
167                                  @Override          public StationBean getByLocation(double latitude, double longitude)  {
168                                  public int compare(JdoStationBean o1, JdoStationBean o2) {                  
169                                          if (o1.distance < o2.distance) {                  List<JdoStationBean> beanList = getAllStations();
170                                                  return -1;  
171                                          } else if (o1.distance > o2.distance) {                  StationBean stationBean = new StationBean();                    
172                                                  return 1;  
173                                          } else {  
174                                                  return 0;                  Geo location = new Geo(latitude,longitude);
175                                          }                  for(JdoStationBean bean : beanList) {
176                            double meter = Geo.distanceKM( location, new Geo(bean.getLatitude(), bean.getLongitude() )) * 1000.0;
177    
178                            bean.distance = (int) meter;
179                    }
180    
181    
182                    Collections.sort(beanList, new Comparator<JdoStationBean>() {
183                            @Override
184                            public int compare(JdoStationBean o1, JdoStationBean o2) {
185                                    if (o1.distance < o2.distance) {
186                                            return -1;
187                                    } else if (o1.distance > o2.distance) {
188                                            return 1;
189                                    } else {
190                                            return 0;
191                                  }                                  }
                         });  
                           
                         for (int i=0; i<LOCATION_LIMIT && i<beanList.size(); i++) {  
                                 stationBean.entries.add( beanList.get(i).toStationEntry() );  
192                          }                          }
193                    });
194    
195                          return stationBean;                                      for (int i=0; i<LOCATION_LIMIT && i<beanList.size(); i++) {
196                  } finally {                          stationBean.entries.add( beanList.get(i).toStationEntry() );
                         pm.close();  
197                  }                  }
198    
199                    return stationBean;                    
200          }          }
201                    
202    
203            @SuppressWarnings("unchecked")
204          public StationBean getByList(String list)  {          public StationBean getByList(String list)  {
205                  PersistenceManager pm = null;                  PersistenceManager pm = null;
206                                    
# Line 128  public class StationDAO { Line 216  public class StationDAO {
216                                  filter.append("id == ").append(part);                                  filter.append("id == ").append(part);
217                          }                                                }                      
218                                                    
                         //String filter = "id == 10 || id == 82"; //TODO: build filter  
219                                                    
220                          pm = PMF.get().getPersistenceManager();                          pm = PMF.get().getPersistenceManager();
221                          Query q = pm.newQuery(JdoStationBean.class);                          Query q = pm.newQuery(JdoStationBean.class);
# Line 151  public class StationDAO { Line 238  public class StationDAO {
238          }          }
239    
240    
241            @SuppressWarnings("unchecked")
242          public int getIdByName(String name)  {          public int getIdByName(String name)  {
243                                    
244                  List<JdoStationBean> beanList = null;                  List<JdoStationBean> beanList = null;
# Line 171  public class StationDAO { Line 259  public class StationDAO {
259                                                    
260                          for(JdoStationBean bean : beanList) {                          for(JdoStationBean bean : beanList) {
261                                  stationBean.entries.add( bean.toStationEntry() );                                  stationBean.entries.add( bean.toStationEntry() );
262                          }                          }                      
263                            
264                                                    
265                  } finally {                  } finally {
266                          pm.close();                          pm.close();

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

  ViewVC Help
Powered by ViewVC 1.1.20