/[projects]/android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/CompareStations.java
ViewVC logotype

Contents of /android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/CompareStations.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1450 - (show annotations) (download)
Thu May 5 10:48:29 2011 UTC (13 years ago) by torben
File size: 2960 byte(s)
Try with treeset instead of arraylist
1 package dk.thoerup.traininfoservice.banedk;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.net.URL;
6 import java.util.ArrayList;
7 import java.util.Set;
8 import java.util.TreeSet;
9
10 import javax.servlet.ServletException;
11 import javax.servlet.annotation.WebServlet;
12 import javax.servlet.http.HttpServlet;
13 import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.http.HttpServletResponse;
15
16 import org.jsoup.nodes.Document;
17 import org.jsoup.nodes.Element;
18 import org.jsoup.select.Elements;
19
20 import dk.thoerup.android.traininfo.common.StationBean;
21 import dk.thoerup.android.traininfo.common.StationEntry;
22 import dk.thoerup.circuitbreaker.CircuitBreaker;
23 import dk.thoerup.circuitbreaker.CircuitBreakerManager;
24 import dk.thoerup.traininfoservice.db.StationDAO;
25
26
27 @WebServlet(urlPatterns={"/CompareStations"})
28 public class CompareStations extends HttpServlet {
29 private static final long serialVersionUID = 1L;
30
31
32 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
33 CircuitBreaker cb = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
34
35 JsoupInvocation jsoup = new JsoupInvocation(new URL("http://trafikinfo.bane.dk/Trafikinformation/Stationsliste"), 5000);
36
37 Set<String> banedkStations = new TreeSet<String>();
38 Set<String> dbStations = new TreeSet<String>();
39
40 try {
41 Document doc = (Document) cb.invoke(jsoup);
42
43 Elements tables = doc.getElementsByClass("Oversigt");
44
45 for(Element e : tables) {
46 if (e.tagName().equals("table") ){
47 Elements links = e.getElementsByTag("a");
48 for (Element link : links) {
49 banedkStations.add( link.text() );
50 }
51 }
52 }
53
54 StationDAO dao = new StationDAO();
55 StationBean bean = dao.dumpAll();
56 for (StationEntry station : bean.entries) {
57 if (station.isRegional() || station.isStrain()) {
58 dbStations.add(station.getName());
59 }
60 }
61
62 } catch (Exception e) {
63 throw new ServletException(e);
64 }
65
66
67
68 StringBuilder sb = new StringBuilder();
69
70 sb.append("<h2>stations on website not in db</h2>");
71 sb.append("<ul>");
72 Set<String> tmpDbStations = new TreeSet<String>( dbStations );
73 Set<String> tmpBanedkStations = new TreeSet<String>( banedkStations) ;
74 tmpBanedkStations.removeAll(tmpDbStations);
75 for(String s : tmpBanedkStations) {
76 sb.append( "<li>" + s + "</li>" );
77 }
78 sb.append("</ul>");
79
80
81 sb.append("<h2>stations in db not on website </h2>");
82 sb.append("<ul>");
83 tmpDbStations = new TreeSet<String>( dbStations );
84 tmpBanedkStations = new TreeSet<String>( banedkStations) ;
85 tmpDbStations.removeAll(tmpBanedkStations);
86 for(String s : tmpDbStations) {
87 sb.append( "<li>" + s + "</li>" );
88 }
89 sb.append("</ul>");
90
91
92 PrintWriter out = response.getWriter();
93 out.print(sb.toString());
94
95
96
97 }
98
99 }

  ViewVC Help
Powered by ViewVC 1.1.20