/[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 421 by torben, Thu Oct 8 12:19:42 2009 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;
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;
13  import com.gargoylesoftware.htmlunit.html.HtmlPage;  import com.gargoylesoftware.htmlunit.html.HtmlPage;
14    
15  import dk.thoerup.curcuitbreaker.CircuitBreaker;  import dk.thoerup.circuitbreaker.CircuitBreaker;
16  import dk.thoerup.curcuitbreaker.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          TimeoutCache<Integer, List<DepartureBean>> cache = new TimeoutCache<Integer,List<DepartureBean>>(120 * 1000);          Map<Integer, List<DepartureBean>> cache;
25            
26            StationDAO stationDao = new StationDAO();
27            
28            private boolean useTempSite;
29            
30            public DepartureFetcher(boolean tempSite, int cacheTimeout) {
31                    useTempSite = tempSite;
32                    cache = new TimeoutMap<Integer,List<DepartureBean>>(cacheTimeout);
33            }
34            
35                    
36                                    
37                    
38          public List<DepartureBean> cachedLookupDepartures(int stationID) throws Throwable {          public List<DepartureBean> cachedLookupDepartures(int stationID) throws Exception {
39    
40                  List<DepartureBean> list = cache.get(stationID);                  List<DepartureBean> list = cache.get(stationID);
41                                    
# Line 39  public class DepartureFetcher { Line 49  public class DepartureFetcher {
49          }          }
50                                    
51    
52          public List<DepartureBean> lookupDepartures(int stationID) throws Throwable {          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                    
73          public List<DepartureBean> lookupDepartures(String stationcode, String type) throws Throwable {          public List<DepartureBean> lookupDepartures(String stationcode, String type) throws Exception {
74                    if (useTempSite == false) {
75                            return lookupDeparturesNormalSite(stationcode, type);
76                    } else {
77                            return lookupDeparturesFromTemporarySite(stationcode, type);
78                    }
79            }
80            
81            public List<DepartureBean> lookupDeparturesNormalSite(String stationcode, String type) throws Exception {
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 136  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;
144            }
145            
146            public List<DepartureBean> lookupDeparturesFromTemporarySite(String stationcode, String type) throws Exception {
147                    
148                    List<DepartureBean> departureList = new ArrayList<DepartureBean>();
149                    
150                final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3);
151                webClient.setTimeout(2500);
152                webClient.setJavaScriptEnabled(false);
153                
154    
155                String uri = "http://bane.dk/lite/station.asp?w=" + type + "&s=" + stationcode;
156                
157                BanedkInvocation wrapper = new BanedkInvocation(webClient, uri);
158                CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
159                
160                HtmlPage page = (HtmlPage) breaker.invoke(wrapper);
161                
162                HtmlElement table = page.getElementById("traf_afgang");
163                
164                if (table != null) {                        
165                        DomNodeList<HtmlElement> tableRows =  table.getElementsByTagName("tr");
166                        
167                        boolean isFirst = true;
168                        
169                        for (HtmlElement currentRow : tableRows) {
170                            if (isFirst == true) { //skip table headers
171                                    isFirst = false;
172                                    continue;
173                            }
174                            
175                            DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td");
176    
177                            DepartureBean departure = new DepartureBean();
178    
179                            String time = fields.get(0).asText().trim();
180    
181                            if (time.equals(""))
182                                    time = "0:00"; //Bane.dk bug work-around
183                            departure.setTime(time);
184    
185    
186                            String trainNumber = fields.get(1).asText();
187                            departure.setTrainNumber(trainNumber);
188    
189                            String destination = fields.get(2).asText();
190                            departure.setDestination(destination);
191    
192                            String origin = fields.get(3).asText();
193                            departure.setOrigin(origin);
194    
195                            String status = fields.get(4).asText();
196                            departure.setStatus(status);
197    
198                            String note = fields.get(5).asText();
199                            departure.setNote(note);
200    
201                            departureList.add(departure);
202                        }
203                } else {
204                    logger.warning("No departures found for station=" + stationcode + ", type=" + type);
205                }
206                webClient.closeAllWindows();
207                
208                            
209              return departureList;              return departureList;
210          }          }
211    
212                    
213          private int extractUpdated(HtmlElement updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif"          private int extractUpdated(HtmlElement updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif"
214                  int updated = -1;                  int updated = -1;
# Line 183  public class DepartureFetcher { Line 253  public class DepartureFetcher {
253                  }                  }
254                                    
255                                    
                   
256                  return number;                  return number;
257          }          }
258                    
259          //test          //test
260          public static void main(String args[]) throws Throwable {          /*
261            public static void main(String args[]) throws Exception {
262                  DepartureFetcher f = new DepartureFetcher();                  DepartureFetcher f = new DepartureFetcher();
263                  List<DepartureBean> deps = f.lookupDepartures("AR", "FJRN");                  List<DepartureBean> deps = f.lookupDepartures("AR", "FJRN");
264                  for(DepartureBean d : deps) {                  for(DepartureBean d : deps) {
# Line 197  public class DepartureFetcher { Line 267  public class DepartureFetcher {
267                  }                  }
268                                    
269                  System.out.println("--------------------------");                  System.out.println("--------------------------");
270          }          }*/
271  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20