1 |
package dk.thoerup.traininfoservice; |
package dk.thoerup.traininfoservice; |
2 |
|
|
3 |
|
import java.io.ByteArrayOutputStream; |
4 |
import java.io.IOException; |
import java.io.IOException; |
|
import java.sql.Connection; |
|
|
import java.sql.PreparedStatement; |
|
|
import java.sql.ResultSet; |
|
5 |
import java.sql.SQLException; |
import java.sql.SQLException; |
6 |
|
import java.util.logging.Level; |
7 |
|
import java.util.logging.Logger; |
8 |
|
|
9 |
import javax.servlet.ServletException; |
import javax.servlet.ServletException; |
10 |
|
import javax.servlet.annotation.WebServlet; |
11 |
import javax.servlet.http.HttpServlet; |
import javax.servlet.http.HttpServlet; |
12 |
import javax.servlet.http.HttpServletRequest; |
import javax.servlet.http.HttpServletRequest; |
13 |
import javax.servlet.http.HttpServletResponse; |
import javax.servlet.http.HttpServletResponse; |
14 |
|
|
15 |
import dk.thoerup.traininfoservice.DBConnection; |
import org.simpleframework.xml.Serializer; |
16 |
|
import org.simpleframework.xml.core.Persister; |
17 |
|
|
18 |
|
import dk.thoerup.android.traininfo.common.StationBean; |
19 |
|
|
20 |
/** |
/** |
21 |
* Servlet implementation class LocateStations |
* Servlet implementation class LocateStations |
22 |
*/ |
*/ |
23 |
|
@WebServlet(urlPatterns={"/LocateStations"}) |
24 |
public class LocateStations extends HttpServlet { |
public class LocateStations extends HttpServlet { |
25 |
private static final long serialVersionUID = 1L; |
private static final long serialVersionUID = 1L; |
|
|
|
|
/** |
|
|
* @see HttpServlet#HttpServlet() |
|
|
*/ |
|
|
public LocateStations() { |
|
|
super(); |
|
|
// TODO Auto-generated constructor stub |
|
|
} |
|
|
|
|
|
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,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); |
|
|
|
|
|
PreparedStatement stmt = null; |
|
|
ResultSet res = null; |
|
|
|
|
|
StringBuffer buff = new StringBuffer(); |
|
|
|
|
|
buff.append("<?xml version=\"1.0\" ?>\n"); |
|
|
buff.append("<stations>\n"); |
|
|
try |
|
|
{ |
|
|
stmt = conn.prepareStatement(SQL); |
|
|
stmt.setDouble(1, latitude); |
|
|
stmt.setDouble(2, longitude); |
|
|
|
|
|
res = stmt.executeQuery(); |
|
|
|
|
|
while (res.next()) { |
|
|
buff.append("<station>\n"); |
|
26 |
|
|
27 |
buff.append("<name>").append( res.getString(1) ) .append("</name>\n"); |
Logger logger = Logger.getLogger( LocateStations.class.toString() ); |
28 |
buff.append("<latitude>").append( res.getDouble(2) ) .append("</latitude>\n"); |
|
29 |
buff.append("<longitude>").append( res.getDouble(3) ) .append("</longitude>\n"); |
StationDAO stationDao = new StationDAO(); |
30 |
buff.append("<stationcode>").append( res.getString(4) ) .append("</stationcode>\n"); |
|
31 |
buff.append("<calcdist>").append( res.getInt(5) ) .append("</calcdist>\n"); |
|
32 |
|
protected String transformToIntList(String input) { |
33 |
buff.append("</station>\n"); |
String strings[] = input.split(","); |
34 |
|
|
35 |
|
StringBuffer sb = new StringBuffer(); |
36 |
|
sb.append("("); |
37 |
|
for (int i = 0; i<strings.length; i++) { |
38 |
|
if (i>0) { |
39 |
|
sb.append(","); |
40 |
} |
} |
41 |
} finally { |
sb.append( Integer.parseInt(strings[i])); //by doing the integer conversion we ensure that it really is a integer |
|
if (res != null && !res.isClosed()) |
|
|
res.close(); |
|
|
if (stmt != null && !stmt.isClosed()) |
|
|
stmt.close(); |
|
42 |
} |
} |
43 |
buff.append("</stations>\n"); |
sb.append(")"); |
44 |
return buff.toString(); |
return sb.toString(); |
45 |
} |
} |
46 |
|
|
47 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
|
48 |
|
protected StationBean getStations(HttpServletRequest req) throws SQLException { |
49 |
double latitude = Double.parseDouble( request.getParameter("latitude") ); |
StationBean stations = null; |
50 |
double longitude = Double.parseDouble( request.getParameter("longitude") ); |
if (req.getParameter("latitude") != null && req.getParameter("latitude") != null) { |
51 |
|
|
52 |
Connection conn = null; |
Statistics.getInstance().incrementStationLookupsLocation(); |
53 |
|
|
54 |
|
double latitude = Double.parseDouble( req.getParameter("latitude") ); |
55 |
|
double longitude = Double.parseDouble( req.getParameter("longitude") ); |
56 |
|
stations = stationDao.getByLocation(latitude, longitude); |
57 |
|
|
58 |
|
} else if (req.getParameter("name") != null) { |
59 |
|
Statistics.getInstance().incrementStationLookupsName(); |
60 |
|
String name = req.getParameter("name").trim(); |
61 |
|
stations = stationDao.getByName(name); |
62 |
|
|
63 |
|
} else if (req.getParameter("list") != null) { |
64 |
|
Statistics.getInstance().incrementStationLookupsFavorites(); |
65 |
|
String list = transformToIntList( req.getParameter("list")); |
66 |
|
stations = stationDao.getByList(list); |
67 |
|
} |
68 |
|
return stations; |
69 |
|
} |
70 |
|
|
71 |
|
|
72 |
|
protected String formatStations(StationBean stations) throws ServletException { |
73 |
|
|
74 |
|
Serializer serializer = new Persister(); |
75 |
|
|
76 |
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
77 |
|
|
78 |
try { |
try { |
79 |
conn = DBConnection.getConnection(); |
serializer.write(stations, out); |
|
|
|
|
String xml = getStations(conn, latitude, longitude); |
|
|
|
|
|
response.setContentType("text/xml"); |
|
|
response.getWriter().print(xml); |
|
|
|
|
|
conn.close(); |
|
|
conn = null; |
|
|
|
|
80 |
} catch (Exception e) { |
} catch (Exception e) { |
81 |
throw new ServletException(e); |
throw new ServletException(e); |
|
} finally { |
|
|
try { |
|
|
if (conn != null) |
|
|
conn.close(); |
|
|
} catch (Throwable t) {} |
|
82 |
} |
} |
83 |
|
|
84 |
|
return out.toString(); |
85 |
|
} |
86 |
|
|
87 |
|
@Override |
88 |
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
89 |
|
|
90 |
|
|
91 |
|
try { |
92 |
|
StationBean stations = getStations(request); |
93 |
|
|
94 |
|
|
95 |
|
if (stations != null){ |
96 |
|
String xml = formatStations(stations); |
97 |
|
|
98 |
|
response.setContentType("text/xml"); |
99 |
|
response.getWriter().print(xml); |
100 |
|
} else { |
101 |
|
response.sendError(400, "not enough parameters"); |
102 |
|
} |
103 |
|
|
104 |
|
|
105 |
|
} catch (Exception e) { |
106 |
|
logger.log(Level.SEVERE, "Exception while finding stations", e); |
107 |
|
response.sendError(500); |
108 |
|
} |
109 |
} |
} |
110 |
|
|
111 |
} |
} |