/[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 1907 - (show annotations) (download)
Fri Jan 4 10:53:49 2013 UTC (11 years, 4 months ago) by torben
File size: 7919 byte(s)
Add support for detecting duplicate station keys
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 Map<String,String> banedkStations = new TreeMap<String,String>();
64 Set<String> dbStations = new TreeSet<String>();
65
66 StringBuilder sb = new StringBuilder();
67 sb.append("<html>");
68 sb.append("<head>");
69 sb.append("<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'></script>\n");
70 sb.append("<script type='text/javascript' src='compareloader.js'></script>\n");
71
72 sb.append("</head>");
73 sb.append("<body>");
74
75 StationDAO dao = new StationDAO();
76
77 try {
78 Document doc = (Document) cb.invoke(jsoup);
79
80 Elements tables = doc.getElementsByClass("Oversigt");
81
82 for(Element e : tables) {
83 if (e.tagName().equals("table") ){
84 Elements links = e.getElementsByTag("a");
85 for (Element link : links) {
86 banedkStations.put( link.text(), link.attr("href") );
87 }
88 }
89 }
90
91 StationBean bean = dao.dumpAll();
92
93 ////////////////////////////////
94 sb.append("<h2>Duplicate ID's in DB</h2>\n");
95 sb.append("<ul>");
96 Map<String,String> stationIDS = new TreeMap<String,String>();
97 for (StationEntry station : bean.entries) {
98 if (station.isRegional()) {
99 String id = station.getRegional();
100 String oldstation = stationIDS.get(id);
101 if (oldstation == null) {
102 stationIDS.put(id, station.getName());
103 } else {
104 sb.append("<li> Duplicate found on key=" + id + " old station=" + oldstation + " current=" + station.getName());
105 }
106 }
107 if (station.isStrain()) {
108 String id = station.getStrain();
109 String oldstation= stationIDS.get(id);
110 if (oldstation == null) {
111 stationIDS.put(id, station.getName());
112 } else {
113 sb.append("<li> Duplicate found on key=" + id + " old station=" + oldstation + " current=" + station.getName());
114 }
115 }
116 }
117 sb.append("</ul>");
118
119 ////////////////////////////////
120
121 sb.append("<h2>Active stations in DB</h2>\n");
122 sb.append("<ul>");
123
124
125 for (StationEntry station : bean.entries) {
126 if (station.isRegional() || station.isStrain()) {
127 String href = banedkStations.get(station.getName() );
128 String hrefCode = "null";
129 if ( href != null) {
130 String hrefParts[] = href.split("/");
131 hrefCode = URLDecoder.decode(hrefParts[4], "UTF-8");
132 }
133
134 String id = "";
135 String uri = "";
136 if (station.isRegional() ) {
137 id = station.getRegional();
138 uri = "/Trafikinformation/AfgangAnkomst/Afgang/" + id + "/Fjerntog";
139 }
140 if (station.isStrain() ) {
141 id = station.getStrain();
142 uri = "/Trafikinformation/AfgangAnkomst/Afgang/" + id + "/S-Tog";
143 }
144
145 String idMismatch = "";
146 if (!hrefCode.equals(id) ) {
147 idMismatch = " ID Mismatch (DB="+id+ ", BaneDK=" + hrefCode + ")";
148 }
149
150
151 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" );
152
153 dbStations.add(station.getName());
154 }
155
156
157 }
158 sb.append("</ul>\n");
159
160 } catch (Exception e) {
161 throw new ServletException(e);
162 }
163
164
165
166 sb.append("<h2>stations on website not in db</h2>\n");
167 sb.append("<ul>");
168 Set<String> tmpDbStations = new TreeSet<String>( dbStations );
169 Set<String> tmpBanedkStations = new TreeSet<String>( banedkStations.keySet()) ;
170 tmpBanedkStations.removeAll(tmpDbStations);
171
172
173
174 for(String s : tmpBanedkStations) {
175 String uri = banedkStations.get(s);
176 String disabled = "";
177 //String data = hasData(uri, false);
178
179 try {
180 boolean tmpdisabled = dao.hasDisabledStation(s);
181 if (tmpdisabled == true)
182 disabled = " - disabled";
183 } catch (Exception e) {
184 throw new ServletException(e);
185 }
186
187 sb.append( "<li><a target='_blank' href='http://trafikinfo.bane.dk" + uri + "'>" + s + "</a>&nbsp;<span class='uri' requiredata='false' uri='" + uri + "'></span>" + disabled + "</li>\n" );
188 }
189 sb.append("</ul>");
190
191
192 sb.append("<h2>stations in db not on website </h2>");
193 sb.append("<ul>");
194 tmpDbStations = new TreeSet<String>( dbStations );
195 tmpBanedkStations = new TreeSet<String>( banedkStations.keySet() ) ;
196 tmpDbStations.removeAll(tmpBanedkStations);
197 for(String s : tmpDbStations) {
198 sb.append( "<li>" + s + "</li>\n" );
199 }
200 sb.append("</ul>");
201 sb.append("<font size='1' id='antal'></font>");
202 sb.append("</body></html>\n");
203
204 return sb.toString();
205
206 }
207
208 String hasData(String uri, boolean requiredata) {
209 String key = uri + requiredata;
210
211 String data = cache.get(key);
212 if (data == null) {
213
214 data = "<font color='yellow'>unknown</font>";
215 try {
216 data = hasDataWorker(uri,requiredata);
217 cache.put(key,data);
218 } catch (Exception e) {
219 System.out.println( e.getMessage() );
220 }
221 }
222 return data;
223 }
224
225 String hasDataWorker(String uri, boolean requiredata) throws Exception {
226 String returnVal = "";
227
228 String parts[] = uri.split("\\/");
229 String id = URLEncoder.encode(parts[4], "ISO-8859-1");
230 String uri2 = "/TrafikInformation/AfgangAnkomst/Afgang/" + id + "/" + parts[5];
231
232 CircuitBreaker cb = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
233
234 JsoupInvocation jsoup = new JsoupInvocation(new URL("http://trafikinfo.bane.dk" + uri2), 2000);
235
236 Document doc = (Document) cb.invoke(jsoup);
237
238 Element tabel = doc.getElementById("afgangtabel");
239 if ( tabel != null) {
240 if (requiredata == true) {
241 returnVal = "";
242 } else {
243 returnVal = "<font color='red'>Data!</font>";
244 }
245 } else {
246 if (requiredata == true) {
247 returnVal = "<font color='red'>No data</font>";
248 } else {
249 returnVal = "";
250 }
251 }
252
253
254 return returnVal;
255 }
256
257
258 }

  ViewVC Help
Powered by ViewVC 1.1.20