1 |
package dk.thoerup.traininfoservice.banedk; |
package dk.thoerup.traininfoservice.banedk; |
2 |
|
|
|
import java.sql.Connection; |
|
|
import java.sql.ResultSet; |
|
|
import java.sql.Statement; |
|
3 |
import java.util.ArrayList; |
import java.util.ArrayList; |
4 |
import java.util.Collections; |
import java.util.Collections; |
5 |
import java.util.List; |
import java.util.List; |
6 |
import java.util.Map; |
import java.util.Map; |
7 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
8 |
|
|
9 |
|
import com.gargoylesoftware.htmlunit.BrowserVersion; |
10 |
import com.gargoylesoftware.htmlunit.WebClient; |
import com.gargoylesoftware.htmlunit.WebClient; |
11 |
import com.gargoylesoftware.htmlunit.html.DomNodeList; |
import com.gargoylesoftware.htmlunit.html.DomNodeList; |
12 |
import com.gargoylesoftware.htmlunit.html.HtmlElement; |
import com.gargoylesoftware.htmlunit.html.HtmlElement; |
14 |
|
|
15 |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
16 |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
17 |
import dk.thoerup.traininfoservice.DBConnection; |
import dk.thoerup.traininfoservice.StationBean; |
18 |
|
import dk.thoerup.traininfoservice.StationDAO; |
19 |
|
import dk.thoerup.traininfoservice.Statistics; |
20 |
|
|
21 |
public class DepartureFetcher { |
public class DepartureFetcher { |
22 |
|
|
23 |
Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); |
Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); |
24 |
|
|
25 |
Map<Integer, List<DepartureBean>> cache = new TimeoutMap<Integer,List<DepartureBean>>(120 * 1000); |
Map<String, List<DepartureBean>> cache; |
26 |
|
|
27 |
|
StationDAO stationDao = new StationDAO(); |
28 |
|
|
29 |
private boolean useTempSite; |
private boolean useTempSite; |
30 |
|
|
31 |
public DepartureFetcher(boolean tempSite) { |
public DepartureFetcher(boolean tempSite, int cacheTimeout) { |
32 |
useTempSite = tempSite; |
useTempSite = tempSite; |
33 |
|
cache = new TimeoutMap<String,List<DepartureBean>>(cacheTimeout); |
34 |
} |
} |
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
public List<DepartureBean> cachedLookupDepartures(int stationID) throws Exception { |
public List<DepartureBean> cachedLookupDepartures(int stationID, boolean arrival) throws Exception { |
40 |
|
final String key = "" + stationID + ":" + arrival; |
41 |
|
|
42 |
|
List<DepartureBean> list = cache.get(key); |
43 |
|
|
|
List<DepartureBean> list = cache.get(stationID); |
|
44 |
|
|
45 |
if (list == null) { |
if (list == null) { |
46 |
list = lookupDepartures(stationID); |
list = lookupDepartures(stationID,arrival); |
47 |
cache.put(stationID, list); |
cache.put(key, list); |
48 |
} else { |
} else { |
49 |
logger.info("Departure: Cache hit " + stationID); //remove before production |
Statistics.getInstance().incrementDepartureCacheHits(); |
50 |
|
logger.info("Departure: Cache hit " + key); //remove before production |
51 |
} |
} |
52 |
return list; |
return list; |
53 |
} |
} |
54 |
|
|
55 |
|
|
56 |
public List<DepartureBean> lookupDepartures(int stationID) throws Exception { |
public List<DepartureBean> lookupDepartures(int stationID, boolean arrival) throws Exception { |
57 |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
58 |
|
|
59 |
Connection conn = null; |
StationBean station = stationDao.getById(stationID); |
60 |
try |
|
61 |
{ |
if (station.getRegional() != null) { |
62 |
conn = DBConnection.getConnection(); |
List<DepartureBean> list = lookupDepartures(station.getRegional(), "FJRN", arrival); |
63 |
|
departureList.addAll(list); |
|
String SQL = "SELECT stationcode_fjrn, stationcode_stog FROM trainstations WHERE id=" + stationID; |
|
|
Statement stmt = conn.createStatement(); |
|
|
ResultSet rs = stmt.executeQuery(SQL); |
|
|
|
|
|
if (rs.next()) { |
|
|
String code = rs.getString( 1 ); |
|
|
if (! rs.wasNull() ) { |
|
|
List<DepartureBean> list = lookupDepartures(code, "FJRN"); |
|
|
departureList.addAll(list); |
|
|
} |
|
|
|
|
|
code = rs.getString(2); |
|
|
if (! rs.wasNull() ) { |
|
|
List<DepartureBean> list = lookupDepartures(code, "S2"); |
|
|
departureList.addAll(list); |
|
|
} |
|
|
Collections.sort( departureList ); |
|
|
|
|
|
} |
|
|
|
|
|
} finally { |
|
|
if (conn != null && !conn.isClosed() ) { |
|
|
conn.close(); |
|
|
} |
|
64 |
} |
} |
65 |
|
|
66 |
|
if (station.getStrain() != null) { |
67 |
|
List<DepartureBean> list = lookupDepartures(station.getStrain(), "S2", arrival); |
68 |
|
departureList.addAll(list); |
69 |
|
} |
70 |
|
|
71 |
|
Collections.sort( departureList ); |
72 |
|
|
73 |
|
|
74 |
return departureList; |
return departureList; |
75 |
} |
} |
76 |
|
|
77 |
public List<DepartureBean> lookupDepartures(String stationcode, String type) throws Exception { |
public List<DepartureBean> lookupDepartures(String stationcode, String type, boolean arrival) throws Exception { |
78 |
if (useTempSite == false) { |
if (useTempSite == false) { |
79 |
return lookupDeparturesNormalSite(stationcode, type); |
return lookupDeparturesNormalSite(stationcode, type, arrival); |
80 |
} else { |
} else { |
81 |
return lookupDeparturesFromTemporarySite(stationcode, type); |
return lookupDeparturesFromTemporarySite(stationcode, type); |
82 |
} |
} |
83 |
} |
} |
84 |
|
|
85 |
public List<DepartureBean> lookupDeparturesNormalSite(String stationcode, String type) throws Exception { |
public List<DepartureBean> lookupDeparturesNormalSite(String stationcode, String type, boolean arrival) throws Exception { |
86 |
|
|
87 |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
88 |
|
|
89 |
final WebClient webClient = new WebClient(); |
final WebClient webClient = new WebClient( BrowserVersion.FIREFOX_3 ); |
90 |
webClient.setTimeout(2500); |
webClient.setTimeout(2500); |
91 |
webClient.setJavaScriptEnabled(false); |
webClient.setJavaScriptEnabled(false); |
92 |
|
|
96 |
|
|
97 |
HtmlPage page = (HtmlPage) breaker.invoke(wrapper); |
HtmlPage page = (HtmlPage) breaker.invoke(wrapper); |
98 |
|
|
99 |
HtmlElement table = page.getElementById("afgangtabel"); |
String tableName = arrival == false ? "afgangtabel" : "ankomsttabel"; |
100 |
|
HtmlElement table = page.getElementById(tableName); |
101 |
|
|
102 |
if (table != null) { |
if (table != null) { |
103 |
DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr"); |
DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr"); |
137 |
String note = extractNote( fields.get(7) ); |
String note = extractNote( fields.get(7) ); |
138 |
departure.setNote(note); |
departure.setNote(note); |
139 |
|
|
140 |
|
departure.setType(type); |
141 |
|
|
142 |
departureList.add(departure); |
departureList.add(departure); |
143 |
} |
} |
144 |
} |
} |
145 |
} else { |
} else { |
146 |
logger.warning("No departures found for station=" + stationcode + ", type=" + type); |
logger.warning("No departures found for station=" + stationcode + ", type=" + type); |
147 |
} |
} |
148 |
|
webClient.closeAllWindows(); |
149 |
|
|
150 |
return departureList; |
return departureList; |
151 |
} |
} |
154 |
|
|
155 |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
156 |
|
|
157 |
final WebClient webClient = new WebClient(); |
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3); |
158 |
webClient.setTimeout(2500); |
webClient.setTimeout(2500); |
159 |
webClient.setJavaScriptEnabled(false); |
webClient.setJavaScriptEnabled(false); |
160 |
|
|
210 |
} else { |
} else { |
211 |
logger.warning("No departures found for station=" + stationcode + ", type=" + type); |
logger.warning("No departures found for station=" + stationcode + ", type=" + type); |
212 |
} |
} |
213 |
|
webClient.closeAllWindows(); |
214 |
|
|
215 |
|
|
216 |
return departureList; |
return departureList; |
217 |
} |
} |
260 |
} |
} |
261 |
|
|
262 |
|
|
|
|
|
263 |
return number; |
return number; |
264 |
} |
} |
265 |
|
|