--- android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java 2010/09/14 06:10:30 1048 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java 2011/04/20 20:33:22 1367 @@ -4,8 +4,6 @@ import java.net.URL; import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; @@ -14,37 +12,40 @@ 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.StationDAO; +import dk.thoerup.genericjavautils.TimeoutMap; import dk.thoerup.traininfoservice.Statistics; +import dk.thoerup.traininfoservice.TraininfoSettings; +import dk.thoerup.traininfoservice.db.StationDAO; public class TimetableFetcher { - Map> cache; + Map cache; Map stationCache; StationDAO stationDao = new StationDAO(); Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); + + TraininfoSettings settings; - private boolean useAzureSite; - private int replyTimeout; - - public TimetableFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) { - useAzureSite = azureSite; - this.replyTimeout = replyTimeout; + public TimetableFetcher(TraininfoSettings settings) { + this.settings = settings; - cache = new TimeoutMap>(cacheTimeout); + cache = new TimeoutMap( settings.getCacheTimeout() ); stationCache = new TimeoutMap( 3*60*60*1000 ); } - List cachedLookupTimetable(String trainID, String type) throws Exception { + TimetableBean cachedLookupTimetable(String trainID, String type) throws Exception { String key = trainID+type; - List list = cache.get(key); + TimetableBean list = cache.get(key); + list = null; //TODO: DEBUG if (list == null) { list = lookupTimetable(trainID,type); @@ -56,12 +57,12 @@ return list; } - List lookupTimetable(String trainID, String type) throws Exception { - if (useAzureSite == true ){ + TimetableBean lookupTimetable(String trainID, String type) throws Exception { + if (settings.getUseAzureSite() == true ){ return lookupTimetableAzureSite(trainID, type); } else { - return lookupTimetableWwwSite(trainID, type); + return lookupTimetableMobileSite(trainID, type); } } @@ -81,14 +82,14 @@ return id; } - List lookupTimetableAzureSite(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://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID; logger.fine("URL:" + url); - JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout); + JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() ); CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); Document doc = (Document) breaker.invoke(wrapper); @@ -117,43 +118,43 @@ continue; } - TimetableBean bean = new TimetableBean(); + TimetableEntry entry = new TimetableEntry(); String station = fields.get(0).text() ; if (station.equals("København")) station = "København H"; //correct inconsistency in naming - bean.setStation( station ); - bean.setArrival( fields.get(1).text() ); - bean.setDeparture( fields.get(2).text() ); + entry.setStation( station ); + entry.setArrival( fields.get(1).text() ); + entry.setDeparture( fields.get(2).text() ); boolean cancelled = fields.get(3).text().equalsIgnoreCase("aflyst"); - bean.setCancelled(cancelled); + entry.setCancelled(cancelled); if (currentStation == true && currentStationSaved == false ) { - bean.setCurrent(currentStation); + entry.setCurrent(currentStation); currentStationSaved = true; } - bean.setStationId( getStationId( station )); + entry.setStationId( getStationId( station )); - timetableList.add(bean); + 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 lookupTimetableWwwSite(String trainID, String type) throws Exception { - List timetableList = new ArrayList(); + TimetableBean lookupTimetableMobileSite(String trainID, String type) throws Exception { + TimetableBean timetableBean = new TimetableBean(); + + String url = "http://mobil.bane.dk/mobilStation.asp?artikelID=5332&tognummer=" + trainID + "&webprofil=" + type + "&mode=rute"; + logger.fine("URL:" + url); + + + JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() ); + CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); + + Document doc = (Document) breaker.invoke(wrapper); + + Element content = doc.getElementsByClass("contentDiv").get(1); + Element dlist = content.child(0); + + + Elements rows = dlist.getElementsByTag("dt"); + + for (int i=0; i