5 |
import java.sql.PreparedStatement; |
import java.sql.PreparedStatement; |
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; |
import java.util.logging.Level; |
10 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
11 |
|
|
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 |
*/ |
*/ |
21 |
private static final long serialVersionUID = 1L; |
private static final long serialVersionUID = 1L; |
22 |
|
|
23 |
Logger logger = Logger.getLogger( LocateStations.class.toString() ); |
Logger logger = Logger.getLogger( LocateStations.class.toString() ); |
24 |
|
|
25 |
|
|
26 |
|
|
27 |
/** |
protected String transformToIntList(String input) { |
28 |
* @see HttpServlet#HttpServlet() |
String strings[] = input.split(","); |
29 |
*/ |
|
30 |
public LocateStations() { |
StringBuffer sb = new StringBuffer(); |
31 |
super(); |
sb.append("("); |
32 |
// TODO Auto-generated constructor stub |
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 |
|
|
|
public enum Requested { |
|
|
BY_NAME, |
|
|
BY_LOCATION, |
|
|
NONE |
|
|
} |
|
42 |
|
|
43 |
|
protected PreparedStatement createStatement(Connection conn, HttpServletRequest req) throws SQLException { |
44 |
|
|
|
protected String getStations(Connection conn, double latitude, double longitude, String name, Requested method) throws SQLException { |
|
|
String SQL = ""; |
|
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, address," + |
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, address, 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,address,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 |
{ |
{ |
|
|
|
|
switch (method) |
|
|
{ |
|
|
case BY_LOCATION: |
|
|
//inner select is workaround from not being able to use a calculated column directly in where clause |
|
|
SQL = "SELECT * FROM ( "+ |
|
|
" SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, " + |
|
|
" earth_distance( ll_to_earth(latitude,longitude), ll_to_earth(?,?))::int AS calcdist " + |
|
|
" FROM trainstations " + |
|
|
" WHERE enabled = true AND latitude IS NOT NULL AND longitude IS NOT NULL " + |
|
|
" ) AS trainstations2 " + |
|
|
"ORDER BY calcdist ASC " + |
|
|
"LIMIT 4 "; |
|
|
stmt = conn.prepareStatement(SQL); |
|
|
stmt.setDouble(1, latitude); |
|
|
stmt.setDouble(2, longitude); |
|
|
|
|
|
break; |
|
|
case BY_NAME: |
|
|
SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,0.0 " + |
|
|
"FROM trainstations " + |
|
|
"WHERE name ILIKE ? AND latitude IS NOT NULL AND longitude IS NOT NULL " + |
|
|
"ORDER BY name "; |
|
|
stmt = conn.prepareStatement(SQL); |
|
|
stmt.setString(1, name + "%"); |
|
|
break; |
|
|
default: |
|
|
// This should not be possible |
|
|
logger.severe("getStations(): default switch case"); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
res = stmt.executeQuery(); |
res = stmt.executeQuery(); |
96 |
|
|
97 |
while (res.next()) { |
while (res.next()) { |
105 |
buff.append("<fjerntog>").append( !res.wasNull() ) .append("</fjerntog>\n"); |
buff.append("<fjerntog>").append( !res.wasNull() ) .append("</fjerntog>\n"); |
106 |
res.getString(6); |
res.getString(6); |
107 |
buff.append("<stog>").append( !res.wasNull() ) .append("</stog>\n"); |
buff.append("<stog>").append( !res.wasNull() ) .append("</stog>\n"); |
108 |
buff.append("<calcdist>").append( res.getInt(7) ) .append("</calcdist>\n"); |
|
109 |
|
String address = res.getString(7); |
110 |
|
buff.append("<address>").append( res.wasNull() ? "" : address ).append("</address>"); |
111 |
|
buff.append("<calcdist>").append( res.getInt(8) ) .append("</calcdist>\n"); |
112 |
|
|
113 |
buff.append("</station>\n"); |
buff.append("</station>\n"); |
114 |
} |
} |
125 |
@Override |
@Override |
126 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
127 |
|
|
128 |
double longitude = 0.0; |
Connection conn = null; |
129 |
double latitude = 0.0; |
try { |
130 |
String name = ""; |
conn = DBConnection.getConnection(); |
131 |
|
|
132 |
Requested method = Requested.NONE; |
PreparedStatement stmt = createStatement(conn, request); |
133 |
|
if (stmt != null){ |
134 |
if (request.getParameter("latitude") != null && request.getParameter("latitude") != null) { |
String xml = formatResultset(stmt); |
|
latitude = Double.parseDouble( request.getParameter("latitude") ); |
|
|
longitude = Double.parseDouble( request.getParameter("longitude") ); |
|
|
method = Requested.BY_LOCATION; |
|
|
} |
|
|
|
|
|
if (request.getParameter("name") != null) { |
|
|
name = request.getParameter("name").trim(); |
|
|
method = Requested.BY_NAME; |
|
|
} |
|
|
|
|
|
|
|
|
if (method != Requested.NONE) { |
|
|
|
|
|
Connection conn = null; |
|
|
try { |
|
|
conn = DBConnection.getConnection(); |
|
|
|
|
|
String xml = getStations(conn, latitude, longitude, name, method); |
|
135 |
|
|
136 |
response.setContentType("text/xml"); |
response.setContentType("text/xml"); |
137 |
response.getWriter().print(xml); |
response.getWriter().print(xml); |
138 |
|
} else { |
139 |
conn.close(); |
response.sendError(400, "not enough parameters"); |
|
conn = null; |
|
|
|
|
|
} catch (Exception e) { |
|
|
logger.log(Level.SEVERE, "Exception while finding stations", e); |
|
|
response.sendError(500); |
|
|
} finally { |
|
|
try { |
|
|
if (conn != null) |
|
|
conn.close(); |
|
|
} catch (Throwable t) {} |
|
140 |
} |
} |
141 |
} else { |
|
142 |
response.sendError(400, "not enough parameters"); |
|
143 |
|
} catch (Exception e) { |
144 |
|
logger.log(Level.SEVERE, "Exception while finding stations", e); |
145 |
|
response.sendError(500); |
146 |
|
} finally { |
147 |
|
try { |
148 |
|
if (conn != null) { |
149 |
|
conn.close(); |
150 |
|
} |
151 |
|
} catch (Exception t) {} |
152 |
} |
} |
153 |
} |
} |
154 |
|
|