--- android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java 2011/04/20 05:25:48 1331 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java 2011/05/02 12:21:49 1412 @@ -12,10 +12,13 @@ import org.jsoup.nodes.Element; import org.jsoup.select.Elements; +import dk.thoerup.android.traininfo.common.StationBean; +import dk.thoerup.android.traininfo.common.StationEntry; 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.genericjavautils.TimeoutMap; import dk.thoerup.traininfoservice.Statistics; import dk.thoerup.traininfoservice.TraininfoSettings; import dk.thoerup.traininfoservice.db.StationDAO; @@ -24,7 +27,7 @@ Map cache; - Map stationCache; + Map stationCache; StationDAO stationDao = new StationDAO(); @@ -37,7 +40,7 @@ this.settings = settings; cache = new TimeoutMap( settings.getCacheTimeout() ); - stationCache = new TimeoutMap( 3*60*60*1000 ); + stationCache = new TimeoutMap( 3*60*60*1000 ); } @@ -56,7 +59,7 @@ } TimetableBean lookupTimetable(String trainID, String type) throws Exception { - if (settings.getUseAzureSite() == true ){ + if (settings.getBackend() == TraininfoSettings.Backend.Azure ){ return lookupTimetableAzureSite(trainID, type); } else { @@ -64,20 +67,29 @@ } } - int getStationId(String name) { - Integer id = stationCache.get(name); + StationEntry getStationId(String name) { + StationEntry station = stationCache.get(name); - if (id == null) { + if (station == null) { try { - id = stationDao.getIdByName(name); - stationCache.put(name, id); + StationBean bean = stationDao.getByName(name); + if (bean.entries.size() == 1) { + station = bean.entries.get(0); + stationCache.put(name,station); + } } catch (SQLException e) { logger.log(Level.SEVERE, "getStationId failed", e); - id = -1; } } - return id; + return station; + } + + String correctStationName(String name) { + if (name.equals("København")) + name = "København H"; //correct inconsistency in naming + + return name; } TimetableBean lookupTimetableAzureSite(String trainID, String type) throws Exception { @@ -118,9 +130,7 @@ TimetableEntry entry = new TimetableEntry(); - String station = fields.get(0).text() ; - if (station.equals("København")) - station = "København H"; //correct inconsistency in naming + String station = correctStationName( fields.get(0).text() ); entry.setStation( station ); entry.setArrival( fields.get(1).text() ); @@ -134,7 +144,7 @@ currentStationSaved = true; } - entry.setStationId( getStationId( station )); + entry.setStationEntry( getStationId( station )); timetableBean.entries.add(entry); } @@ -171,7 +181,53 @@ } TimetableBean lookupTimetableMobileSite(String trainID, String type) throws Exception { - return new TimetableBean(); //dummy skeleton method + TimetableBean timetableBean = new TimetableBean(); + + String url = "http://mobil.bane.dk/mobilStation.asp?artikelID=5332&tognummer=" + trainID + "&webprofil=" + type + "&mode=rute"; + logger.fine("URL:" + url); + + + JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() ); + CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); + + Document doc = (Document) breaker.invoke(wrapper); + + Element content = doc.getElementsByClass("contentDiv").get(1); + Element dlist = content.child(0); + + + Elements rows = dlist.getElementsByTag("dt"); + + for (int i=0; i