30 |
|
|
31 |
Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); |
Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); |
32 |
|
|
33 |
private boolean useTempSite; |
private boolean useAzureSite; |
34 |
private int replyTimeout; |
private int replyTimeout; |
35 |
|
|
36 |
public TimetableFetcher(boolean tmpSite, int cacheTimeout, int replyTimeout) { |
public TimetableFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) { |
37 |
useTempSite = tmpSite; |
useAzureSite = azureSite; |
38 |
this.replyTimeout = replyTimeout; |
this.replyTimeout = replyTimeout; |
39 |
|
|
40 |
cache = new TimeoutMap<String,List<TimetableBean>>(cacheTimeout); |
cache = new TimeoutMap<String,List<TimetableBean>>(cacheTimeout); |
57 |
} |
} |
58 |
|
|
59 |
List<TimetableBean> lookupTimetable(String trainID, String type) throws Exception { |
List<TimetableBean> lookupTimetable(String trainID, String type) throws Exception { |
60 |
if (useTempSite == false ){ |
if (useAzureSite == true ){ |
61 |
return lookupTimetableRealSite(trainID, type); |
return lookupTimetableAzureSite(trainID, type); |
62 |
|
|
63 |
} else { |
} else { |
64 |
return new ArrayList<TimetableBean>(); // no timetable data on temp site |
return lookupTimetableWwwSite(trainID, type); |
65 |
} |
} |
66 |
} |
} |
67 |
|
|
81 |
return id; |
return id; |
82 |
} |
} |
83 |
|
|
84 |
List<TimetableBean> lookupTimetableRealSite(String trainID, String type) throws Exception { |
List<TimetableBean> lookupTimetableAzureSite(String trainID, String type) throws Exception { |
85 |
List<TimetableBean> timetableList = new ArrayList<TimetableBean>(); |
List<TimetableBean> timetableList = new ArrayList<TimetableBean>(); |
86 |
|
|
87 |
//String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276"; |
//String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276"; |
170 |
|
|
171 |
return timetableList; |
return timetableList; |
172 |
} |
} |
173 |
|
|
174 |
|
List<TimetableBean> lookupTimetableWwwSite(String trainID, String type) throws Exception { |
175 |
|
List<TimetableBean> timetableList = new ArrayList<TimetableBean>(); |
176 |
|
|
177 |
|
String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276"; |
178 |
|
|
179 |
|
|
180 |
|
|
181 |
|
JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout); |
182 |
|
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
183 |
|
|
184 |
|
Document doc = (Document) breaker.invoke(wrapper); |
185 |
|
|
186 |
|
|
187 |
|
boolean currentStation = false; |
188 |
|
boolean currentStationSaved = false; |
189 |
|
|
190 |
|
Elements tables = doc.getElementsByClass("Rute"); |
191 |
|
|
192 |
|
if (tables.size() == 1) { |
193 |
|
Element timetable = tables.get(0); |
194 |
|
Elements rows = timetable.getElementsByTag("tr"); |
195 |
|
|
196 |
|
for (int i=0; i<rows.size(); i++) { |
197 |
|
if (i==0) //First row is column headers |
198 |
|
continue; |
199 |
|
|
200 |
|
|
201 |
|
Element row = rows.get(i); |
202 |
|
Elements fields = row.getElementsByTag("td"); |
203 |
|
|
204 |
|
|
205 |
|
if (currentStationSaved == false && fields.get(0).attr("class").equalsIgnoreCase("Tidsstreg")) { |
206 |
|
currentStation = true; |
207 |
|
continue; |
208 |
|
} |
209 |
|
|
210 |
|
TimetableBean bean = new TimetableBean(); |
211 |
|
|
212 |
|
String station = fields.get(0).text() ; |
213 |
|
if (station.equals("København")) |
214 |
|
station = "København H"; //correct inconsistency in naming |
215 |
|
|
216 |
|
bean.setStation( station ); |
217 |
|
bean.setArrival( fields.get(1).text() ); |
218 |
|
bean.setDeparture( fields.get(2).text() ); |
219 |
|
|
220 |
|
|
221 |
|
if (currentStation == true && currentStationSaved == false ) { |
222 |
|
bean.setCurrent(currentStation); |
223 |
|
currentStationSaved = true; |
224 |
|
} |
225 |
|
|
226 |
|
bean.setStationId( getStationId( station )); |
227 |
|
|
228 |
|
timetableList.add(bean); |
229 |
|
} |
230 |
|
|
231 |
|
} else { |
232 |
|
logger.warning("No time table found, trainID=" + trainID + " type=" + type); |
233 |
|
} |
234 |
|
|
235 |
|
|
236 |
|
return timetableList; |
237 |
|
} |
238 |
|
|
239 |
} |
} |