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