/[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 1026 by torben, Thu Sep 2 18:37:49 2010 UTC revision 1410 by torben, Mon May 2 12:02:54 2011 UTC
# Line 4  package dk.thoerup.traininfoservice.bane Line 4  package dk.thoerup.traininfoservice.bane
4    
5  import java.net.URL;  import java.net.URL;
6  import java.sql.SQLException;  import java.sql.SQLException;
 import java.util.ArrayList;  
 import java.util.List;  
7  import java.util.Map;  import java.util.Map;
8  import java.util.logging.Level;  import java.util.logging.Level;
9  import java.util.logging.Logger;  import java.util.logging.Logger;
# Line 14  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    
26                                    
27          Map<String, List<TimetableBean>> cache;          Map<String, TimetableBean> cache;
28          Map<String, Integer> stationCache;          Map<String, Integer> stationCache;
29    
30          StationDAO stationDao = new StationDAO();          StationDAO stationDao = new StationDAO();
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 useTempSite;          public TimetableFetcher(TraininfoSettings settings) {
38          private int replyTimeout;                  this.settings = settings;
           
         public TimetableFetcher(boolean tmpSite, int cacheTimeout, int replyTimeout) {  
                 useTempSite = tmpSite;  
                 this.replyTimeout = replyTimeout;  
39                                    
40                  cache = new TimeoutMap<String,List<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                    
44                    
45          List<TimetableBean> cachedLookupTimetable(String trainID, String type) throws Exception {          TimetableBean cachedLookupTimetable(String trainID, String type) throws Exception {
46                  String key = trainID+type;                  String key = trainID+type;
47                  List<TimetableBean> list = cache.get(key);                  TimetableBean list = cache.get(key);
48                                    
49                  if (list == null) {                  if (list == null) {
50                          list = lookupTimetable(trainID,type);                          list = lookupTimetable(trainID,type);
# Line 56  public class TimetableFetcher { Line 56  public class TimetableFetcher {
56                  return list;                  return list;
57          }          }
58                    
59          List<TimetableBean> lookupTimetable(String trainID, String type) throws Exception {          TimetableBean lookupTimetable(String trainID, String type) throws Exception {
60                  if (useTempSite == false ){                  if (settings.getBackend() == TraininfoSettings.Backend.Azure ){
61                          return lookupTimetableRealSite(trainID, type);                          return lookupTimetableAzureSite(trainID, type);
62                            
63                  } else {                  } else {
64                          return new ArrayList<TimetableBean>(); // no timetable data on temp site                          return lookupTimetableMobileSite(trainID, type);
65                  }                  }
66          }          }
67                    
# Line 79  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          List<TimetableBean> lookupTimetableRealSite(String trainID, String type) throws Exception {                      TimetableBean lookupTimetableAzureSite(String trainID, String type) throws Exception {          
92                  List<TimetableBean> timetableList = new ArrayList<TimetableBean>();                  TimetableBean timetableBean = new TimetableBean();
93                                    
                 //String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276";  
                 String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID;                  
94    
95                    String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID;                
96                    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 116  public class TimetableFetcher { Line 124  public class TimetableFetcher {
124                                  continue;                                  continue;
125                          }                          }
126                                                    
127                          TimetableBean bean = new TimetableBean();                          TimetableEntry entry = new TimetableEntry();
128                            
129                            String station = correctStationName( fields.get(0).text() );
130                                                    
131                          String station = fields.get(0).text() ;                          entry.setStation( station );
132                          if (station.equals("København"))                          entry.setArrival( fields.get(1).text() );
133                                  station = "København H"; //correct inconsistency in naming                          entry.setDeparture( fields.get(2).text() );
                           
                         bean.setStation( station );  
                         bean.setArrival( fields.get(1).text() );  
                         bean.setDeparture( fields.get(2).text() );  
134                                                    
135                          boolean cancelled = fields.get(3).text().equalsIgnoreCase("aflyst");                          boolean cancelled = fields.get(3).text().equalsIgnoreCase("aflyst");
136                          bean.setCancelled(cancelled);                          entry.setCancelled(cancelled);
137                                                    
138                          if (currentStation == true && currentStationSaved == false ) {                          if (currentStation == true && currentStationSaved == false ) {
139                                  bean.setCurrent(currentStation);                                  entry.setCurrent(currentStation);
140                                  currentStationSaved = true;                                  currentStationSaved = true;
141                          }                          }
142                                                    
143                          bean.setStationId( getStationId( station ));                          entry.setStationId( getStationId( station ));
144                                                    
145                          timetableList.add(bean);                          timetableBean.entries.add(entry);
146                  }                  }
147                                    
148                  //TODO: There is an off-by-one error in this cancelled parser thingie                  //TODO: There is an off-by-one error in this cancelled parser thingie
149                  final String cancelledString = "Aflyst";                  final String cancelledString = "Aflyst";
150                  for (int i=0;i<timetableList.size(); i++) { //handle cancelled labels                  for (int i=0;i<timetableBean.entries.size(); i++) { //handle cancelled labels
151                          final int lastIdx = (timetableList.size() - 1);                          final int lastIdx = (timetableBean.entries.size() - 1);
152                                                    
153                          TimetableBean current = timetableList.get(i);                          TimetableEntry current = timetableBean.entries.get(i);
154                          if (current.isCancelled()) {                          if (current.isCancelled()) {
155                                  if (i == 0) {                                  if (i == 0) {
156                                          current.setDeparture(cancelledString);                                          current.setDeparture(cancelledString);
157                                  } else if (i == lastIdx) {                                  } else if (i == lastIdx) {
158                                          current.setArrival(cancelledString);                                          current.setArrival(cancelledString);
159                                  } else if (i>0 && i<lastIdx) {                                  } else if (i>0 && i<lastIdx) {
160                                          TimetableBean next = timetableList.get(i+1);                                          TimetableEntry next = timetableBean.entries.get(i+1);
161                                          TimetableBean prev = timetableList.get(i-1);                                          TimetableEntry prev = timetableBean.entries.get(i-1);
162                                                                                    
163                                          if (next.isCancelled())                                          if (next.isCancelled())
164                                                  current.setDeparture(cancelledString);                                                  current.setDeparture(cancelledString);
# Line 167  public class TimetableFetcher { Line 173  public class TimetableFetcher {
173              }              }
174                            
175                                    
176                  return timetableList;                  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 {            
231                    TimetableBean timetableBean = new TimetableBean();
232                    
233                    String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276";
234                    logger.fine("URL:" + url);
235    
236                
237                JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() );
238                CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
239                
240                Document doc = (Document) breaker.invoke(wrapper);
241                        
242                
243                boolean currentStation = false;
244                boolean currentStationSaved = false;
245                
246                Elements tables = doc.getElementsByClass("Rute");
247                
248                if (tables.size() == 1) {
249                    Element timetable = tables.get(0);
250                    Elements rows = timetable.getElementsByTag("tr");
251                    
252                    for (int i=0; i<rows.size(); i++) {
253                            if (i==0) //First row is column headers
254                                    continue;
255                            
256                            
257                            Element row = rows.get(i);
258                            Elements fields = row.getElementsByTag("td");
259    
260                            
261                            if (currentStationSaved == false && fields.get(0).attr("class").equalsIgnoreCase("Tidsstreg")) {
262                                    currentStation = true;
263                                    continue;
264                            }
265                            
266                            TimetableEntry entry = new TimetableEntry();
267                            
268                            String station = DepartureFetcher.cleanText( fields.get(0).text() ) ;
269                            station = correctStationName(station);
270    
271                            
272                            String arrival = DepartureFetcher.cleanText( fields.get(1).text() );
273                            String departure = DepartureFetcher.cleanText( fields.get(2).text() );
274                            
275                            entry.setStation( station );
276                            entry.setArrival( arrival );
277                            entry.setDeparture( departure );
278                    
279                            
280                            if (currentStation == true && currentStationSaved == false ) {
281                                    entry.setCurrent(currentStation);
282                                    currentStationSaved = true;
283                            }
284                            
285                            entry.setStationId( getStationId( station ));
286                            
287                            timetableBean.entries.add(entry);
288                    }              
289                    
290                } else {
291                    logger.warning("No time table found, trainID=" + trainID + " type=" + type);
292                }
293                
294                    
295                    return timetableBean;
296            }      
297                    
298  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20