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; |
26 |
|
|
27 |
/** |
Logger logger = Logger.getLogger( LocateStations.class.toString() ); |
|
* @see HttpServlet#HttpServlet() |
|
|
*/ |
|
|
public LocateStations() { |
|
|
super(); |
|
|
// TODO Auto-generated constructor stub |
|
|
} |
|
|
|
|
|
public enum Requested { |
|
|
BY_NAME, |
|
|
BY_LOCATION, |
|
|
NONE |
|
|
} |
|
|
|
|
28 |
|
|
29 |
protected String getStations(Connection conn, double latitude, double longitude, String name, Requested method) throws SQLException { |
StationDAO stationDao = new StationDAO(); |
|
String SQL = ""; |
|
|
PreparedStatement stmt = null; |
|
|
ResultSet res = null; |
|
30 |
|
|
|
StringBuffer buff = new StringBuffer(); |
|
31 |
|
|
32 |
buff.append("<?xml version=\"1.0\" ?>\n"); |
protected String transformToIntList(String input) { |
33 |
buff.append("<stations>\n"); |
String strings[] = input.split(","); |
|
|
|
|
try |
|
|
{ |
|
34 |
|
|
35 |
switch (method) |
StringBuffer sb = new StringBuffer(); |
36 |
{ |
sb.append("("); |
37 |
case BY_LOCATION: |
for (int i = 0; i<strings.length; i++) { |
38 |
//inner select is workaround from not being able to use a calculated column directly in where clause |
if (i>0) { |
39 |
SQL = "SELECT * FROM ( "+ |
sb.append(","); |
|
" 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 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: |
|
|
throw new SQLException("not enough parameters"); |
|
40 |
} |
} |
41 |
|
sb.append( Integer.parseInt(strings[i])); //by doing the integer conversion we ensure that it really is a integer |
42 |
|
} |
43 |
|
sb.append(")"); |
44 |
|
return sb.toString(); |
45 |
|
} |
46 |
|
|
47 |
|
|
48 |
|
protected StationBean getStations(HttpServletRequest req) throws SQLException { |
49 |
|
StationBean stations = null; |
50 |
|
if (req.getParameter("latitude") != null && req.getParameter("latitude") != null) { |
51 |
|
|
52 |
|
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 |
res = stmt.executeQuery(); |
protected String formatStations(StationBean stations) throws ServletException { |
73 |
|
|
74 |
while (res.next()) { |
Serializer serializer = new Persister(); |
|
buff.append("<station>\n"); |
|
75 |
|
|
76 |
buff.append("<id>").append( res.getInt(1) ).append("</id>\n"); |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
|
buff.append("<name>").append( res.getString(2) ) .append("</name>\n"); |
|
|
buff.append("<latitude>").append( res.getDouble(3) ) .append("</latitude>\n"); |
|
|
buff.append("<longitude>").append( res.getDouble(4) ) .append("</longitude>\n"); |
|
|
res.getString(5); |
|
|
buff.append("<fjerntog>").append( !res.wasNull() ) .append("</fjerntog>\n"); |
|
|
res.getString(6); |
|
|
buff.append("<stog>").append( !res.wasNull() ) .append("</stog>\n"); |
|
|
buff.append("<calcdist>").append( res.getInt(7) ) .append("</calcdist>\n"); |
|
77 |
|
|
78 |
buff.append("</station>\n"); |
try { |
79 |
} |
serializer.write(stations, out); |
80 |
} finally { |
} catch (Exception e) { |
81 |
if (res != null && !res.isClosed()) |
throw new ServletException(e); |
|
res.close(); |
|
|
if (stmt != null && !stmt.isClosed()) |
|
|
stmt.close(); |
|
82 |
} |
} |
83 |
buff.append("</stations>\n"); |
|
84 |
return buff.toString(); |
return out.toString(); |
85 |
} |
} |
86 |
|
|
87 |
|
@Override |
88 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
89 |
|
|
|
double longitude = 0.0; |
|
|
double latitude = 0.0; |
|
|
String name = ""; |
|
|
|
|
|
Requested method = Requested.NONE; |
|
|
|
|
|
if (request.getParameter("latitude") != null && request.getParameter("latitude") != null) { |
|
|
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"); |
|
|
method = Requested.BY_NAME; |
|
|
} |
|
90 |
|
|
91 |
|
try { |
92 |
|
StationBean stations = getStations(request); |
93 |
|
|
|
if (method != Requested.NONE) { |
|
94 |
|
|
95 |
Connection conn = null; |
if (stations != null){ |
96 |
try { |
String xml = formatStations(stations); |
|
conn = DBConnection.getConnection(); |
|
|
|
|
|
String xml = getStations(conn, latitude, longitude, name, method); |
|
97 |
|
|
98 |
response.setContentType("text/xml"); |
response.setContentType("text/xml"); |
99 |
response.getWriter().print(xml); |
response.getWriter().print(xml); |
100 |
|
} else { |
101 |
|
response.sendError(400, "not enough parameters"); |
102 |
|
} |
103 |
|
|
|
conn.close(); |
|
|
conn = null; |
|
104 |
|
|
105 |
} catch (Exception e) { |
} catch (Exception e) { |
106 |
throw new ServletException(e); |
logger.log(Level.SEVERE, "Exception while finding stations", e); |
107 |
} finally { |
response.sendError(500); |
108 |
try { |
} |
|
if (conn != null) |
|
|
conn.close(); |
|
|
} catch (Throwable t) {} |
|
|
} |
|
|
} else { |
|
|
response.sendError(400, "not enough parameters"); |
|
|
} |
|
109 |
} |
} |
110 |
|
|
111 |
} |
} |