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

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

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

revision 1060 by torben, Thu Sep 16 13:32:10 2010 UTC revision 1410 by torben, Mon May 2 12:02:54 2011 UTC
# Line 12  import org.jsoup.nodes.Document; Line 12  import org.jsoup.nodes.Document;
12  import org.jsoup.nodes.Element;  import org.jsoup.nodes.Element;
13  import org.jsoup.select.Elements;  import org.jsoup.select.Elements;
14    
15    import dk.thoerup.android.traininfo.common.TimetableBean;
16    import dk.thoerup.android.traininfo.common.TimetableEntry;
17  import dk.thoerup.circuitbreaker.CircuitBreaker;  import dk.thoerup.circuitbreaker.CircuitBreaker;
18  import dk.thoerup.circuitbreaker.CircuitBreakerManager;  import dk.thoerup.circuitbreaker.CircuitBreakerManager;
19  import dk.thoerup.traininfoservice.StationDAO;  import dk.thoerup.genericjavautils.TimeoutMap;
20  import dk.thoerup.traininfoservice.Statistics;  import dk.thoerup.traininfoservice.Statistics;
21    import dk.thoerup.traininfoservice.TraininfoSettings;
22    import dk.thoerup.traininfoservice.db.StationDAO;
23    
24  public class TimetableFetcher {  public class TimetableFetcher {
25    
# Line 27  public class TimetableFetcher { Line 31  public class TimetableFetcher {
31                    
32                    
33          Logger logger = Logger.getLogger(TimetableFetcher.class.getName());          Logger logger = Logger.getLogger(TimetableFetcher.class.getName());
34    
35            TraininfoSettings settings;    
36                    
37          private boolean useAzureSite;          public TimetableFetcher(TraininfoSettings settings) {
38          private int replyTimeout;                  this.settings = settings;
           
         public TimetableFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) {  
                 useAzureSite = azureSite;  
                 this.replyTimeout = replyTimeout;  
39                                    
40                  cache = new TimeoutMap<String,TimetableBean>(cacheTimeout);                  cache = new TimeoutMap<String,TimetableBean>( settings.getCacheTimeout() );
41                  stationCache = new TimeoutMap<String,Integer>( 3*60*60*1000 );                  stationCache = new TimeoutMap<String,Integer>( 3*60*60*1000 );
42          }          }
43                    
# Line 55  public class TimetableFetcher { Line 57  public class TimetableFetcher {
57          }          }
58                    
59          TimetableBean lookupTimetable(String trainID, String type) throws Exception {          TimetableBean lookupTimetable(String trainID, String type) throws Exception {
60                  if (useAzureSite == true ){                  if (settings.getBackend() == TraininfoSettings.Backend.Azure ){
61                          return lookupTimetableAzureSite(trainID, type);                          return lookupTimetableAzureSite(trainID, type);
62                                                    
63                  } else {                  } else {
64                          return lookupTimetableWwwSite(trainID, type);                          return lookupTimetableMobileSite(trainID, type);
65                  }                  }
66          }          }
67                    
# Line 78  public class TimetableFetcher { Line 80  public class TimetableFetcher {
80    
81                  return id;                  return id;
82          }          }
83            
84            String correctStationName(String name) {
85                    if (name.equals("København"))
86                            name = "København H"; //correct inconsistency in naming
87                    
88                    return name;            
89            }
90    
91          TimetableBean lookupTimetableAzureSite(String trainID, String type) throws Exception {                    TimetableBean lookupTimetableAzureSite(String trainID, String type) throws Exception {          
92                  TimetableBean timetableBean = new TimetableBean();                  TimetableBean timetableBean = new TimetableBean();
# Line 86  public class TimetableFetcher { Line 95  public class TimetableFetcher {
95                  String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID;                                  String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID;                
96                  logger.fine("URL:" + url);                  logger.fine("URL:" + url);
97                            
98              JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout);              JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() );
99              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
100                            
101              Document doc = (Document) breaker.invoke(wrapper);              Document doc = (Document) breaker.invoke(wrapper);
# Line 117  public class TimetableFetcher { Line 126  public class TimetableFetcher {
126                                                    
127                          TimetableEntry entry = new TimetableEntry();                          TimetableEntry entry = new TimetableEntry();
128                                                    
129                          String station = fields.get(0).text() ;                          String station = correctStationName( fields.get(0).text() );
                         if (station.equals("København"))  
                                 station = "København H"; //correct inconsistency in naming  
