26 |
super(); |
super(); |
27 |
// TODO Auto-generated constructor stub |
// TODO Auto-generated constructor stub |
28 |
} |
} |
29 |
|
|
30 |
|
|
31 |
protected String getStations(Connection conn, double latitude, double longitude) throws SQLException { |
|
32 |
|
protected String getStations(Connection conn, double latitude, double longitude, String name) throws SQLException { |
33 |
|
String SQL = ""; |
34 |
|
|
35 |
|
if (latitude >= 0.0 && longitude >= 0.0) |
36 |
|
{ |
37 |
//inner select is workaround from not being able to use a calculated column directly in where clause |
//inner select is workaround from not being able to use a calculated column directly in where clause |
38 |
final String SQL = "SELECT * FROM ( "+ |
SQL = "SELECT * FROM ( "+ |
39 |
" SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, " + |
" SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, " + |
40 |
" earth_distance( ll_to_earth(latitude,longitude), ll_to_earth(?,?))::int AS calcdist " + |
" earth_distance( ll_to_earth(latitude,longitude), ll_to_earth(?,?))::int AS calcdist " + |
41 |
" FROM trainstations " + |
" FROM trainstations " + |
43 |
" ) AS trainstations2 " + |
" ) AS trainstations2 " + |
44 |
"ORDER BY calcdist ASC " + |
"ORDER BY calcdist ASC " + |
45 |
"LIMIT 4 "; |
"LIMIT 4 "; |
46 |
|
} else if (name != null) { |
47 |
|
SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,0.0 " + |
48 |
|
"FROM trainstations " + |
49 |
|
"WHERE name ILIKE ? AND latitude IS NOT NULL AND longitude IS NOT NULL " + |
50 |
|
"ORDER BY name "; |
51 |
|
} else throw new SQLException("not enough parameters"); |
52 |
|
|
53 |
System.out.println(SQL); |
System.out.println(SQL); |
54 |
|
|
62 |
try |
try |
63 |
{ |
{ |
64 |
stmt = conn.prepareStatement(SQL); |
stmt = conn.prepareStatement(SQL); |
65 |
stmt.setDouble(1, latitude); |
if (latitude >= 0 && longitude >= 0) { |
66 |
stmt.setDouble(2, longitude); |
stmt.setDouble(1, latitude); |
67 |
|
stmt.setDouble(2, longitude); |
68 |
|
} |
69 |
|
if (name != null) { |
70 |
|
stmt.setString(1, "%" + name + "%"); |
71 |
|
} |
72 |
|
|
73 |
res = stmt.executeQuery(); |
res = stmt.executeQuery(); |
74 |
|
|
99 |
|
|
100 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
101 |
|
|
102 |
double latitude = Double.parseDouble( request.getParameter("latitude") ); |
double latitude = -1.0; |
103 |
double longitude = Double.parseDouble( request.getParameter("longitude") ); |
if (request.getParameter("latitude") != null) |
104 |
|
latitude = Double.parseDouble( request.getParameter("latitude") ); |
105 |
|
|
106 |
|
double longitude = -1.0; |
107 |
|
if (request.getParameter("latitude") != null) |
108 |
|
longitude = Double.parseDouble( request.getParameter("longitude") ); |
109 |
|
|
110 |
|
String name = request.getParameter("name"); |
111 |
|
|
112 |
Connection conn = null; |
Connection conn = null; |
113 |
try { |
try { |
114 |
conn = DBConnection.getConnection(); |
conn = DBConnection.getConnection(); |
115 |
|
|
116 |
String xml = getStations(conn, latitude, longitude); |
String xml = getStations(conn, latitude, longitude, name); |
117 |
|
|
118 |
response.setContentType("text/xml"); |
response.setContentType("text/xml"); |
119 |
response.getWriter().print(xml); |
response.getWriter().print(xml); |