4 |
|
|
5 |
import java.net.URL; |
import java.net.URL; |
6 |
import java.sql.SQLException; |
import java.sql.SQLException; |
|
import java.util.ArrayList; |
|
|
import java.util.List; |
|
7 |
import java.util.Map; |
import java.util.Map; |
8 |
import java.util.logging.Level; |
import java.util.logging.Level; |
9 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
20 |
public class TimetableFetcher { |
public class TimetableFetcher { |
21 |
|
|
22 |
|
|
23 |
Map<String, List<TimetableBean>> cache; |
Map<String, TimetableBean> cache; |
24 |
Map<String, Integer> stationCache; |
Map<String, Integer> stationCache; |
25 |
|
|
26 |
StationDAO stationDao = new StationDAO(); |
StationDAO stationDao = new StationDAO(); |
35 |
useAzureSite = azureSite; |
useAzureSite = azureSite; |
36 |
this.replyTimeout = replyTimeout; |
this.replyTimeout = replyTimeout; |
37 |
|
|
38 |
cache = new TimeoutMap<String,List<TimetableBean>>(cacheTimeout); |
cache = new TimeoutMap<String,TimetableBean>(cacheTimeout); |
39 |
stationCache = new TimeoutMap<String,Integer>( 3*60*60*1000 ); |
stationCache = new TimeoutMap<String,Integer>( 3*60*60*1000 ); |
40 |
} |
} |
41 |
|
|
42 |
|
|
43 |
List<TimetableBean> cachedLookupTimetable(String trainID, String type) throws Exception { |
TimetableBean cachedLookupTimetable(String trainID, String type) throws Exception { |
44 |
String key = trainID+type; |
String key = trainID+type; |
45 |
List<TimetableBean> list = cache.get(key); |
TimetableBean list = cache.get(key); |
46 |
|
|
47 |
if (list == null) { |
if (list == null) { |
48 |
list = lookupTimetable(trainID,type); |
list = lookupTimetable(trainID,type); |
54 |
return list; |
return list; |
55 |
} |
} |
56 |
|
|
57 |
List<TimetableBean> lookupTimetable(String trainID, String type) throws Exception { |
TimetableBean lookupTimetable(String trainID, String type) throws Exception { |
58 |
if (useAzureSite == true ){ |
if (useAzureSite == true ){ |
59 |
return lookupTimetableAzureSite(trainID, type); |
return lookupTimetableAzureSite(trainID, type); |
60 |
|
|
79 |
return id; |
return id; |
80 |
} |
} |
81 |
|
|
82 |
List<TimetableBean> lookupTimetableAzureSite(String trainID, String type) throws Exception { |
TimetableBean lookupTimetableAzureSite(String trainID, String type) throws Exception { |
83 |
List<TimetableBean> timetableList = new ArrayList<TimetableBean>(); |
TimetableBean timetableBean = new TimetableBean(); |
84 |
|
|
85 |
|
|
86 |
String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID; |
String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID; |
115 |
continue; |
continue; |
116 |
} |
} |
117 |
|
|
118 |
TimetableBean bean = new TimetableBean(); |
TimetableEntry entry = new TimetableEntry(); |
119 |
|
|
120 |
String station = fields.get(0).text() ; |
String station = fields.get(0).text() ; |
121 |
if (station.equals("København")) |
if (station.equals("København")) |
122 |
station = "København H"; //correct inconsistency in naming |
station = "København H"; //correct inconsistency in naming |
123 |
|
|
124 |
bean.setStation( station ); |
entry.setStation( station ); |
125 |
bean.setArrival( fields.get(1).text() ); |
entry.setArrival( fields.get(1).text() ); |
126 |
bean.setDeparture( fields.get(2).text() ); |
entry.setDeparture( fields.get(2).text() ); |
127 |
|
|
128 |
boolean cancelled = fields.get(3).text().equalsIgnoreCase("aflyst"); |
boolean cancelled = fields.get(3).text().equalsIgnoreCase("aflyst"); |
129 |
bean.setCancelled(cancelled); |
entry.setCancelled(cancelled); |
130 |
|
|
131 |
if (currentStation == true && currentStationSaved == false ) { |
if (currentStation == true && currentStationSaved == false ) { |
132 |
bean.setCurrent(currentStation); |
entry.setCurrent(currentStation); |
133 |
currentStationSaved = true; |
currentStationSaved = true; |
134 |
} |
} |
135 |
|
|
136 |
bean.setStationId( getStationId( station )); |
entry.setStationId( getStationId( station )); |
137 |
|
|
138 |
timetableList.add(bean); |
timetableBean.entries.add(entry); |
139 |
} |
} |
140 |
|
|
141 |
//TODO: There is an off-by-one error in this cancelled parser thingie |
//TODO: There is an off-by-one error in this cancelled parser thingie |
142 |
final String cancelledString = "Aflyst"; |
final String cancelledString = "Aflyst"; |
143 |
for (int i=0;i<timetableList.size(); i++) { //handle cancelled labels |
for (int i=0;i<timetableBean.entries.size(); i++) { //handle cancelled labels |
144 |
final int lastIdx = (timetableList.size() - 1); |
final int lastIdx = (timetableBean.entries.size() - 1); |
145 |
|
|
146 |
TimetableBean current = timetableList.get(i); |
TimetableEntry current = timetableBean.entries.get(i); |
147 |
if (current.isCancelled()) { |
if (current.isCancelled()) { |
148 |
if (i == 0) { |
if (i == 0) { |
149 |
current.setDeparture(cancelledString); |
current.setDeparture(cancelledString); |
150 |
} else if (i == lastIdx) { |
} else if (i == lastIdx) { |
151 |
current.setArrival(cancelledString); |
current.setArrival(cancelledString); |
152 |
} else if (i>0 && i<lastIdx) { |
} else if (i>0 && i<lastIdx) { |
153 |
TimetableBean next = timetableList.get(i+1); |
TimetableEntry next = timetableBean.entries.get(i+1); |
154 |
TimetableBean prev = timetableList.get(i-1); |
TimetableEntry prev = timetableBean.entries.get(i-1); |
155 |
|
|
156 |
if (next.isCancelled()) |
if (next.isCancelled()) |
157 |
current.setDeparture(cancelledString); |
current.setDeparture(cancelledString); |
166 |
} |
} |
167 |
|
|
168 |
|
|
169 |
return timetableList; |
return timetableBean; |
170 |
} |
} |
171 |
|
|
172 |
List<TimetableBean> lookupTimetableWwwSite(String trainID, String type) throws Exception { |
TimetableBean lookupTimetableWwwSite(String trainID, String type) throws Exception { |
173 |
List<TimetableBean> timetableList = new ArrayList<TimetableBean>(); |
TimetableBean timetableBean = new TimetableBean(); |
174 |
|
|
175 |
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"; |
176 |
logger.fine("URL:" + url); |
logger.fine("URL:" + url); |
205 |
continue; |
continue; |
206 |
} |
} |
207 |
|
|
208 |
TimetableBean bean = new TimetableBean(); |
TimetableEntry entry = new TimetableEntry(); |
209 |
|
|
210 |
String station = DepartureFetcher.cleanText( fields.get(0).text() ) ; |
String station = DepartureFetcher.cleanText( fields.get(0).text() ) ; |
211 |
if (station.equals("København")) |
if (station.equals("København")) |
214 |
String arrival = DepartureFetcher.cleanText( fields.get(1).text() ); |
String arrival = DepartureFetcher.cleanText( fields.get(1).text() ); |
215 |
String departure = DepartureFetcher.cleanText( fields.get(2).text() ); |
String departure = DepartureFetcher.cleanText( fields.get(2).text() ); |
216 |
|
|
217 |
bean.setStation( station ); |
entry.setStation( station ); |
218 |
bean.setArrival( arrival ); |
entry.setArrival( arrival ); |
219 |
bean.setDeparture( departure ); |
entry.setDeparture( departure ); |
220 |
|
|
221 |
|
|
222 |
if (currentStation == true && currentStationSaved == false ) { |
if (currentStation == true && currentStationSaved == false ) { |
223 |
bean.setCurrent(currentStation); |
entry.setCurrent(currentStation); |
224 |
currentStationSaved = true; |
currentStationSaved = true; |
225 |
} |
} |
226 |
|
|
227 |
bean.setStationId( getStationId( station )); |
entry.setStationId( getStationId( station )); |
228 |
|
|
229 |
timetableList.add(bean); |
timetableBean.entries.add(entry); |
230 |
} |
} |
231 |
|
|
232 |
} else { |
} else { |
234 |
} |
} |
235 |
|
|
236 |
|
|
237 |
return timetableList; |
return timetableBean; |
238 |
} |
} |
239 |
|
|
240 |
} |
} |