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

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

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

android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java revision 1040 by torben, Sun Sep 12 19:37:56 2010 UTC android/TrainInfoServiceGoogle/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java revision 1093 by torben, Tue Sep 21 20:10:46 2010 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;
7  import java.util.ArrayList;  import java.util.HashMap;
 import java.util.List;  
8  import java.util.Map;  import java.util.Map;
9  import java.util.logging.Level;  import java.util.logging.Level;
10  import java.util.logging.Logger;  import java.util.logging.Logger;
11    
12    import net.sf.jsr107cache.Cache;
13    import net.sf.jsr107cache.CacheException;
14    import net.sf.jsr107cache.CacheManager;
15    
16  import org.jsoup.nodes.Document;  import org.jsoup.nodes.Document;
17  import org.jsoup.nodes.Element;  import org.jsoup.nodes.Element;
18  import org.jsoup.select.Elements;  import org.jsoup.select.Elements;
19    
20    import com.google.appengine.api.memcache.jsr107cache.GCacheFactory;
21    
22    import dk.thoerup.android.traininfo.common.TimetableBean;
23    import dk.thoerup.android.traininfo.common.TimetableEntry;
24  import dk.thoerup.circuitbreaker.CircuitBreaker;  import dk.thoerup.circuitbreaker.CircuitBreaker;
25  import dk.thoerup.circuitbreaker.CircuitBreakerManager;  import dk.thoerup.circuitbreaker.CircuitBreakerManager;
26  import dk.thoerup.traininfoservice.StationDAO;  import dk.thoerup.traininfoservice.StationDAO;
# Line 22  import dk.thoerup.traininfoservice.Stati Line 29  import dk.thoerup.traininfoservice.Stati
29  public class TimetableFetcher {  public class TimetableFetcher {
30    
31                                    
32          Map<String, List<TimetableBean>> cache;          Cache cache;
33          Map<String, Integer> stationCache;          Cache stationCache;
34    
35          StationDAO stationDao = new StationDAO();          StationDAO stationDao = new StationDAO();
36                    
# Line 33  public class TimetableFetcher { Line 40  public class TimetableFetcher {
40          private boolean useAzureSite;          private boolean useAzureSite;
41          private int replyTimeout;          private int replyTimeout;
42                    
43            @SuppressWarnings("unchecked")
44          public TimetableFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) {          public TimetableFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) {
45                  useAzureSite = azureSite;                  useAzureSite = azureSite;
46                  this.replyTimeout = replyTimeout;                  this.replyTimeout = replyTimeout;              
47                    
48            Map props = new HashMap();
49            props.put(GCacheFactory.EXPIRATION_DELTA_MILLIS, cacheTimeout);        
50                    
51                    try {
52                cache = CacheManager.getInstance().getCacheFactory().createCache(props);            
53            } catch (CacheException e) {
54                    logger.log(Level.WARNING, "error creating cache", e);
55            }
56            
57            props = new HashMap();
58            props.put(GCacheFactory.EXPIRATION_DELTA_MILLIS, 3*60*60*1000);        
59                                    
60                  cache = new TimeoutMap<String,List<TimetableBean>>(cacheTimeout);                  try {
61                  stationCache = new TimeoutMap<String,Integer>( 3*60*60*1000 );                          stationCache = CacheManager.getInstance().getCacheFactory().createCache(props);            
62            } catch (CacheException e) {
63                    logger.log(Level.WARNING, "error creating cache", e);
64            }              
65          }          }
66                    
67                    
68          List<TimetableBean> cachedLookupTimetable(String trainID, String type) throws Exception {          TimetableBean cachedLookupTimetable(String trainID, String type) throws Exception {
69                  String key = trainID+type;                  String key = trainID+type;
70                  List<TimetableBean> list = cache.get(key);                  TimetableBean list = cache.get(key);
71                                    
72                  if (list == null) {                  if (list == null) {
73                          list = lookupTimetable(trainID,type);                          list = lookupTimetable(trainID,type);
# Line 56  public class TimetableFetcher { Line 79  public class TimetableFetcher {
79                  return list;                  return list;
80          }          }
81                    
82          List<TimetableBean> lookupTimetable(String trainID, String type) throws Exception {          TimetableBean lookupTimetable(String trainID, String type) throws Exception {
83                  if (useAzureSite == true ){                  if (useAzureSite == true ){
84                          return lookupTimetableAzureSite(trainID, type);                          return lookupTimetableAzureSite(trainID, type);
85                                                    
# Line 81  public class TimetableFetcher { Line 104  public class TimetableFetcher {
104                  return id;                  return id;
105          }          }
106    
107          List<TimetableBean> lookupTimetableAzureSite(String trainID, String type) throws Exception {                      TimetableBean lookupTimetableAzureSite(String trainID, String type) throws Exception {          
108                  List<TimetableBean> timetableList = new ArrayList<TimetableBean>();                  TimetableBean timetableBean = new TimetableBean();
109                                    
                 //String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276";  
                 String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID;                  
110    
111                    String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID;                
112                    logger.fine("URL:" + url);
113                            
114              JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout);              JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout);
115              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
# Line 117  public class TimetableFetcher { Line 140  public class TimetableFetcher {
140                                  continue;                                  continue;
141                          }                          }
142                                                    
143                          TimetableBean bean = new TimetableBean();                          TimetableEntry entry = new TimetableEntry();
144                                                    
145                          String station = fields.get(0).text() ;                          String station = fields.get(0).text() ;
146                          if (station.equals("København"))                          if (station.equals("København"))
147                                  station = "København H"; //correct inconsistency in naming                                  station = "København H"; //correct inconsistency in naming
148                                                    
149                          bean.setStation( station );                          entry.setStation( station );
150                          bean.setArrival( fields.get(1).text() );                          entry.setArrival( fields.get(1).text() );
151                          bean.setDeparture( fields.get(2).text() );                          entry.setDeparture( fields.get(2).text() );
152                                                    
153                          boolean cancelled = fields.get(3).text().equalsIgnoreCase("aflyst");                          boolean cancelled = fields.get(3).text().equalsIgnoreCase("aflyst");
154                          bean.setCancelled(cancelled);                          entry.setCancelled(cancelled);
155                                                    
156                          if (currentStation == true && currentStationSaved == false ) {                          if (currentStation == true && currentStationSaved == false ) {
157                                  bean.setCurrent(currentStation);                                  entry.setCurrent(currentStation);
158                                  currentStationSaved = true;                                  currentStationSaved = true;
159                          }                          }
160                                                    
161                          bean.setStationId( getStationId( station ));                          entry.setStationId( getStationId( station ));
162                                                    
163                          timetableList.add(bean);                          timetableBean.entries.add(entry);
164                  }                  }
165                                    
166                  //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
167                  final String cancelledString = "Aflyst";                  final String cancelledString = "Aflyst";
168                  for (int i=0;i<timetableList.size(); i++) { //handle cancelled labels                  for (int i=0;i<timetableBean.entries.size(); i++) { //handle cancelled labels
169                          final int lastIdx = (timetableList.size() - 1);                          final int lastIdx = (timetableBean.entries.size() - 1);
170                                                    
171                          TimetableBean current = timetableList.get(i);                          TimetableEntry current = timetableBean.entries.get(i);
172                          if (current.isCancelled()) {                          if (current.isCancelled()) {
173                                  if (i == 0) {                                  if (i == 0) {
174                                          current.setDeparture(cancelledString);                                          current.setDeparture(cancelledString);
175                                  } else if (i == lastIdx) {                                  } else if (i == lastIdx) {
176                                          current.setArrival(cancelledString);                                          current.setArrival(cancelledString);
177                                  } else if (i>0 && i<lastIdx) {                                  } else if (i>0 && i<lastIdx) {
178                                          TimetableBean next = timetableList.get(i+1);                                          TimetableEntry next = timetableBean.entries.get(i+1);
179                                          TimetableBean prev = timetableList.get(i-1);                                          TimetableEntry prev = timetableBean.entries.get(i-1);
180                                                                                    
181                                          if (next.isCancelled())                                          if (next.isCancelled())
182                                                  current.setDeparture(cancelledString);                                                  current.setDeparture(cancelledString);
# Line 168  public class TimetableFetcher { Line 191  public class TimetableFetcher {
191              }              }
192                            
193                                    
194                  return timetableList;                  return timetableBean;
195          }          }
196    
197          List<TimetableBean> lookupTimetableWwwSite(String trainID, String type) throws Exception {                        TimetableBean lookupTimetableWwwSite(String trainID, String type) throws Exception {            
198                  List<TimetableBean> timetableList = new ArrayList<TimetableBean>();                  TimetableBean timetableBean = new TimetableBean();
199                                    
200                  String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276";                  String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276";
201                    logger.fine("URL:" + url);
202    
203                            
204              JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout);              JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout);
# Line 207  public class TimetableFetcher { Line 230  public class TimetableFetcher {
230                                  continue;                                  continue;
231                          }                          }
232                                                    
233                          TimetableBean bean = new TimetableBean();                          TimetableEntry entry = new TimetableEntry();
234                                                    
235                          String station = DepartureFetcher.cleanText( fields.get(0).text() ) ;                          String station = DepartureFetcher.cleanText( fields.get(0).text() ) ;
236                          if (station.equals("København"))                          if (station.equals("København"))
# Line 216  public class TimetableFetcher { Line 239  public class TimetableFetcher {
239                          String arrival = DepartureFetcher.cleanText( fields.get(1).text() );                          String arrival = DepartureFetcher.cleanText( fields.get(1).text() );
240                          String departure = DepartureFetcher.cleanText( fields.get(2).text() );                          String departure = DepartureFetcher.cleanText( fields.get(2).text() );
241                                                    
242                          bean.setStation( station );                          entry.setStation( station );
243                          bean.setArrival( arrival );                          entry.setArrival( arrival );
244                          bean.setDeparture( departure );                          entry.setDeparture( departure );
245                                    
246                                                    
247                          if (currentStation == true && currentStationSaved == false ) {                          if (currentStation == true && currentStationSaved == false ) {
248                                  bean.setCurrent(currentStation);                                  entry.setCurrent(currentStation);
249                                  currentStationSaved = true;                                  currentStationSaved = true;
250                          }                          }
251                                                    
252                          bean.setStationId( getStationId( station ));                          entry.setStationId( getStationId( station ));
253                                                    
254                          timetableList.add(bean);                          timetableBean.entries.add(entry);
255                  }                                }              
256                                    
257              } else {              } else {
# Line 236  public class TimetableFetcher { Line 259  public class TimetableFetcher {
259              }              }
260                            
261                                    
262                  return timetableList;                  return timetableBean;
263          }                }      
264                    
265  }  }

Legend:
Removed from v.1040  
changed lines
  Added in v.1093

  ViewVC Help
Powered by ViewVC 1.1.20