--- android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java 2009/10/22 06:04:45 468 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java 2010/06/11 20:50:40 842 @@ -3,11 +3,14 @@ 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; import com.gargoylesoftware.htmlunit.Page; import com.gargoylesoftware.htmlunit.RefreshHandler; import com.gargoylesoftware.htmlunit.WebClient; @@ -17,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 { @@ -26,11 +31,23 @@ } - Map> cache = new TimeoutMap>(120 * 1000); + Map> cache; + Map stationCache; + + StationDAO stationDao = new StationDAO(); Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); + private boolean useTempSite; + + public TimetableFetcher(boolean tmpSite, int cacheTimeout) { + useTempSite = tmpSite; + + cache = new TimeoutMap>(cacheTimeout); + stationCache = new TimeoutMap( 3*60*60*1000 ); + } + List cachedLookupTimetable(String trainID, String type) throws Exception { String key = trainID+type; @@ -40,18 +57,43 @@ list = lookupTimetable(trainID,type); cache.put(key, list); } else { + Statistics.getInstance().incrementTimetableCacheHits(); logger.info("Timetable: Cache hit " + trainID); } return list; } + + List lookupTimetable(String trainID, String type) throws Exception { + if (useTempSite == false ){ + return lookupTimetableRealSite(trainID, type); + } else { + 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 lookupTimetable(String trainID, String type) throws Exception { + 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"; - final WebClient webClient = new WebClient(); + final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3); webClient.setTimeout(2500); webClient.setJavaScriptEnabled(false); webClient.setRefreshHandler( new NullRefreshHandler() ); @@ -86,7 +128,12 @@ } 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() ); @@ -95,6 +142,8 @@ currentStationSaved = true; } + bean.setStationId( getStationId( station )); + timetableList.add(bean); } @@ -102,6 +151,7 @@ } else { logger.warning("No time table found, trainID=" + trainID + " type=" + type); } + webClient.closeAllWindows(); return timetableList; }