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 |
/** |
/** |
19 |
* Servlet implementation class LocateStations |
* Servlet implementation class LocateStations |
20 |
*/ |
*/ |
21 |
|
@WebServlet(urlPatterns={"/LocateStations"}) |
22 |
public class LocateStations extends HttpServlet { |
public class LocateStations extends HttpServlet { |
23 |
private static final long serialVersionUID = 1L; |
private static final long serialVersionUID = 1L; |
|
|
|
|
/** |
|
|
* @see HttpServlet#HttpServlet() |
|
|
*/ |
|
|
public LocateStations() { |
|
|
super(); |
|
|
// TODO Auto-generated constructor stub |
|
|
} |
|
|
|
|
24 |
|
|
25 |
|
Logger logger = Logger.getLogger( LocateStations.class.toString() ); |
26 |
|
|
27 |
|
StationDAO stationDao = new StationDAO(); |
28 |
|
|
|
protected String getStations(Connection conn, double latitude, double longitude, String name) throws SQLException { |
|
|
String SQL = ""; |
|
|
|
|
|
if (latitude >= 0.0 && longitude >= 0.0) |
|
|
{ |
|
|
//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 latitude IS NOT NULL AND longitude IS NOT NULL " + |
|
|
" ) AS trainstations2 " + |
|
|
"ORDER BY calcdist ASC " + |
|
|
"LIMIT 4 "; |
|
|
} else if (name != null) { |
|
|
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 "; |
|
|
} else throw new SQLException("not enough parameters"); |
|
|
|
|
|
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); |
|
|
if (latitude >= 0 && longitude >= 0) { |
|
|
stmt.setDouble(1, latitude); |
|
|
stmt.setDouble(2, longitude); |
|
|
} |
|
|
if (name != null) { |
|
|
stmt.setString(1, "%" + name + "%"); |
|
|
} |
|
|
|
|
|
res = stmt.executeQuery(); |
|
|
|
|
|
while (res.next()) { |
|
|
buff.append("<station>\n"); |
|
29 |
|
|
30 |
buff.append("<id>").append( res.getInt(1) ).append("</id>\n"); |
protected String transformToIntList(String input) { |
31 |
buff.append("<name>").append( res.getString(2) ) .append("</name>\n"); |
String strings[] = input.split(","); |
32 |
buff.append("<latitude>").append( res.getDouble(3) ) .append("</latitude>\n"); |
|
33 |
buff.append("<longitude>").append( res.getDouble(4) ) .append("</longitude>\n"); |
StringBuffer sb = new StringBuffer(); |
34 |
res.getString(5); |
sb.append("("); |
35 |
buff.append("<fjerntog>").append( !res.wasNull() ) .append("</fjerntog>\n"); |
for (int i = 0; i<strings.length; i++) { |
36 |
res.getString(6); |
if (i>0) { |
37 |
buff.append("<stog>").append( !res.wasNull() ) .append("</stog>\n"); |
sb.append(","); |
|
buff.append("<calcdist>").append( res.getInt(7) ) .append("</calcdist>\n"); |
|
|
|
|
|
buff.append("</station>\n"); |
|
38 |
} |
} |
39 |
} 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(); |
|
40 |
} |
} |
41 |
buff.append("</stations>\n"); |
sb.append(")"); |
42 |
return buff.toString(); |
return sb.toString(); |
43 |
} |
} |
44 |
|
|
45 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
|
46 |
|
protected StationBean getStations(HttpServletRequest req) throws SQLException { |
47 |
double latitude = -1.0; |
StationBean stations = null; |
48 |
if (request.getParameter("latitude") != null) |
if (req.getParameter("latitude") != null && req.getParameter("latitude") != null) { |
49 |
latitude = Double.parseDouble( request.getParameter("latitude") ); |
|
50 |
|
Statistics.getInstance().incrementStationLookupsLocation(); |
51 |
double longitude = -1.0; |
|
52 |
if (request.getParameter("latitude") != null) |
double latitude = Double.parseDouble( req.getParameter("latitude") ); |
53 |
longitude = Double.parseDouble( request.getParameter("longitude") ); |
double longitude = Double.parseDouble( req.getParameter("longitude") ); |
54 |
|
stations = stationDao.getByLocation(latitude, longitude); |
55 |
String name = request.getParameter("name"); |
|
56 |
|
} else if (req.getParameter("name") != null) { |
57 |
Connection conn = null; |
Statistics.getInstance().incrementStationLookupsName(); |
58 |
|
String name = req.getParameter("name").trim(); |
59 |
|
stations = stationDao.getByName(name); |
60 |
|
|
61 |
|
} else if (req.getParameter("list") != null) { |
62 |
|
Statistics.getInstance().incrementStationLookupsFavorites(); |
63 |
|
String list = transformToIntList( req.getParameter("list")); |
64 |
|
stations = stationDao.getByList(list); |
65 |
|
} |
66 |
|
return stations; |
67 |
|
} |
68 |
|
|
69 |
|
|
70 |
|
protected String formatStations(StationBean stations) throws ServletException { |
71 |
|
|
72 |
|
Serializer serializer = new Persister(); |
73 |
|
|
74 |
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
75 |
|
|
76 |
try { |
try { |
77 |
conn = DBConnection.getConnection(); |
serializer.write(stations, out); |
|
|
|
|
String xml = getStations(conn, latitude, longitude, name); |
|
|
|
|
|
response.setContentType("text/xml"); |
|
|
response.getWriter().print(xml); |
|
|
|
|
|
conn.close(); |
|
|
conn = null; |
|
|
|
|
78 |
} catch (Exception e) { |
} catch (Exception e) { |
79 |
throw new ServletException(e); |
throw new ServletException(e); |
|
} finally { |
|
|
try { |
|
|
if (conn != null) |
|
|
conn.close(); |
|
|
} catch (Throwable t) {} |
|
80 |
} |
} |
81 |
|
|
82 |
|
return out.toString(); |
83 |
|
} |
84 |
|
|
85 |
|
@Override |
86 |
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
87 |
|
|
88 |
|
|
89 |
|
try { |
90 |
|
StationBean stations = getStations(request); |
91 |
|
|
92 |
|
|
93 |
|
if (stations != null){ |
94 |
|
String xml = formatStations(stations); |
95 |
|
|
96 |
|
response.setContentType("text/xml"); |
97 |
|
response.getWriter().print(xml); |
98 |
|
} else { |
99 |
|
response.sendError(400, "not enough parameters"); |
100 |
|
} |
101 |
|
|
102 |
|
|
103 |
|
} catch (Exception e) { |
104 |
|
logger.log(Level.SEVERE, "Exception while finding stations", e); |
105 |
|
response.sendError(500); |
106 |
|
} |
107 |
} |
} |
108 |
|
|
109 |
} |
} |