/[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 1036 by torben, Wed Sep 8 12:49:22 2010 UTC revision 1406 by torben, Mon May 2 09:34:53 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 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,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                    list = null; //TODO: DEBUG
49                                    
50                  if (list == null) {                  if (list == null) {
51                          list = lookupTimetable(trainID,type);                          list = lookupTimetable(trainID,type);
# Line 56  public class TimetableFetcher { Line 57  public class TimetableFetcher {
57                  return list;                  return list;
58          }          }
59                    
60          List<TimetableBean> lookupTimetable(String trainID, String type) throws Exception {          TimetableBean lookupTimetable(String trainID, String type) throws Exception {
61                  if (useAzureSite == true ){                  if (settings.getBackend() == TraininfoSettings.Backend.Azure ){
62                          return lookupTimetableAzureSite(trainID, type);                          return lookupTimetableAzureSite(trainID, type);
63                                                    
64                  } else {                  } else {
65                          return lookupTimetableWwwSite(trainID, type);                          return lookupTimetableMobileSite(trainID, type);
66                  }                  }
67          }          }
68                    
# Line 80  public class TimetableFetcher { Line 81  public class TimetableFetcher {
81    
82                  return id;                  return id;
83          }          }
84            
85            String correctStationName(String name) {
86                    if (name.equals("København"))
87                            name = "København H"; //correct inconsistency in naming
88                    
89                    return name;            
90            }
91    
92          List<TimetableBean> lookupTimetableAzureSite(String trainID, String type) throws Exception {                      TimetableBean lookupTimetableAzureSite(String trainID, String type) throws Exception {          
93                  List<TimetableBean> timetableList = new ArrayList<TimetableBean>();                  TimetableBean timetableBean = new TimetableBean();
94                                    
                 //String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276";  
                 String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID;                  
95    
96                    String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID;                
97                    logger.fine("URL:" + url);
98                            
99              JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout);              JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() );
100              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
101                            
102              Document doc = (Document) breaker.invoke(wrapper);              Document doc = (Document) breaker.invoke(wrapper);
# Line 117  public class TimetableFetcher { Line 125  public class TimetableFetcher {
125                                  continue;                                  continue;
126                          }                          }
127                                                    
128                          TimetableBean bean = new TimetableBean();                          TimetableEntry entry = new TimetableEntry();
129                            
130                            String station = correctStationName( fields.get(0).text() );
131                                                    
132                          String station = fields.get(0).text() ;                          entry.setStation( station );
133                          if (station.equals("København"))                          entry.setArrival( fields.get(1).text() );
134                                  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() );  
135                                                    
136                          boolean cancelled = fields.get(3).text().equalsIgnoreCase("aflyst");                          boolean cancelled = fields.get(3).text().equalsIgnoreCase("aflyst");
137                          bean.setCancelled(cancelled);                          entry.setCancelled(cancelled);
138                                                    
139                          if (currentStation == true && currentStationSaved == false ) {                          if (currentStation == true && currentStationSaved == false ) {
140                                  bean.setCurrent(currentStation);                                  entry.setCurrent(currentStation);
141                                  currentStationSaved = true;                                  currentStationSaved = true;
142                          }                          }
143                                                    
144                          bean.setStationId( getStationId( station ));                          entry.setStationId( getStationId( station ));
145                                                    
146                          timetableList.add(bean);                          timetableBean.entries.add(entry);
147                  }                  }
148                                    
149                  //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
150                  final String cancelledString = "Aflyst";                  final String cancelledString = "Aflyst";
151                  for (int i=0;i<timetableList.size(); i++) { //handle cancelled labels                  for (int i=0;i<timetableBean.entries.size(); i++) { //handle cancelled labels
152                          final int lastIdx = (timetableList.size() - 1);                          final int lastIdx = (timetableBean.entries.size() - 1);
153                                                    
154                          TimetableBean current = timetableList.get(i);                          TimetableEntry current = timetableBean.entries.get(i);
155                          if (current.isCancelled()) {                          if (current.isCancelled()) {
156                                  if (i == 0) {                                  if (i == 0) {
157                                          current.setDeparture(cancelledString);                                          current.setDeparture(cancelledString);
158                                  } else if (i == lastIdx) {                                  } else if (i == lastIdx) {
159                                          current.setArrival(cancelledString);                                          current.setArrival(cancelledString);
160                                  } else if (i>0 && i<lastIdx) {                                  } else if (i>0 && i<lastIdx) {
161                                          TimetableBean next = timetableList.get(i+1);                                          TimetableEntry next = timetableBean.entries.get(i+1);
162                                          TimetableBean prev = timetableList.get(i-1);                                          TimetableEntry prev = timetableBean.entries.get(i-1);
163                                                                                    
164                                          if (next.isCancelled())                                          if (next.isCancelled())
165                                                  current.setDeparture(cancelledString);                                                  current.setDeparture(cancelledString);
# Line 168  public class TimetableFetcher { Line 174  public class TimetableFetcher {
174              }              }
175                            
176                                    
177                  return timetableList;                  return timetableBean;
178          }          }
179    
180          List<TimetableBean> lookupTimetableWwwSite(String trainID, String type) throws Exception {                        TimetableBean lookupTimetableMobileSite(String trainID, String type) throws Exception {
181                  List<TimetableBean> timetableList = new ArrayList<TimetableBean>();                  TimetableBean timetableBean = new TimetableBean();
182                                    
183                  String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276";                  String url = "http://mobil.bane.dk/mobilStation.asp?artikelID=5332&tognummer=" + trainID + "&webprofil=" + type + "&mode=rute";
184                    logger.fine("URL:" + url);
185    
186                
187                JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() );
188                CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
189                
190                Document doc = (Document) breaker.invoke(wrapper);
191    
192                Element content = doc.getElementsByClass("contentDiv").get(1);
193                Element dlist = content.child(0);
194    
195                
196                Elements rows = dlist.getElementsByTag("dt");
197    
198                for (int i=0; i<rows.size(); i++) {
199    
200                    Element row = rows.get(i);
201                    
202                    logger.fine( row.text() );
203                    
204                    String parts[] = row.text().split(",");
205                    
206                    TimetableEntry entry = new TimetableEntry();
207    
208                    String station = DepartureFetcher.cleanText( parts[0] ) ;
209                    station = correctStationName(station);
210    
211    
212                    String arrival = DepartureFetcher.cleanText( parts[1] );
213                    String departure = DepartureFetcher.cleanText( "" );
214    
215                    entry.setStation( station );
216                    entry.setArrival( arrival );
217                    entry.setDeparture( departure );
218    
219    
220                    entry.setStationId( getStationId( station ));
221    
222                    timetableBean.entries.add(entry);
223                }          
224    
225    
226                return timetableBean;
227    
228            }
229            
230            @Deprecated
231            TimetableBean lookupTimetableWwwSite(String trainID, String type) throws Exception {            
232                    TimetableBean timetableBean = new TimetableBean();
233                    
234                    String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276";
235                    logger.fine("URL:" + url);
236    
237                            
238              JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout);              JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() );
239              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
240                            
241              Document doc = (Document) breaker.invoke(wrapper);              Document doc = (Document) breaker.invoke(wrapper);
# Line 207  public class TimetableFetcher { Line 264  public class TimetableFetcher {
264                                  continue;                                  continue;
265                          }                          }
266                                                    
267                          TimetableBean bean = new TimetableBean();                          TimetableEntry entry = new TimetableEntry();
268                            
269                            String station = DepartureFetcher.cleanText( fields.get(0).text() ) ;
270                            station = correctStationName(station);
271    
272                            
273                            String arrival = DepartureFetcher.cleanText( fields.get(1).text() );
274                            String departure = DepartureFetcher.cleanText( fields.get(2).text() );
275                                                    
276                          String station = fields.get(0).text() ;                          entry.setStation( station );
277                          if (station.equals("København"))                          entry.setArrival( arrival );
278                                  station = "København H"; //correct inconsistency in naming                          entry.setDeparture( departure );
                           
                         bean.setStation( station );  
                         bean.setArrival( fields.get(1).text() );  
                         bean.setDeparture( fields.get(2).text() );  
279                                    
280                                                    
281                          if (currentStation == true && currentStationSaved == false ) {                          if (currentStation == true && currentStationSaved == false ) {
282                                  bean.setCurrent(currentStation);                                  entry.setCurrent(currentStation);
283                                  currentStationSaved = true;                                  currentStationSaved = true;
284                          }                          }
285                                                    
286                          bean.setStationId( getStationId( station ));                          entry.setStationId( getStationId( station ));
287                                                    
288                          timetableList.add(bean);                          timetableBean.entries.add(entry);
289                  }                                }              
290                                    
291              } else {              } else {
# Line 233  public class TimetableFetcher { Line 293  public class TimetableFetcher {
293              }              }
294                            
295                                    
296                  return timetableList;                  return timetableBean;
297          }                }      
298                    
299  }  }

Legend:
Removed from v.1036  
changed lines
  Added in v.1406

  ViewVC Help
Powered by ViewVC 1.1.20