--- android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java 2010/02/10 14:07:04 591 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java 2010/07/10 09:10:50 976 @@ -3,9 +3,11 @@ import java.io.IOException; 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; import com.gargoylesoftware.htmlunit.BrowserVersion; @@ -18,6 +20,8 @@ import dk.thoerup.circuitbreaker.CircuitBreaker; import dk.thoerup.circuitbreaker.CircuitBreakerManager; +import dk.thoerup.traininfoservice.StationDAO; +import dk.thoerup.traininfoservice.Statistics; public class TimetableFetcher { @@ -28,6 +32,9 @@ } Map> cache; + Map stationCache; + + StationDAO stationDao = new StationDAO(); Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); @@ -38,6 +45,7 @@ useTempSite = tmpSite; cache = new TimeoutMap>(cacheTimeout); + stationCache = new TimeoutMap( 3*60*60*1000 ); } @@ -49,6 +57,7 @@ list = lookupTimetable(trainID,type); cache.put(key, list); } else { + Statistics.getInstance().incrementTimetableCacheHits(); logger.info("Timetable: Cache hit " + trainID); } return list; @@ -61,12 +70,28 @@ return new ArrayList(); // no timetable data on temp site } } + + 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 lookupTimetableRealSite(String trainID, String type) throws Exception { List timetableList = new ArrayList(); - String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276"; - + //String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276"; + String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID; final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3); webClient.setTimeout(2500); @@ -75,7 +100,7 @@ webClient.setCssEnabled(false); - BanedkInvocation wrapper = new BanedkInvocation(webClient, url); + HtmlunitInvocation wrapper = new HtmlunitInvocation(webClient, url); CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); HtmlPage page = (HtmlPage) breaker.invoke(wrapper); @@ -103,17 +128,48 @@ } TimetableBean bean = new TimetableBean(); - bean.setStation( fields.get(0).asText() ); + + String station = fields.get(0).asText() ; + if (station.equals("København")) + station = "København H"; //correct inconsistency in naming + + bean.setStation( station ); bean.setArrival( fields.get(1).asText() ); bean.setDeparture( fields.get(2).asText() ); + boolean cancelled = fields.get(3).asText().equalsIgnoreCase("aflyst"); + bean.setCancelled(cancelled); + if (currentStation == true && currentStationSaved == false ) { bean.setCurrent(currentStation); currentStationSaved = true; } + bean.setStationId( getStationId( station )); + timetableList.add(bean); - + } + + final String cancelledString = "Aflyst"; + for (int i=0;i0 && i