18 |
*/ |
*/ |
19 |
public class LocateStations extends HttpServlet { |
public class LocateStations extends HttpServlet { |
20 |
private static final long serialVersionUID = 1L; |
private static final long serialVersionUID = 1L; |
21 |
|
|
22 |
/** |
/** |
23 |
* @see HttpServlet#HttpServlet() |
* @see HttpServlet#HttpServlet() |
24 |
*/ |
*/ |
25 |
public LocateStations() { |
public LocateStations() { |
26 |
super(); |
super(); |
27 |
// TODO Auto-generated constructor stub |
// TODO Auto-generated constructor stub |
28 |
} |
} |
29 |
|
|
30 |
protected String getStations(Connection conn, double latitude, double longitude) throws SQLException { |
public enum Requested { |
31 |
//inner select is workaround from not being able to use a calculated column directly in where clause |
BY_NAME, |
32 |
final String SQL = "SELECT * FROM ( "+ |
BY_LOCATION, |
33 |
" SELECT name,latitude,longitude,address,stationcode, " + |
NONE |
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 " + |
protected String getStations(Connection conn, double latitude, double longitude, String name, Requested method) throws SQLException { |
38 |
"LIMIT 4 "; |
String SQL = ""; |
|
|
|
|
System.out.println(SQL); |
|
|
|
|
39 |
PreparedStatement stmt = null; |
PreparedStatement stmt = null; |
40 |
ResultSet res = null; |
ResultSet res = null; |
41 |
|
|
42 |
StringBuffer buff = new StringBuffer(); |
StringBuffer buff = new StringBuffer(); |
43 |
|
|
44 |
buff.append("<?xml version=\"1.0\" ?>\n"); |
buff.append("<?xml version=\"1.0\" ?>\n"); |
45 |
buff.append("<stations>\n"); |
buff.append("<stations>\n"); |
46 |
|
|
47 |
try |
try |
48 |
{ |
{ |
49 |
stmt = conn.prepareStatement(SQL); |
|
50 |
stmt.setDouble(1, latitude); |
switch (method) |
51 |
stmt.setDouble(2, longitude); |
{ |
52 |
|
case BY_LOCATION: |
53 |
|
//inner select is workaround from not being able to use a calculated column directly in where clause |
54 |
|
SQL = "SELECT * FROM ( "+ |
55 |
|
" SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, " + |
56 |
|
" earth_distance( ll_to_earth(latitude,longitude), ll_to_earth(?,?))::int AS calcdist " + |
57 |
|
" FROM trainstations " + |
58 |
|
" WHERE latitude IS NOT NULL AND longitude IS NOT NULL " + |
59 |
|
" ) AS trainstations2 " + |
60 |
|
"ORDER BY calcdist ASC " + |
61 |
|
"LIMIT 4 "; |
62 |
|
stmt = conn.prepareStatement(SQL); |
63 |
|
stmt.setDouble(1, latitude); |
64 |
|
stmt.setDouble(2, longitude); |
65 |
|
|
66 |
|
break; |
67 |
|
case BY_NAME: |
68 |
|
SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,0.0 " + |
69 |
|
"FROM trainstations " + |
70 |
|
"WHERE name ILIKE ? AND latitude IS NOT NULL AND longitude IS NOT NULL " + |
71 |
|
"ORDER BY name "; |
72 |
|
stmt = conn.prepareStatement(SQL); |
73 |
|
stmt.setString(1, "%" + name + "%"); |
74 |
|
break; |
75 |
|
default: |
76 |
|
throw new SQLException("not enough parameters"); |
77 |
|
} |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
res = stmt.executeQuery(); |
res = stmt.executeQuery(); |
83 |
|
|
84 |
while (res.next()) { |
while (res.next()) { |
85 |
buff.append("<station>\n"); |
buff.append("<station>\n"); |
86 |
|
|
87 |
buff.append("<name>").append( res.getString(1) ) .append("</name>\n"); |
buff.append("<id>").append( res.getInt(1) ).append("</id>\n"); |
88 |
buff.append("<latitude>").append( res.getDouble(2) ) .append("</latitude>\n"); |
buff.append("<name>").append( res.getString(2) ) .append("</name>\n"); |
89 |
buff.append("<longitude>").append( res.getDouble(3) ) .append("</longitude>\n"); |
buff.append("<latitude>").append( res.getDouble(3) ) .append("</latitude>\n"); |
90 |
buff.append("<address>").append( res.getString(4) ) .append("</address>\n"); |
buff.append("<longitude>").append( res.getDouble(4) ) .append("</longitude>\n"); |
91 |
buff.append("<stationcode>").append( res.getString(5) ) .append("</stationcode>\n"); |
res.getString(5); |
92 |
buff.append("<calcdist>").append( res.getDouble(6) ) .append("</calcdist>\n"); |
buff.append("<fjerntog>").append( !res.wasNull() ) .append("</fjerntog>\n"); |
93 |
|
res.getString(6); |
94 |
|
buff.append("<stog>").append( !res.wasNull() ) .append("</stog>\n"); |
95 |
|
buff.append("<calcdist>").append( res.getInt(7) ) .append("</calcdist>\n"); |
96 |
|
|
97 |
buff.append("</station>\n"); |
buff.append("</station>\n"); |
98 |
} |
} |
99 |
} finally { |
} finally { |
107 |
} |
} |
108 |
|
|
109 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
110 |
|
|
111 |
double latitude = Double.parseDouble( request.getParameter("latitude") ); |
double longitude = 0.0; |
112 |
double longitude = Double.parseDouble( request.getParameter("longitude") ); |
double latitude = 0.0; |
113 |
|
String name = ""; |
114 |
Connection conn = null; |
|
115 |
try { |
Requested method = Requested.NONE; |
116 |
conn = DBConnection.getConnection(); |
|
117 |
|
if (request.getParameter("latitude") != null && request.getParameter("latitude") != null) { |
118 |
String xml = getStations(conn, latitude, longitude); |
latitude = Double.parseDouble( request.getParameter("latitude") ); |
119 |
|
longitude = Double.parseDouble( request.getParameter("longitude") ); |
120 |
response.setContentType("text/xml"); |
method = Requested.BY_LOCATION; |
121 |
response.getWriter().print(xml); |
} |
122 |
|
|
123 |
conn.close(); |
if (request.getParameter("name") != null) { |
124 |
conn = null; |
name = request.getParameter("name"); |
125 |
|
method = Requested.BY_NAME; |
126 |
} catch (Exception e) { |
} |
127 |
throw new ServletException(e); |
|
128 |
} finally { |
|
129 |
|
if (method != Requested.NONE) { |
130 |
|
|
131 |
|
Connection conn = null; |
132 |
try { |
try { |
133 |
if (conn != null) |
conn = DBConnection.getConnection(); |
134 |
conn.close(); |
|
135 |
} catch (Throwable t) {} |
String xml = getStations(conn, latitude, longitude, name, method); |
136 |
|
|
137 |
|
response.setContentType("text/xml"); |
138 |
|
response.getWriter().print(xml); |
139 |
|
|
140 |
|
conn.close(); |
141 |
|
conn = null; |
142 |
|
|
143 |
|
} catch (Exception e) { |
144 |
|
throw new ServletException(e); |
145 |
|
} finally { |
146 |
|
try { |
147 |
|
if (conn != null) |
148 |
|
conn.close(); |
149 |
|
} catch (Throwable t) {} |
150 |
|
} |
151 |
|
} else { |
152 |
|
response.sendError(400, "not enough parameters"); |
153 |
} |
} |
154 |
} |
} |
155 |
|
|