1 |
torben |
292 |
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 |
torben |
293 |
" SELECT name,latitude,longitude,stationcode, " + |
34 |
torben |
292 |
" earth_distance( ll_to_earth(latitude,longitude), ll_to_earth(?,?))::int AS calcdist " + |
35 |
|
|
" FROM trainstations " + |
36 |
torben |
301 |
" WHERE latitude IS NOT NULL AND longitude IS NOT NULL " + |
37 |
torben |
292 |
" ) AS trainstations2 " + |
38 |
|
|
"ORDER BY calcdist ASC " + |
39 |
|
|
"LIMIT 4 "; |
40 |
|
|
|
41 |
|
|
System.out.println(SQL); |
42 |
|
|
|
43 |
|
|
PreparedStatement stmt = null; |
44 |
|
|
ResultSet res = null; |
45 |
|
|
|
46 |
|
|
StringBuffer buff = new StringBuffer(); |
47 |
|
|
|
48 |
|
|
buff.append("<?xml version=\"1.0\" ?>\n"); |
49 |
|
|
buff.append("<stations>\n"); |
50 |
|
|
try |
51 |
|
|
{ |
52 |
|
|
stmt = conn.prepareStatement(SQL); |
53 |
|
|
stmt.setDouble(1, latitude); |
54 |
|
|
stmt.setDouble(2, longitude); |
55 |
|
|
|
56 |
|
|
res = stmt.executeQuery(); |
57 |
|
|
|
58 |
|
|
while (res.next()) { |
59 |
|
|
buff.append("<station>\n"); |
60 |
|
|
|
61 |
|
|
buff.append("<name>").append( res.getString(1) ) .append("</name>\n"); |
62 |
|
|
buff.append("<latitude>").append( res.getDouble(2) ) .append("</latitude>\n"); |
63 |
torben |
293 |
buff.append("<longitude>").append( res.getDouble(3) ) .append("</longitude>\n"); |
64 |
|
|
buff.append("<stationcode>").append( res.getString(4) ) .append("</stationcode>\n"); |
65 |
|
|
buff.append("<calcdist>").append( res.getInt(5) ) .append("</calcdist>\n"); |
66 |
torben |
292 |
|
67 |
|
|
buff.append("</station>\n"); |
68 |
|
|
} |
69 |
|
|
} finally { |
70 |
|
|
if (res != null && !res.isClosed()) |
71 |
|
|
res.close(); |
72 |
|
|
if (stmt != null && !stmt.isClosed()) |
73 |
|
|
stmt.close(); |
74 |
|
|
} |
75 |
|
|
buff.append("</stations>\n"); |
76 |
|
|
return buff.toString(); |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
80 |
|
|
|
81 |
|
|
double latitude = Double.parseDouble( request.getParameter("latitude") ); |
82 |
|
|
double longitude = Double.parseDouble( request.getParameter("longitude") ); |
83 |
|
|
|
84 |
|
|
Connection conn = null; |
85 |
|
|
try { |
86 |
|
|
conn = DBConnection.getConnection(); |
87 |
|
|
|
88 |
|
|
String xml = getStations(conn, latitude, longitude); |
89 |
|
|
|
90 |
|
|
response.setContentType("text/xml"); |
91 |
|
|
response.getWriter().print(xml); |
92 |
|
|
|
93 |
|
|
conn.close(); |
94 |
|
|
conn = null; |
95 |
|
|
|
96 |
|
|
} catch (Exception e) { |
97 |
|
|
throw new ServletException(e); |
98 |
|
|
} finally { |
99 |
|
|
try { |
100 |
|
|
if (conn != null) |
101 |
|
|
conn.close(); |
102 |
|
|
} catch (Throwable t) {} |
103 |
|
|
} |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
} |