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 |
1093 |
import java.util.HashMap; |
8 |
torben |
428 |
import java.util.Map; |
9 |
torben |
836 |
import java.util.logging.Level; |
10 |
torben |
350 |
import java.util.logging.Logger; |
11 |
|
|
|
12 |
torben |
1093 |
import net.sf.jsr107cache.Cache; |
13 |
|
|
import net.sf.jsr107cache.CacheException; |
14 |
|
|
import net.sf.jsr107cache.CacheManager; |
15 |
|
|
|
16 |
torben |
992 |
import org.jsoup.nodes.Document; |
17 |
|
|
import org.jsoup.nodes.Element; |
18 |
|
|
import org.jsoup.select.Elements; |
19 |
torben |
350 |
|
20 |
torben |
1093 |
import com.google.appengine.api.memcache.jsr107cache.GCacheFactory; |
21 |
|
|
|
22 |
torben |
1061 |
import dk.thoerup.android.traininfo.common.TimetableBean; |
23 |
|
|
import dk.thoerup.android.traininfo.common.TimetableEntry; |
24 |
torben |
468 |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
25 |
|
|
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
26 |
torben |
836 |
import dk.thoerup.traininfoservice.StationDAO; |
27 |
torben |
711 |
import dk.thoerup.traininfoservice.Statistics; |
28 |
torben |
421 |
|
29 |
torben |
350 |
public class TimetableFetcher { |
30 |
torben |
992 |
|
31 |
torben |
350 |
|
32 |
torben |
1093 |
Cache cache; |
33 |
|
|
Cache stationCache; |
34 |
torben |
836 |
|
35 |
|
|
StationDAO stationDao = new StationDAO(); |
36 |
torben |
350 |
|
37 |
torben |
387 |
|
38 |
torben |
350 |
Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); |
39 |
torben |
387 |
|
40 |
torben |
1036 |
private boolean useAzureSite; |
41 |
torben |
1026 |
private int replyTimeout; |
42 |
torben |
387 |
|
43 |
torben |
1093 |
@SuppressWarnings("unchecked") |
44 |
torben |
1036 |
public TimetableFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) { |
45 |
|
|
useAzureSite = azureSite; |
46 |
torben |
1093 |
this.replyTimeout = replyTimeout; |
47 |
torben |
584 |
|
48 |
torben |
1093 |
Map props = new HashMap(); |
49 |
|
|
props.put(GCacheFactory.EXPIRATION_DELTA_MILLIS, cacheTimeout); |
50 |
|
|
|
51 |
|
|
try { |
52 |
|
|
cache = CacheManager.getInstance().getCacheFactory().createCache(props); |
53 |
|
|
} catch (CacheException e) { |
54 |
|
|
logger.log(Level.WARNING, "error creating cache", e); |
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
props = new HashMap(); |
58 |
|
|
props.put(GCacheFactory.EXPIRATION_DELTA_MILLIS, 3*60*60*1000); |
59 |
|
|
|
60 |
|
|
try { |
61 |
|
|
stationCache = CacheManager.getInstance().getCacheFactory().createCache(props); |
62 |
|
|
} catch (CacheException e) { |
63 |
|
|
logger.log(Level.WARNING, "error creating cache", e); |
64 |
|
|
} |
65 |
torben |
581 |
} |
66 |
|
|
|
67 |
|
|
|
68 |
torben |
1060 |
TimetableBean cachedLookupTimetable(String trainID, String type) throws Exception { |
69 |
torben |
387 |
String key = trainID+type; |
70 |
torben |
1095 |
TimetableBean list = (TimetableBean) cache.get(key); |
71 |
torben |
387 |
|
72 |
|
|
if (list == null) { |
73 |
|
|
list = lookupTimetable(trainID,type); |
74 |
|
|
cache.put(key, list); |
75 |
|
|
} else { |
76 |
torben |
711 |
Statistics.getInstance().incrementTimetableCacheHits(); |
77 |
torben |
389 |
logger.info("Timetable: Cache hit " + trainID); |
78 |
torben |
387 |
} |
79 |
|
|
return list; |
80 |
|
|
} |
81 |
torben |
581 |
|
82 |
torben |
1060 |
TimetableBean lookupTimetable(String trainID, String type) throws Exception { |
83 |
torben |
1036 |
if (useAzureSite == true ){ |
84 |
|
|
return lookupTimetableAzureSite(trainID, type); |
85 |
|
|
|
86 |
torben |
581 |
} else { |
87 |
torben |
1036 |
return lookupTimetableWwwSite(trainID, type); |
88 |
torben |
581 |
} |
89 |
|
|
} |
90 |
torben |
836 |
|
91 |
|
|
int getStationId(String name) { |
92 |
torben |
1095 |
Integer id = (Integer) stationCache.get(name); |
93 |
torben |
836 |
|
94 |
|
|
if (id == null) { |
95 |
torben |
1105 |
|
96 |
|
|
id = stationDao.getIdByName(name); |
97 |
|
|
stationCache.put(name, id); |
98 |
torben |
836 |
} |
99 |
torben |
350 |
|
100 |
torben |
836 |
return id; |
101 |
|
|
} |
102 |
|
|
|
103 |
torben |
1060 |
TimetableBean lookupTimetableAzureSite(String trainID, String type) throws Exception { |
104 |
|
|
TimetableBean timetableBean = new TimetableBean(); |
105 |
torben |
350 |
|
106 |
torben |
1048 |
|
107 |
torben |
970 |
String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID; |
108 |
torben |
1048 |
logger.fine("URL:" + url); |
109 |
torben |
350 |
|
110 |
torben |
1026 |
JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout); |
111 |
torben |
421 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
112 |
torben |
350 |
|
113 |
torben |
992 |
Document doc = (Document) breaker.invoke(wrapper); |
114 |
torben |
421 |
|
115 |
torben |
350 |
|
116 |
|
|
boolean currentStation = false; |
117 |
|
|
boolean currentStationSaved = false; |
118 |
|
|
|
119 |
torben |
992 |
Elements tables = doc.getElementsByClass("Rute"); |
120 |
|
|
|
121 |
torben |
350 |
if (tables.size() == 1) { |
122 |
torben |
992 |
Element timetable = tables.get(0); |
123 |
|
|
Elements rows = timetable.getElementsByTag("tr"); |
124 |
torben |
350 |
|
125 |
|
|
for (int i=0; i<rows.size(); i++) { |
126 |
|
|
if (i==0) //First row is column headers |
127 |
|
|
continue; |
128 |
|
|
|
129 |
|
|
|
130 |
torben |
992 |
Element row = rows.get(i); |
131 |
|
|
Elements fields = row.getElementsByTag("td"); |
132 |
|
|
|
133 |
torben |
350 |
|
134 |
torben |
992 |
if (currentStationSaved == false && fields.get(0).attr("class").equalsIgnoreCase("Tidsstreg")) { |
135 |
torben |
350 |
currentStation = true; |
136 |
|
|
continue; |
137 |
|
|
} |
138 |
|
|
|
139 |
torben |
1060 |
TimetableEntry entry = new TimetableEntry(); |
140 |
torben |
836 |
|
141 |
torben |
992 |
String station = fields.get(0).text() ; |
142 |
torben |
836 |
if (station.equals("København")) |
143 |
|
|
station = "København H"; //correct inconsistency in naming |
144 |
|
|
|
145 |
torben |
1060 |
entry.setStation( station ); |
146 |
|
|
entry.setArrival( fields.get(1).text() ); |
147 |
|
|
entry.setDeparture( fields.get(2).text() ); |
148 |
torben |
350 |
|
149 |
torben |
992 |
boolean cancelled = fields.get(3).text().equalsIgnoreCase("aflyst"); |
150 |
torben |
1060 |
entry.setCancelled(cancelled); |
151 |
torben |
975 |
|
152 |
torben |
350 |
if (currentStation == true && currentStationSaved == false ) { |
153 |
torben |
1060 |
entry.setCurrent(currentStation); |
154 |
torben |
350 |
currentStationSaved = true; |
155 |
|
|
} |
156 |
|
|
|
157 |
torben |
1415 |
//TODO: fix this to new setStationEntry |
158 |
|
|
//entry.setStationId( getStationId( station )); |
159 |
torben |
836 |
|
160 |
torben |
1060 |
timetableBean.entries.add(entry); |
161 |
torben |
350 |
} |
162 |
|
|
|
163 |
torben |
977 |
//TODO: There is an off-by-one error in this cancelled parser thingie |
164 |
torben |
975 |
final String cancelledString = "Aflyst"; |
165 |
torben |
1060 |
for (int i=0;i<timetableBean.entries.size(); i++) { //handle cancelled labels |
166 |
|
|
final int lastIdx = (timetableBean.entries.size() - 1); |
167 |
torben |
975 |
|
168 |
torben |
1060 |
TimetableEntry current = timetableBean.entries.get(i); |
169 |
torben |
976 |
if (current.isCancelled()) { |
170 |
|
|
if (i == 0) { |
171 |
|
|
current.setDeparture(cancelledString); |
172 |
|
|
} else if (i == lastIdx) { |
173 |
|
|
current.setArrival(cancelledString); |
174 |
|
|
} else if (i>0 && i<lastIdx) { |
175 |
torben |
1060 |
TimetableEntry next = timetableBean.entries.get(i+1); |
176 |
|
|
TimetableEntry prev = timetableBean.entries.get(i-1); |
177 |
torben |
976 |
|
178 |
|
|
if (next.isCancelled()) |
179 |
|
|
current.setDeparture(cancelledString); |
180 |
|
|
if (prev.isCancelled()) |
181 |
|
|
current.setArrival(cancelledString); |
182 |
|
|
} |
183 |
torben |
975 |
} |
184 |
|
|
} |
185 |
|
|
|
186 |
torben |
350 |
} else { |
187 |
|
|
logger.warning("No time table found, trainID=" + trainID + " type=" + type); |
188 |
|
|
} |
189 |
torben |
992 |
|
190 |
torben |
350 |
|
191 |
torben |
1060 |
return timetableBean; |
192 |
torben |
350 |
} |
193 |
torben |
1036 |
|
194 |
torben |
1060 |
TimetableBean lookupTimetableWwwSite(String trainID, String type) throws Exception { |
195 |
|
|
TimetableBean timetableBean = new TimetableBean(); |
196 |
torben |
1036 |
|
197 |
|
|
String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276"; |
198 |
torben |
1048 |
logger.fine("URL:" + url); |
199 |
torben |
1036 |
|
200 |
|
|
|
201 |
|
|
JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout); |
202 |
|
|
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
203 |
|
|
|
204 |
|
|
Document doc = (Document) breaker.invoke(wrapper); |
205 |
|
|
|
206 |
|
|
|
207 |
|
|
boolean currentStation = false; |
208 |
|
|
boolean currentStationSaved = false; |
209 |
|
|
|
210 |
|
|
Elements tables = doc.getElementsByClass("Rute"); |
211 |
|
|
|
212 |
|
|
if (tables.size() == 1) { |
213 |
|
|
Element timetable = tables.get(0); |
214 |
|
|
Elements rows = timetable.getElementsByTag("tr"); |
215 |
|
|
|
216 |
|
|
for (int i=0; i<rows.size(); i++) { |
217 |
|
|
if (i==0) //First row is column headers |
218 |
|
|
continue; |
219 |
|
|
|
220 |
|
|
|
221 |
|
|
Element row = rows.get(i); |
222 |
|
|
Elements fields = row.getElementsByTag("td"); |
223 |
|
|
|
224 |
|
|
|
225 |
|
|
if (currentStationSaved == false && fields.get(0).attr("class").equalsIgnoreCase("Tidsstreg")) { |
226 |
|
|
currentStation = true; |
227 |
|
|
continue; |
228 |
|
|
} |
229 |
|
|
|
230 |
torben |
1060 |
TimetableEntry entry = new TimetableEntry(); |
231 |
torben |
1036 |
|
232 |
torben |
1040 |
String station = DepartureFetcher.cleanText( fields.get(0).text() ) ; |
233 |
torben |
1036 |
if (station.equals("København")) |
234 |
|
|
station = "København H"; //correct inconsistency in naming |
235 |
|
|
|
236 |
torben |
1040 |
String arrival = DepartureFetcher.cleanText( fields.get(1).text() ); |
237 |
|
|
String departure = DepartureFetcher.cleanText( fields.get(2).text() ); |
238 |
|
|
|
239 |
torben |
1060 |
entry.setStation( station ); |
240 |
|
|
entry.setArrival( arrival ); |
241 |
|
|
entry.setDeparture( departure ); |
242 |
torben |
1036 |
|
243 |
|
|
|
244 |
|
|
if (currentStation == true && currentStationSaved == false ) { |
245 |
torben |
1060 |
entry.setCurrent(currentStation); |
246 |
torben |
1036 |
currentStationSaved = true; |
247 |
|
|
} |
248 |
|
|
|
249 |
torben |
1415 |
//TODO: fix this to new setStationEntry |
250 |
|
|
//entry.setStationId( getStationId( station )); |
251 |
|
|
|
252 |
torben |
1036 |
|
253 |
torben |
1060 |
timetableBean.entries.add(entry); |
254 |
torben |
1036 |
} |
255 |
|
|
|
256 |
|
|
} else { |
257 |
|
|
logger.warning("No time table found, trainID=" + trainID + " type=" + type); |
258 |
|
|
} |
259 |
|
|
|
260 |
|
|
|
261 |
torben |
1060 |
return timetableBean; |
262 |
torben |
1036 |
} |
263 |
torben |
350 |
|
264 |
|
|
} |