--- android/TrainInfoService/src/dk/thoerup/traininfoservice/LocateStations.java 2009/09/13 20:17:32 321 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/LocateStations.java 2009/09/14 07:29:59 322 @@ -26,10 +26,16 @@ super(); // TODO Auto-generated constructor stub } + - protected String getStations(Connection conn, double latitude, double longitude) throws SQLException { + + protected String getStations(Connection conn, double latitude, double longitude, String name) 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 - final String SQL = "SELECT * FROM ( "+ + 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 " + @@ -37,6 +43,12 @@ " ) 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); @@ -50,8 +62,13 @@ try { stmt = conn.prepareStatement(SQL); - stmt.setDouble(1, latitude); - stmt.setDouble(2, longitude); + if (latitude >= 0 && longitude >= 0) { + stmt.setDouble(1, latitude); + stmt.setDouble(2, longitude); + } + if (name != null) { + stmt.setString(1, "%" + name + "%"); + } res = stmt.executeQuery(); @@ -82,14 +99,21 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - double latitude = Double.parseDouble( request.getParameter("latitude") ); - double longitude = Double.parseDouble( request.getParameter("longitude") ); + double latitude = -1.0; + if (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); + String xml = getStations(conn, latitude, longitude, name); response.setContentType("text/xml"); response.getWriter().print(xml);