/[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 352 by torben, Tue Sep 29 13:35:13 2009 UTC revision 699 by torben, Mon May 3 11:19:18 2010 UTC
# Line 4  import java.util.ArrayList; Line 4  import java.util.ArrayList;
4  import java.util.List;  import java.util.List;
5    
6  import org.w3c.dom.Document;  import org.w3c.dom.Document;
7    
8  import org.w3c.dom.Node;  import org.w3c.dom.Node;
9  import org.w3c.dom.NodeList;  import org.w3c.dom.NodeList;
10    
11  import android.util.Log;  import android.util.Log;
12  import dk.thoerup.traininfo.TimetableBean;  import dk.thoerup.traininfo.TimetableBean;
13    import dk.thoerup.traininfo.util.AndroidTimeoutCache;
14  import dk.thoerup.traininfo.util.DownloadUtil;  import dk.thoerup.traininfo.util.DownloadUtil;
15  import dk.thoerup.traininfo.util.XmlUtil;  import dk.thoerup.traininfo.util.XmlUtil;
16    
17  public class XmlTimetableProvider implements TimetableProvider {  public class XmlTimetableProvider implements TimetableProvider {
18                    
19          List<TimetableBean> timetables = new ArrayList<TimetableBean>();          final static int CACHE_TIMEOUT = 60*1000;
20            
21            List<TimetableBean> timetables;
22    
23            AndroidTimeoutCache<String,List<TimetableBean>> departureCache = new AndroidTimeoutCache<String,List<TimetableBean>>(CACHE_TIMEOUT);
24            
25          @Override          @Override
26          public List<TimetableBean> getTimetable() {          public List<TimetableBean> getTimetable(String type, String trainID) {
27                  return timetables;                  String key = type + "-" + trainID;
28                    List<TimetableBean> list = departureCache.get(key);
29                    
30                    if (list == null) {                    
31                            list = new ArrayList<TimetableBean>();
32                    }
33                    
34                    return list;
35          }          }
36    
37          @Override          @Override
38          public boolean lookupTimetable(String trainID) {          public boolean lookupTimetable(String type, String trainID) {
39                    boolean success;
40                    
41                    String trainNumber = extractTrainNumber(trainID);
42                    
43                    String key = type + "-" + trainID;
44                    timetables = departureCache.get(key);
45            
46                    if (timetables == null) {                      
47                            success = lookupTimetableWorker(type, trainNumber);
48                            
49                            if (success) {                  
50                                    departureCache.put(key, timetables);
51                            }
52                            
53                    } else {
54                            Log.i("XmlTimetableProvider", "cache hit !!!");
55                            success = true;
56                    }              
57                    
58                    return success;
59            }
60            
61    
62            public boolean lookupTimetableWorker(String type, String trainNumber) {
63                  boolean success = false;                  boolean success = false;
64                  String url = "http://app.t-hoerup.dk/TrainInfoServiceTest/TimetableServlet?train=" + trainID ;                  String url = XmlUtil.SERVICE_BASE + "/TimetableServlet?train=" + trainNumber + "&type=" + type;
65                  Log.i("url", url);                  Log.i("url", url);
66                  try {                  try {
67                          timetables.clear();                          timetables = new ArrayList<TimetableBean>();
68                                                    
69                          String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1");                          String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1");
70                                                    
# Line 45  public class XmlTimetableProvider implem Line 82  public class XmlTimetableProvider implem
82                    
83                                  TimetableBean timetable = new TimetableBean();                                  TimetableBean timetable = new TimetableBean();
84                                                                    
85                                  NodeList entries = entryNode.getChildNodes();                                  NodeList entries = entryNode.getChildNodes();                          
86                                    
87                                    if (entryNode.hasAttributes() && entryNode.getAttributes().getNamedItem("current") != null) {                                  
88                                            timetable.setCurrent( Boolean.parseBoolean(entryNode.getAttributes().getNamedItem("current").getNodeValue()));
89                                    } else {
90                                            timetable.setCurrent(false);
91                                    }
92                                    
93                                  for (int j=0; j<entries.getLength(); j++) {                                      for (int j=0; j<entries.getLength(); j++) {    
94                                          Node current = entries.item(j);                                          Node current = entries.item(j);
95                                                                                                                                                                    
# Line 64  public class XmlTimetableProvider implem Line 108  public class XmlTimetableProvider implem
108                                          if (nodeName.equals("departure"))                                          if (nodeName.equals("departure"))
109                                                  timetable.setDeparture( content );                                                  timetable.setDeparture( content );
110                                                                                    
111                                          if (nodeName.equals("current"))                                          /*if (nodeName.equals("current"))
112                                                  timetable.setCurrent( Boolean.parseBoolean(content) );                                                  timetable.setCurrent( Boolean.parseBoolean(content) );*/
113    
114                                  }                                  }
115                                  timetables.add(timetable);                                                                timetables.add(timetable);                              
# Line 76  public class XmlTimetableProvider implem Line 120  public class XmlTimetableProvider implem
120                  } catch (Exception e) {                  } catch (Exception e) {
121                          Log.e("XmlStationProvider", "lookupStations: ", e);                          Log.e("XmlStationProvider", "lookupStations: ", e);
122                  }                  }
123    
124                  return success;                  return success;
125    
126          }          }
127            
128            private String extractTrainNumber (String trainID) {
129    
130                    String parts[]  = trainID.split(" ");
131                    if (parts.length == 2) {
132                            return parts[1];
133                    } else {
134                            return parts[0];
135                    }
136            }
137    
138  }  }

Legend:
Removed from v.352  
changed lines
  Added in v.699

  ViewVC Help
Powered by ViewVC 1.1.20