/[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 1255 by torben, Mon Apr 4 10:56:44 2011 UTC revision 1412 by torben, Mon May 2 12:21:49 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.StationBean;
16    import dk.thoerup.android.traininfo.common.StationEntry;
17  import dk.thoerup.android.traininfo.common.TimetableBean;  import dk.thoerup.android.traininfo.common.TimetableBean;
18  import dk.thoerup.android.traininfo.common.TimetableEntry;  import dk.thoerup.android.traininfo.common.TimetableEntry;
19  import dk.thoerup.circuitbreaker.CircuitBreaker;  import dk.thoerup.circuitbreaker.CircuitBreaker;
20  import dk.thoerup.circuitbreaker.CircuitBreakerManager;  import dk.thoerup.circuitbreaker.CircuitBreakerManager;
21    import dk.thoerup.genericjavautils.TimeoutMap;
22  import dk.thoerup.traininfoservice.Statistics;  import dk.thoerup.traininfoservice.Statistics;
23    import dk.thoerup.traininfoservice.TraininfoSettings;
24  import dk.thoerup.traininfoservice.db.StationDAO;  import dk.thoerup.traininfoservice.db.StationDAO;
25    
26  public class TimetableFetcher {  public class TimetableFetcher {
27    
28                                    
29          Map<String, TimetableBean> cache;          Map<String, TimetableBean> cache;
30          Map<String, Integer> stationCache;          Map<String, StationEntry> stationCache;
31    
32          StationDAO stationDao = new StationDAO();          StationDAO stationDao = new StationDAO();
33                    
34                    
35          Logger logger = Logger.getLogger(TimetableFetcher.class.getName());          Logger logger = Logger.getLogger(TimetableFetcher.class.getName());
36    
37            TraininfoSettings settings;    
38                    
39          private boolean useAzureSite;          public TimetableFetcher(TraininfoSettings settings) {
40          private int replyTimeout;                  this.settings = settings;
           
         public TimetableFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) {  
                 useAzureSite = azureSite;  
                 this.replyTimeout = replyTimeout;  
41                                    
42                  cache = new TimeoutMap<String,TimetableBean>(cacheTimeout);                  cache = new TimeoutMap<String,TimetableBean>( settings.getCacheTimeout() );
43                  stationCache = new TimeoutMap<String,Integer>( 3*60*60*1000 );                  stationCache = new TimeoutMap<String,StationEntry>( 3*60*60*1000 );
44          }          }
45                    
46                    
# Line 57  public class TimetableFetcher { Line 59  public class TimetableFetcher {
59          }          }
60                    
61          TimetableBean lookupTimetable(String trainID, String type) throws Exception {          TimetableBean lookupTimetable(String trainID, String type) throws Exception {
62                  if (useAzureSite == true ){                  if (settings.getBackend() == TraininfoSettings.Backend.Azure ){
63                          return lookupTimetableAzureSite(trainID, type);                          return lookupTimetableAzureSite(trainID, type);
64                                                    
65                  } else {                  } else {
66                          return lookupTimetableWwwSite(trainID, type);                          return lookupTimetableMobileSite(trainID, type);
67                  }                  }
68          }          }
69                    
70          int getStationId(String name) {          StationEntry getStationId(String name) {
71                  Integer id = stationCache.get(name);                  StationEntry station = stationCache.get(name);
72                                    
73                  if (id == null) {                  if (station == null) {
74                          try {                          try {
75                                  id = stationDao.getIdByName(name);                                  StationBean bean = stationDao.getByName(name);
76                                  stationCache.put(name, id);                                  if (bean.entries.size() == 1) {
77                                            station = bean.entries.get(0);
78                                            stationCache.put(name,station);
79                                    }
80                          } catch (SQLException e) {                          } catch (SQLException e) {
81                                  logger.log(Level.SEVERE, "getStationId failed", e);                                  logger.log(Level.SEVERE, "getStationId failed", e);
                                 id = -1;  
82                          }                          }
83                  }                  }
84    
85                  return id;                  return station;
86            }
87            
88            String correctStationName(String name) {
89                    if (name.equals("København"))
90                            name = "København H"; //correct inconsistency in naming
91                    
92                    return name;            
93          }          }
94    
95          TimetableBean lookupTimetableAzureSite(String trainID, String type) throws Exception {                    TimetableBean lookupTimetableAzureSite(String trainID, String type) throws Exception {          
# Line 88  public class TimetableFetcher { Line 99  public class TimetableFetcher {
99                  String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID;                                  String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID;                
100                  logger.fine("URL:" + url);                  logger.fine("URL:" + url);
101                            
102              JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout);              JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() );
103              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
104                            
105              Document doc = (Document) breaker.invoke(wrapper);              Document doc = (Document) breaker.invoke(wrapper);
# Line 119  public class TimetableFetcher { Line 130  public class TimetableFetcher {
130                                                    
131                          TimetableEntry entry = new TimetableEntry();                          TimetableEntry entry = new TimetableEntry();
132                                                    
133                          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  
134                                                    
135                          entry.setStation( station );                          entry.setStation( station );
136                          entry.setArrival( fields.get(1).text() );                          entry.setArrival( fields.get(1).text() );
# Line 135  public class TimetableFetcher { Line 144  public class TimetableFetcher {
144                                  currentStationSaved = true;                                  currentStationSaved = true;
145                          }                          }
146                                                    
147                          entry.setStationId( getStationId( station ));                          entry.setStationEntry( getStationId( station ));
148                                                    
149                          timetableBean.entries.add(entry);                          timetableBean.entries.add(entry);
150                  }                  }
# Line 171  public class TimetableFetcher { Line 180  public class TimetableFetcher {
180                  return timetableBean;                  return timetableBean;
181          }          }
182    
183            TimetableBean lookupTimetableMobileSite(String trainID, String type) throws Exception {
184                    TimetableBean timetableBean = new TimetableBean();
185                    
186                    String url = "http://mobil.bane.dk/mobilStation.asp?artikelID=5332&tognummer=" + trainID + "&webprofil=" + type + "&mode=rute";
187                    logger.fine("URL:" + url);
188    
189                
190                JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() );
191                CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
192                
193                Document doc = (Document) breaker.invoke(wrapper);
194    
195                Element content = doc.getElementsByClass("contentDiv").get(1);
196                Element dlist = content.child(0);
197    
198                
199                Elements rows = dlist.getElementsByTag("dt");
200    
201                for (int i=0; i<rows.size(); i++) {
202    
203                    Element row = rows.get(i);
204                    
205                    logger.fine( row.text() );
206                    
207                    String parts[] = row.text().split(",");
208                    
209                    TimetableEntry entry = new TimetableEntry();
210    
211                    String station = DepartureFetcher.cleanText( parts[0] ) ;
212                    station = correctStationName(station);
213    
214    
215                    String arrival = DepartureFetcher.cleanText( parts[1] );
216                    String departure = DepartureFetcher.cleanText( "" );
217    
218                    entry.setStation( station );
219                    entry.setArrival( arrival );
220                    entry.setDeparture( departure );
221    
222    
223                    entry.setStationEntry( getStationId( station ));
224    
225                    timetableBean.entries.add(entry);
226                }          
227    
228    
229                return timetableBean;
230    
231            }
232            
233            @Deprecated
234          TimetableBean lookupTimetableWwwSite(String trainID, String type) throws Exception {                      TimetableBean lookupTimetableWwwSite(String trainID, String type) throws Exception {            
235                  TimetableBean timetableBean = new TimetableBean();                  TimetableBean timetableBean = new TimetableBean();
236                                    
# Line 178  public class TimetableFetcher { Line 238  public class TimetableFetcher {
238                  logger.fine("URL:" + url);                  logger.fine("URL:" + url);
239    
240                            
241              JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout);              JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() );
242              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
243                            
244              Document doc = (Document) breaker.invoke(wrapper);              Document doc = (Document) breaker.invoke(wrapper);
# Line 210  public class TimetableFetcher { Line 270  public class TimetableFetcher {
270                          TimetableEntry entry = new TimetableEntry();                          TimetableEntry entry = new TimetableEntry();
271                                                    
272                          String station = DepartureFetcher.cleanText( fields.get(0).text() ) ;                          String station = DepartureFetcher.cleanText( fields.get(0).text() ) ;
273                          if (station.equals("København"))                          station = correctStationName(station);
274                                  station = "København H"; //correct inconsistency in naming  
275                                                    
276                          String arrival = DepartureFetcher.cleanText( fields.get(1).text() );                          String arrival = DepartureFetcher.cleanText( fields.get(1).text() );
277                          String departure = DepartureFetcher.cleanText( fields.get(2).text() );                          String departure = DepartureFetcher.cleanText( fields.get(2).text() );
# Line 226  public class TimetableFetcher { Line 286  public class TimetableFetcher {
286                                  currentStationSaved = true;                                  currentStationSaved = true;
287                          }                          }
288                                                    
289                          entry.setStationId( getStationId( station ));                          entry.setStationEntry( getStationId( station ));
290                                                    
291                          timetableBean.entries.add(entry);                          timetableBean.entries.add(entry);
292                  }                                }              

Legend:
Removed from v.1255  
changed lines
  Added in v.1412

  ViewVC Help
Powered by ViewVC 1.1.20