130                                                    
131                          entry.setStation( station );                          entry.setStation( station );
132                          entry.setArrival( fields.get(1).text() );                          entry.setArrival( fields.get(1).text() );
# Line 169  public class TimetableFetcher { Line 176  public class TimetableFetcher {
176                  return timetableBean;                  return timetableBean;
177          }          }
178    
179            TimetableBean lookupTimetableMobileSite(String trainID, String type) throws Exception {
180                    TimetableBean timetableBean = new TimetableBean();
181                    
182                    String url = "http://mobil.bane.dk/mobilStation.asp?artikelID=5332&tognummer=" + trainID + "&webprofil=" + type + "&mode=rute";
183                    logger.fine("URL:" + url);
184    
185                
186                JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() );
187                CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
188                
189                Document doc = (Document) breaker.invoke(wrapper);
190    
191                Element content = doc.getElementsByClass("contentDiv").get(1);
192                Element dlist = content.child(0);
193    
194                
195                Elements rows = dlist.getElementsByTag("dt");
196    
197                for (int i=0; i<rows.size(); i++) {
198    
199                    Element row = rows.get(i);
200                    
201                    logger.fine( row.text() );
202                    
203                    String parts[] = row.text().split(",");
204                    
205                    TimetableEntry entry = new TimetableEntry();
206    
207                    String station = DepartureFetcher.cleanText( parts[0] ) ;
208                    station = correctStationName(station);
209    
210    
211                    String arrival = DepartureFetcher.cleanText( parts[1] );
212                    String departure = DepartureFetcher.cleanText( "" );
213    
214                    entry.setStation( station );
215                    entry.setArrival( arrival );
216                    entry.setDeparture( departure );
217    
218    
219                    entry.setStationId( getStationId( station ));
220    
221                    timetableBean.entries.add(entry);
222                }          
223    
224    
225                return timetableBean;
226    
227            }
228            
229            @Deprecated
230          TimetableBean lookupTimetableWwwSite(String trainID, String type) throws Exception {                      TimetableBean lookupTimetableWwwSite(String trainID, String type) throws Exception {            
231                  TimetableBean timetableBean = new TimetableBean();                  TimetableBean timetableBean = new TimetableBean();
232                                    
# Line 176  public class TimetableFetcher { Line 234  public class TimetableFetcher {
234                  logger.fine("URL:" + url);                  logger.fine("URL:" + url);
235    
236                            
237              JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout);              JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() );
238              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
239                            
240              Document doc = (Document) breaker.invoke(wrapper);              Document doc = (Document) breaker.invoke(wrapper);
# Line 208  public class TimetableFetcher { Line 266  public class TimetableFetcher {
266                          TimetableEntry entry = new TimetableEntry();                          TimetableEntry entry = new TimetableEntry();
267                                                    
268                          String station = DepartureFetcher.cleanText( fields.get(0).text() ) ;                          String station = DepartureFetcher.cleanText( fields.get(0).text() ) ;
269                          if (station.equals("København"))                          station = correctStationName(station);
270                                  station = "København H"; //correct inconsistency in naming  
271                                                    
272                          String arrival = DepartureFetcher.cleanText( fields.get(1).text() );                          String arrival = DepartureFetcher.cleanText( fields.get(1).text() );
273                          String departure = DepartureFetcher.cleanText( fields.get(2).text() );                          String departure = DepartureFetcher.cleanText( fields.get(2).text() );

Legend:
Removed from v.1060  
changed lines
  Added in v.1410

  ViewVC Help
Powered by ViewVC 1.1.20