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

Diff of /android/TrainInfoServiceGoogle/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java

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

revision 387 by torben, Fri Oct 2 15:06:08 2009 UTC revision 977 by torben, Sat Jul 10 09:57:04 2010 UTC
# Line 3  package dk.thoerup.traininfoservice.bane Line 3  package dk.thoerup.traininfoservice.bane
3    
4  import java.io.IOException;  import java.io.IOException;
5  import java.net.URL;  import java.net.URL;
6    import java.sql.SQLException;
7  import java.util.ArrayList;  import java.util.ArrayList;
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 com.gargoylesoftware.htmlunit.BrowserVersion;
14  import com.gargoylesoftware.htmlunit.Page;  import com.gargoylesoftware.htmlunit.Page;
15  import com.gargoylesoftware.htmlunit.RefreshHandler;  import com.gargoylesoftware.htmlunit.RefreshHandler;
16  import com.gargoylesoftware.htmlunit.WebClient;  import com.gargoylesoftware.htmlunit.WebClient;
# Line 14  import com.gargoylesoftware.htmlunit.htm Line 18  import com.gargoylesoftware.htmlunit.htm
18  import com.gargoylesoftware.htmlunit.html.HtmlElement;  import com.gargoylesoftware.htmlunit.html.HtmlElement;
19  import com.gargoylesoftware.htmlunit.html.HtmlPage;  import com.gargoylesoftware.htmlunit.html.HtmlPage;
20    
21    import dk.thoerup.circuitbreaker.CircuitBreaker;
22    import dk.thoerup.circuitbreaker.CircuitBreakerManager;
23    import dk.thoerup.traininfoservice.StationDAO;
24    import dk.thoerup.traininfoservice.Statistics;
25    
26  public class TimetableFetcher {  public class TimetableFetcher {
27                    
28          class NullRefreshHandler implements RefreshHandler {          class NullRefreshHandler implements RefreshHandler {
# Line 22  public class TimetableFetcher { Line 31  public class TimetableFetcher {
31                                    
32          }          }
33                                    
34          TimeoutCache<String, List<TimetableBean>> cache = new TimeoutCache<String,List<TimetableBean>>(120 * 1000);          Map<String, List<TimetableBean>> cache;
35            Map<String, Integer> stationCache;
36    
37            StationDAO stationDao = new StationDAO();
38                    
39                    
40          Logger logger = Logger.getLogger(TimetableFetcher.class.getName());          Logger logger = Logger.getLogger(TimetableFetcher.class.getName());
41                    
42            private boolean useTempSite;
43            
44            public TimetableFetcher(boolean tmpSite, int cacheTimeout) {
45                    useTempSite = tmpSite;
46                    
47                    cache = new TimeoutMap<String,List<TimetableBean>>(cacheTimeout);
48                    stationCache = new TimeoutMap<String,Integer>( 3*60*60*1000 );
49            }
50            
51                    
52          List<TimetableBean> cachedLookupTimetable(String trainID, String type) throws Exception {          List<TimetableBean> cachedLookupTimetable(String trainID, String type) throws Exception {
53                  String key = trainID+type;                  String key = trainID+type;
54                  List<TimetableBean> list = cache.get(key);                  List<TimetableBean> list = cache.get(key);
55                                    
56                  if (list == null) {                  if (list == null) {
                         logger.warning("Timetable: Cache miss " + trainID); //remove before production  
57                          list = lookupTimetable(trainID,type);                          list = lookupTimetable(trainID,type);
58                          cache.put(key, list);                          cache.put(key, list);
59                  } else {                  } else {
60                          logger.warning("Timetable: Cache hit " + trainID); //remove before production                          Statistics.getInstance().incrementTimetableCacheHits();
61                            logger.info("Timetable: Cache hit " + trainID);
62                  }                  }
63                  return list;                  return list;
64          }          }
65            
66            List<TimetableBean> lookupTimetable(String trainID, String type) throws Exception {
67                    if (useTempSite == false ){
68                            return lookupTimetableRealSite(trainID, type);
69                    } else {
70                            return new ArrayList<TimetableBean>(); // no timetable data on temp site
71                    }
72            }
73            
74            int getStationId(String name) {
75                    Integer id = stationCache.get(name);
76                    
77                    if (id == null) {
78                            try {
79                                    id = stationDao.getIdByName(name);
80                                    stationCache.put(name, id);
81                            } catch (SQLException e) {
82                                    logger.log(Level.SEVERE, "getStationId failed", e);
83                                    id = -1;
84                            }
85                    }
86    
87                    return id;
88            }
89    
90          List<TimetableBean> lookupTimetable(String trainID, String type) throws Exception {                      List<TimetableBean> lookupTimetableRealSite(String trainID, String type) throws Exception {            
91                  List<TimetableBean> timetableList = new ArrayList<TimetableBean>();                  List<TimetableBean> timetableList = new ArrayList<TimetableBean>();
92                                    
93                  String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276";                  //String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276";
94                                                    String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID;                
95    
96              final WebClient webClient = new WebClient();              final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3);
97              webClient.setTimeout(2500);              webClient.setTimeout(2500);
98              webClient.setJavaScriptEnabled(false);                      webClient.setJavaScriptEnabled(false);        
99              webClient.setRefreshHandler( new NullRefreshHandler() );              webClient.setRefreshHandler( new NullRefreshHandler() );
100              webClient.setCssEnabled(false);              webClient.setCssEnabled(false);
101                            
102                            
103              final HtmlPage page = webClient.getPage(url);              HtmlunitInvocation wrapper = new HtmlunitInvocation(webClient, url);
104                CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
105                            
106                HtmlPage page = (HtmlPage) breaker.invoke(wrapper);
107                        
108                            
109              boolean currentStation = false;              boolean currentStation = false;
110              boolean currentStationSaved = false;              boolean currentStationSaved = false;
# Line 80  public class TimetableFetcher { Line 128  public class TimetableFetcher {
128                          }                          }
129                                                    
130                          TimetableBean bean = new TimetableBean();                          TimetableBean bean = new TimetableBean();
131                          bean.setStation( fields.get(0).asText() );                          
132                            String station = fields.get(0).asText() ;
133                            if (station.equals("København"))
134                                    station = "København H"; //correct inconsistency in naming
135                            
136                            bean.setStation( station );
137                          bean.setArrival( fields.get(1).asText() );                          bean.setArrival( fields.get(1).asText() );
138                          bean.setDeparture( fields.get(2).asText() );                          bean.setDeparture( fields.get(2).asText() );
139                                                    
140                            boolean cancelled = fields.get(3).asText().equalsIgnoreCase("aflyst");
141                            bean.setCancelled(cancelled);
142                            
143                          if (currentStation == true && currentStationSaved == false ) {                          if (currentStation == true && currentStationSaved == false ) {
144                                  bean.setCurrent(currentStation);                                  bean.setCurrent(currentStation);
145                                  currentStationSaved = true;                                  currentStationSaved = true;
146                          }                          }
147                                                    
148                            bean.setStationId( getStationId( station ));
149                            
150                          timetableList.add(bean);                          timetableList.add(bean);
151                    }
152                    
153                    //TODO: There is an off-by-one error in this cancelled parser thingie
154                    final String cancelledString = "Aflyst";
155                    for (int i=0;i<timetableList.size(); i++) { //handle cancelled labels
156                            final int lastIdx = (timetableList.size() - 1);
157                            
158                            TimetableBean current = timetableList.get(i);
159                            if (current.isCancelled()) {
160                                    if (i == 0) {
161                                            current.setDeparture(cancelledString);
162                                    } else if (i == lastIdx) {
163                                            current.setArrival(cancelledString);
164                                    } else if (i>0 && i<lastIdx) {
165                                            TimetableBean next = timetableList.get(i+1);
166                                            TimetableBean prev = timetableList.get(i-1);
167                                            
168                                            if (next.isCancelled())
169                                                    current.setDeparture(cancelledString);
170                                            if (prev.isCancelled())
171                                                    current.setArrival(cancelledString);
172                                    }
173                            }
174                  }                  }
175                                    
176              } else {              } else {
177                  logger.warning("No time table found, trainID=" + trainID + " type=" + type);                  logger.warning("No time table found, trainID=" + trainID + " type=" + type);
178              }              }
179                webClient.closeAllWindows();
180                                    
181                  return timetableList;                  return timetableList;
182          }          }

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

  ViewVC Help
Powered by ViewVC 1.1.20