/[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 697 by torben, Mon May 3 07:42:02 2010 UTC revision 971 by torben, Fri Jul 9 21:46:45 2010 UTC
# Line 16  import dk.thoerup.circuitbreaker.Circuit Line 16  import dk.thoerup.circuitbreaker.Circuit
16  import dk.thoerup.circuitbreaker.CircuitBreakerManager;  import dk.thoerup.circuitbreaker.CircuitBreakerManager;
17  import dk.thoerup.traininfoservice.StationBean;  import dk.thoerup.traininfoservice.StationBean;
18  import dk.thoerup.traininfoservice.StationDAO;  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;          Map<String, List<DepartureBean>> cache;
26                    
27          StationDAO stationDao = new StationDAO();          StationDAO stationDao = new StationDAO();
28                    
# Line 29  public class DepartureFetcher { Line 30  public class DepartureFetcher {
30                    
31          public DepartureFetcher(boolean tempSite, int cacheTimeout) {          public DepartureFetcher(boolean tempSite, int cacheTimeout) {
32                  useTempSite = tempSite;                  useTempSite = tempSite;
33                  cache = new TimeoutMap<Integer,List<DepartureBean>>(cacheTimeout);                  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                  StationBean station = stationDao.getById(stationID);                  StationBean station = stationDao.getById(stationID);
60                                    
61                  if (station.getRegional() != null) {                  if (station.getRegional() != null) {
62                          List<DepartureBean> list = lookupDepartures(station.getRegional(), "FJRN");                          List<DepartureBean> list = lookupDepartures(station.getRegional(), "Fjerntog", arrival);
63                          departureList.addAll(list);                                              departureList.addAll(list);                    
64                  }                  }
65                                    
66                  if (station.getStrain() != null) {                  if (station.getStrain() != null) {
67                          List<DepartureBean> list = lookupDepartures(station.getStrain(), "S2");                          List<DepartureBean> list = lookupDepartures(station.getStrain(), "S-Tog", arrival);
68                          departureList.addAll(list);                              departureList.addAll(list);    
69                  }                                }              
70                                    
# Line 70  public class DepartureFetcher { Line 74  public class DepartureFetcher {
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( BrowserVersion.FIREFOX_3 );              final WebClient webClient = new WebClient( BrowserVersion.FIREFOX_3 );
90              webClient.setTimeout(2500);              webClient.setTimeout(2500);
91              webClient.setJavaScriptEnabled(false);              webClient.setJavaScriptEnabled(false);
92                
93                String arrivalDeparture = (arrival==false) ? "Afgang" : "Ankomst";
94                                                            
95              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;
96              BanedkInvocation wrapper = new BanedkInvocation(webClient, uri);              String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" +type + "/UdvidetVisning";
97    
98                //logger.info("URI: " + uri);
99                HtmlunitInvocation wrapper = new HtmlunitInvocation(webClient, uri);
100              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
101                            
102              HtmlPage page = (HtmlPage) breaker.invoke(wrapper);              HtmlPage page = (HtmlPage) breaker.invoke(wrapper);
103                            
104              HtmlElement table = page.getElementById("afgangtabel");              String tableName = arrival == false ? "afgangtabel" : "ankomsttabel";
105                HtmlElement table = page.getElementById(tableName);
106                            
107              if (table != null) {              if (table != null) {
108                      DomNodeList<HtmlElement> tableRows =  table.getElementsByTagName("tr");                      DomNodeList<HtmlElement> tableRows =  table.getElementsByTagName("tr");
# Line 113  public class DepartureFetcher { Line 123  public class DepartureFetcher {
123                                  departure.setUpdated(updated);                                  departure.setUpdated(updated);
124                                                                    
125                                  String trainNumber = fields.get(2).asText();                                  String trainNumber = fields.get(2).asText();
126                                  if (type.equalsIgnoreCase("S2")) //If it is S-train we need to extract the trainNumber                                  if (type.equalsIgnoreCase("S-Tog")) //If it is S-train we need to extract the trainNumber
127                                          trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2));                                          trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2));
128                                  departure.setTrainNumber(trainNumber);                                  departure.setTrainNumber(trainNumber);
129                                                                    
# Line 126  public class DepartureFetcher { Line 136  public class DepartureFetcher {
136                                  String location = fields.get(5).asText();                                  String location = fields.get(5).asText();
137                                  departure.setLocation(location);                                  departure.setLocation(location);
138                                                                    
139                                  String status = fields.get(6).asText();                                  String status = fields.get(6).asText().trim();
140                                  departure.setStatus(status);                                  departure.setStatus(status);
141                                                                    
142                                  String note = extractNote( fields.get(7) );                                  String note = extractNote( fields.get(7) );
# Line 156  public class DepartureFetcher { Line 166  public class DepartureFetcher {
166    
167              String uri = "http://bane.dk/lite/station.asp?w=" + type + "&s=" + stationcode;              String uri = "http://bane.dk/lite/station.asp?w=" + type + "&s=" + stationcode;
168                            
169              BanedkInvocation wrapper = new BanedkInvocation(webClient, uri);              HtmlunitInvocation wrapper = new HtmlunitInvocation(webClient, uri);
170              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
171                            
172              HtmlPage page = (HtmlPage) breaker.invoke(wrapper);              HtmlPage page = (HtmlPage) breaker.invoke(wrapper);
# Line 246  public class DepartureFetcher { Line 256  public class DepartureFetcher {
256                  String href = anchorElement.getAttribute("href");                  String href = anchorElement.getAttribute("href");
257                  String argstring = href.substring( href.indexOf('?') + 1);                  String argstring = href.substring( href.indexOf('?') + 1);
258                                    
259                  String args[] = argstring.split("&");                  String args[] = argstring.split("/");
260                  for (String arg : args) {                  number = args[args.length-1];
261                    
262                    /*for (String arg : args) {
263                          String pair[] = arg.split("="); // Key=pair[0], Value=pair[1]                          String pair[] = arg.split("="); // Key=pair[0], Value=pair[1]
264                                                    
265                          if (pair[0].equalsIgnoreCase("TogNr"))                          if (pair[0].equalsIgnoreCase("TogNr"))
266                                  number = pair[1];                                  number = pair[1];
267                  }                  }*/
268                                    
269                                    
270                  return number;                  return number;

Legend:
Removed from v.697  
changed lines
  Added in v.971

  ViewVC Help
Powered by ViewVC 1.1.20