--- android/TrainInfoService/src/dk/thoerup/traininfoservice/LocateStations.java 2009/09/25 07:48:13 346 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/LocateStations.java 2009/09/25 13:54:16 347 @@ -18,59 +18,69 @@ */ public class LocateStations extends HttpServlet { private static final long serialVersionUID = 1L; - - /** - * @see HttpServlet#HttpServlet() - */ - public LocateStations() { - super(); - // TODO Auto-generated constructor stub - } - + /** + * @see HttpServlet#HttpServlet() + */ + public LocateStations() { + super(); + // TODO Auto-generated constructor stub + } + + public enum Requested { + BY_NAME, + BY_LOCATION, + NONE + } - protected String getStations(Connection conn, double latitude, double longitude, String name) throws SQLException { + + protected String getStations(Connection conn, double latitude, double longitude, String name, Requested method) throws SQLException { 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"); - - PreparedStatement stmt = null; ResultSet res = null; - + StringBuffer buff = new StringBuffer(); - + buff.append("\n"); - buff.append("\n"); + buff.append("\n"); + try { - stmt = conn.prepareStatement(SQL); - if (latitude >= 0 && longitude >= 0) { + + switch (method) + { + case BY_LOCATION: + //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 "; + stmt = conn.prepareStatement(SQL); stmt.setDouble(1, latitude); stmt.setDouble(2, longitude); - } - if (name != null) { + + break; + case BY_NAME: + 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 "; + stmt = conn.prepareStatement(SQL); stmt.setString(1, "%" + name + "%"); + break; + default: + throw new SQLException("not enough parameters"); } - + + + + res = stmt.executeQuery(); - + while (res.next()) { buff.append("\n"); @@ -83,7 +93,7 @@ res.getString(6); buff.append("").append( !res.wasNull() ) .append("\n"); buff.append("").append( res.getInt(7) ) .append("\n"); - + buff.append("\n"); } } finally { @@ -97,36 +107,49 @@ } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - - double latitude = -1.0; - if (request.getParameter("latitude") != null) + + double longitude = 0.0; + double latitude = 0.0; + String name = ""; + + Requested method = Requested.NONE; + + if (request.getParameter("latitude") != null && request.getParameter("latitude") != null) { latitude = Double.parseDouble( request.getParameter("latitude") ); - - double longitude = -1.0; - if (request.getParameter("latitude") != null) longitude = Double.parseDouble( request.getParameter("longitude") ); - - String name = request.getParameter("name"); - - Connection conn = null; - try { - conn = DBConnection.getConnection(); - - String xml = getStations(conn, latitude, longitude, name); - - response.setContentType("text/xml"); - response.getWriter().print(xml); - - conn.close(); - conn = null; - - } catch (Exception e) { - throw new ServletException(e); - } finally { + method = Requested.BY_LOCATION; + } + + if (request.getParameter("name") != null) { + name = request.getParameter("name"); + method = Requested.BY_NAME; + } + + + if (method != Requested.NONE) { + + Connection conn = null; try { - if (conn != null) - conn.close(); - } catch (Throwable t) {} + conn = DBConnection.getConnection(); + + String xml = getStations(conn, latitude, longitude, name, method); + + response.setContentType("text/xml"); + response.getWriter().print(xml); + + conn.close(); + conn = null; + + } catch (Exception e) { + throw new ServletException(e); + } finally { + try { + if (conn != null) + conn.close(); + } catch (Throwable t) {} + } + } else { + response.sendError(400, "not enough parameters"); } }