--- android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/CompareStations.java 2011/05/05 10:48:29 1450 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/CompareStations.java 2012/02/24 19:10:18 1693 @@ -3,8 +3,9 @@ import java.io.IOException; import java.io.PrintWriter; import java.net.URL; -import java.util.ArrayList; +import java.util.Map; import java.util.Set; +import java.util.TreeMap; import java.util.TreeSet; import javax.servlet.ServletException; @@ -34,9 +35,11 @@ JsoupInvocation jsoup = new JsoupInvocation(new URL("http://trafikinfo.bane.dk/Trafikinformation/Stationsliste"), 5000); - Set banedkStations = new TreeSet(); + Map banedkStations = new TreeMap(); Set dbStations = new TreeSet(); + StationDAO dao = new StationDAO(); + try { Document doc = (Document) cb.invoke(jsoup); @@ -45,13 +48,12 @@ for(Element e : tables) { if (e.tagName().equals("table") ){ Elements links = e.getElementsByTag("a"); - for (Element link : links) { - banedkStations.add( link.text() ); + for (Element link : links) { + banedkStations.put( link.text(), link.attr("href") ); } } } - StationDAO dao = new StationDAO(); StationBean bean = dao.dumpAll(); for (StationEntry station : bean.entries) { if (station.isRegional() || station.isStrain()) { @@ -70,10 +72,25 @@ sb.append("

stations on website not in db

"); sb.append("
    "); Set tmpDbStations = new TreeSet( dbStations ); - Set tmpBanedkStations = new TreeSet( banedkStations) ; + Set tmpBanedkStations = new TreeSet( banedkStations.keySet()) ; tmpBanedkStations.removeAll(tmpDbStations); + + + for(String s : tmpBanedkStations) { - sb.append( "
  • " + s + "
  • " ); + String uri = banedkStations.get(s); + String disabled = ""; + String data = hasData(uri); + + try { + boolean tmpdisabled = dao.hasDisabledStation(s); + if (tmpdisabled == true) + disabled = "disabled"; + } catch (Exception e) { + throw new ServletException(e); + } + + sb.append( "
  • " + s + "  " + data + " - " + disabled + "
  • " ); } sb.append("
"); @@ -81,7 +98,7 @@ sb.append("

stations in db not on website

"); sb.append("
    "); tmpDbStations = new TreeSet( dbStations ); - tmpBanedkStations = new TreeSet( banedkStations) ; + tmpBanedkStations = new TreeSet( banedkStations.keySet() ) ; tmpDbStations.removeAll(tmpBanedkStations); for(String s : tmpDbStations) { sb.append( "
  • " + s + "
  • " ); @@ -92,8 +109,31 @@ PrintWriter out = response.getWriter(); out.print(sb.toString()); + } + + String hasData(String uri) { + String returnVal = "unknown"; + try { + CircuitBreaker cb = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); + + JsoupInvocation jsoup = new JsoupInvocation(new URL("http://trafikinfo.bane.dk" + uri), 5000); + + Document doc = (Document) cb.invoke(jsoup); + + Element tabel = doc.getElementById("afgangtabel"); + if ( tabel != null) { + returnVal = "Data!"; + } else { + returnVal = ""; + } + + } catch (Exception e) { + System.out.println( e.getMessage() ); + } + return returnVal; } + }