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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 975 - (hide annotations) (download)
Sat Jul 10 08:35:16 2010 UTC (13 years, 10 months ago) by torben
File size: 5805 byte(s)
Make cancelled strings from new site(trafikinfo.bane.dk) appear like they did on the old one
1 torben 350 package dk.thoerup.traininfoservice.banedk;
2    
3    
4     import java.io.IOException;
5     import java.net.URL;
6 torben 836 import java.sql.SQLException;
7 torben 350 import java.util.ArrayList;
8     import java.util.List;
9 torben 428 import java.util.Map;
10 torben 836 import java.util.logging.Level;
11 torben 350 import java.util.logging.Logger;
12    
13 torben 591 import com.gargoylesoftware.htmlunit.BrowserVersion;
14 torben 387 import com.gargoylesoftware.htmlunit.Page;
15     import com.gargoylesoftware.htmlunit.RefreshHandler;
16     import com.gargoylesoftware.htmlunit.WebClient;
17     import com.gargoylesoftware.htmlunit.html.DomNodeList;
18     import com.gargoylesoftware.htmlunit.html.HtmlElement;
19     import com.gargoylesoftware.htmlunit.html.HtmlPage;
20 torben 350
21 torben 468 import dk.thoerup.circuitbreaker.CircuitBreaker;
22     import dk.thoerup.circuitbreaker.CircuitBreakerManager;
23 torben 836 import dk.thoerup.traininfoservice.StationDAO;
24 torben 711 import dk.thoerup.traininfoservice.Statistics;
25 torben 421
26 torben 350 public class TimetableFetcher {
27    
28     class NullRefreshHandler implements RefreshHandler {
29     public void handleRefresh(Page arg0, URL arg1, int arg2) throws IOException {
30     }
31    
32     }
33 torben 387
34 torben 584 Map<String, List<TimetableBean>> cache;
35 torben 836 Map<String, Integer> stationCache;
36    
37     StationDAO stationDao = new StationDAO();
38 torben 350
39 torben 387
40 torben 350 Logger logger = Logger.getLogger(TimetableFetcher.class.getName());
41 torben 387
42 torben 581 private boolean useTempSite;
43 torben 387
44 torben 584 public TimetableFetcher(boolean tmpSite, int cacheTimeout) {
45 torben 581 useTempSite = tmpSite;
46 torben 584
47     cache = new TimeoutMap<String,List<TimetableBean>>(cacheTimeout);
48 torben 836 stationCache = new TimeoutMap<String,Integer>( 3*60*60*1000 );
49 torben 581 }
50    
51    
52 torben 451 List<TimetableBean> cachedLookupTimetable(String trainID, String type) throws Exception {
53 torben 387 String key = trainID+type;
54 torben 837 List<TimetableBean> list = cache.get(key);
55 torben 387
56     if (list == null) {
57     list = lookupTimetable(trainID,type);
58     cache.put(key, list);
59     } else {
60 torben 711 Statistics.getInstance().incrementTimetableCacheHits();
61 torben 389 logger.info("Timetable: Cache hit " + trainID);
62 torben 387 }
63     return list;
64     }
65 torben 581
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 torben 836
74     int getStationId(String name) {
75     Integer id = stationCache.get(name);
76    
77     if (id == null) {
78     try {
79 torben 842 id = stationDao.getIdByName(name);
80 torben 836 stationCache.put(name, id);
81     } catch (SQLException e) {
82     logger.log(Level.SEVERE, "getStationId failed", e);
83     id = -1;
84     }
85     }
86 torben 350
87 torben 836 return id;
88     }
89    
90 torben 581 List<TimetableBean> lookupTimetableRealSite(String trainID, String type) throws Exception {
91 torben 350 List<TimetableBean> timetableList = new ArrayList<TimetableBean>();
92    
93 torben 970 //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 torben 350
96 torben 591 final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3);
97 torben 386 webClient.setTimeout(2500);
98 torben 350 webClient.setJavaScriptEnabled(false);
99     webClient.setRefreshHandler( new NullRefreshHandler() );
100     webClient.setCssEnabled(false);
101    
102    
103 torben 939 HtmlunitInvocation wrapper = new HtmlunitInvocation(webClient, url);
104 torben 421 CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
105 torben 350
106 torben 421 HtmlPage page = (HtmlPage) breaker.invoke(wrapper);
107    
108 torben 350
109     boolean currentStation = false;
110     boolean currentStationSaved = false;
111    
112     List<HtmlElement> tables = page.getDocumentElement().getElementsByAttribute("table", "class", "Rute");
113     if (tables.size() == 1) {
114     HtmlElement timetable = tables.get(0);
115     DomNodeList<HtmlElement> rows = timetable.getElementsByTagName("tr");
116    
117     for (int i=0; i<rows.size(); i++) {
118     if (i==0) //First row is column headers
119     continue;
120    
121    
122     HtmlElement row = rows.get(i);
123     DomNodeList<HtmlElement> fields = row.getElementsByTagName("td");
124    
125     if (currentStationSaved == false && fields.get(0).getAttribute("class").equalsIgnoreCase("Tidsstreg")) {
126     currentStation = true;
127     continue;
128     }
129    
130     TimetableBean bean = new TimetableBean();
131 torben 836
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 torben 350 bean.setArrival( fields.get(1).asText() );
138     bean.setDeparture( fields.get(2).asText() );
139    
140 torben 975 boolean cancelled = fields.get(3).asText().equalsIgnoreCase("aflyst");
141     bean.setCancelled(cancelled);
142    
143 torben 350 if (currentStation == true && currentStationSaved == false ) {
144     bean.setCurrent(currentStation);
145     currentStationSaved = true;
146     }
147    
148 torben 836 bean.setStationId( getStationId( station ));
149    
150 torben 350 timetableList.add(bean);
151     }
152    
153 torben 975 final String cancelledString = "Aflyst";
154     for (int i=0;i<timetableList.size(); i++) { //handle cancelled labels
155     final int lastIdx = (timetableList.size() - 1);
156    
157     TimetableBean current = timetableList.get(i);
158     if (current.isCancelled() && i == 0) {
159     current.setDeparture(cancelledString);
160     } else if (current.isCancelled() && i == lastIdx) {
161     current.setArrival(cancelledString);
162     } else if (current.isCancelled() && i>0 && i<lastIdx) {
163     TimetableBean next = timetableList.get(i+1);
164     TimetableBean prev = timetableList.get(i-1);
165    
166     if (next.isCancelled())
167     current.setDeparture(cancelledString);
168     if (prev.isCancelled())
169     current.setArrival(cancelledString);
170     }
171     }
172    
173 torben 350 } else {
174     logger.warning("No time table found, trainID=" + trainID + " type=" + type);
175     }
176 torben 591 webClient.closeAllWindows();
177 torben 350
178     return timetableList;
179     }
180    
181     }

  ViewVC Help
Powered by ViewVC 1.1.20