/[projects]/android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureFetcher.java
ViewVC logotype

Diff of /android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureFetcher.java

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

revision 977 by torben, Fri Jul 9 22:30:18 2010 UTC revision 978 by torben, Sat Jul 10 10:53:44 2010 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfoservice.banedk;  package dk.thoerup.traininfoservice.banedk;
2    
3  import java.util.ArrayList;  
4  import java.util.Collections;  import java.util.Collections;
5  import java.util.List;  import java.util.List;
6  import java.util.Map;  import java.util.Map;
# Line 27  public class DepartureFetcher { Line 27  public class DepartureFetcher {
27                    
28          Logger logger = Logger.getLogger(DepartureFetcher.class.getName());          Logger logger = Logger.getLogger(DepartureFetcher.class.getName());
29                    
30          Map<String, List<DepartureBean>> cache;          Map<String, DepartureBean> cache;
31                    
32          StationDAO stationDao = new StationDAO();          StationDAO stationDao = new StationDAO();
33                    
# Line 35  public class DepartureFetcher { Line 35  public class DepartureFetcher {
35                    
36          public DepartureFetcher(boolean tempSite, int cacheTimeout) {          public DepartureFetcher(boolean tempSite, int cacheTimeout) {
37                  useTempSite = tempSite;                  useTempSite = tempSite;
38                  cache = new TimeoutMap<String,List<DepartureBean>>(cacheTimeout);                  cache = new TimeoutMap<String,DepartureBean>(cacheTimeout);
39          }          }
40                    
41                    
42                                    
43                    
44          public List<DepartureBean> cachedLookupDepartures(int stationID, boolean arrival) throws Exception {          public DepartureBean cachedLookupDepartures(int stationID, boolean arrival) throws Exception {
45                  final String key = "" + stationID + ":" + arrival;                  final String key = "" + stationID + ":" + arrival;
46                                    /*
47                  List<DepartureBean> list = cache.get(key);                  DepartureBean departureBean = cache.get(key);
48    
49                                    
50                  if (list == null) {                  if (departureBean == null) {
51                          list = lookupDepartures(stationID,arrival);                          departureBean = lookupDepartures(stationID,arrival);
52                          cache.put(key, list);                          cache.put(key, departureBean);
53                  } else {                  } else {
54                          Statistics.getInstance().incrementDepartureCacheHits();                          Statistics.getInstance().incrementDepartureCacheHits();
55                          logger.info("Departure: Cache hit " + key); //remove before production                          logger.info("Departure: Cache hit " + key); //remove before production
56                  }                  }
57                  return list;                  return departureBean;*/
58                    
59                    return lookupDepartures(stationID,arrival);
60          }          }
61                                    
62    
63          public List<DepartureBean> lookupDepartures(int stationID, boolean arrival) throws Exception {          public DepartureBean lookupDepartures(int stationID, boolean arrival) throws Exception {
64                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();                  
65                    DepartureBean departureBean = new DepartureBean();
66                                    
67                  StationBean station = stationDao.getById(stationID);                  StationBean station = stationDao.getById(stationID);
68                                    
69                  if (station.getRegional() != null) {                  if (station.getRegional() != null) {
70                          List<DepartureBean> list = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival);                          DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival);
71                          departureList.addAll(list);                                              departureBean.departureEntries.addAll( tempBean.departureEntries );
72                            departureBean.notifications.addAll(tempBean.notifications);
73                  }                  }
74                                    
75                  if (station.getStrain() != null) {                  if (station.getStrain() != null) {
76                          List<DepartureBean> list = lookupDepartures(station.getStrain(), TrainType.STOG, arrival);                          DepartureBean tempBean = lookupDepartures(station.getStrain(), TrainType.STOG, arrival);
77                          departureList.addAll(list);                              departureBean.departureEntries.addAll( tempBean.departureEntries );
78                            departureBean.notifications.addAll(tempBean.notifications);
79                  }                                }              
80                                    
81                  Collections.sort( departureList );                  Collections.sort( departureBean.departureEntries );
82    
83                                    
84                  return departureList;                  return departureBean;
85          }          }
86                    
87          public List<DepartureBean> lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception {          public DepartureBean lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception {
88                  if (useTempSite == false) {                  if (useTempSite == false) {
89                          return lookupDeparturesNormalSite(stationcode, type, arrival);                          return lookupDeparturesNormalSite(stationcode, type, arrival);
90                  } else {                  } else {
# Line 100  public class DepartureFetcher { Line 105  public class DepartureFetcher {
105                  }                  }
106          }          }
107                    
108          public List<DepartureBean> lookupDeparturesNormalSite(String stationcode, TrainType type, boolean arrival) throws Exception {          public DepartureBean lookupDeparturesNormalSite(String stationcode, TrainType type, boolean arrival) throws Exception {
109                                    
110                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();                  DepartureBean departureBean = new DepartureBean();
111                                    
112              final WebClient webClient = new WebClient( BrowserVersion.FIREFOX_3 );              final WebClient webClient = new WebClient( BrowserVersion.FIREFOX_3 );
113              webClient.setTimeout(2500);              webClient.setTimeout(2500);
# Line 132  public class DepartureFetcher { Line 137  public class DepartureFetcher {
137                          if (rowClass != null && rowClass.toLowerCase().contains("station") ) {                          if (rowClass != null && rowClass.toLowerCase().contains("station") ) {
138                                  DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td");                                  DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td");
139                    
140                                  DepartureBean departure = new DepartureBean();                                  DepartureEntry departure = new DepartureEntry();
141                                                                    
142                                  String time = fields.get(0).asText();                                  String time = fields.get(0).asText();
143                                  if (time.equals(""))                                  if (time.equals(""))
# Line 164  public class DepartureFetcher { Line 169  public class DepartureFetcher {
169                                                                    
170                                  departure.setType(typeString);                                  departure.setType(typeString);
171                                                                    
172                                  departureList.add(departure);                                  departureBean.departureEntries.add( departure );
173                          }                          }
174                      }                      }
175              } else {              } else {
176                  logger.warning("No departures found for station=" + stationcode + ", type=" + type);                  logger.warning("No departures found for station=" + stationcode + ", type=" + type);
177              }              }
178                
179                HtmlElement notifDiv = page.getElementById("station_planlagte_text");
180                if (notifDiv != null) {
181    
182                    DomNodeList<HtmlElement> tables = notifDiv.getElementsByTagName("table");
183                    for (HtmlElement tab : tables) {
184    
185                            DomNodeList<HtmlElement> anchors = tab.getElementsByTagName("a");              
186                            if (anchors.size() == 2) {
187                                    departureBean.notifications.add(  anchors.get(1).getTextContent() );
188                            }
189                    }
190                    
191                }
192                
193                
194              webClient.closeAllWindows();              webClient.closeAllWindows();
195                            
196              return departureList;              return departureBean;
197          }          }
198                    
199          /*          /*

Legend:
Removed from v.977  
changed lines
  Added in v.978

  ViewVC Help
Powered by ViewVC 1.1.20