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.net.URLDecoder;
|
7 |
import java.net.URLEncoder;
|
8 |
import java.util.Map;
|
9 |
import java.util.Set;
|
10 |
import java.util.TreeMap;
|
11 |
import java.util.TreeSet;
|
12 |
|
13 |
import javax.servlet.ServletException;
|
14 |
import javax.servlet.annotation.WebServlet;
|
15 |
import javax.servlet.http.HttpServlet;
|
16 |
import javax.servlet.http.HttpServletRequest;
|
17 |
import javax.servlet.http.HttpServletResponse;
|
18 |
|
19 |
import org.jsoup.nodes.Document;
|
20 |
import org.jsoup.nodes.Element;
|
21 |
import org.jsoup.select.Elements;
|
22 |
|
23 |
import dk.thoerup.android.traininfo.common.StationBean;
|
24 |
import dk.thoerup.android.traininfo.common.StationEntry;
|
25 |
import dk.thoerup.circuitbreaker.CircuitBreaker;
|
26 |
import dk.thoerup.circuitbreaker.CircuitBreakerManager;
|
27 |
import dk.thoerup.traininfoservice.db.StationDAO;
|
28 |
|
29 |
import dk.thoerup.genericjavautils.TimeoutMap;
|
30 |
|
31 |
|
32 |
@WebServlet(urlPatterns={"/CompareStations"})
|
33 |
public class CompareStations extends HttpServlet {
|
34 |
private static final long serialVersionUID = 1L;
|
35 |
|
36 |
Map<String,String> cache = new TimeoutMap<String,String>(600 * 1000);
|
37 |
|
38 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
39 |
try {
|
40 |
String data = "";
|
41 |
String uri = request.getParameter("uri");
|
42 |
|
43 |
if (uri != null) {
|
44 |
boolean require = Boolean.parseBoolean( request.getParameter("requiredata") );
|
45 |
data = hasData(uri, require);
|
46 |
} else {
|
47 |
data = getData(request);
|
48 |
}
|
49 |
|
50 |
PrintWriter out = response.getWriter();
|
51 |
out.print(data);
|
52 |
|
53 |
} catch (Exception e) {
|
54 |
throw new ServletException(e);
|
55 |
}
|
56 |
}
|
57 |
|
58 |
String getData(HttpServletRequest request) throws Exception {
|
59 |
CircuitBreaker cb = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
|
60 |
|
61 |
JsoupInvocation jsoup = new JsoupInvocation(new URL("http://trafikinfo.bane.dk/Trafikinformation/Stationsliste"), 5000);
|
62 |
|
63 |
|
64 |
Map<String,String> idToStation = new TreeMap<String,String>();
|
65 |
Map<String,String> stationToId = new TreeMap<String,String>();
|
66 |
Map<String,String> banedkStations = new TreeMap<String,String>();
|
67 |
|
68 |
Set<String> dbStations = new TreeSet<String>();
|
69 |
|
70 |
StringBuilder sb = new StringBuilder();
|
71 |
sb.append("<html>");
|
72 |
sb.append("<head>");
|
73 |
sb.append("<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'></script>\n");
|
74 |
sb.append("<script type='text/javascript' src='compareloader.js'></script>\n");
|
75 |
|
76 |
sb.append("</head>");
|
77 |
sb.append("<body>");
|
78 |
|
79 |
StationDAO dao = new StationDAO();
|
80 |
|
81 |
try {
|
82 |
Document doc = (Document) cb.invoke(jsoup);
|
83 |
|
84 |
Elements tables = doc.getElementsByClass("Oversigt");
|
85 |
|
86 |
for(Element e : tables) {
|
87 |
if (e.tagName().equals("table") ){
|
88 |
Elements links = e.getElementsByTag("a");
|
89 |
for (Element link : links) {
|
90 |
String name = link.text();
|
91 |
String href = link.attr("href");
|
92 |
|
93 |
String hrefParts[] = href.split("/");
|
94 |
String hrefCode = URLDecoder.decode(hrefParts[4], "UTF-8");
|
95 |
|
96 |
idToStation.put(hrefCode, name);
|
97 |
stationToId.put(name, hrefCode);
|
98 |
banedkStations.put( name, href );
|
99 |
}
|
100 |
}
|
101 |
}
|
102 |
|
103 |
StationBean bean = dao.dumpAll();
|
104 |
|
105 |
////////////////////////////////
|
106 |
sb.append("<h2>Duplicate ID's in DB</h2>\n");
|
107 |
sb.append("<ul>");
|
108 |
Map<String,String> stationIDS = new TreeMap<String,String>();
|
109 |
for (StationEntry station : bean.entries) {
|
110 |
if (station.isRegional()) {
|
111 |
String id = station.getRegional();
|
112 |
String oldstation = stationIDS.get(id);
|
113 |
if (oldstation == null) {
|
114 |
stationIDS.put(id, station.getName());
|
115 |
} else {
|
116 |
sb.append("<li> Duplicate found on key=" + id + " old station=" + oldstation + " current=" + station.getName());
|
117 |
}
|
118 |
}
|
119 |
if (station.isStrain()) {
|
120 |
String id = station.getStrain();
|
121 |
String oldstation= stationIDS.get(id);
|
122 |
if (oldstation == null) {
|
123 |
stationIDS.put(id, station.getName());
|
124 |
} else {
|
125 |
sb.append("<li> Duplicate found on key=" + id + " old station=" + oldstation + " current=" + station.getName());
|
126 |
}
|
127 |
}
|
128 |
}
|
129 |
sb.append("</ul>");
|
130 |
|
131 |
////////////////////////////////
|
132 |
|
133 |
sb.append("<h2>Active stations in DB</h2>\n");
|
134 |
sb.append("<ul>");
|
135 |
|
136 |
|
137 |
for (StationEntry station : bean.entries) {
|
138 |
if (station.isRegional() || station.isStrain()) {
|
139 |
String hrefCode = stationToId.get(station.getName());
|
140 |
if (hrefCode == null)
|
141 |
hrefCode = "null";
|
142 |
|
143 |
String id = "";
|
144 |
String uri = "";
|
145 |
if (station.isRegional() ) {
|
146 |
id = station.getRegional();
|
147 |
uri = "/Trafikinformation/AfgangAnkomst/Afgang/" + id + "/Fjerntog";
|
148 |
}
|
149 |
if (station.isStrain() ) {
|
150 |
id = station.getStrain();
|
151 |
uri = "/Trafikinformation/AfgangAnkomst/Afgang/" + id + "/S-Tog";
|
152 |
}
|
153 |
|
154 |
String idMismatch = "";
|
155 |
if (!hrefCode.equals(id) ) {
|
156 |
idMismatch = " ID Mismatch (DB="+id+ ", BaneDK=" + hrefCode + ")";
|
157 |
}
|
158 |
|
159 |
|
160 |
sb.append( "<li><a target='_blank' href='http://trafikinfo.bane.dk" + uri + "'>" + station.getName() + "</a> <span class='uri' requiredata='true' uri='" + uri + "'></span> " + idMismatch + "</li>\n" );
|
161 |
|
162 |
dbStations.add(station.getName());
|
163 |
}
|
164 |
|
165 |
|
166 |
}
|
167 |
sb.append("</ul>\n");
|
168 |
|
169 |
} catch (Exception e) {
|
170 |
throw new ServletException(e);
|
171 |
}
|
172 |
|
173 |
|
174 |
|
175 |
sb.append("<h2>stations on website not in db</h2>\n");
|
176 |
sb.append("<ul>");
|
177 |
Set<String> tmpDbStations = new TreeSet<String>( dbStations );
|
178 |
Set<String> tmpBanedkStations = new TreeSet<String>( banedkStations.keySet()) ;
|
179 |
tmpBanedkStations.removeAll(tmpDbStations);
|
180 |
|
181 |
|
182 |
|
183 |
for(String s : tmpBanedkStations) {
|
184 |
String uri = banedkStations.get(s);
|
185 |
String disabled = "";
|
186 |
//String data = hasData(uri, false);
|
187 |
|
188 |
try {
|
189 |
boolean tmpdisabled = dao.hasDisabledStation(s);
|
190 |
if (tmpdisabled == true)
|
191 |
disabled = " - disabled";
|
192 |
} catch (Exception e) {
|
193 |
throw new ServletException(e);
|
194 |
}
|
195 |
|
196 |
sb.append( "<li><a target='_blank' href='http://trafikinfo.bane.dk" + uri + "'>" + s + "</a> <span class='uri' requiredata='false' uri='" + uri + "'></span>" + disabled + "</li>\n" );
|
197 |
}
|
198 |
sb.append("</ul>");
|
199 |
|
200 |
|
201 |
sb.append("<h2>stations in db not on website </h2>");
|
202 |
sb.append("<ul>");
|
203 |
tmpDbStations = new TreeSet<String>( dbStations );
|
204 |
tmpBanedkStations = new TreeSet<String>( banedkStations.keySet() ) ;
|
205 |
tmpDbStations.removeAll(tmpBanedkStations);
|
206 |
for(String s : tmpDbStations) {
|
207 |
sb.append( "<li>" + s + "</li>\n" );
|
208 |
}
|
209 |
sb.append("</ul>");
|
210 |
sb.append("<font size='1' id='antal'></font>");
|
211 |
sb.append("</body></html>\n");
|
212 |
|
213 |
return sb.toString();
|
214 |
|
215 |
}
|
216 |
|
217 |
String hasData(String uri, boolean requiredata) {
|
218 |
String key = uri + requiredata;
|
219 |
|
220 |
String data = cache.get(key);
|
221 |
if (data == null) {
|
222 |
|
223 |
data = "<font color='yellow'>unknown</font>";
|
224 |
try {
|
225 |
data = hasDataWorker(uri,requiredata);
|
226 |
cache.put(key,data);
|
227 |
} catch (Exception e) {
|
228 |
System.out.println( e.getMessage() );
|
229 |
}
|
230 |
}
|
231 |
return data;
|
232 |
}
|
233 |
|
234 |
String hasDataWorker(String uri, boolean requiredata) throws Exception {
|
235 |
String returnVal = "";
|
236 |
|
237 |
String parts[] = uri.split("\\/");
|
238 |
String id = URLEncoder.encode(parts[4], "ISO-8859-1");
|
239 |
String uri2 = "/TrafikInformation/AfgangAnkomst/Afgang/" + id + "/" + parts[5];
|
240 |
|
241 |
CircuitBreaker cb = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
|
242 |
|
243 |
JsoupInvocation jsoup = new JsoupInvocation(new URL("http://trafikinfo.bane.dk" + uri2), 2000);
|
244 |
|
245 |
Document doc = (Document) cb.invoke(jsoup);
|
246 |
|
247 |
Element tabel = doc.getElementById("afgangtabel");
|
248 |
if ( tabel != null) {
|
249 |
if (requiredata == true) {
|
250 |
returnVal = "";
|
251 |
} else {
|
252 |
returnVal = "<font color='red'>Data!</font>";
|
253 |
}
|
254 |
} else {
|
255 |
if (requiredata == true) {
|
256 |
returnVal = "<font color='red'>No data</font>";
|
257 |
} else {
|
258 |
returnVal = "";
|
259 |
}
|
260 |
}
|
261 |
|
262 |
|
263 |
return returnVal;
|
264 |
}
|
265 |
|
266 |
|
267 |
}
|