1 |
package dk.thoerup.traininfoservice; |
2 |
|
3 |
import java.io.IOException; |
4 |
import java.sql.Connection; |
5 |
import java.sql.PreparedStatement; |
6 |
import java.sql.ResultSet; |
7 |
import java.sql.SQLException; |
8 |
|
9 |
import javax.servlet.ServletException; |
10 |
import javax.servlet.http.HttpServlet; |
11 |
import javax.servlet.http.HttpServletRequest; |
12 |
import javax.servlet.http.HttpServletResponse; |
13 |
|
14 |
import dk.thoerup.traininfoservice.DBConnection; |
15 |
|
16 |
/** |
17 |
* Servlet implementation class LocateStations |
18 |
*/ |
19 |
public class LocateStations extends HttpServlet { |
20 |
private static final long serialVersionUID = 1L; |
21 |
|
22 |
/** |
23 |
* @see HttpServlet#HttpServlet() |
24 |
*/ |
25 |
public LocateStations() { |
26 |
super(); |
27 |
// TODO Auto-generated constructor stub |
28 |
} |
29 |
|
30 |
protected String getStations(Connection conn, double latitude, double longitude) throws SQLException { |
31 |
//inner select is workaround from not being able to use a calculated column directly in where clause |
32 |
final String SQL = "SELECT * FROM ( "+ |
33 |
" SELECT name,latitude,longitude,stationcode, " + |
34 |
" earth_distance( ll_to_earth(latitude,longitude), ll_to_earth(?,?))::int AS calcdist " + |
35 |
" FROM trainstations " + |
36 |
" ) AS trainstations2 " + |
37 |
"ORDER BY calcdist ASC " + |
38 |
"LIMIT 4 "; |
39 |
|
40 |
System.out.println(SQL); |
41 |
|
42 |
PreparedStatement stmt = null; |
43 |
ResultSet res = null; |
44 |
|
45 |
StringBuffer buff = new StringBuffer(); |
46 |
|
47 |
buff.append("<?xml version=\"1.0\" ?>\n"); |
48 |
buff.append("<stations>\n"); |
49 |
try |
50 |
{ |
51 |
stmt = conn.prepareStatement(SQL); |
52 |
stmt.setDouble(1, latitude); |
53 |
stmt.setDouble(2, longitude); |
54 |
|
55 |
res = stmt.executeQuery(); |
56 |
|
57 |
while (res.next()) { |
58 |
buff.append("<station>\n"); |
59 |
|
60 |
buff.append("<name>").append( res.getString(1) ) .append("</name>\n"); |
61 |
buff.append("<latitude>").append( res.getDouble(2) ) .append("</latitude>\n"); |
62 |
buff.append("<longitude>").append( res.getDouble(3) ) .append("</longitude>\n"); |
63 |
buff.append("<stationcode>").append( res.getString(4) ) .append("</stationcode>\n"); |
64 |
buff.append("<calcdist>").append( res.getInt(5) ) .append("</calcdist>\n"); |
65 |
|
66 |
buff.append("</station>\n"); |
67 |
} |
68 |
} finally { |
69 |
if (res != null && !res.isClosed()) |
70 |
res.close(); |
71 |
if (stmt != null && !stmt.isClosed()) |
72 |
stmt.close(); |
73 |
} |
74 |
buff.append("</stations>\n"); |
75 |
return buff.toString(); |
76 |
} |
77 |
|
78 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
79 |
|
80 |
double latitude = Double.parseDouble( request.getParameter("latitude") ); |
81 |
double longitude = Double.parseDouble( request.getParameter("longitude") ); |
82 |
|
83 |
Connection conn = null; |
84 |
try { |
85 |
conn = DBConnection.getConnection(); |
86 |
|
87 |
String xml = getStations(conn, latitude, longitude); |
88 |
|
89 |
response.setContentType("text/xml"); |
90 |
response.getWriter().print(xml); |
91 |
|
92 |
conn.close(); |
93 |
conn = null; |
94 |
|
95 |
} catch (Exception e) { |
96 |
throw new ServletException(e); |
97 |
} finally { |
98 |
try { |
99 |
if (conn != null) |
100 |
conn.close(); |
101 |
} catch (Throwable t) {} |
102 |
} |
103 |
} |
104 |
|
105 |
} |