/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlTimetableProvider.java
ViewVC logotype

Diff of /android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlTimetableProvider.java

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

revision 391 by torben, Sat Oct 3 10:55:43 2009 UTC revision 1027 by torben, Wed Sep 8 06:03:45 2010 UTC
# Line 17  import dk.thoerup.traininfo.util.XmlUtil Line 17  import dk.thoerup.traininfo.util.XmlUtil
17  public class XmlTimetableProvider implements TimetableProvider {  public class XmlTimetableProvider implements TimetableProvider {
18                    
19          final static int CACHE_TIMEOUT = 60*1000;          final static int CACHE_TIMEOUT = 60*1000;
           
         List<TimetableBean> timetables;  
20    
21          AndroidTimeoutCache<String,List<TimetableBean>> departureCache = new AndroidTimeoutCache<String,List<TimetableBean>>(CACHE_TIMEOUT);          AndroidTimeoutCache<String,List<TimetableBean>> timetableCache = new AndroidTimeoutCache<String,List<TimetableBean>>(CACHE_TIMEOUT);    
           
         @Override  
         public List<TimetableBean> getTimetable(String trainID) {  
                 List<TimetableBean> list = departureCache.get(trainID);  
                   
                 if (list == null) {                      
                         list = new ArrayList<TimetableBean>();  
                 }  
                   
                 return list;  
         }  
22    
23          @Override          @Override
24          public boolean lookupTimetable(String trainID) {          public List<TimetableBean> lookupTimetable(String type, String trainID) {
25                  boolean success;                  
26                    String trainNumber = extractTrainNumber(trainID);
27                                    
28                  timetables = departureCache.get(trainID);                  String key = type + "-" + trainID;
29                    List<TimetableBean> timetables = timetableCache.get(key);
30                    
31                  if (timetables == null) {                                        if (timetables == null) {                      
32                          success = lookupTimetableWorker(trainID);                          timetables = lookupTimetableWorker(type, trainNumber);
33                                                    
34                          if (success) {                                            if (timetables != null) {                      
35                                  departureCache.put(trainID, timetables);                                  timetableCache.put(key, timetables);
36                          }                          }
37                                                    
38                  } else {                  } else {
39                          Log.i("XmlTimetableProvider", "cache hit !!!");                          Log.i("XmlTimetableProvider", "cache hit !!!");
                         success = true;  
40                  }                                }              
41                                    
42                  return success;                  return timetables;
43          }          }
44                    
45    
46          public boolean lookupTimetableWorker(String trainID) {          public List<TimetableBean> lookupTimetableWorker(String type, String trainNumber) {
47                  boolean success = false;  
48                  String url = XmlUtil.SERVICE_BASE + "/TimetableServlet?train=" + trainID.replace(" ", "%20") ;                  String url = XmlUtil.SERVICE_BASE + "/TimetableServlet?train=" + trainNumber + "&type=" + type;
49                  Log.i("url", url);                  Log.i("url", url);
50                  try {                  try {
51                          timetables = new ArrayList<TimetableBean>();                          List<TimetableBean> timetables = new ArrayList<TimetableBean>();
52                                                    
53                          String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1");                          String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1");
54                                                    
# Line 104  public class XmlTimetableProvider implem Line 92  public class XmlTimetableProvider implem
92                                          if (nodeName.equals("departure"))                                          if (nodeName.equals("departure"))
93                                                  timetable.setDeparture( content );                                                  timetable.setDeparture( content );
94                                                                                    
95                                            if (nodeName.equals("stationid"))
96                                                    timetable.setStationId( Integer.parseInt(content));
97                                            
98                                          /*if (nodeName.equals("current"))                                          /*if (nodeName.equals("current"))
99                                                  timetable.setCurrent( Boolean.parseBoolean(content) );*/                                                  timetable.setCurrent( Boolean.parseBoolean(content) );*/
100    
101                                  }                                  }
102                                  timetables.add(timetable);                                                                timetables.add(timetable);                              
103                          }                          }
104                          success = true;                          return timetables;
105                                                    
106                                                    
107                  } catch (Exception e) {                  } catch (Exception e) {
108                          Log.e("XmlStationProvider", "lookupStations: ", e);                          Log.e("XmlStationProvider", "lookupStations: ", e);
109                  }                  }
                 return success;  
110    
111                    return null;
112            }
113            
114            private String extractTrainNumber (String trainID) {
115    
116                    String parts[]  = trainID.split(" ");
117                    if (parts.length == 2) {
118                            return parts[1];
119                    } else {
120                            return parts[0];
121                    }
122          }          }
123    
124  }  }

Legend:
Removed from v.391  
changed lines
  Added in v.1027

  ViewVC Help
Powered by ViewVC 1.1.20