--- android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureFetcher.java 2010/09/12 18:41:17 1038 +++ android/TrainInfoServiceGoogle/src/dk/thoerup/traininfoservice/banedk/DepartureFetcher.java 2010/09/22 21:09:39 1105 @@ -4,16 +4,26 @@ import java.net.URL; import java.net.URLEncoder; import java.util.Collections; +import java.util.HashMap; import java.util.Map; +import java.util.logging.Level; import java.util.logging.Logger; +import net.sf.jsr107cache.Cache; +import net.sf.jsr107cache.CacheException; +import net.sf.jsr107cache.CacheManager; + import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; +import com.google.appengine.api.memcache.jsr107cache.GCacheFactory; + +import dk.thoerup.android.traininfo.common.DepartureBean; +import dk.thoerup.android.traininfo.common.DepartureEntry; +import dk.thoerup.android.traininfo.common.StationBean.StationEntry; import dk.thoerup.circuitbreaker.CircuitBreaker; import dk.thoerup.circuitbreaker.CircuitBreakerManager; -import dk.thoerup.traininfoservice.StationBean; import dk.thoerup.traininfoservice.StationDAO; import dk.thoerup.traininfoservice.Statistics; @@ -23,38 +33,48 @@ STOG, REGIONAL } + Cache cache; - Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); - - Map cache; + Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); StationDAO stationDao = new StationDAO(); private boolean useAzureSite; private int replyTimeout; + @SuppressWarnings("unchecked") public DepartureFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) { this.replyTimeout = replyTimeout; useAzureSite = azureSite; - cache = new TimeoutMap(cacheTimeout); + + Map props = new HashMap(); + props.put(GCacheFactory.EXPIRATION_DELTA_MILLIS, cacheTimeout); + + try { + cache = CacheManager.getInstance().getCacheFactory().createCache(props); + } catch (CacheException e) { + logger.log(Level.WARNING, "error creating cache", e); + } + } public DepartureBean cachedLookupDepartures(int stationID, boolean arrival) throws Exception { - final String key = "" + stationID + ":" + arrival; + final String key = "departure:" + stationID + ":" + arrival; - DepartureBean departureBean = cache.get(key); - + DepartureBean departureBean = (DepartureBean) cache.get(key); if (departureBean == null) { departureBean = lookupDepartures(stationID,arrival); cache.put(key, departureBean); + logger.info("Departure: Cache miss " + key + " !!! "); //remove before production } else { Statistics.getInstance().incrementDepartureCacheHits(); - logger.info("Departure: Cache hit " + key); //remove before production + logger.info("Departure: Cache hit " + key); } + return departureBean; } @@ -63,27 +83,28 @@ DepartureBean departureBean = new DepartureBean(); - StationBean station = stationDao.getById(stationID); + StationEntry station = stationDao.getById(stationID); + departureBean.stationName = station.getName(); if (station.getRegional() != null) { DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival); - departureBean.departureEntries.addAll( tempBean.departureEntries ); + departureBean.entries.addAll( tempBean.entries ); departureBean.notifications.addAll(tempBean.notifications); } if (station.getStrain() != null) { DepartureBean tempBean = lookupDepartures(station.getStrain(), TrainType.STOG, arrival); - departureBean.departureEntries.addAll( tempBean.departureEntries ); + departureBean.entries.addAll( tempBean.entries ); departureBean.notifications.addAll(tempBean.notifications); } - if (departureBean.departureEntries.size() == 0) { + if (departureBean.entries.size() == 0) { logger.info("No departures found for station " + stationID); } - Collections.sort( departureBean.departureEntries ); + Collections.sort( departureBean.entries ); return departureBean; @@ -131,7 +152,7 @@ String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning"; - //logger.info("URI: " + uri); + logger.fine("URI: " + uri); JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); @@ -173,7 +194,7 @@ String trainNumber = fields.get(2).text(); if (type == TrainType.STOG) //If it is S-train we need to extract the trainNumber - trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2)); + trainNumber = trainNumber + " " + extractTrainNumberAzure(fields.get(2)); departure.setTrainNumber(trainNumber); String destination = fields.get(3).text(); @@ -193,7 +214,7 @@ departure.setType(typeString); - departureBean.departureEntries.add( departure ); + departureBean.entries.add( departure ); } } } else { @@ -220,8 +241,8 @@ - String cleanText(String input) { - //apparently JSoup interprets some of space characters on www.bane.dk as 0xA0 + public static String cleanText(String input) { + //apparently JSoup translates   characters on www.bane.dk to 0xA0 return input.replace((char) 0xA0, (char)0x20).trim(); } @@ -231,8 +252,13 @@ String type = getTypeStringWww(trainType); + stationcode = URLEncoder.encode(stationcode, "ISO-8859-1"); + String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode; + logger.fine("URI:" + uri); + + JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); @@ -241,11 +267,26 @@ String tableName = arrival == false ? "afgangtabel" : "ankomsttabel"; Element table = page.getElementById(tableName); + + if (table != null) { Elements tableRows = table.getElementsByTag("tr"); + boolean passedTidsstreg = false; + boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0); + for (Element currentRow : tableRows) { String rowClass = currentRow.attr("class"); + + if (tidsstregExists == true && passedTidsstreg == false) { + if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) { + passedTidsstreg = true; + } else { + continue; + } + } + + if (rowClass != null && rowClass.toLowerCase().contains("station") ) { Elements fields = currentRow.getElementsByTag("td"); @@ -263,7 +304,7 @@ String trainNumber = cleanText( fields.get(2).text() ); if (type.equalsIgnoreCase("S2")) //If it is S-train we need to extract the trainNumber - trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2)); + trainNumber = trainNumber + " " + extractTrainNumberWww(fields.get(2)); departure.setTrainNumber(trainNumber); String destination = cleanText( fields.get(3).text() ); @@ -283,7 +324,7 @@ departure.setType(type); - departureBean.departureEntries.add(departure); + departureBean.entries.add(departure); } @@ -326,7 +367,7 @@ return note.trim(); } - private String extractTrainNumber(Element trainTd) { + private String extractTrainNumberAzure(Element trainTd) { Element anchorElement = trainTd.getElementsByTag("a").get(0); String href = anchorElement.attr("href"); @@ -336,6 +377,25 @@ return number; } + private String extractTrainNumberWww(Element trainTd) { + String number = ""; + Element anchorElement = trainTd.getElementsByTag("a").get(0); + String href = anchorElement.attr("href"); + String argstring = href.substring( href.indexOf('?') + 1); + + String args[] = argstring.split("&"); + for (String arg : args) { + String pair[] = arg.split("="); // Key=pair[0], Value=pair[1] + + if (pair[0].equalsIgnoreCase("TogNr")) + number = pair[1]; + } + + + return number; + } + + //test /* public static void main(String args[]) throws Exception {