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

Diff of /android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureFetcher.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 939 by torben, Mon Jun 28 12:35:35 2010 UTC revision 980 by torben, Sat Jul 10 11:01:07 2010 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfoservice.banedk;  package dk.thoerup.traininfoservice.banedk;
2    
3  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;
# Line 20  import dk.thoerup.traininfoservice.Stati Line 20  import dk.thoerup.traininfoservice.Stati
20    
21  public class DepartureFetcher {  public class DepartureFetcher {
22                    
23            enum TrainType{
24                    STOG,
25                    REGIONAL
26            }
27            
28          Logger logger = Logger.getLogger(DepartureFetcher.class.getName());          Logger logger = Logger.getLogger(DepartureFetcher.class.getName());
29                    
30          Map<String, List<DepartureBean>> cache;          Map<String, DepartureBean> cache;
31                    
32          StationDAO stationDao = new StationDAO();          StationDAO stationDao = new StationDAO();
33                    
# Line 30  public class DepartureFetcher { Line 35  public class DepartureFetcher {
35                    
36          public DepartureFetcher(boolean tempSite, int cacheTimeout) {          public DepartureFetcher(boolean tempSite, int cacheTimeout) {
37                  useTempSite = tempSite;                  useTempSite = tempSite;
38                  cache = new TimeoutMap<String,List<DepartureBean>>(cacheTimeout);                  cache = new TimeoutMap<String,DepartureBean>(cacheTimeout);
39          }          }
40                    
41                    
42                                    
43                    
44          public List<DepartureBean> cachedLookupDepartures(int stationID, boolean arrival) throws Exception {          public DepartureBean cachedLookupDepartures(int stationID, boolean arrival) throws Exception {
45                  final String key = "" + stationID + ":" + arrival;                  final String key = "" + stationID + ":" + arrival;
46                                    
47                  List<DepartureBean> list = cache.get(key);                  DepartureBean departureBean = cache.get(key);
48    
49                                    
50                  if (list == null) {                  if (departureBean == null) {
51                          list = lookupDepartures(stationID,arrival);                          departureBean = lookupDepartures(stationID,arrival);
52                          cache.put(key, list);                          cache.put(key, departureBean);
53                  } else {                  } else {
54                          Statistics.getInstance().incrementDepartureCacheHits();                          Statistics.getInstance().incrementDepartureCacheHits();
55                          logger.info("Departure: Cache hit " + key); //remove before production                          logger.info("Departure: Cache hit " + key); //remove before production
56                  }                  }
57                  return list;                  return departureBean;
58          }          }
59                                    
60    
61          public List<DepartureBean> lookupDepartures(int stationID, boolean arrival) throws Exception {          public DepartureBean lookupDepartures(int stationID, boolean arrival) throws Exception {
62                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();                  
63                    DepartureBean departureBean = new DepartureBean();
64                                    
65                  StationBean station = stationDao.getById(stationID);                  StationBean station = stationDao.getById(stationID);
66                                    
67                  if (station.getRegional() != null) {                  if (station.getRegional() != null) {
68                          List<DepartureBean> list = lookupDepartures(station.getRegional(), "FJRN", arrival);                          DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival);
69                          departureList.addAll(list);                                              departureBean.departureEntries.addAll( tempBean.departureEntries );
70                            departureBean.notifications.addAll(tempBean.notifications);
71                  }                  }
72                                    
73                  if (station.getStrain() != null) {                  if (station.getStrain() != null) {
74                          List<DepartureBean> list = lookupDepartures(station.getStrain(), "S2", arrival);                          DepartureBean tempBean = lookupDepartures(station.getStrain(), TrainType.STOG, arrival);
75                          departureList.addAll(list);                              departureBean.departureEntries.addAll( tempBean.departureEntries );
76                            departureBean.notifications.addAll(tempBean.notifications);
77                  }                                }              
78                                    
79                  Collections.sort( departureList );                  Collections.sort( departureBean.departureEntries );
80    
81                                    
82                  return departureList;                  return departureBean;
83          }          }
84                    
85          public List<DepartureBean> lookupDepartures(String stationcode, String type, boolean arrival) throws Exception {          public DepartureBean lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception {
86                  if (useTempSite == false) {                  if (useTempSite == false) {
87                          return lookupDeparturesNormalSite(stationcode, type, arrival);                          return lookupDeparturesNormalSite(stationcode, type, arrival);
88                  } else {                  } else {
89                          return lookupDeparturesFromTemporarySite(stationcode, type);                          //return lookupDeparturesFromTemporarySite(stationcode, type);
90                            //TODO: find out what to to if they ever put a temp site up on trafikinfo.bane.dk
91                            return null;
92                  }                  }
93          }          }
94                    
95          public List<DepartureBean> lookupDeparturesNormalSite(String stationcode, String type, boolean arrival) throws Exception {          private String getTypeString(TrainType type) {
96                    switch (type) {
97                    case STOG:
98                            return "S-Tog";
99                    case REGIONAL:
100                            return "Fjerntog";
101                    default:
102                            return ""; //Can not happen
103                    }
104            }
105            
106            public DepartureBean lookupDeparturesNormalSite(String stationcode, TrainType type, boolean arrival) throws Exception {
107                                    
108                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();                  DepartureBean departureBean = new DepartureBean();
109                                    
110              final WebClient webClient = new WebClient( BrowserVersion.FIREFOX_3 );              final WebClient webClient = new WebClient( BrowserVersion.FIREFOX_3 );
111              webClient.setTimeout(2500);              webClient.setTimeout(2500);
112              webClient.setJavaScriptEnabled(false);              webClient.setJavaScriptEnabled(false);
113                
114                
115                String typeString = getTypeString(type);
116                String arrivalDeparture = (arrival==false) ? "Afgang" : "Ankomst";
117                                                            
118              String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode;              //String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode;
119                String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning";
120    
121                //logger.info("URI: " + uri);
122              HtmlunitInvocation wrapper = new HtmlunitInvocation(webClient, uri);              HtmlunitInvocation wrapper = new HtmlunitInvocation(webClient, uri);
123              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
124                            
# Line 107  public class DepartureFetcher { Line 135  public class DepartureFetcher {
135                          if (rowClass != null && rowClass.toLowerCase().contains("station") ) {                          if (rowClass != null && rowClass.toLowerCase().contains("station") ) {
136                                  DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td");                                  DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td");
137                    
138                                  DepartureBean departure = new DepartureBean();                                  DepartureEntry departure = new DepartureEntry();
139                                                                    
140                                  String time = fields.get(0).asText();                                  String time = fields.get(0).asText();
141                                  if (time.equals(""))                                  if (time.equals(""))
# Line 118  public class DepartureFetcher { Line 146  public class DepartureFetcher {
146                                  departure.setUpdated(updated);                                  departure.setUpdated(updated);
147                                                                    
148                                  String trainNumber = fields.get(2).asText();                                  String trainNumber = fields.get(2).asText();
149                                  if (type.equalsIgnoreCase("S2")) //If it is S-train we need to extract the trainNumber                                  if (type == TrainType.STOG) //If it is S-train we need to extract the trainNumber
150                                          trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2));                                          trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2));
151                                  departure.setTrainNumber(trainNumber);                                  departure.setTrainNumber(trainNumber);
152                                                                    
# Line 137  public class DepartureFetcher { Line 165  public class DepartureFetcher {
165                                  String note = extractNote( fields.get(7) );                                  String note = extractNote( fields.get(7) );
166                                  departure.setNote(note);                                  departure.setNote(note);
167                                                                    
168                                  departure.setType(type);                                  departure.setType(typeString);
169                                                                    
170                                  departureList.add(departure);                                  departureBean.departureEntries.add( departure );
171                          }                          }
172                      }                      }
173              } else {              } else {
174                  logger.warning("No departures found for station=" + stationcode + ", type=" + type);                  logger.warning("No departures found for station=" + stationcode + ", type=" + type);
175              }              }
176                
177                HtmlElement notifDiv = page.getElementById("station_planlagte_text");
178                if (notifDiv != null) {
179    
180                    DomNodeList<HtmlElement> tables = notifDiv.getElementsByTagName("table");
181                    for (HtmlElement tab : tables) {
182    
183                            DomNodeList<HtmlElement> anchors = tab.getElementsByTagName("a");              
184                            if (anchors.size() == 2) {
185                                    departureBean.notifications.add(  anchors.get(1).getTextContent() );
186                            }
187                    }
188                    
189                }
190                
191                
192              webClient.closeAllWindows();              webClient.closeAllWindows();
193                            
194              return departureList;              return departureBean;
195          }          }
196                    
197            /*
198            @Deprecated
199          public List<DepartureBean> lookupDeparturesFromTemporarySite(String stationcode, String type) throws Exception {          public List<DepartureBean> lookupDeparturesFromTemporarySite(String stationcode, String type) throws Exception {
200                                    
201                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();
# Line 214  public class DepartureFetcher { Line 260  public class DepartureFetcher {
260                            
261                            
262              return departureList;              return departureList;
263          }          }*/
264    
265                    
266          private int extractUpdated(HtmlElement updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif"          private int extractUpdated(HtmlElement updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif"
# Line 246  public class DepartureFetcher { Line 292  public class DepartureFetcher {
292          }          }
293                    
294          private String extractTrainNumber(HtmlElement trainTd) {          private String extractTrainNumber(HtmlElement trainTd) {
                 String number = "";  
295                  HtmlElement anchorElement = trainTd.getElementsByTagName("a").get(0);                  HtmlElement anchorElement = trainTd.getElementsByTagName("a").get(0);
296                  String href = anchorElement.getAttribute("href");                  String href = anchorElement.getAttribute("href");
                 String argstring = href.substring( href.indexOf('?') + 1);  
                   
                 String args[] = argstring.split("&");  
                 for (String arg : args) {  
                         String pair[] = arg.split("="); // Key=pair[0], Value=pair[1]  
                           
                         if (pair[0].equalsIgnoreCase("TogNr"))  
                                 number = pair[1];  
                 }  
297                                    
298                    int pos = href.lastIndexOf('/');
299                    String number = href.substring(pos+1);
300                                    
301                  return number;                  return number;
302          }          }

Legend:
Removed from v.939  
changed lines
  Added in v.980

  ViewVC Help
Powered by ViewVC 1.1.20