--- android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java 2010/09/08 12:49:22 1036 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java 2011/04/04 10:56:44 1255 @@ -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,15 +12,17 @@ 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.traininfoservice.Statistics; +import dk.thoerup.traininfoservice.db.StationDAO; public class TimetableFetcher { - Map> cache; + Map cache; Map stationCache; StationDAO stationDao = new StationDAO(); @@ -37,14 +37,14 @@ 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); @@ -56,7 +56,7 @@ return list; } - List lookupTimetable(String trainID, String type) throws Exception { + TimetableBean lookupTimetable(String trainID, String type) throws Exception { if (useAzureSite == true ){ return lookupTimetableAzureSite(trainID, type); @@ -81,12 +81,12 @@ 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://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) , replyTimeout); CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); @@ -117,43 +117,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 lookupTimetableWwwSite(String trainID, String type) throws Exception { + TimetableBean timetableBean = new TimetableBean(); String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276"; - + logger.fine("URL:" + url); JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout); @@ -207,25 +207,28 @@ continue; } - TimetableBean bean = new TimetableBean(); + TimetableEntry entry = new TimetableEntry(); - String station = fields.get(0).text() ; + String station = DepartureFetcher.cleanText( 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() ); + String arrival = DepartureFetcher.cleanText( fields.get(1).text() ); + String departure = DepartureFetcher.cleanText( fields.get(2).text() ); + + entry.setStation( station ); + entry.setArrival( arrival ); + entry.setDeparture( departure ); 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); } } else { @@ -233,7 +236,7 @@ } - return timetableList; + return timetableBean; } }