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

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

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

revision 591 by torben, Wed Feb 10 14:07:04 2010 UTC revision 1331 by torben, Wed Apr 20 05:25:48 2011 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfoservice.banedk;  package dk.thoerup.traininfoservice.banedk;
2    
3    
4  import java.io.IOException;  
5  import java.net.URL;  import java.net.URL;
6  import java.util.ArrayList;  import java.sql.SQLException;
 import java.util.List;  
7  import java.util.Map;  import java.util.Map;
8    import java.util.logging.Level;
9  import java.util.logging.Logger;  import java.util.logging.Logger;
10    
11  import com.gargoylesoftware.htmlunit.BrowserVersion;  import org.jsoup.nodes.Document;
12  import com.gargoylesoftware.htmlunit.Page;  import org.jsoup.nodes.Element;
13  import com.gargoylesoftware.htmlunit.RefreshHandler;  import org.jsoup.select.Elements;
 import com.gargoylesoftware.htmlunit.WebClient;  
 import com.gargoylesoftware.htmlunit.html.DomNodeList;  
 import com.gargoylesoftware.htmlunit.html.HtmlElement;  
 import com.gargoylesoftware.htmlunit.html.HtmlPage;  
14    
15    import dk.thoerup.android.traininfo.common.TimetableBean;
16    import dk.thoerup.android.traininfo.common.TimetableEntry;
17  import dk.thoerup.circuitbreaker.CircuitBreaker;  import dk.thoerup.circuitbreaker.CircuitBreaker;
18  import dk.thoerup.circuitbreaker.CircuitBreakerManager;  import dk.thoerup.circuitbreaker.CircuitBreakerManager;
19    import dk.thoerup.traininfoservice.Statistics;
20    import dk.thoerup.traininfoservice.TraininfoSettings;
21    import dk.thoerup.traininfoservice.db.StationDAO;
22    
23  public class TimetableFetcher {  public class TimetableFetcher {
24            
         class NullRefreshHandler implements RefreshHandler {  
                 public void handleRefresh(Page arg0, URL arg1, int arg2) throws IOException {                    
                 }  
                   
         }  
25                                    
26          Map<String, List<TimetableBean>> cache;          Map<String, TimetableBean> cache;
27            Map<String, Integer> stationCache;
28    
29            StationDAO stationDao = new StationDAO();
30                    
31                    
32          Logger logger = Logger.getLogger(TimetableFetcher.class.getName());          Logger logger = Logger.getLogger(TimetableFetcher.class.getName());
33    
34            TraininfoSettings settings;    
35                    
36          private boolean useTempSite;          public TimetableFetcher(TraininfoSettings settings) {
37                            this.settings = settings;
         public TimetableFetcher(boolean tmpSite, int cacheTimeout) {  
                 useTempSite = tmpSite;  
38                                    
39                  cache = new TimeoutMap<String,List<TimetableBean>>(cacheTimeout);                  cache = new TimeoutMap<String,TimetableBean>( settings.getCacheTimeout() );
40                    stationCache = new TimeoutMap<String,Integer>( 3*60*60*1000 );
41          }          }
42                    
43                    
44          List<TimetableBean> cachedLookupTimetable(String trainID, String type) throws Exception {          TimetableBean cachedLookupTimetable(String trainID, String type) throws Exception {
45                  String key = trainID+type;                  String key = trainID+type;
46                  List<TimetableBean> list = cache.get(key);                  TimetableBean list = cache.get(key);
47                                    
48                  if (list == null) {                  if (list == null) {
49                          list = lookupTimetable(trainID,type);                          list = lookupTimetable(trainID,type);
50                          cache.put(key, list);                          cache.put(key, list);
51                  } else {                  } else {
52                            Statistics.getInstance().incrementTimetableCacheHits();
53                          logger.info("Timetable: Cache hit " + trainID);                          logger.info("Timetable: Cache hit " + trainID);
54                  }                  }
55                  return list;                  return list;
56          }          }
57                    
58          List<TimetableBean> lookupTimetable(String trainID, String type) throws Exception {          TimetableBean lookupTimetable(String trainID, String type) throws Exception {
59                  if (useTempSite == false ){                  if (settings.getUseAzureSite() == true ){
60                          return lookupTimetableRealSite(trainID, type);                          return lookupTimetableAzureSite(trainID, type);
61                            
62                  } else {                  } else {
63                          return new ArrayList<TimetableBean>(); // no timetable data on temp site                          return lookupTimetableMobileSite(trainID, type);
64                  }                  }
65          }          }
66            
67            int getStationId(String name) {
68                    Integer id = stationCache.get(name);
69                    
70                    if (id == null) {
71                            try {
72                                    id = stationDao.getIdByName(name);
73                                    stationCache.put(name, id);
74                            } catch (SQLException e) {
75                                    logger.log(Level.SEVERE, "getStationId failed", e);
76                                    id = -1;
77                            }
78                    }
79    
80                    return id;
81            }
82    
83          List<TimetableBean> lookupTimetableRealSite(String trainID, String type) throws Exception {                      TimetableBean lookupTimetableAzureSite(String trainID, String type) throws Exception {          
84                  List<TimetableBean> timetableList = new ArrayList<TimetableBean>();                  TimetableBean timetableBean = new TimetableBean();
85                                    
                 String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276";  
                                   
86    
87              final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3);                  String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID;                
88              webClient.setTimeout(2500);                  logger.fine("URL:" + url);
             webClient.setJavaScriptEnabled(false);          
             webClient.setRefreshHandler( new NullRefreshHandler() );  
             webClient.setCssEnabled(false);  
               
89                            
90              BanedkInvocation wrapper = new BanedkInvocation(webClient, url);              JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() );
91              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
92                            
93              HtmlPage page = (HtmlPage) breaker.invoke(wrapper);              Document doc = (Document) breaker.invoke(wrapper);
94                                            
95                            
96              boolean currentStation = false;              boolean currentStation = false;
97              boolean currentStationSaved = false;              boolean currentStationSaved = false;
98                            
99              List<HtmlElement> tables = page.getDocumentElement().getElementsByAttribute("table", "class", "Rute");              Elements tables = doc.getElementsByClass("Rute");
100                
101              if (tables.size() == 1) {              if (tables.size() == 1) {
102                  HtmlElement timetable = tables.get(0);                  Element timetable = tables.get(0);
103                  DomNodeList<HtmlElement> rows = timetable.getElementsByTagName("tr");                  Elements rows = timetable.getElementsByTag("tr");
104                                    
105                  for (int i=0; i<rows.size(); i++) {                  for (int i=0; i<rows.size(); i++) {
106                          if (i==0) //First row is column headers                          if (i==0) //First row is column headers
107                                  continue;                                  continue;
108                                                    
109                                                    
110                          HtmlElement row = rows.get(i);                          Element row = rows.get(i);
111                          DomNodeList<HtmlElement> fields = row.getElementsByTagName("td");                          Elements fields = row.getElementsByTag("td");
112    
113                                                    
114                          if (currentStationSaved == false && fields.get(0).getAttribute("class").equalsIgnoreCase("Tidsstreg")) {                          if (currentStationSaved == false && fields.get(0).attr("class").equalsIgnoreCase("Tidsstreg")) {
115                                  currentStation = true;                                  currentStation = true;
116                                  continue;                                  continue;
117                          }                          }
118                                                    
119                          TimetableBean bean = new TimetableBean();                          TimetableEntry entry = new TimetableEntry();
120                          bean.setStation( fields.get(0).asText() );                          
121                          bean.setArrival( fields.get(1).asText() );                          String station = fields.get(0).text() ;
122                          bean.setDeparture( fields.get(2).asText() );                          if (station.equals("København"))
123                                    station = "København H"; //correct inconsistency in naming
124                            
125                            entry.setStation( station );
126                            entry.setArrival( fields.get(1).text() );
127                            entry.setDeparture( fields.get(2).text() );
128                            
129                            boolean cancelled = fields.get(3).text().equalsIgnoreCase("aflyst");
130                            entry.setCancelled(cancelled);
131                                                    
132                          if (currentStation == true && currentStationSaved == false ) {                          if (currentStation == true && currentStationSaved == false ) {
133                                  bean.setCurrent(currentStation);                                  entry.setCurrent(currentStation);
134                                  currentStationSaved = true;                                  currentStationSaved = true;
135                          }                          }
136                                                    
137                          timetableList.add(bean);                          entry.setStationId( getStationId( station ));
138                            
139                            timetableBean.entries.add(entry);
140                    }
141                    
142                    //TODO: There is an off-by-one error in this cancelled parser thingie
143                    final String cancelledString = "Aflyst";
144                    for (int i=0;i<timetableBean.entries.size(); i++) { //handle cancelled labels
145                            final int lastIdx = (timetableBean.entries.size() - 1);
146                            
147                            TimetableEntry current = timetableBean.entries.get(i);
148                            if (current.isCancelled()) {
149                                    if (i == 0) {
150                                            current.setDeparture(cancelledString);
151                                    } else if (i == lastIdx) {
152                                            current.setArrival(cancelledString);
153                                    } else if (i>0 && i<lastIdx) {
154                                            TimetableEntry next = timetableBean.entries.get(i+1);
155                                            TimetableEntry prev = timetableBean.entries.get(i-1);
156                                            
157                                            if (next.isCancelled())
158                                                    current.setDeparture(cancelledString);
159                                            if (prev.isCancelled())
160                                                    current.setArrival(cancelledString);
161                                    }
162                            }
163                  }                  }
164                                    
165              } else {              } else {
166                  logger.warning("No time table found, trainID=" + trainID + " type=" + type);                  logger.warning("No time table found, trainID=" + trainID + " type=" + type);
167              }              }
168              webClient.closeAllWindows();              
169                                    
170                  return timetableList;                  return timetableBean;
171          }          }
172    
173            TimetableBean lookupTimetableMobileSite(String trainID, String type) throws Exception {
174                    return new TimetableBean(); //dummy skeleton method
175            }
176            
177            @Deprecated
178            TimetableBean lookupTimetableWwwSite(String trainID, String type) throws Exception {            
179                    TimetableBean timetableBean = new TimetableBean();
180                    
181                    String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276";
182                    logger.fine("URL:" + url);
183    
184                
185                JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() );
186                CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
187                
188                Document doc = (Document) breaker.invoke(wrapper);
189                        
190                
191                boolean currentStation = false;
192                boolean currentStationSaved = false;
193                
194                Elements tables = doc.getElementsByClass("Rute");
195                
196                if (tables.size() == 1) {
197                    Element timetable = tables.get(0);
198                    Elements rows = timetable.getElementsByTag("tr");
199                    
200                    for (int i=0; i<rows.size(); i++) {
201                            if (i==0) //First row is column headers
202                                    continue;
203                            
204                            
205                            Element row = rows.get(i);
206                            Elements fields = row.getElementsByTag("td");
207    
208                            
209                            if (currentStationSaved == false && fields.get(0).attr("class").equalsIgnoreCase("Tidsstreg")) {
210                                    currentStation = true;
211                                    continue;
212                            }
213                            
214                            TimetableEntry entry = new TimetableEntry();
215                            
216                            String station = DepartureFetcher.cleanText( fields.get(0).text() ) ;
217                            if (station.equals("København"))
218                                    station = "København H"; //correct inconsistency in naming
219                            
220                            String arrival = DepartureFetcher.cleanText( fields.get(1).text() );
221                            String departure = DepartureFetcher.cleanText( fields.get(2).text() );
222                            
223                            entry.setStation( station );
224                            entry.setArrival( arrival );
225                            entry.setDeparture( departure );
226                    
227                            
228                            if (currentStation == true && currentStationSaved == false ) {
229                                    entry.setCurrent(currentStation);
230                                    currentStationSaved = true;
231                            }
232                            
233                            entry.setStationId( getStationId( station ));
234                            
235                            timetableBean.entries.add(entry);
236                    }              
237                    
238                } else {
239                    logger.warning("No time table found, trainID=" + trainID + " type=" + type);
240                }
241                
242                    
243                    return timetableBean;
244            }      
245                    
246  }  }

Legend:
Removed from v.591  
changed lines
  Added in v.1331

  ViewVC Help
Powered by ViewVC 1.1.20