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

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

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

revision 468 by torben, Thu Oct 22 06:04:45 2009 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    
3  import java.io.IOException;  import java.io.IOException;
 import java.sql.Connection;  
 import java.sql.ResultSet;  
 import java.sql.Statement;  
4  import java.util.List;  import java.util.List;
5  import java.util.logging.Level;  import java.util.logging.Level;
6  import java.util.logging.Logger;  import java.util.logging.Logger;
# Line 25  import org.w3c.dom.Document; Line 22  import org.w3c.dom.Document;
22  import org.w3c.dom.Element;  import org.w3c.dom.Element;
23    
24  import dk.thoerup.circuitbreaker.CircuitBreakerException;  import dk.thoerup.circuitbreaker.CircuitBreakerException;
25  import dk.thoerup.traininfoservice.DBConnection;  import dk.thoerup.traininfoservice.StationDAO;
26    import dk.thoerup.traininfoservice.Statistics;
27    
28  /**  /**
29   * Servlet implementation class DepartureServlet   * Servlet implementation class DepartureServlet
# Line 35  public class DepartureServlet extends Ht Line 33  public class DepartureServlet extends Ht
33                    
34          Logger logger = Logger.getLogger( DepartureServlet.class.getName() );          Logger logger = Logger.getLogger( DepartureServlet.class.getName() );
35    
36          DepartureFetcher fetcher = new DepartureFetcher();          DepartureFetcher fetcher;
37      TransformerFactory transformerFactory = TransformerFactory.newInstance();      TransformerFactory transformerFactory = TransformerFactory.newInstance();
38          DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();          DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
39                    
40    
41          @Override          @Override
42          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {          public void init() throws ServletException {
43                    super.init();
44    
45                    boolean useTempSite =  Boolean.parseBoolean( getServletContext().getInitParameter("usetempsite") );
46                    int cacheTimeout =  Integer.parseInt( getServletContext().getInitParameter("cache_timeout") );
47                    logger.info( "DepartureServlet, use temp site=" + useTempSite + ", cache=" + cacheTimeout);
48                    fetcher = new DepartureFetcher(useTempSite, cacheTimeout);
49            }
50    
51            @Override
52            protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {          
53                  if (req.getParameter("station") == null) {                  if (req.getParameter("station") == null) {
54                          resp.sendError(400, "not enough parameters");                          resp.sendError(400, "not enough parameters");
55                          return;                          return;
56                  }                  }
57                    Statistics.getInstance().incrementDepartureLookups();
58                                    
59                  int station = Integer.parseInt( req.getParameter("station") );                  int station = Integer.parseInt( req.getParameter("station") );
60                  String format = req.getParameter("format");                  String format = req.getParameter("format");
61    
62                  List<DepartureBean> beans;                  List<DepartureBean> beans;
63    
64                  String stationName = getStationName(station);                  String stationName = StationDAO.getStationName(station);
65    
66                  try {                  try {
67                          beans = fetcher.cachedLookupDepartures(station);                          beans = fetcher.cachedLookupDepartures(station);
# Line 77  public class DepartureServlet extends Ht Line 86  public class DepartureServlet extends Ht
86                          resp.setContentType("text/xml");                          resp.setContentType("text/xml");
87                          resp.getWriter().print( formatXml(beans, stationName) );                          resp.getWriter().print( formatXml(beans, stationName) );
88                  } else if (format.equalsIgnoreCase("html")) {                  } else if (format.equalsIgnoreCase("html")) {
89                            
90                            String advStr = req.getParameter("advanced");
91                            boolean advanced = advStr != null ? Boolean.parseBoolean(advStr) : false;
92                            
93                            req.setAttribute("advanced", advanced);
94                          req.setAttribute("stationname", stationName );                          req.setAttribute("stationname", stationName );
95                          req.setAttribute("departurebeans", beans);                          req.setAttribute("departurebeans", beans);
96                          req.setAttribute("stationID", station );                          req.setAttribute("stationID", station );
# Line 132  public class DepartureServlet extends Ht Line 146  public class DepartureServlet extends Ht
146                          note.setTextContent( departure.getNote() );                          note.setTextContent( departure.getNote() );
147                          train.appendChild(note);                          train.appendChild(note);
148                                                    
149                            Element type = doc.createElement("type");
150                            type.setTextContent( departure.getType() );
151                            train.appendChild(type);
152                            
153                          root.appendChild(train);                          root.appendChild(train);
154                  }                  }
155                                    
# Line 158  public class DepartureServlet extends Ht Line 176  public class DepartureServlet extends Ht
176                  return xml;                  return xml;
177          }          }
178    
         protected String getStationName(int stationID) {  
                 String station = "";  
   
                 Connection conn = null;  
                 try {  
                         conn = DBConnection.getConnection();  
                         Statement stmt = conn.createStatement();  
                         ResultSet rs = stmt.executeQuery("SELECT name FROM trainstations WHERE id=" + stationID);  
                         if (rs.next()) {  
                                 station = rs.getString(1);  
                         }  
   
                 } catch (Exception e) {  
                 } finally {  
                         try {  
                                 if (conn != null && !conn.isClosed())  
                                         conn.close();  
                         } catch (Exception e) {}  
                 }  
   
                 return station;  
         }  
   
179  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20