6 |
import java.sql.ResultSet; |
import java.sql.ResultSet; |
7 |
import java.sql.SQLException; |
import java.sql.SQLException; |
8 |
|
|
9 |
|
import java.util.logging.Level; |
10 |
|
import java.util.logging.Logger; |
11 |
|
|
12 |
import javax.servlet.ServletException; |
import javax.servlet.ServletException; |
13 |
import javax.servlet.http.HttpServlet; |
import javax.servlet.http.HttpServlet; |
14 |
import javax.servlet.http.HttpServletRequest; |
import javax.servlet.http.HttpServletRequest; |
15 |
import javax.servlet.http.HttpServletResponse; |
import javax.servlet.http.HttpServletResponse; |
16 |
|
|
|
import dk.thoerup.traininfoservice.DBConnection; |
|
|
|
|
17 |
/** |
/** |
18 |
* Servlet implementation class LocateStations |
* Servlet implementation class LocateStations |
19 |
*/ |
*/ |
20 |
public class LocateStations extends HttpServlet { |
public class LocateStations extends HttpServlet { |
21 |
private static final long serialVersionUID = 1L; |
private static final long serialVersionUID = 1L; |
22 |
|
|
23 |
/** |
Logger logger = Logger.getLogger( LocateStations.class.toString() ); |
24 |
* @see HttpServlet#HttpServlet() |
|
25 |
*/ |
|
26 |
public LocateStations() { |
|
27 |
super(); |
protected String transformToIntList(String input) { |
28 |
// TODO Auto-generated constructor stub |
String strings[] = input.split(","); |
|
} |
|
|
|
|
|
protected String getStations(Connection conn, double latitude, double longitude) throws SQLException { |
|
|
//inner select is workaround from not being able to use a calculated column directly in where clause |
|
|
final String SQL = "SELECT * FROM ( "+ |
|
|
" SELECT name,latitude,longitude,address,stationcode, " + |
|
|
" earth_distance( ll_to_earth(latitude,longitude), ll_to_earth(?,?))::int AS calcdist " + |
|
|
" FROM trainstations " + |
|
|
" ) AS trainstations2 " + |
|
|
"ORDER BY calcdist ASC " + |
|
|
"LIMIT 4 "; |
|
|
|
|
|
System.out.println(SQL); |
|
29 |
|
|
30 |
|
StringBuffer sb = new StringBuffer(); |
31 |
|
sb.append("("); |
32 |
|
for (int i = 0; i<strings.length; i++) { |
33 |
|
if (i>0) { |
34 |
|
sb.append(","); |
35 |
|
} |
36 |
|
sb.append( Integer.parseInt(strings[i])); //by doing the integer conversion we ensure that it really is a integer |
37 |
|
} |
38 |
|
sb.append(")"); |
39 |
|
return sb.toString(); |
40 |
|
} |
41 |
|
|
42 |
|
|
43 |
|
protected PreparedStatement createStatement(Connection conn, HttpServletRequest req) throws SQLException { |
44 |
|
|
45 |
PreparedStatement stmt = null; |
PreparedStatement stmt = null; |
46 |
|
String SQL; |
47 |
|
|
48 |
|
if (req.getParameter("latitude") != null && req.getParameter("latitude") != null) { |
49 |
|
//inner select is workaround from not being able to use a calculated column directly in where clause |
50 |
|
SQL = "SELECT * FROM ( "+ |
51 |
|
" SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, " + |
52 |
|
" earth_distance( ll_to_earth(latitude,longitude), ll_to_earth(?,?))::int AS calcdist " + |
53 |
|
" FROM trainstations " + |
54 |
|
" WHERE enabled = true " + |
55 |
|
" ) AS trainstations2 " + |
56 |
|
"ORDER BY calcdist ASC " + |
57 |
|
"LIMIT 4 "; |
58 |
|
double latitude = Double.parseDouble( req.getParameter("latitude") ); |
59 |
|
double longitude = Double.parseDouble( req.getParameter("longitude") ); |
60 |
|
stmt = conn.prepareStatement(SQL); |
61 |
|
stmt.setDouble(1, latitude); |
62 |
|
stmt.setDouble(2, longitude); |
63 |
|
} else if (req.getParameter("name") != null) { |
64 |
|
SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,0.0 " + |
65 |
|
"FROM trainstations " + |
66 |
|
"WHERE name ILIKE ? AND enabled = true " + |
67 |
|
"ORDER BY name "; |
68 |
|
|
69 |
|
String name = req.getParameter("name").trim(); |
70 |
|
stmt = conn.prepareStatement(SQL); |
71 |
|
stmt.setString(1, name + "%"); |
72 |
|
} else if (req.getParameter("list") != null) { |
73 |
|
String list = transformToIntList( req.getParameter("list")); |
74 |
|
SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,0.0 " + |
75 |
|
"FROM trainstations " + |
76 |
|
"WHERE id IN " + list + " AND enabled = true " + |
77 |
|
"ORDER BY name "; |
78 |
|
stmt = conn.prepareStatement(SQL); |
79 |
|
} |
80 |
|
|
81 |
|
return stmt; |
82 |
|
} |
83 |
|
|
84 |
|
|
85 |
|
protected String formatResultset(PreparedStatement stmt) throws SQLException { |
86 |
ResultSet res = null; |
ResultSet res = null; |
87 |
|
|
88 |
StringBuffer buff = new StringBuffer(); |
StringBuffer buff = new StringBuffer(); |
89 |
|
|
90 |
buff.append("<?xml version=\"1.0\" ?>\n"); |
buff.append("<?xml version=\"1.0\" ?>\n"); |
91 |
buff.append("<stations>\n"); |
buff.append("<stations>\n"); |
92 |
|
|
93 |
try |
try |
94 |
{ |
{ |
|
stmt = conn.prepareStatement(SQL); |
|
|
stmt.setDouble(1, latitude); |
|
|
stmt.setDouble(2, longitude); |
|
|
|
|
95 |
res = stmt.executeQuery(); |
res = stmt.executeQuery(); |
96 |
|
|
97 |
while (res.next()) { |
while (res.next()) { |
98 |
buff.append("<station>\n"); |
buff.append("<station>\n"); |
99 |
|
|
100 |
buff.append("<name>").append( res.getString(1) ) .append("</name>\n"); |
buff.append("<id>").append( res.getInt(1) ).append("</id>\n"); |
101 |
buff.append("<latitude>").append( res.getDouble(2) ) .append("</latitude>\n"); |
buff.append("<name>").append( res.getString(2) ) .append("</name>\n"); |
102 |
buff.append("<longitude>").append( res.getDouble(3) ) .append("</longitude>\n"); |
buff.append("<latitude>").append( res.getDouble(3) ) .append("</latitude>\n"); |
103 |
buff.append("<address>").append( res.getString(4) ) .append("</address>\n"); |
buff.append("<longitude>").append( res.getDouble(4) ) .append("</longitude>\n"); |
104 |
buff.append("<stationcode>").append( res.getString(5) ) .append("</stationcode>\n"); |
res.getString(5); |
105 |
buff.append("<calcdist>").append( res.getDouble(6) ) .append("</calcdist>\n"); |
buff.append("<fjerntog>").append( !res.wasNull() ) .append("</fjerntog>\n"); |
106 |
|
res.getString(6); |
107 |
|
buff.append("<stog>").append( !res.wasNull() ) .append("</stog>\n"); |
108 |
|
buff.append("<calcdist>").append( res.getInt(7) ) .append("</calcdist>\n"); |
109 |
|
|
110 |
buff.append("</station>\n"); |
buff.append("</station>\n"); |
111 |
} |
} |
112 |
} finally { |
} finally { |
119 |
return buff.toString(); |
return buff.toString(); |
120 |
} |
} |
121 |
|
|
122 |
|
@Override |
123 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
124 |
|
|
|
double latitude = Double.parseDouble( request.getParameter("latitude") ); |
|
|
double longitude = Double.parseDouble( request.getParameter("longitude") ); |
|
|
|
|
125 |
Connection conn = null; |
Connection conn = null; |
126 |
try { |
try { |
127 |
conn = DBConnection.getConnection(); |
conn = DBConnection.getConnection(); |
128 |
|
|
129 |
String xml = getStations(conn, latitude, longitude); |
PreparedStatement stmt = createStatement(conn, request); |
130 |
|
if (stmt != null){ |
131 |
response.setContentType("text/xml"); |
String xml = formatResultset(stmt); |
132 |
response.getWriter().print(xml); |
|
133 |
|
response.setContentType("text/xml"); |
134 |
conn.close(); |
response.getWriter().print(xml); |
135 |
conn = null; |
} else { |
136 |
|
response.sendError(400, "not enough parameters"); |
137 |
} catch (Exception e) { |
} |
138 |
throw new ServletException(e); |
|
139 |
|
|
140 |
|
} catch (Exception e) { |
141 |
|
logger.log(Level.SEVERE, "Exception while finding stations", e); |
142 |
|
response.sendError(500); |
143 |
} finally { |
} finally { |
144 |
try { |
try { |
145 |
if (conn != null) |
if (conn != null) { |
146 |
conn.close(); |
conn.close(); |
147 |
|
} |
148 |
} catch (Throwable t) {} |
} catch (Throwable t) {} |
149 |
} |
} |
150 |
} |
} |