--- android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java 2009/10/02 13:44:31 386 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java 2011/04/04 10:56:44 1255 @@ -1,81 +1,242 @@ package dk.thoerup.traininfoservice.banedk; -import java.io.IOException; + import java.net.URL; -import java.util.ArrayList; -import java.util.List; +import java.sql.SQLException; +import java.util.Map; +import java.util.logging.Level; import java.util.logging.Logger; -import com.gargoylesoftware.htmlunit.*; -import com.gargoylesoftware.htmlunit.html.*; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.select.Elements; + +import dk.thoerup.android.traininfo.common.TimetableBean; +import dk.thoerup.android.traininfo.common.TimetableEntry; +import dk.thoerup.circuitbreaker.CircuitBreaker; +import dk.thoerup.circuitbreaker.CircuitBreakerManager; +import dk.thoerup.traininfoservice.Statistics; +import dk.thoerup.traininfoservice.db.StationDAO; public class TimetableFetcher { + + + Map cache; + Map stationCache; + + StationDAO stationDao = new StationDAO(); - class NullRefreshHandler implements RefreshHandler { - public void handleRefresh(Page arg0, URL arg1, int arg2) throws IOException { - } + + Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); + + private boolean useAzureSite; + private int replyTimeout; + + public TimetableFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) { + useAzureSite = azureSite; + this.replyTimeout = replyTimeout; + cache = new TimeoutMap(cacheTimeout); + stationCache = new TimeoutMap( 3*60*60*1000 ); } - Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); + + TimetableBean cachedLookupTimetable(String trainID, String type) throws Exception { + String key = trainID+type; + TimetableBean list = cache.get(key); + + if (list == null) { + list = lookupTimetable(trainID,type); + cache.put(key, list); + } else { + Statistics.getInstance().incrementTimetableCacheHits(); + logger.info("Timetable: Cache hit " + trainID); + } + return list; + } + + TimetableBean lookupTimetable(String trainID, String type) throws Exception { + if (useAzureSite == true ){ + return lookupTimetableAzureSite(trainID, type); + + } else { + return lookupTimetableWwwSite(trainID, type); + } + } + + int getStationId(String name) { + Integer id = stationCache.get(name); + + if (id == null) { + try { + id = stationDao.getIdByName(name); + stationCache.put(name, id); + } catch (SQLException e) { + logger.log(Level.SEVERE, "getStationId failed", e); + id = -1; + } + } + + return id; + } - List lookupTimetable(String trainID, String type) throws Exception { - List timetableList = new ArrayList(); + TimetableBean lookupTimetableAzureSite(String trainID, String type) throws Exception { + TimetableBean timetableBean = new TimetableBean(); - String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276"; - - final WebClient webClient = new WebClient(); - webClient.setTimeout(2500); - webClient.setJavaScriptEnabled(false); - webClient.setRefreshHandler( new NullRefreshHandler() ); - webClient.setCssEnabled(false); - + String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID; + logger.fine("URL:" + url); - final HtmlPage page = webClient.getPage(url); + JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout); + CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); + Document doc = (Document) breaker.invoke(wrapper); + boolean currentStation = false; boolean currentStationSaved = false; - List tables = page.getDocumentElement().getElementsByAttribute("table", "class", "Rute"); + Elements tables = doc.getElementsByClass("Rute"); + if (tables.size() == 1) { - HtmlElement timetable = tables.get(0); - DomNodeList rows = timetable.getElementsByTagName("tr"); + Element timetable = tables.get(0); + Elements rows = timetable.getElementsByTag("tr"); for (int i=0; i fields = row.getElementsByTagName("td"); + Element row = rows.get(i); + Elements fields = row.getElementsByTag("td"); + - if (currentStationSaved == false && fields.get(0).getAttribute("class").equalsIgnoreCase("Tidsstreg")) { + if (currentStationSaved == false && fields.get(0).attr("class").equalsIgnoreCase("Tidsstreg")) { currentStation = true; continue; } - TimetableBean bean = new TimetableBean(); - bean.setStation( fields.get(0).asText() ); - bean.setArrival( fields.get(1).asText() ); - bean.setDeparture( fields.get(2).asText() ); + TimetableEntry entry = new TimetableEntry(); + + String station = fields.get(0).text() ; + if (station.equals("København")) + station = "København H"; //correct inconsistency in naming + + entry.setStation( station ); + entry.setArrival( fields.get(1).text() ); + entry.setDeparture( fields.get(2).text() ); + + boolean cancelled = fields.get(3).text().equalsIgnoreCase("aflyst"); + entry.setCancelled(cancelled); if (currentStation == true && currentStationSaved == false ) { - bean.setCurrent(currentStation); + entry.setCurrent(currentStation); currentStationSaved = true; } - timetableList.add(bean); - + entry.setStationId( getStationId( station )); + + timetableBean.entries.add(entry); + } + + //TODO: There is an off-by-one error in this cancelled parser thingie + final String cancelledString = "Aflyst"; + for (int i=0;i0 && i