2 |
|
|
3 |
|
|
4 |
import java.net.URL; |
import java.net.URL; |
5 |
|
import java.net.URLEncoder; |
6 |
import java.util.Collections; |
import java.util.Collections; |
7 |
import java.util.Map; |
import java.util.Map; |
8 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
30 |
|
|
31 |
StationDAO stationDao = new StationDAO(); |
StationDAO stationDao = new StationDAO(); |
32 |
|
|
33 |
private boolean useTempSite; |
private boolean useAzureSite; |
34 |
|
private int replyTimeout; |
35 |
|
|
36 |
public DepartureFetcher(boolean tempSite, int cacheTimeout) { |
public DepartureFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) { |
37 |
useTempSite = tempSite; |
this.replyTimeout = replyTimeout; |
38 |
|
useAzureSite = azureSite; |
39 |
cache = new TimeoutMap<String,DepartureBean>(cacheTimeout); |
cache = new TimeoutMap<String,DepartureBean>(cacheTimeout); |
40 |
} |
} |
41 |
|
|
65 |
|
|
66 |
StationBean station = stationDao.getById(stationID); |
StationBean station = stationDao.getById(stationID); |
67 |
|
|
68 |
|
departureBean.stationName = station.getName(); |
69 |
|
|
70 |
if (station.getRegional() != null) { |
if (station.getRegional() != null) { |
71 |
DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival); |
DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival); |
72 |
departureBean.departureEntries.addAll( tempBean.departureEntries ); |
departureBean.departureEntries.addAll( tempBean.departureEntries ); |
79 |
departureBean.notifications.addAll(tempBean.notifications); |
departureBean.notifications.addAll(tempBean.notifications); |
80 |
} |
} |
81 |
|
|
82 |
|
if (departureBean.departureEntries.size() == 0) { |
83 |
|
logger.info("No departures found for station " + stationID); |
84 |
|
} |
85 |
|
|
86 |
Collections.sort( departureBean.departureEntries ); |
Collections.sort( departureBean.departureEntries ); |
87 |
|
|
88 |
|
|
90 |
} |
} |
91 |
|
|
92 |
public DepartureBean lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception { |
public DepartureBean lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception { |
93 |
if (useTempSite == false) { |
if (useAzureSite == true) { |
94 |
return lookupDeparturesNormalSite(stationcode, type, arrival); |
return lookupDeparturesAzureSite(stationcode, type, arrival); |
95 |
} else { |
} else { |
96 |
//return lookupDeparturesFromTemporarySite(stationcode, type); |
return lookupDeparturesWwwSite(stationcode, type, arrival); |
|
//TODO: find out what to to if they ever put a temp site up on trafikinfo.bane.dk |
|
|
return null; |
|
97 |
} |
} |
98 |
} |
} |
99 |
|
|
100 |
private String getTypeString(TrainType type) { |
private String getTypeStringAzure(TrainType type) { |
101 |
switch (type) { |
switch (type) { |
102 |
case STOG: |
case STOG: |
103 |
return "S-Tog"; |
return "S-Tog"; |
108 |
} |
} |
109 |
} |
} |
110 |
|
|
111 |
public DepartureBean lookupDeparturesNormalSite(String stationcode, TrainType type, boolean arrival) throws Exception { |
private String getTypeStringWww(TrainType type) { |
112 |
|
switch (type) { |
113 |
|
case STOG: |
114 |
|
return "S2"; |
115 |
|
case REGIONAL: |
116 |
|
return "FJRN"; |
117 |
|
default: |
118 |
|
return ""; //Can not happen |
119 |
|
} |
120 |
|
} |
121 |
|
|
122 |
|
public DepartureBean lookupDeparturesAzureSite(String stationcode, TrainType type, boolean arrival) throws Exception { |
123 |
|
|
124 |
DepartureBean departureBean = new DepartureBean(); |
DepartureBean departureBean = new DepartureBean(); |
125 |
|
|
126 |
|
|
127 |
String typeString = getTypeString(type); |
String typeString = getTypeStringAzure(type); |
128 |
String arrivalDeparture = (arrival==false) ? "Afgang" : "Ankomst"; |
String arrivalDeparture = (arrival==false) ? "Afgang" : "Ankomst"; |
129 |
|
|
130 |
//String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode; |
stationcode = URLEncoder.encode(stationcode,"ISO-8859-1"); |
|
String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning"; |
|
131 |
|
|
132 |
|
String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning"; |
133 |
|
|
134 |
//logger.info("URI: " + uri); |
//logger.info("URI: " + uri); |
135 |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), 2500); |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); |
136 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
137 |
|
|
138 |
Document page = (Document) breaker.invoke(wrapper); |
Document page = (Document) breaker.invoke(wrapper); |
143 |
if (table != null) { |
if (table != null) { |
144 |
Elements tableRows = table.getElementsByTag("tr"); |
Elements tableRows = table.getElementsByTag("tr"); |
145 |
|
|
146 |
|
boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0); |
147 |
|
boolean passedTidsstreg = false; |
148 |
|
|
149 |
for (Element currentRow : tableRows) { |
for (Element currentRow : tableRows) { |
150 |
String rowClass = currentRow.attr("class"); |
String rowClass = currentRow.attr("class"); |
151 |
|
|
152 |
|
if (tidsstregExists == true && passedTidsstreg == false) { |
153 |
|
if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) { |
154 |
|
passedTidsstreg = true; |
155 |
|
} else { |
156 |
|
continue; |
157 |
|
} |
158 |
|
} |
159 |
|
|
160 |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
161 |
|
|
162 |
Elements fields = currentRow.getElementsByTag("td"); |
Elements fields = currentRow.getElementsByTag("td"); |
163 |
|
|
164 |
DepartureEntry departure = new DepartureEntry(); |
DepartureEntry departure = new DepartureEntry(); |
218 |
return departureBean; |
return departureBean; |
219 |
} |
} |
220 |
|
|
221 |
/* |
public DepartureBean lookupDeparturesWwwSite(String stationcode, TrainType trainType, boolean arrival) throws Exception { |
|
@Deprecated |
|
|
public List<DepartureBean> lookupDeparturesFromTemporarySite(String stationcode, String type) throws Exception { |
|
222 |
|
|
223 |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
DepartureBean departureBean = new DepartureBean(); |
224 |
|
|
225 |
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3); |
String type = getTypeStringWww(trainType); |
226 |
webClient.setTimeout(2500); |
|
227 |
webClient.setJavaScriptEnabled(false); |
|
228 |
|
String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode; |
229 |
|
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); |
|
String uri = "http://bane.dk/lite/station.asp?w=" + type + "&s=" + stationcode; |
|
|
|
|
|
HtmlunitInvocation wrapper = new HtmlunitInvocation(webClient, uri); |
|
230 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
231 |
|
|
232 |
HtmlPage page = (HtmlPage) breaker.invoke(wrapper); |
Element page = (Element) breaker.invoke(wrapper); |
233 |
|
|
234 |
HtmlElement table = page.getElementById("traf_afgang"); |
String tableName = arrival == false ? "afgangtabel" : "ankomsttabel"; |
235 |
|
Element table = page.getElementById(tableName); |
236 |
|
|
237 |
if (table != null) { |
if (table != null) { |
238 |
DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr"); |
Elements tableRows = table.getElementsByTag("tr"); |
|
|
|
|
boolean isFirst = true; |
|
239 |
|
|
240 |
for (HtmlElement currentRow : tableRows) { |
for (Element currentRow : tableRows) { |
241 |
if (isFirst == true) { //skip table headers |
String rowClass = currentRow.attr("class"); |
242 |
isFirst = false; |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
243 |
continue; |
Elements fields = currentRow.getElementsByTag("td"); |
244 |
} |
|
245 |
|
DepartureEntry departure = new DepartureEntry(); |
246 |
DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td"); |
|
|
|
|
|
DepartureBean departure = new DepartureBean(); |
|
|
|
|
|
String time = fields.get(0).asText().trim(); |
|
|
|
|
|
if (time.equals("")) |
|
|
time = "0:00"; //Bane.dk bug work-around |
|
|
departure.setTime(time); |
|
|
|
|
|
|
|
|
String trainNumber = fields.get(1).asText(); |
|
|
departure.setTrainNumber(trainNumber); |
|
|
|
|
|
String destination = fields.get(2).asText(); |
|
|
departure.setDestination(destination); |
|
|
|
|
|
String origin = fields.get(3).asText(); |
|
|
departure.setOrigin(origin); |
|
|
|
|
|
String status = fields.get(4).asText(); |
|
|
departure.setStatus(status); |
|
|
|
|
|
String note = fields.get(5).asText(); |
|
|
departure.setNote(note); |
|
247 |
|
|
248 |
departureList.add(departure); |
|
249 |
|
String time = fields.get(0).getAllElements().get(2).text(); |
250 |
|
if (time.equals("")) |
251 |
|
time = "0:00"; //Bane.dk bug work-around |
252 |
|
departure.setTime(time); |
253 |
|
|
254 |
|
int updated = extractUpdated( fields.get(1) ); |
255 |
|
departure.setUpdated(updated); |
256 |
|
|
257 |
|
String trainNumber = fields.get(2).text(); |
258 |
|
if (type.equalsIgnoreCase("S2")) //If it is S-train we need to extract the trainNumber |
259 |
|
trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2)); |
260 |
|
departure.setTrainNumber(trainNumber); |
261 |
|
|
262 |
|
String destination = fields.get(3).text(); |
263 |
|
departure.setDestination(destination); |
264 |
|
|
265 |
|
String origin = fields.get(4).text(); |
266 |
|
departure.setOrigin(origin); |
267 |
|
|
268 |
|
String location = fields.get(5).text(); |
269 |
|
departure.setLocation(location); |
270 |
|
|
271 |
|
String status = fields.get(6).text().trim(); |
272 |
|
departure.setStatus(status); |
273 |
|
|
274 |
|
String note = extractNote( fields.get(7) ); |
275 |
|
departure.setNote(note); |
276 |
|
|
277 |
|
departure.setType(type); |
278 |
|
|
279 |
|
departureBean.departureEntries.add(departure); |
280 |
|
|
281 |
|
|
282 |
|
} |
283 |
} |
} |
284 |
} else { |
} else { |
285 |
logger.warning("No departures found for station=" + stationcode + ", type=" + type); |
logger.warning("No departures found for station=" + stationcode + ", type=" + type); |
286 |
} |
} |
|
webClient.closeAllWindows(); |
|
287 |
|
|
288 |
|
|
289 |
return departureList; |
return departureBean; |
290 |
}*/ |
} |
291 |
|
|
292 |
|
|
293 |
private int extractUpdated(Element updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif" |
private int extractUpdated(Element updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif" |
294 |
int updated = -1; |
int updated = -1; |