1 |
package dk.thoerup.traininfoservice; |
package dk.thoerup.traininfoservice; |
2 |
|
|
3 |
import java.io.IOException; |
import java.io.IOException; |
|
import java.sql.Connection; |
|
|
import java.sql.PreparedStatement; |
|
|
import java.sql.ResultSet; |
|
4 |
import java.sql.SQLException; |
import java.sql.SQLException; |
5 |
|
import java.util.List; |
6 |
import java.util.logging.Level; |
import java.util.logging.Level; |
7 |
import java.util.logging.Logger; |
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; |
15 |
/** |
/** |
16 |
* Servlet implementation class LocateStations |
* Servlet implementation class LocateStations |
17 |
*/ |
*/ |
18 |
|
@WebServlet(urlPatterns={"/LocateStations"}) |
19 |
public class LocateStations extends HttpServlet { |
public class LocateStations extends HttpServlet { |
20 |
private static final long serialVersionUID = 1L; |
private static final long serialVersionUID = 1L; |
21 |
|
|
22 |
Logger logger = Logger.getLogger( LocateStations.class.toString() ); |
Logger logger = Logger.getLogger( LocateStations.class.toString() ); |
23 |
|
|
24 |
|
StationDAO stationDao = new StationDAO(); |
25 |
|
|
26 |
|
|
|
|
|
27 |
protected String transformToIntList(String input) { |
protected String transformToIntList(String input) { |
28 |
String strings[] = input.split(","); |
String strings[] = input.split(","); |
29 |
|
|
30 |
StringBuffer sb = new StringBuffer(); |
StringBuffer sb = new StringBuffer(); |
31 |
sb.append("("); |
sb.append("("); |
32 |
for (int i = 0; i<strings.length; i++) { |
for (int i = 0; i<strings.length; i++) { |
40 |
} |
} |
41 |
|
|
42 |
|
|
43 |
protected PreparedStatement createStatement(Connection conn, HttpServletRequest req) throws SQLException { |
protected List<StationBean> getStations(HttpServletRequest req) throws SQLException { |
44 |
|
List<StationBean> stations = null; |
|
PreparedStatement stmt = null; |
|
|
String SQL; |
|
|
|
|
45 |
if (req.getParameter("latitude") != null && req.getParameter("latitude") != null) { |
if (req.getParameter("latitude") != null && req.getParameter("latitude") != null) { |
46 |
//inner select is workaround from not being able to use a calculated column directly in where clause |
|
47 |
SQL = "SELECT * FROM ( "+ |
Statistics.getInstance().incrementStationLookupsLocation(); |
48 |
" 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 "; |
|
49 |
double latitude = Double.parseDouble( req.getParameter("latitude") ); |
double latitude = Double.parseDouble( req.getParameter("latitude") ); |
50 |
double longitude = Double.parseDouble( req.getParameter("longitude") ); |
double longitude = Double.parseDouble( req.getParameter("longitude") ); |
51 |
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 "; |
|
52 |
|
|
53 |
|
} else if (req.getParameter("name") != null) { |
54 |
|
Statistics.getInstance().incrementStationLookupsName(); |
55 |
String name = req.getParameter("name").trim(); |
String name = req.getParameter("name").trim(); |
56 |
stmt = conn.prepareStatement(SQL); |
stations = stationDao.getByName(name); |
57 |
stmt.setString(1, name + "%"); |
|
58 |
} else if (req.getParameter("list") != null) { |
} else if (req.getParameter("list") != null) { |
59 |
|
Statistics.getInstance().incrementStationLookupsFavorites(); |
60 |
String list = transformToIntList( req.getParameter("list")); |
String list = transformToIntList( req.getParameter("list")); |
61 |
SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro, address,0.0 " + |
stations = stationDao.getByList(list); |
62 |
"FROM trainstations " + |
} |
63 |
"WHERE id IN " + list + " AND enabled = true " + |
return stations; |
|
"ORDER BY name "; |
|
|
stmt = conn.prepareStatement(SQL); |
|
|
} |
|
|
|
|
|
return stmt; |
|
64 |
} |
} |
65 |
|
|
66 |
|
|
67 |
protected String formatResultset(PreparedStatement stmt) throws SQLException { |
protected String formatStations(List<StationBean> stations) { |
|
ResultSet res = null; |
|
68 |
|
|
69 |
StringBuffer buff = new StringBuffer(); |
StringBuffer buff = new StringBuffer(); |
70 |
|
|
71 |
buff.append("<?xml version=\"1.0\" ?>\n"); |
//buff.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"); |
72 |
buff.append("<stations>\n"); |
buff.append("<stations>\n"); |
73 |
|
|
|
try |
|
|
{ |
|
|
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"); |
|
74 |
|
|
75 |
buff.append("</station>\n"); |
for (int i=0; i<stations.size(); i++) { |
76 |
} |
StationBean station = stations.get(i); |
77 |
} finally { |
|
78 |
if (res != null && !res.isClosed()) |
buff.append("<station>\n"); |
79 |
res.close(); |
|
80 |
if (stmt != null && !stmt.isClosed()) |
buff.append("<id>").append( station.getId() ).append("</id>\n"); |
81 |
stmt.close(); |
buff.append("<name>").append( station.getName() ) .append("</name>\n"); |
82 |
} |
buff.append("<latitude>").append( station.getLatitude() ) .append("</latitude>\n"); |
83 |
|
buff.append("<longitude>").append( station.getLongitude() ) .append("</longitude>\n"); |
84 |
|
buff.append("<regional>").append( station.getRegional() != null ) .append("</regional>\n"); |
85 |
|
buff.append("<strain>").append( station.getStrain() != null ) .append("</strain>\n"); |
86 |
|
buff.append("<metro>").append( station.getMetro() != null ).append("</metro>\n"); |
87 |
|
buff.append("<address>").append( station.getAddress() != null ? station.getAddress() : "").append("</address>"); |
88 |
|
buff.append("<calcdist>").append( station.getCalcdist() ) .append("</calcdist>\n"); |
89 |
|
|
90 |
|
buff.append("</station>\n"); |
91 |
|
} |
92 |
buff.append("</stations>\n"); |
buff.append("</stations>\n"); |
93 |
return buff.toString(); |
return buff.toString(); |
94 |
} |
} |
96 |
@Override |
@Override |
97 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
98 |
|
|
99 |
Connection conn = null; |
|
100 |
try { |
try { |
101 |
conn = DBConnection.getConnection(); |
List<StationBean> stations = getStations(request); |
102 |
|
|
103 |
|
|
104 |
PreparedStatement stmt = createStatement(conn, request); |
if (stations != null){ |
105 |
if (stmt != null){ |
String xml = formatStations(stations); |
|
String xml = formatResultset(stmt); |
|
106 |
|
|
107 |
response.setContentType("text/xml"); |
response.setContentType("text/xml"); |
108 |
response.getWriter().print(xml); |
response.getWriter().print(xml); |
114 |
} catch (Exception e) { |
} catch (Exception e) { |
115 |
logger.log(Level.SEVERE, "Exception while finding stations", e); |
logger.log(Level.SEVERE, "Exception while finding stations", e); |
116 |
response.sendError(500); |
response.sendError(500); |
117 |
} finally { |
} |
|
try { |
|
|
if (conn != null) { |
|
|
conn.close(); |
|
|
} |
|
|
} catch (Exception t) {} |
|
|
} |
|
118 |
} |
} |
119 |
|
|
120 |
} |
} |