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 name,latitude,longitude,stationcode, " + |
" 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 " + |
42 |
|
" WHERE latitude IS NOT NULL AND longitude IS NOT NULL " + |
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 |
|
|
75 |
while (res.next()) { |
while (res.next()) { |
76 |
buff.append("<station>\n"); |
buff.append("<station>\n"); |
77 |
|
|
78 |
buff.append("<name>").append( res.getString(1) ) .append("</name>\n"); |
buff.append("<id>").append( res.getInt(1) ).append("</id>\n"); |
79 |
buff.append("<latitude>").append( res.getDouble(2) ) .append("</latitude>\n"); |
buff.append("<name>").append( res.getString(2) ) .append("</name>\n"); |
80 |
buff.append("<longitude>").append( res.getDouble(3) ) .append("</longitude>\n"); |
buff.append("<latitude>").append( res.getDouble(3) ) .append("</latitude>\n"); |
81 |
buff.append("<stationcode>").append( res.getString(4) ) .append("</stationcode>\n"); |
buff.append("<longitude>").append( res.getDouble(4) ) .append("</longitude>\n"); |
82 |
buff.append("<calcdist>").append( res.getInt(5) ) .append("</calcdist>\n"); |
res.getString(5); |
83 |
|
buff.append("<fjerntog>").append( !res.wasNull() ) .append("</fjerntog>\n"); |
84 |
|
res.getString(6); |
85 |
|
buff.append("<stog>").append( !res.wasNull() ) .append("</stog>\n"); |
86 |
|
buff.append("<calcdist>").append( res.getInt(7) ) .append("</calcdist>\n"); |
87 |
|
|
88 |
buff.append("</station>\n"); |
buff.append("</station>\n"); |
89 |
} |
} |
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); |