1 |
package dk.thoerup.traininfoservice; |
package dk.thoerup.traininfoservice; |
2 |
|
|
3 |
|
|
4 |
import java.io.IOException; |
import java.io.IOException; |
5 |
import java.sql.Connection; |
import java.io.StringWriter; |
|
import java.sql.PreparedStatement; |
|
|
import java.sql.ResultSet; |
|
6 |
import java.sql.SQLException; |
import java.sql.SQLException; |
|
|
|
7 |
import java.util.logging.Level; |
import java.util.logging.Level; |
8 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
9 |
|
|
10 |
import javax.servlet.ServletException; |
import javax.servlet.ServletException; |
11 |
|
import javax.servlet.annotation.WebServlet; |
12 |
import javax.servlet.http.HttpServlet; |
import javax.servlet.http.HttpServlet; |
13 |
import javax.servlet.http.HttpServletRequest; |
import javax.servlet.http.HttpServletRequest; |
14 |
import javax.servlet.http.HttpServletResponse; |
import javax.servlet.http.HttpServletResponse; |
15 |
|
|
16 |
|
import org.simpleframework.xml.Serializer; |
17 |
|
import org.simpleframework.xml.core.Persister; |
18 |
|
|
19 |
|
import dk.thoerup.android.traininfo.common.StationBean; |
20 |
|
|
21 |
/** |
/** |
22 |
* Servlet implementation class LocateStations |
* Servlet implementation class LocateStations |
23 |
*/ |
*/ |
24 |
|
@WebServlet(urlPatterns={"/LocateStations"}) |
25 |
public class LocateStations extends HttpServlet { |
public class LocateStations extends HttpServlet { |
26 |
private static final long serialVersionUID = 1L; |
private static final long serialVersionUID = 1L; |
27 |
|
|
28 |
Logger logger = Logger.getLogger( LocateStations.class.toString() ); |
Logger logger = Logger.getLogger( LocateStations.class.toString() ); |
29 |
|
|
30 |
|
StationDAO stationDao = new StationDAO(); |
31 |
|
|
32 |
|
|
|
|
|
33 |
protected String transformToIntList(String input) { |
protected String transformToIntList(String input) { |
34 |
String strings[] = input.split(","); |
String strings[] = input.split(","); |
35 |
|
|
36 |
StringBuffer sb = new StringBuffer(); |
StringBuffer sb = new StringBuffer(); |
37 |
sb.append("("); |
sb.append("("); |
38 |
for (int i = 0; i<strings.length; i++) { |
for (int i = 0; i<strings.length; i++) { |
46 |
} |
} |
47 |
|
|
48 |
|
|
49 |
protected PreparedStatement createStatement(Connection conn, HttpServletRequest req) throws SQLException { |
protected StationBean getStations(HttpServletRequest req) throws SQLException { |
50 |
|
StationBean stations = null; |
|
PreparedStatement stmt = null; |
|
|
String SQL; |
|
|
|
|
51 |
if (req.getParameter("latitude") != null && req.getParameter("latitude") != null) { |
if (req.getParameter("latitude") != null && req.getParameter("latitude") != null) { |
52 |
//inner select is workaround from not being able to use a calculated column directly in where clause |
|
53 |
SQL = "SELECT * FROM ( "+ |
Statistics.getInstance().incrementStationLookupsLocation(); |
54 |
" SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address," + |
|
|
" earth_distance( ll_to_earth(latitude,longitude), ll_to_earth(?,?))::int AS calcdist " + |
|
|
" FROM trainstations " + |
|
|
" WHERE enabled = true " + |
|
|
" ) AS trainstations2 " + |
|
|
"ORDER BY calcdist ASC " + |
|
|
"LIMIT 4 "; |
|
55 |
double latitude = Double.parseDouble( req.getParameter("latitude") ); |
double latitude = Double.parseDouble( req.getParameter("latitude") ); |
56 |
double longitude = Double.parseDouble( req.getParameter("longitude") ); |
double longitude = Double.parseDouble( req.getParameter("longitude") ); |
57 |
stmt = conn.prepareStatement(SQL); |
stations = stationDao.getByLocation(latitude, longitude); |
|
stmt.setDouble(1, latitude); |
|
|
stmt.setDouble(2, longitude); |
|
|
} else if (req.getParameter("name") != null) { |
|
|
SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " + |
|
|
"FROM trainstations " + |
|
|
"WHERE name ILIKE ? AND enabled = true " + |
|
|
"ORDER BY name "; |
|
58 |
|
|
59 |
|
} else if (req.getParameter("name") != null) { |
60 |
|
Statistics.getInstance().incrementStationLookupsName(); |
61 |
String name = req.getParameter("name").trim(); |
String name = req.getParameter("name").trim(); |
62 |
stmt = conn.prepareStatement(SQL); |
stations = stationDao.getByName(name); |
63 |
stmt.setString(1, name + "%"); |
|
64 |
} else if (req.getParameter("list") != null) { |
} else if (req.getParameter("list") != null) { |
65 |
|
Statistics.getInstance().incrementStationLookupsFavorites(); |
66 |
String list = transformToIntList( req.getParameter("list")); |
String list = transformToIntList( req.getParameter("list")); |
67 |
SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro, address,0.0 " + |
stations = stationDao.getByList(list); |
68 |
"FROM trainstations " + |
} else if (req.getParameter("dump") != null) { |
69 |
"WHERE id IN " + list + " AND enabled = true " + |
stations = stationDao.dumpAll(); |
70 |
"ORDER BY name "; |
} |
71 |
stmt = conn.prepareStatement(SQL); |
return stations; |
|
} |
|
|
|
|
|
return stmt; |
|
72 |
} |
} |
73 |
|
|
74 |
|
|
75 |
protected String formatResultset(PreparedStatement stmt) throws SQLException { |
protected String formatStations(StationBean stations) throws ServletException { |
|
ResultSet res = null; |
|
76 |
|
|
77 |
StringBuffer buff = new StringBuffer(); |
Serializer serializer = new Persister(); |
78 |
|
|
79 |
buff.append("<?xml version=\"1.0\" ?>\n"); |
StringWriter out = new StringWriter(); |
|
buff.append("<stations>\n"); |
|
80 |
|
|
81 |
try |
try { |
82 |
{ |
serializer.write(stations, out); |
83 |
res = stmt.executeQuery(); |
} catch (Exception e) { |
84 |
|
throw new ServletException(e); |
|
while (res.next()) { |
|
|
buff.append("<station>\n"); |
|
|
|
|
|
buff.append("<id>").append( res.getInt(1) ).append("</id>\n"); |
|
|
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"); |
|
|
res.getString(7); |
|
|
buff.append("<metro>").append( !res.wasNull() ).append("</metro>\n"); |
|
|
|
|
|
String address = res.getString(8); |
|
|
buff.append("<address>").append( res.wasNull() ? "" : address ).append("</address>"); |
|
|
buff.append("<calcdist>").append( res.getInt(9) ) .append("</calcdist>\n"); |
|
|
|
|
|
buff.append("</station>\n"); |
|
|
} |
|
|
} finally { |
|
|
if (res != null && !res.isClosed()) |
|
|
res.close(); |
|
|
if (stmt != null && !stmt.isClosed()) |
|
|
stmt.close(); |
|
85 |
} |
} |
86 |
buff.append("</stations>\n"); |
|
87 |
return buff.toString(); |
return out.toString(); |
88 |
} |
} |
89 |
|
|
90 |
@Override |
@Override |
91 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
92 |
|
|
93 |
Connection conn = null; |
|
94 |
try { |
try { |
95 |
conn = DBConnection.getConnection(); |
StationBean stations = getStations(request); |
96 |
|
|
97 |
|
|
98 |
PreparedStatement stmt = createStatement(conn, request); |
if (stations != null){ |
99 |
if (stmt != null){ |
String xml = formatStations(stations); |
|
String xml = formatResultset(stmt); |
|
100 |
|
|
101 |
response.setContentType("text/xml"); |
response.setContentType("text/xml"); |
102 |
response.getWriter().print(xml); |
response.getWriter().print(xml); |
108 |
} catch (Exception e) { |
} catch (Exception e) { |
109 |
logger.log(Level.SEVERE, "Exception while finding stations", e); |
logger.log(Level.SEVERE, "Exception while finding stations", e); |
110 |
response.sendError(500); |
response.sendError(500); |
111 |
} finally { |
} |
|
try { |
|
|
if (conn != null) { |
|
|
conn.close(); |
|
|
} |
|
|
} catch (Exception t) {} |
|
|
} |
|
112 |
} |
} |
113 |
|
|
114 |
} |
} |