/[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 711 by torben, Wed May 5 20:11:03 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    import dk.thoerup.traininfoservice.Statistics;
20    
21  public class DepartureFetcher {  public class DepartureFetcher {
22                    
23          Logger logger = Logger.getLogger(DepartureFetcher.class.getName());          Logger logger = Logger.getLogger(DepartureFetcher.class.getName());
24                    
25          Map<Integer, List<DepartureBean>> cache = new TimeoutMap<Integer,List<DepartureBean>>(120 * 1000);          Map<Integer, List<DepartureBean>> cache;
26            
27            StationDAO stationDao = new StationDAO();
28                    
29          private boolean useTempSite;          private boolean useTempSite;
30                    
31          public DepartureFetcher(boolean tempSite) {          public DepartureFetcher(boolean tempSite, int cacheTimeout) {
32                  useTempSite = tempSite;                  useTempSite = tempSite;
33                    cache = new TimeoutMap<Integer,List<DepartureBean>>(cacheTimeout);
34          }          }
35                    
36                    
# Line 41  public class DepartureFetcher { Line 44  public class DepartureFetcher {
44                          list = lookupDepartures(stationID);                          list = lookupDepartures(stationID);
45                          cache.put(stationID, list);                          cache.put(stationID, list);
46                  } else {                  } else {
47                            Statistics.getInstance().incrementDepartureCacheHits();
48                          logger.info("Departure: Cache hit " + stationID); //remove before production                          logger.info("Departure: Cache hit " + stationID); //remove before production
49                  }                  }
50                  return list;                  return list;
# Line 50  public class DepartureFetcher { Line 54  public class DepartureFetcher {
54          public List<DepartureBean> lookupDepartures(int stationID) throws Exception {          public List<DepartureBean> lookupDepartures(int stationID) throws Exception {
55                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();
56                                    
57                  Connection conn = null;                  StationBean station = stationDao.getById(stationID);
58                  try                  
59                  {                  if (station.getRegional() != null) {
60                          conn = DBConnection.getConnection();                          List<DepartureBean> list = lookupDepartures(station.getRegional(), "FJRN");
61                                            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();  
                         }  
62                  }                  }
63                                    
64                    if (station.getStrain() != null) {
65                            List<DepartureBean> list = lookupDepartures(station.getStrain(), "S2");
66                            departureList.addAll(list);    
67                    }              
68                    
69                    Collections.sort( departureList );
70    
71                    
72                  return departureList;                  return departureList;
73          }          }
74                    
# Line 96  public class DepartureFetcher { Line 84  public class DepartureFetcher {
84                                    
85                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();
86                                    
87              final WebClient webClient = new WebClient();              final WebClient webClient = new WebClient( BrowserVersion.FIREFOX_3 );
88              webClient.setTimeout(2500);              webClient.setTimeout(2500);
89              webClient.setJavaScriptEnabled(false);              webClient.setJavaScriptEnabled(false);
90                                                            
# Line 146  public class DepartureFetcher { Line 134  public class DepartureFetcher {
134                                  String note = extractNote( fields.get(7) );                                  String note = extractNote( fields.get(7) );
135                                  departure.setNote(note);                                  departure.setNote(note);
136                                                                    
137                                    departure.setType(type);
138                                    
139                                  departureList.add(departure);                                  departureList.add(departure);
140                          }                          }
141                      }                      }
142              } else {              } else {
143                  logger.warning("No departures found for station=" + stationcode + ", type=" + type);                  logger.warning("No departures found for station=" + stationcode + ", type=" + type);
144              }              }
145                webClient.closeAllWindows();
146                            
147              return departureList;              return departureList;
148          }          }
# Line 160  public class DepartureFetcher { Line 151  public class DepartureFetcher {
151                                    
152                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();
153                                    
154              final WebClient webClient = new WebClient();              final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3);
155              webClient.setTimeout(2500);              webClient.setTimeout(2500);
156              webClient.setJavaScriptEnabled(false);              webClient.setJavaScriptEnabled(false);
157                            
# Line 190  public class DepartureFetcher { Line 181  public class DepartureFetcher {
181                          DepartureBean departure = new DepartureBean();                          DepartureBean departure = new DepartureBean();
182    
183                          String time = fields.get(0).asText().trim();                          String time = fields.get(0).asText().trim();
184                          logger.info("time:" + time);  
185                          if (time.equals(""))                          if (time.equals(""))
186                                  time = "0:00"; //Bane.dk bug work-around                                  time = "0:00"; //Bane.dk bug work-around
187                          departure.setTime(time);                          departure.setTime(time);
# Line 216  public class DepartureFetcher { Line 207  public class DepartureFetcher {
207              } else {              } else {
208                  logger.warning("No departures found for station=" + stationcode + ", type=" + type);                  logger.warning("No departures found for station=" + stationcode + ", type=" + type);
209              }              }
210                webClient.closeAllWindows();
211                
212                            
213              return departureList;              return departureList;
214          }          }
# Line 264  public class DepartureFetcher { Line 257  public class DepartureFetcher {
257                  }                  }
258                                    
259                                    
                   
260                  return number;                  return number;
261          }          }
262                    

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

  ViewVC Help
Powered by ViewVC 1.1.20