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 |
|
import dk.thoerup.traininfoservice.db.StationDAO; |
21 |
|
|
22 |
/** |
/** |
23 |
* Servlet implementation class LocateStations |
* Servlet implementation class LocateStations |
24 |
*/ |
*/ |
25 |
|
@WebServlet(urlPatterns={"/LocateStations"}) |
26 |
public class LocateStations extends HttpServlet { |
public class LocateStations extends HttpServlet { |
27 |
private static final long serialVersionUID = 1L; |
private static final long serialVersionUID = 1L; |
28 |
|
|
29 |
Logger logger = Logger.getLogger( LocateStations.class.toString() ); |
Logger logger = Logger.getLogger( LocateStations.class.toString() ); |
30 |
|
|
31 |
|
StationDAO stationDao = new StationDAO(); |
32 |
|
|
33 |
|
|
|
|
|
34 |
protected String transformToIntList(String input) { |
protected String transformToIntList(String input) { |
35 |
String strings[] = input.split(","); |
String strings[] = input.split(","); |
36 |
|
|
37 |
StringBuffer sb = new StringBuffer(); |
StringBuffer sb = new StringBuffer(); |
38 |
sb.append("("); |
sb.append("("); |
39 |
for (int i = 0; i<strings.length; i++) { |
for (int i = 0; i<strings.length; i++) { |
47 |
} |
} |
48 |
|
|
49 |
|
|
50 |
protected PreparedStatement createStatement(Connection conn, HttpServletRequest req) throws SQLException { |
protected StationBean getStations(HttpServletRequest req) throws SQLException { |
51 |
|
StationBean stations = null; |
52 |
PreparedStatement stmt = null; |
|
53 |
String SQL; |
boolean dummy = req.getParameter("dummy") != null; |
54 |
|
if (dummy == true) |
55 |
|
stations = new StationBean(); |
56 |
|
|
57 |
if (req.getParameter("latitude") != null && req.getParameter("latitude") != null) { |
if (req.getParameter("latitude") != null && req.getParameter("latitude") != null) { |
58 |
//inner select is workaround from not being able to use a calculated column directly in where clause |
|
59 |
SQL = "SELECT * FROM ( "+ |
Statistics.getInstance().incrementStationLookupsLocation(); |
60 |
" 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 "; |
|
61 |
double latitude = Double.parseDouble( req.getParameter("latitude") ); |
double latitude = Double.parseDouble( req.getParameter("latitude") ); |
62 |
double longitude = Double.parseDouble( req.getParameter("longitude") ); |
double longitude = Double.parseDouble( req.getParameter("longitude") ); |
63 |
stmt = conn.prepareStatement(SQL); |
if (dummy == false) |
64 |
stmt.setDouble(1, latitude); |
stations = stationDao.getByLocation(latitude, longitude); |
|
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 "; |
|
65 |
|
|
66 |
|
} else if (req.getParameter("name") != null) { |
67 |
|
Statistics.getInstance().incrementStationLookupsName(); |
68 |
String name = req.getParameter("name").trim(); |
String name = req.getParameter("name").trim(); |
69 |
stmt = conn.prepareStatement(SQL); |
if (dummy == false) |
70 |
stmt.setString(1, name + "%"); |
stations = stationDao.getByName(name); |
71 |
|
|
72 |
} else if (req.getParameter("list") != null) { |
} else if (req.getParameter("list") != null) { |
73 |
|
Statistics.getInstance().incrementStationLookupsFavorites(); |
74 |
String list = transformToIntList( req.getParameter("list")); |
String list = transformToIntList( req.getParameter("list")); |
75 |
SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro, address,0.0 " + |
if (dummy == false) |
76 |
"FROM trainstations " + |
stations = stationDao.getByList(list); |
77 |
"WHERE id IN " + list + " AND enabled = true " + |
} else if (req.getParameter("dump") != null) { |
78 |
"ORDER BY name "; |
stations = stationDao.dumpAll(); |
79 |
stmt = conn.prepareStatement(SQL); |
} |
80 |
} |
return stations; |
|
|
|
|
return stmt; |
|
81 |
} |
} |
82 |
|
|
83 |
|
|
84 |
protected String formatResultset(PreparedStatement stmt) throws SQLException { |
protected String formatStations(StationBean stations) throws ServletException { |
|
ResultSet res = null; |
|
|
|
|
|
StringBuffer buff = new StringBuffer(); |
|
85 |
|
|
86 |
buff.append("<?xml version=\"1.0\" ?>\n"); |
Serializer serializer = new Persister(); |
|
buff.append("<stations>\n"); |
|
87 |
|
|
88 |
try |
StringWriter out = new StringWriter(); |
|
{ |
|
|
res = stmt.executeQuery(); |
|
|
|
|
|
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"); |
|
89 |
|
|
90 |
buff.append("</station>\n"); |
try { |
91 |
} |
serializer.write(stations, out); |
92 |
} finally { |
} catch (Exception e) { |
93 |
if (res != null && !res.isClosed()) |
throw new ServletException(e); |
|
res.close(); |
|
|
if (stmt != null && !stmt.isClosed()) |
|
|
stmt.close(); |
|
94 |
} |
} |
95 |
buff.append("</stations>\n"); |
|
96 |
return buff.toString(); |
return out.toString(); |
97 |
} |
} |
98 |
|
|
99 |
@Override |
@Override |
100 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
101 |
|
|
102 |
Connection conn = null; |
|
103 |
try { |
try { |
104 |
conn = DBConnection.getConnection(); |
StationBean stations = getStations(request); |
105 |
|
|
106 |
PreparedStatement stmt = createStatement(conn, request); |
|
107 |
if (stmt != null){ |
if (stations != null){ |
108 |
String xml = formatResultset(stmt); |
String xml = formatStations(stations); |
109 |
|
|
110 |
response.setContentType("text/xml"); |
response.setContentType("text/xml"); |
111 |
response.getWriter().print(xml); |
response.getWriter().print(xml); |
117 |
} catch (Exception e) { |
} catch (Exception e) { |
118 |
logger.log(Level.SEVERE, "Exception while finding stations", e); |
logger.log(Level.SEVERE, "Exception while finding stations", e); |
119 |
response.sendError(500); |
response.sendError(500); |
120 |
} finally { |
} |
|
try { |
|
|
if (conn != null) { |
|
|
conn.close(); |
|
|
} |
|
|
} catch (Exception t) {} |
|
|
} |
|
121 |
} |
} |
122 |
|
|
123 |
} |
} |