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; |
13 |
|
|
14 |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
15 |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
16 |
import dk.thoerup.traininfoservice.DBConnection; |
import dk.thoerup.traininfoservice.StationBean; |
17 |
|
import dk.thoerup.traininfoservice.StationDAO; |
18 |
|
|
19 |
public class DepartureFetcher { |
public class DepartureFetcher { |
20 |
|
|
21 |
Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); |
Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); |
22 |
|
|
23 |
Map<Integer, List<DepartureBean>> cache = new TimeoutMap<Integer,List<DepartureBean>>(120 * 1000); |
Map<Integer, List<DepartureBean>> cache; |
24 |
|
|
25 |
|
StationDAO stationDao = new StationDAO(); |
26 |
|
|
27 |
private boolean useTempSite; |
private boolean useTempSite; |
28 |
|
|
29 |
public DepartureFetcher(boolean tempSite) { |
public DepartureFetcher(boolean tempSite, int cacheTimeout) { |
30 |
useTempSite = tempSite; |
useTempSite = tempSite; |
31 |
|
cache = new TimeoutMap<Integer,List<DepartureBean>>(cacheTimeout); |
32 |
} |
} |
33 |
|
|
34 |
|
|
51 |
public List<DepartureBean> lookupDepartures(int stationID) throws Exception { |
public List<DepartureBean> lookupDepartures(int stationID) throws Exception { |
52 |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
53 |
|
|
54 |
Connection conn = null; |
StationBean station = stationDao.getById(stationID); |
55 |
try |
|
56 |
{ |
if (station.getRegional() != null) { |
57 |
conn = DBConnection.getConnection(); |
List<DepartureBean> list = lookupDepartures(station.getRegional(), "FJRN"); |
58 |
|
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(); |
|
|
} |
|
59 |
} |
} |
60 |
|
|
61 |
|
if (station.getStrain() != null) { |
62 |
|
List<DepartureBean> list = lookupDepartures(station.getStrain(), "S2"); |
63 |
|
departureList.addAll(list); |
64 |
|
} |
65 |
|
|
66 |
|
Collections.sort( departureList ); |
67 |
|
|
68 |
|
|
69 |
return departureList; |
return departureList; |
70 |
} |
} |
71 |
|
|
175 |
DepartureBean departure = new DepartureBean(); |
DepartureBean departure = new DepartureBean(); |
176 |
|
|
177 |
String time = fields.get(0).asText().trim(); |
String time = fields.get(0).asText().trim(); |
178 |
logger.info("time:" + time); |
|
179 |
if (time.equals("")) |
if (time.equals("")) |
180 |
time = "0:00"; //Bane.dk bug work-around |
time = "0:00"; //Bane.dk bug work-around |
181 |
departure.setTime(time); |
departure.setTime(time); |
182 |
|
|
183 |
|
|
184 |
String trainNumber = fields.get(1).asText(); |
String trainNumber = fields.get(1).asText(); |
|
if (type.equalsIgnoreCase("S2")) //If it is S-train we need to extract the trainNumber |
|
|
trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2)); |
|
185 |
departure.setTrainNumber(trainNumber); |
departure.setTrainNumber(trainNumber); |
186 |
|
|
187 |
String destination = fields.get(2).asText(); |
String destination = fields.get(2).asText(); |
193 |
String status = fields.get(4).asText(); |
String status = fields.get(4).asText(); |
194 |
departure.setStatus(status); |
departure.setStatus(status); |
195 |
|
|
196 |
String note = extractNote( fields.get(5) ); |
String note = fields.get(5).asText(); |
197 |
departure.setNote(note); |
departure.setNote(note); |
198 |
|
|
199 |
departureList.add(departure); |
departureList.add(departure); |