1 |
torben |
350 |
package dk.thoerup.traininfoservice.banedk; |
2 |
|
|
|
3 |
|
|
|
4 |
torben |
992 |
|
5 |
torben |
350 |
import java.net.URL; |
6 |
torben |
836 |
import java.sql.SQLException; |
7 |
torben |
350 |
import java.util.ArrayList; |
8 |
|
|
import java.util.List; |
9 |
torben |
428 |
import java.util.Map; |
10 |
torben |
836 |
import java.util.logging.Level; |
11 |
torben |
350 |
import java.util.logging.Logger; |
12 |
|
|
|
13 |
torben |
992 |
import org.jsoup.nodes.Document; |
14 |
|
|
import org.jsoup.nodes.Element; |
15 |
|
|
import org.jsoup.select.Elements; |
16 |
torben |
350 |
|
17 |
torben |
468 |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
18 |
|
|
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
19 |
torben |
836 |
import dk.thoerup.traininfoservice.StationDAO; |
20 |
torben |
711 |
import dk.thoerup.traininfoservice.Statistics; |
21 |
torben |
421 |
|
22 |
torben |
350 |
public class TimetableFetcher { |
23 |
torben |
992 |
|
24 |
torben |
350 |
|
25 |
torben |
584 |
Map<String, List<TimetableBean>> cache; |
26 |
torben |
836 |
Map<String, Integer> stationCache; |
27 |
|
|
|
28 |
|
|
StationDAO stationDao = new StationDAO(); |
29 |
torben |
350 |
|
30 |
torben |
387 |
|
31 |
torben |
350 |
Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); |
32 |
torben |
387 |
|
33 |
torben |
581 |
private boolean useTempSite; |
34 |
torben |
1026 |
private int replyTimeout; |
35 |
torben |
387 |
|
36 |
torben |
1026 |
public TimetableFetcher(boolean tmpSite, int cacheTimeout, int replyTimeout) { |
37 |
torben |
581 |
useTempSite = tmpSite; |
38 |
torben |
1026 |
this.replyTimeout = replyTimeout; |
39 |
torben |
584 |
|
40 |
|
|
cache = new TimeoutMap<String,List<TimetableBean>>(cacheTimeout); |
41 |
torben |
836 |
stationCache = new TimeoutMap<String,Integer>( 3*60*60*1000 ); |
42 |
torben |
581 |
} |
43 |
|
|
|
44 |
|
|
|
45 |
torben |
451 |
List<TimetableBean> cachedLookupTimetable(String trainID, String type) throws Exception { |
46 |
torben |
387 |
String key = trainID+type; |
47 |
torben |
837 |
List<TimetableBean> list = cache.get(key); |
48 |
torben |
387 |
|
49 |
|
|
if (list == null) { |
50 |
|
|
list = lookupTimetable(trainID,type); |
51 |
|
|
cache.put(key, list); |
52 |
|
|
} else { |
53 |
torben |
711 |
Statistics.getInstance().incrementTimetableCacheHits(); |
54 |
torben |
389 |
logger.info("Timetable: Cache hit " + trainID); |
55 |
torben |
387 |
} |
56 |
|
|
return list; |
57 |
|
|
} |
58 |
torben |
581 |
|
59 |
|
|
List<TimetableBean> lookupTimetable(String trainID, String type) throws Exception { |
60 |
|
|
if (useTempSite == false ){ |
61 |
|
|
return lookupTimetableRealSite(trainID, type); |
62 |
|
|
} else { |
63 |
|
|
return new ArrayList<TimetableBean>(); // no timetable data on temp site |
64 |
|
|
} |
65 |
|
|
} |
66 |
torben |
836 |
|
67 |
|
|
int getStationId(String name) { |
68 |
|
|
Integer id = stationCache.get(name); |
69 |
|
|
|
70 |
|
|
if (id == null) { |
71 |
|
|
try { |
72 |
torben |
842 |
id = stationDao.getIdByName(name); |
73 |
torben |
836 |
stationCache.put(name, id); |
74 |
|
|
} catch (SQLException e) { |
75 |
|
|
logger.log(Level.SEVERE, "getStationId failed", e); |
76 |
|
|
id = -1; |
77 |
|
|
} |
78 |
|
|
} |
79 |
torben |
350 |
|
80 |
torben |
836 |
return id; |
81 |
|
|
} |
82 |
|
|
|
83 |
torben |
581 |
List<TimetableBean> lookupTimetableRealSite(String trainID, String type) throws Exception { |
84 |
torben |
350 |
List<TimetableBean> timetableList = new ArrayList<TimetableBean>(); |
85 |
|
|
|
86 |
torben |
970 |
//String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276"; |
87 |
|
|
String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID; |
88 |
torben |
350 |
|
89 |
|
|
|
90 |
torben |
1026 |
JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout); |
91 |
torben |
421 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
92 |
torben |
350 |
|
93 |
torben |
992 |
Document doc = (Document) breaker.invoke(wrapper); |
94 |
torben |
421 |
|
95 |
torben |
350 |
|
96 |
|
|
boolean currentStation = false; |
97 |
|
|
boolean currentStationSaved = false; |
98 |
|
|
|
99 |
torben |
992 |
Elements tables = doc.getElementsByClass("Rute"); |
100 |
|
|
|
101 |
torben |
350 |
if (tables.size() == 1) { |
102 |
torben |
992 |
Element timetable = tables.get(0); |
103 |
|
|
Elements rows = timetable.getElementsByTag("tr"); |
104 |
torben |
350 |
|
105 |
|
|
for (int i=0; i<rows.size(); i++) { |
106 |
|
|
if (i==0) //First row is column headers |
107 |
|
|
continue; |
108 |
|
|
|
109 |
|
|
|
110 |
torben |
992 |
Element row = rows.get(i); |
111 |
|
|
Elements fields = row.getElementsByTag("td"); |
112 |
|
|
|
113 |
torben |
350 |
|
114 |
torben |
992 |
if (currentStationSaved == false && fields.get(0).attr("class").equalsIgnoreCase("Tidsstreg")) { |
115 |
torben |
350 |
currentStation = true; |
116 |
|
|
continue; |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
TimetableBean bean = new TimetableBean(); |
120 |
torben |
836 |
|
121 |
torben |
992 |
String station = fields.get(0).text() ; |
122 |
torben |
836 |
if (station.equals("København")) |
123 |
|
|
station = "København H"; //correct inconsistency in naming |
124 |
|
|
|
125 |
|
|
bean.setStation( station ); |
126 |
torben |
992 |
bean.setArrival( fields.get(1).text() ); |
127 |
|
|
bean.setDeparture( fields.get(2).text() ); |
128 |
torben |
350 |
|
129 |
torben |
992 |
boolean cancelled = fields.get(3).text().equalsIgnoreCase("aflyst"); |
130 |
torben |
975 |
bean.setCancelled(cancelled); |
131 |
|
|
|
132 |
torben |
350 |
if (currentStation == true && currentStationSaved == false ) { |
133 |
|
|
bean.setCurrent(currentStation); |
134 |
|
|
currentStationSaved = true; |
135 |
|
|
} |
136 |
|
|
|
137 |
torben |
836 |
bean.setStationId( getStationId( station )); |
138 |
|
|
|
139 |
torben |
350 |
timetableList.add(bean); |
140 |
|
|
} |
141 |
|
|
|
142 |
torben |
977 |
//TODO: There is an off-by-one error in this cancelled parser thingie |
143 |
torben |
975 |
final String cancelledString = "Aflyst"; |
144 |
|
|
for (int i=0;i<timetableList.size(); i++) { //handle cancelled labels |
145 |
|
|
final int lastIdx = (timetableList.size() - 1); |
146 |
|
|
|
147 |
|
|
TimetableBean current = timetableList.get(i); |
148 |
torben |
976 |
if (current.isCancelled()) { |
149 |
|
|
if (i == 0) { |
150 |
|
|
current.setDeparture(cancelledString); |
151 |
|
|
} else if (i == lastIdx) { |
152 |
|
|
current.setArrival(cancelledString); |
153 |
|
|
} else if (i>0 && i<lastIdx) { |
154 |
|
|
TimetableBean next = timetableList.get(i+1); |
155 |
|
|
TimetableBean prev = timetableList.get(i-1); |
156 |
|
|
|
157 |
|
|
if (next.isCancelled()) |
158 |
|
|
current.setDeparture(cancelledString); |
159 |
|
|
if (prev.isCancelled()) |
160 |
|
|
current.setArrival(cancelledString); |
161 |
|
|
} |
162 |
torben |
975 |
} |
163 |
|
|
} |
164 |
|
|
|
165 |
torben |
350 |
} else { |
166 |
|
|
logger.warning("No time table found, trainID=" + trainID + " type=" + type); |
167 |
|
|
} |
168 |
torben |
992 |
|
169 |
torben |
350 |
|
170 |
|
|
return timetableList; |
171 |
|
|
} |
172 |
|
|
|
173 |
|
|
} |