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

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

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

revision 582 by torben, Tue Feb 2 19:05:08 2010 UTC revision 591 by torben, Wed Feb 10 14:07:04 2010 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfoservice.banedk;  package dk.thoerup.traininfoservice.banedk;
2    
 import java.sql.Connection;  
 import java.sql.ResultSet;  
 import java.sql.Statement;  
3  import java.util.ArrayList;  import java.util.ArrayList;
4  import java.util.Collections;  import java.util.Collections;
5  import java.util.List;  import java.util.List;
6  import java.util.Map;  import java.util.Map;
7  import java.util.logging.Logger;  import java.util.logging.Logger;
8    
9    import com.gargoylesoftware.htmlunit.BrowserVersion;
10  import com.gargoylesoftware.htmlunit.WebClient;  import com.gargoylesoftware.htmlunit.WebClient;
11  import com.gargoylesoftware.htmlunit.html.DomNodeList;  import com.gargoylesoftware.htmlunit.html.DomNodeList;
12  import com.gargoylesoftware.htmlunit.html.HtmlElement;  import com.gargoylesoftware.htmlunit.html.HtmlElement;
# Line 16  import com.gargoylesoftware.htmlunit.htm Line 14  import com.gargoylesoftware.htmlunit.htm
14    
15  import dk.thoerup.circuitbreaker.CircuitBreaker;  import dk.thoerup.circuitbreaker.CircuitBreaker;
16  import dk.thoerup.circuitbreaker.CircuitBreakerManager;  import dk.thoerup.circuitbreaker.CircuitBreakerManager;
17  import dk.thoerup.traininfoservice.DBConnection;  import dk.thoerup.traininfoservice.StationBean;
18    import dk.thoerup.traininfoservice.StationDAO;
19    
20  public class DepartureFetcher {  public class DepartureFetcher {
21                    
22          Logger logger = Logger.getLogger(DepartureFetcher.class.getName());          Logger logger = Logger.getLogger(DepartureFetcher.class.getName());
23                    
24          Map<Integer, List<DepartureBean>> cache = new TimeoutMap<Integer,List<DepartureBean>>(120 * 1000);          Map<Integer, List<DepartureBean>> cache;
25            
26            StationDAO stationDao = new StationDAO();
27                    
28          private boolean useTempSite;          private boolean useTempSite;
29                    
30          public DepartureFetcher(boolean tempSite) {          public DepartureFetcher(boolean tempSite, int cacheTimeout) {
31                  useTempSite = tempSite;                  useTempSite = tempSite;
32                    cache = new TimeoutMap<Integer,List<DepartureBean>>(cacheTimeout);
33          }          }
34                    
35                    
# Line 50  public class DepartureFetcher { Line 52  public class DepartureFetcher {
52          public List<DepartureBean> lookupDepartures(int stationID) throws Exception {          public List<DepartureBean> lookupDepartures(int stationID) throws Exception {
53                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();
54                                    
55                  Connection conn = null;                  StationBean station = stationDao.getById(stationID);
56                  try                  
57                  {                  if (station.getRegional() != null) {
58                          conn = DBConnection.getConnection();                          List<DepartureBean> list = lookupDepartures(station.getRegional(), "FJRN");
59                                            departureList.addAll(list);                    
                         String SQL = "SELECT stationcode_fjrn, stationcode_stog FROM trainstations WHERE id=" + stationID;  
                         Statement stmt = conn.createStatement();  
                         ResultSet rs = stmt.executeQuery(SQL);  
                           
                         if (rs.next()) {  
                                 String code = rs.getString( 1 );  
                                 if (! rs.wasNull() ) {  
                                         List<DepartureBean> list = lookupDepartures(code, "FJRN");  
                                         departureList.addAll(list);  
                                 }  
                                   
                                 code = rs.getString(2);  
                                 if (! rs.wasNull() ) {  
                                         List<DepartureBean> list = lookupDepartures(code, "S2");  
                                         departureList.addAll(list);      
                                 }  
                                 Collections.sort( departureList );  
                           
                         }  
                           
                 } finally {  
                         if (conn != null && !conn.isClosed() ) {  
                                 conn.close();  
                         }  
60                  }                  }
61                                    
62                    if (station.getStrain() != null) {
63                            List<DepartureBean> list = lookupDepartures(station.getStrain(), "S2");
64                            departureList.addAll(list);    
65                    }              
66                    
67                    Collections.sort( departureList );
68    
69                    
70                  return departureList;                  return departureList;
71          }          }
72                    
# Line 96  public class DepartureFetcher { Line 82  public class DepartureFetcher {
82                                    
83                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();
84                                    
85              final WebClient webClient = new WebClient();              final WebClient webClient = new WebClient( BrowserVersion.FIREFOX_3 );
86              webClient.setTimeout(2500);              webClient.setTimeout(2500);
87              webClient.setJavaScriptEnabled(false);              webClient.setJavaScriptEnabled(false);
88                                                            
# Line 152  public class DepartureFetcher { Line 138  public class DepartureFetcher {
138              } else {              } else {
139                  logger.warning("No departures found for station=" + stationcode + ", type=" + type);                  logger.warning("No departures found for station=" + stationcode + ", type=" + type);
140              }              }
141                webClient.closeAllWindows();
142                            
143              return departureList;              return departureList;
144          }          }
# Line 160  public class DepartureFetcher { Line 147  public class DepartureFetcher {
147                                    
148                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();
149                                    
150              final WebClient webClient = new WebClient();              final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3);
151              webClient.setTimeout(2500);              webClient.setTimeout(2500);
152              webClient.setJavaScriptEnabled(false);              webClient.setJavaScriptEnabled(false);
153                            
# Line 190  public class DepartureFetcher { Line 177  public class DepartureFetcher {
177                          DepartureBean departure = new DepartureBean();                          DepartureBean departure = new DepartureBean();
178    
179                          String time = fields.get(0).asText().trim();                          String time = fields.get(0).asText().trim();
180                          logger.info("time:" + time);  
181                          if (time.equals(""))                          if (time.equals(""))
182                                  time = "0:00"; //Bane.dk bug work-around                                  time = "0:00"; //Bane.dk bug work-around
183                          departure.setTime(time);                          departure.setTime(time);
# Line 216  public class DepartureFetcher { Line 203  public class DepartureFetcher {
203              } else {              } else {
204                  logger.warning("No departures found for station=" + stationcode + ", type=" + type);                  logger.warning("No departures found for station=" + stationcode + ", type=" + type);
205              }              }
206                webClient.closeAllWindows();
207                
208                            
209              return departureList;              return departureList;
210          }          }
# Line 264  public class DepartureFetcher { Line 253  public class DepartureFetcher {
253                  }                  }
254                                    
255                                    
                   
256                  return number;                  return number;
257          }          }
258                    

Legend:
Removed from v.582  
changed lines
  Added in v.591

  ViewVC Help
Powered by ViewVC 1.1.20