--- android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java 2010/07/14 08:05:31 992 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java 2010/09/16 13:32:10 1060 @@ -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; @@ -22,7 +20,7 @@ public class TimetableFetcher { - Map> cache; + Map cache; Map stationCache; StationDAO stationDao = new StationDAO(); @@ -30,19 +28,21 @@ Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); - private boolean useTempSite; + private boolean useAzureSite; + private int replyTimeout; - public TimetableFetcher(boolean tmpSite, int cacheTimeout) { - useTempSite = tmpSite; + public TimetableFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) { + useAzureSite = azureSite; + this.replyTimeout = replyTimeout; - cache = new TimeoutMap>(cacheTimeout); + cache = new TimeoutMap(cacheTimeout); 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); if (list == null) { list = lookupTimetable(trainID,type); @@ -54,11 +54,12 @@ return list; } - List lookupTimetable(String trainID, String type) throws Exception { - if (useTempSite == false ){ - return lookupTimetableRealSite(trainID, type); + TimetableBean lookupTimetable(String trainID, String type) throws Exception { + if (useAzureSite == true ){ + return lookupTimetableAzureSite(trainID, type); + } else { - return new ArrayList(); // no timetable data on temp site + return lookupTimetableWwwSite(trainID, type); } } @@ -78,14 +79,14 @@ return id; } - List lookupTimetableRealSite(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"; - String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID; + String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID; + logger.fine("URL:" + url); - JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , 2500); + JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout); CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); Document doc = (Document) breaker.invoke(wrapper); @@ -114,43 +115,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