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