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

Diff of /android/TrainInfoService/src/dk/thoerup/traininfoservice/LocateStations.java

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

revision 322 by torben, Mon Sep 14 07:29:59 2009 UTC revision 425 by torben, Thu Oct 8 20:46:40 2009 UTC
# Line 5  import java.sql.Connection; Line 5  import java.sql.Connection;
5  import java.sql.PreparedStatement;  import java.sql.PreparedStatement;
6  import java.sql.ResultSet;  import java.sql.ResultSet;
7  import java.sql.SQLException;  import java.sql.SQLException;
8    import java.util.logging.Level;
9    import java.util.logging.Logger;
10    
11  import javax.servlet.ServletException;  import javax.servlet.ServletException;
12  import javax.servlet.http.HttpServlet;  import javax.servlet.http.HttpServlet;
# Line 18  import dk.thoerup.traininfoservice.DBCon Line 20  import dk.thoerup.traininfoservice.DBCon
20   */   */
21  public class LocateStations extends HttpServlet {  public class LocateStations extends HttpServlet {
22          private static final long serialVersionUID = 1L;          private static final long serialVersionUID = 1L;
         
     /**  
      * @see HttpServlet#HttpServlet()  
      */  
     public LocateStations() {  
         super();  
         // TODO Auto-generated constructor stub  
     }  
       
23    
24            Logger logger = Logger.getLogger( LocateStations.class.toString() );
25            
26            /**
27             * @see HttpServlet#HttpServlet()
28             */
29            public LocateStations() {
30                    super();
31                    // TODO Auto-generated constructor stub
32            }
33    
34            public enum Requested {
35                    BY_NAME,
36                    BY_LOCATION,
37                    NONE
38            }
39    
40          protected String getStations(Connection conn, double latitude, double longitude, String name) throws SQLException {  
41            protected String getStations(Connection conn, double latitude, double longitude, String name, Requested method) throws SQLException {
42                  String SQL = "";                  String SQL = "";
                   
                 if (latitude >= 0.0 && longitude >= 0.0)  
                 {  
                 //inner select is workaround from not being able to use a calculated column directly in where clause  
                         SQL = "SELECT * FROM ( "+  
                                                    "               SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, " +  
                                                    "                     earth_distance( ll_to_earth(latitude,longitude), ll_to_earth(?,?))::int AS calcdist " +  
                                                    "               FROM trainstations " +  
                                                    "               WHERE latitude IS NOT NULL AND longitude IS NOT NULL " +  
                                                    "       ) AS trainstations2 " +  
                                                "ORDER BY calcdist ASC " +  
                                                "LIMIT 4 ";  
                 } else if (name != null) {  
                         SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,0.0 " +  
                                   "FROM trainstations " +  
                                   "WHERE name ILIKE ? AND latitude IS NOT NULL AND longitude IS NOT NULL " +  
                                   "ORDER BY name ";  
                 } else throw new SQLException("not enough parameters");  
                   
                 System.out.println(SQL);  
                   
43                  PreparedStatement stmt = null;                  PreparedStatement stmt = null;
44                  ResultSet res = null;                  ResultSet res = null;
45                    
46                  StringBuffer buff = new StringBuffer();                  StringBuffer buff = new StringBuffer();
47                    
48                  buff.append("<?xml version=\"1.0\" ?>\n");                  buff.append("<?xml version=\"1.0\" ?>\n");
49                  buff.append("<stations>\n");                  buff.append("<stations>\n");    
50                    
51                  try                  try
52                  {                  {
53                          stmt = conn.prepareStatement(SQL);  
54                          if (latitude >= 0 && longitude >= 0) {                          switch (method)
55                            {
56                            case BY_LOCATION:
57                                    //inner select is workaround from not being able to use a calculated column directly in where clause
58                                    SQL = "SELECT * FROM ( "+
59                                                    "               SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, " +
60                                                    "                     earth_distance( ll_to_earth(latitude,longitude), ll_to_earth(?,?))::int AS calcdist " +
61                                                    "               FROM trainstations " +
62                                                    "               WHERE enabled = true AND latitude IS NOT NULL AND longitude IS NOT NULL " +
63                                                    "       ) AS trainstations2 " +
64                                                    "ORDER BY calcdist ASC " +
65                                                    "LIMIT 4 ";
66                                    stmt = conn.prepareStatement(SQL);
67                                  stmt.setDouble(1, latitude);                                  stmt.setDouble(1, latitude);
68                                  stmt.setDouble(2, longitude);                                  stmt.setDouble(2, longitude);
69    
70                                    break;
71                            case BY_NAME:
72                                    SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,0.0 " +
73                                            "FROM trainstations " +
74                                            "WHERE name ILIKE ? AND latitude IS NOT NULL AND longitude IS NOT NULL " +
75                                            "ORDER BY name ";
76                                    stmt = conn.prepareStatement(SQL);
77                                    stmt.setString(1, name + "%");
78                                    break;
79                            default:
80                                    // This should not be possible
81                                    logger.severe("getStations(): default switch case");
82                          }                          }
83                          if (name != null) {  
84                                  stmt.setString(1, "%" + name + "%");  
85                          }  
86                            
87                          res = stmt.executeQuery();                          res = stmt.executeQuery();
88                            
89                          while (res.next()) {                          while (res.next()) {
90                                  buff.append("<station>\n");                                  buff.append("<station>\n");
91    
# Line 84  public class LocateStations extends Http Line 98  public class LocateStations extends Http
98                                  res.getString(6);                                  res.getString(6);
99                                  buff.append("<stog>").append( !res.wasNull() ) .append("</stog>\n");                                  buff.append("<stog>").append( !res.wasNull() ) .append("</stog>\n");
100                                  buff.append("<calcdist>").append( res.getInt(7) ) .append("</calcdist>\n");                                  buff.append("<calcdist>").append( res.getInt(7) ) .append("</calcdist>\n");
101                                    
102                                  buff.append("</station>\n");                                                              buff.append("</station>\n");                            
103                          }                          }
104                  } finally {                  } finally {
# Line 97  public class LocateStations extends Http Line 111  public class LocateStations extends Http
111                  return buff.toString();                  return buff.toString();
112          }          }
113    
114            @Override
115          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
116                    
117                  double latitude = -1.0;                  double longitude = 0.0;
118                  if (request.getParameter("latitude") != null)                  double latitude = 0.0;
119                    String name = "";
120    
121                    Requested method = Requested.NONE;
122    
123                    if (request.getParameter("latitude") != null && request.getParameter("latitude") != null) {
124                          latitude = Double.parseDouble( request.getParameter("latitude") );                          latitude = Double.parseDouble( request.getParameter("latitude") );
                   
                 double longitude = -1.0;  
                 if (request.getParameter("latitude") != null)  
125                          longitude = Double.parseDouble( request.getParameter("longitude") );                          longitude = Double.parseDouble( request.getParameter("longitude") );
126                                            method = Requested.BY_LOCATION;
127                  String name = request.getParameter("name");                  }
128                                    
129                  Connection conn = null;                  if (request.getParameter("name") != null) {
130                  try {                          name = request.getParameter("name").trim();
131                          conn = DBConnection.getConnection();                          method = Requested.BY_NAME;
132                                            }
133                          String xml = getStations(conn, latitude, longitude, name);  
134                            
135                          response.setContentType("text/xml");                  if (method != Requested.NONE) {
136                          response.getWriter().print(xml);  
137                                                    Connection conn = null;
                         conn.close();  
                         conn = null;  
                           
                 } catch (Exception e) {  
                         throw new ServletException(e);  
                 } finally {  
138                          try {                          try {
139                                  if (conn != null)                                  conn = DBConnection.getConnection();
140                                          conn.close();  
141                          } catch (Throwable t) {}                                  String xml = getStations(conn, latitude, longitude, name, method);
142    
143                                    response.setContentType("text/xml");
144                                    response.getWriter().print(xml);
145    
146                                    conn.close();
147                                    conn = null;
148    
149                            } catch (Exception e) {                        
150                                    logger.log(Level.SEVERE, "Exception while finding stations", e);
151                                    response.sendError(500);                                                
152                            } finally {
153                                    try {
154                                            if (conn != null)
155                                                    conn.close();
156                                    } catch (Throwable t) {}
157                            }
158                    } else {
159                            response.sendError(400, "not enough parameters");
160                  }                  }
161          }          }
162    

Legend:
Removed from v.322  
changed lines
  Added in v.425

  ViewVC Help
Powered by ViewVC 1.1.20