--- android/TrainInfoService/src/dk/thoerup/traininfoservice/LocateStations.java 2009/10/09 09:06:43 430 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/LocateStations.java 2009/10/10 08:33:27 431 @@ -5,6 +5,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; + import java.util.logging.Level; import java.util.logging.Logger; @@ -13,8 +14,6 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import dk.thoerup.traininfoservice.DBConnection; - /** * Servlet implementation class LocateStations */ @@ -22,68 +21,77 @@ private static final long serialVersionUID = 1L; Logger logger = Logger.getLogger( LocateStations.class.toString() ); + + - /** - * @see HttpServlet#HttpServlet() - */ - public LocateStations() { - super(); - // TODO Auto-generated constructor stub + protected String transformToIntList(String input) { + String strings[] = input.split(","); + + StringBuffer sb = new StringBuffer(); + sb.append("("); + for (int i = 0; i0) { + sb.append(","); + } + sb.append( Integer.parseInt(strings[i])); //by doing the integer conversion we ensure that it really is a integer + } + sb.append(")"); + return sb.toString(); } - public enum Requested { - BY_NAME, - BY_LOCATION, - NONE - } + protected PreparedStatement createStatement(Connection conn, HttpServletRequest req) throws SQLException { - protected String getStations(Connection conn, double latitude, double longitude, String name, Requested method) throws SQLException { - String SQL = ""; PreparedStatement stmt = null; + String SQL; + + if (req.getParameter("latitude") != null && req.getParameter("latitude") != null) { + //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 enabled = true " + + " ) AS trainstations2 " + + "ORDER BY calcdist ASC " + + "LIMIT 4 "; + double latitude = Double.parseDouble( req.getParameter("latitude") ); + double longitude = Double.parseDouble( req.getParameter("longitude") ); + stmt = conn.prepareStatement(SQL); + stmt.setDouble(1, latitude); + stmt.setDouble(2, longitude); + } else if (req.getParameter("name") != null) { + SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,0.0 " + + "FROM trainstations " + + "WHERE name ILIKE ? AND enabled = true " + + "ORDER BY name "; + + String name = req.getParameter("name").trim(); + stmt = conn.prepareStatement(SQL); + stmt.setString(1, name + "%"); + } else if (req.getParameter("list") != null) { + String list = transformToIntList( req.getParameter("list")); + SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,0.0 " + + "FROM trainstations " + + "WHERE id IN " + list + " AND enabled = true " + + "ORDER BY name "; + stmt = conn.prepareStatement(SQL); + } + + return stmt; + } + + + protected String formatResultset(PreparedStatement stmt) throws SQLException { ResultSet res = null; StringBuffer buff = new StringBuffer(); buff.append("\n"); buff.append("\n"); - + try { - - 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 enabled = true AND 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); - - 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: - // This should not be possible - logger.severe("getStations(): default switch case"); - } - - - - res = stmt.executeQuery(); while (res.next()) { @@ -114,49 +122,30 @@ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - 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") ); - longitude = Double.parseDouble( request.getParameter("longitude") ); - method = Requested.BY_LOCATION; - } - - if (request.getParameter("name") != null) { - name = request.getParameter("name").trim(); - method = Requested.BY_NAME; - } - - - if (method != Requested.NONE) { - - Connection conn = null; - try { - conn = DBConnection.getConnection(); - - String xml = getStations(conn, latitude, longitude, name, method); + Connection conn = null; + try { + conn = DBConnection.getConnection(); + + PreparedStatement stmt = createStatement(conn, request); + if (stmt != null){ + String xml = formatResultset(stmt); response.setContentType("text/xml"); response.getWriter().print(xml); - - conn.close(); - conn = null; - - } catch (Exception e) { - logger.log(Level.SEVERE, "Exception while finding stations", e); - response.sendError(500); - } finally { - try { - if (conn != null) - conn.close(); - } catch (Throwable t) {} + } else { + response.sendError(400, "not enough parameters"); } - } else { - response.sendError(400, "not enough parameters"); + + + } catch (Exception e) { + logger.log(Level.SEVERE, "Exception while finding stations", e); + response.sendError(500); + } finally { + try { + if (conn != null) { + conn.close(); + } + } catch (Throwable t) {} } }