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