--- android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java 2010/06/11 14:05:24 835 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java 2010/06/11 17:12:29 836 @@ -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,7 @@ import dk.thoerup.circuitbreaker.CircuitBreaker; import dk.thoerup.circuitbreaker.CircuitBreakerManager; +import dk.thoerup.traininfoservice.StationDAO; import dk.thoerup.traininfoservice.Statistics; public class TimetableFetcher { @@ -29,6 +32,9 @@ } Map> cache; + Map stationCache; + + StationDAO stationDao = new StationDAO(); Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); @@ -39,12 +45,13 @@ 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; - List list = cache.get(key); + List list = null;//cache.get(key); if (list == null) { list = lookupTimetable(trainID,type); @@ -63,6 +70,22 @@ return new ArrayList(); // no timetable data on temp site } } + + int getStationId(String name) { + Integer id = stationCache.get(name); + + if (id == null) { + try { + id = stationDao.getBySpecificName(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(); @@ -105,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() ); @@ -114,6 +142,8 @@ currentStationSaved = true; } + bean.setStationId( getStationId( station )); + timetableList.add(bean); }