/[projects]/android/TrainInfoServiceGoogle/src/dk/thoerup/traininfoservice/banedk/DepartureFetcher.java
ViewVC logotype

Annotation of /android/TrainInfoServiceGoogle/src/dk/thoerup/traininfoservice/banedk/DepartureFetcher.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 584 - (hide annotations) (download)
Fri Feb 5 13:57:39 2010 UTC (14 years, 3 months ago) by torben
Original Path: android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureFetcher.java
File size: 9053 byte(s)
Move some various parameters to web.xml
1 torben 305 package dk.thoerup.traininfoservice.banedk;
2    
3 torben 307 import java.sql.Connection;
4     import java.sql.ResultSet;
5     import java.sql.Statement;
6 torben 305 import java.util.ArrayList;
7 torben 307 import java.util.Collections;
8 torben 305 import java.util.List;
9 torben 428 import java.util.Map;
10 torben 348 import java.util.logging.Logger;
11 torben 305
12     import com.gargoylesoftware.htmlunit.WebClient;
13     import com.gargoylesoftware.htmlunit.html.DomNodeList;
14     import com.gargoylesoftware.htmlunit.html.HtmlElement;
15     import com.gargoylesoftware.htmlunit.html.HtmlPage;
16    
17 torben 468 import dk.thoerup.circuitbreaker.CircuitBreaker;
18     import dk.thoerup.circuitbreaker.CircuitBreakerManager;
19 torben 307 import dk.thoerup.traininfoservice.DBConnection;
20    
21 torben 305 public class DepartureFetcher {
22 torben 348
23     Logger logger = Logger.getLogger(DepartureFetcher.class.getName());
24 torben 387
25 torben 584 Map<Integer, List<DepartureBean>> cache;
26 torben 387
27 torben 580 private boolean useTempSite;
28    
29 torben 584 public DepartureFetcher(boolean tempSite, int cacheTimeout) {
30 torben 580 useTempSite = tempSite;
31 torben 584 cache = new TimeoutMap<Integer,List<DepartureBean>>(cacheTimeout);
32 torben 580 }
33    
34    
35 torben 307
36 torben 387
37 torben 451 public List<DepartureBean> cachedLookupDepartures(int stationID) throws Exception {
38 torben 308
39 torben 387 List<DepartureBean> list = cache.get(stationID);
40    
41     if (list == null) {
42     list = lookupDepartures(stationID);
43     cache.put(stationID, list);
44     } else {
45 torben 389 logger.info("Departure: Cache hit " + stationID); //remove before production
46 torben 387 }
47     return list;
48     }
49    
50    
51 torben 451 public List<DepartureBean> lookupDepartures(int stationID) throws Exception {
52 torben 307 List<DepartureBean> departureList = new ArrayList<DepartureBean>();
53    
54     Connection conn = null;
55     try
56     {
57     conn = DBConnection.getConnection();
58    
59     String SQL = "SELECT stationcode_fjrn, stationcode_stog FROM trainstations WHERE id=" + stationID;
60     Statement stmt = conn.createStatement();
61     ResultSet rs = stmt.executeQuery(SQL);
62    
63     if (rs.next()) {
64     String code = rs.getString( 1 );
65     if (! rs.wasNull() ) {
66     List<DepartureBean> list = lookupDepartures(code, "FJRN");
67     departureList.addAll(list);
68     }
69    
70     code = rs.getString(2);
71     if (! rs.wasNull() ) {
72     List<DepartureBean> list = lookupDepartures(code, "S2");
73     departureList.addAll(list);
74     }
75     Collections.sort( departureList );
76    
77     }
78    
79     } finally {
80     if (conn != null && !conn.isClosed() ) {
81     conn.close();
82     }
83     }
84    
85     return departureList;
86 torben 305 }
87    
88 torben 451 public List<DepartureBean> lookupDepartures(String stationcode, String type) throws Exception {
89 torben 580 if (useTempSite == false) {
90     return lookupDeparturesNormalSite(stationcode, type);
91     } else {
92     return lookupDeparturesFromTemporarySite(stationcode, type);
93     }
94     }
95    
96     public List<DepartureBean> lookupDeparturesNormalSite(String stationcode, String type) throws Exception {
97 torben 305
98     List<DepartureBean> departureList = new ArrayList<DepartureBean>();
99    
100     final WebClient webClient = new WebClient();
101 torben 386 webClient.setTimeout(2500);
102 torben 313 webClient.setJavaScriptEnabled(false);
103    
104 torben 421 String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode;
105     BanedkInvocation wrapper = new BanedkInvocation(webClient, uri);
106     CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
107 torben 305
108 torben 421 HtmlPage page = (HtmlPage) breaker.invoke(wrapper);
109 torben 305
110     HtmlElement table = page.getElementById("afgangtabel");
111    
112 torben 342 if (table != null) {
113     DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr");
114    
115     for (HtmlElement currentRow : tableRows) {
116     String rowClass = currentRow.getAttribute("class");
117     if (rowClass != null && rowClass.toLowerCase().contains("station") ) {
118     DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td");
119    
120     DepartureBean departure = new DepartureBean();
121    
122     String time = fields.get(0).asText();
123 torben 375 if (time.equals(""))
124     time = "0:00"; //Bane.dk bug work-around
125 torben 342 departure.setTime(time);
126    
127     int updated = extractUpdated( fields.get(1) );
128     departure.setUpdated(updated);
129    
130     String trainNumber = fields.get(2).asText();
131 torben 410 if (type.equalsIgnoreCase("S2")) //If it is S-train we need to extract the trainNumber
132 torben 349 trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2));
133 torben 342 departure.setTrainNumber(trainNumber);
134    
135     String destination = fields.get(3).asText();
136     departure.setDestination(destination);
137    
138     String origin = fields.get(4).asText();
139     departure.setOrigin(origin);
140    
141     String location = fields.get(5).asText();
142     departure.setLocation(location);
143    
144     String status = fields.get(6).asText();
145     departure.setStatus(status);
146    
147     String note = extractNote( fields.get(7) );
148     departure.setNote(note);
149    
150     departureList.add(departure);
151     }
152     }
153 torben 348 } else {
154     logger.warning("No departures found for station=" + stationcode + ", type=" + type);
155 torben 305 }
156 torben 348
157 torben 305 return departureList;
158     }
159    
160 torben 580 public List<DepartureBean> lookupDeparturesFromTemporarySite(String stationcode, String type) throws Exception {
161    
162     List<DepartureBean> departureList = new ArrayList<DepartureBean>();
163    
164     final WebClient webClient = new WebClient();
165     webClient.setTimeout(2500);
166     webClient.setJavaScriptEnabled(false);
167    
168    
169     String uri = "http://bane.dk/lite/station.asp?w=" + type + "&s=" + stationcode;
170    
171     BanedkInvocation wrapper = new BanedkInvocation(webClient, uri);
172     CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
173    
174     HtmlPage page = (HtmlPage) breaker.invoke(wrapper);
175    
176     HtmlElement table = page.getElementById("traf_afgang");
177    
178     if (table != null) {
179     DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr");
180    
181     boolean isFirst = true;
182    
183     for (HtmlElement currentRow : tableRows) {
184     if (isFirst == true) { //skip table headers
185     isFirst = false;
186     continue;
187     }
188    
189     DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td");
190    
191     DepartureBean departure = new DepartureBean();
192    
193     String time = fields.get(0).asText().trim();
194 torben 583
195 torben 580 if (time.equals(""))
196     time = "0:00"; //Bane.dk bug work-around
197     departure.setTime(time);
198    
199    
200     String trainNumber = fields.get(1).asText();
201     departure.setTrainNumber(trainNumber);
202    
203     String destination = fields.get(2).asText();
204     departure.setDestination(destination);
205    
206     String origin = fields.get(3).asText();
207     departure.setOrigin(origin);
208    
209     String status = fields.get(4).asText();
210     departure.setStatus(status);
211    
212 torben 582 String note = fields.get(5).asText();
213 torben 580 departure.setNote(note);
214    
215     departureList.add(departure);
216     }
217     } else {
218     logger.warning("No departures found for station=" + stationcode + ", type=" + type);
219     }
220    
221     return departureList;
222     }
223    
224    
225 torben 305 private int extractUpdated(HtmlElement updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif"
226     int updated = -1;
227    
228     DomNodeList<HtmlElement> updatedImgs = updatedTd.getElementsByTagName("img");
229     String updatedStr = updatedImgs.get(0).getAttribute("src");
230    
231     if (updatedStr != null) {
232     for (int i=0; i<updatedStr.length(); i++) {
233     char c = updatedStr.charAt(i);
234     if ( Character.isDigit(c)) {
235     updated = Character.digit(c, 10);
236     break;
237     }
238     }
239     }
240     return updated;
241     }
242    
243 torben 313 private String extractNote(HtmlElement noteTd) {
244     String note = noteTd.asText().trim();
245    
246     List<HtmlElement> elems = noteTd.getElementsByAttribute("span", "class", "bemtype");
247     if (elems.size() > 0 && note.charAt(note.length()-1) == 'i')
248     note = note.substring(0,note.length() -1 );
249    
250     return note;
251     }
252    
253 torben 349 private String extractTrainNumber(HtmlElement trainTd) {
254     String number = "";
255     HtmlElement anchorElement = trainTd.getElementsByTagName("a").get(0);
256     String href = anchorElement.getAttribute("href");
257     String argstring = href.substring( href.indexOf('?') + 1);
258    
259     String args[] = argstring.split("&");
260     for (String arg : args) {
261     String pair[] = arg.split("="); // Key=pair[0], Value=pair[1]
262    
263     if (pair[0].equalsIgnoreCase("TogNr"))
264     number = pair[1];
265     }
266    
267    
268    
269     return number;
270     }
271    
272 torben 305 //test
273 torben 580 /*
274 torben 451 public static void main(String args[]) throws Exception {
275 torben 305 DepartureFetcher f = new DepartureFetcher();
276 torben 307 List<DepartureBean> deps = f.lookupDepartures("AR", "FJRN");
277 torben 305 for(DepartureBean d : deps) {
278     System.out.println( d.getTime() + ";" + d.getUpdated() + ";" + d.getTrainNumber() + ";" +
279     d.getDestination() + ";" + d.getOrigin() + ";" + d.getLocation() + ";" + d.getStatus() + ";" + d.getNote() );
280     }
281    
282     System.out.println("--------------------------");
283 torben 580 }*/
284 torben 305 }

  ViewVC Help
Powered by ViewVC 1.1.20