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 |
|
|
19 |
|
|
20 |
Logger logger = Logger.getLogger( LocateStations.class.toString() ); |
Logger logger = Logger.getLogger( LocateStations.class.toString() ); |
21 |
|
|
22 |
|
StationDAO stationDao = new StationDAO(); |
23 |
|
|
24 |
|
|
|
|
|
25 |
protected String transformToIntList(String input) { |
protected String transformToIntList(String input) { |
26 |
String strings[] = input.split(","); |
String strings[] = input.split(","); |
27 |
|
|
28 |
StringBuffer sb = new StringBuffer(); |
StringBuffer sb = new StringBuffer(); |
29 |
sb.append("("); |
sb.append("("); |
30 |
for (int i = 0; i<strings.length; i++) { |
for (int i = 0; i<strings.length; i++) { |
38 |
} |
} |
39 |
|
|
40 |
|
|
41 |
protected PreparedStatement createStatement(Connection conn, HttpServletRequest req) throws SQLException { |
protected List<StationBean> getStations(HttpServletRequest req) throws SQLException { |
42 |
|
List<StationBean> stations = null; |
|
PreparedStatement stmt = null; |
|
|
String SQL; |
|
|
|
|
43 |
if (req.getParameter("latitude") != null && req.getParameter("latitude") != null) { |
if (req.getParameter("latitude") != null && req.getParameter("latitude") != null) { |
|
//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 enabled = true " + |
|
|
" ) AS trainstations2 " + |
|
|
"ORDER BY calcdist ASC " + |
|
|
"LIMIT 4 "; |
|
44 |
double latitude = Double.parseDouble( req.getParameter("latitude") ); |
double latitude = Double.parseDouble( req.getParameter("latitude") ); |
45 |
double longitude = Double.parseDouble( req.getParameter("longitude") ); |
double longitude = Double.parseDouble( req.getParameter("longitude") ); |
46 |
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,0.0 " + |
|
|
"FROM trainstations " + |
|
|
"WHERE name ILIKE ? AND enabled = true " + |
|
|
"ORDER BY name "; |
|
47 |
|
|
48 |
|
} else if (req.getParameter("name") != null) { |
49 |
String name = req.getParameter("name").trim(); |
String name = req.getParameter("name").trim(); |
50 |
stmt = conn.prepareStatement(SQL); |
stations = stationDao.getByName(name); |
51 |
stmt.setString(1, name + "%"); |
|
52 |
} else if (req.getParameter("list") != null) { |
} else if (req.getParameter("list") != null) { |
53 |
String list = transformToIntList( req.getParameter("list")); |
String list = transformToIntList( req.getParameter("list")); |
54 |
SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,0.0 " + |
stations = stationDao.getByList(list); |
55 |
"FROM trainstations " + |
} |
56 |
"WHERE id IN " + list + " AND enabled = true " + |
return stations; |
|
"ORDER BY name "; |
|
|
stmt = conn.prepareStatement(SQL); |
|
|
} |
|
|
|
|
|
return stmt; |
|
57 |
} |
} |
58 |
|
|
59 |
|
|
60 |
protected String formatResultset(PreparedStatement stmt) throws SQLException { |
protected String formatStations(List<StationBean> stations) { |
|
ResultSet res = null; |
|
61 |
|
|
62 |
StringBuffer buff = new StringBuffer(); |
StringBuffer buff = new StringBuffer(); |
63 |
|
|
64 |
buff.append("<?xml version=\"1.0\" ?>\n"); |
buff.append("<?xml version=\"1.0\" ?>\n"); |
65 |
buff.append("<stations>\n"); |
buff.append("<stations>\n"); |
66 |
|
|
|
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"); |
|
|
buff.append("<calcdist>").append( res.getInt(7) ) .append("</calcdist>\n"); |
|
67 |
|
|
68 |
buff.append("</station>\n"); |
for (int i=0; i<stations.size(); i++) { |
69 |
} |
StationBean station = stations.get(i); |
70 |
} finally { |
|
71 |
if (res != null && !res.isClosed()) |
buff.append("<station>\n"); |
72 |
res.close(); |
|
73 |
if (stmt != null && !stmt.isClosed()) |
buff.append("<id>").append( station.getId() ).append("</id>\n"); |
74 |
stmt.close(); |
buff.append("<name>").append( station.getName() ) .append("</name>\n"); |
75 |
} |
buff.append("<latitude>").append( station.getLatitude() ) .append("</latitude>\n"); |
76 |
|
buff.append("<longitude>").append( station.getLongitude() ) .append("</longitude>\n"); |
77 |
|
buff.append("<regional>").append( station.getRegional() != null ) .append("</regional>\n"); |
78 |
|
buff.append("<strain>").append( station.getStrain() != null ) .append("</strain>\n"); |
79 |
|
buff.append("<metro>").append( station.getMetro() != null ).append("</metro>\n"); |
80 |
|
buff.append("<address>").append( station.getAddress() != null ? station.getAddress() : "").append("</address>"); |
81 |
|
buff.append("<calcdist>").append( station.getCalcdist() ) .append("</calcdist>\n"); |
82 |
|
|
83 |
|
buff.append("</station>\n"); |
84 |
|
} |
85 |
buff.append("</stations>\n"); |
buff.append("</stations>\n"); |
86 |
return buff.toString(); |
return buff.toString(); |
87 |
} |
} |
89 |
@Override |
@Override |
90 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
91 |
|
|
92 |
Connection conn = null; |
|
93 |
try { |
try { |
94 |
conn = DBConnection.getConnection(); |
List<StationBean> stations = getStations(request); |
95 |
|
|
96 |
|
|
97 |
PreparedStatement stmt = createStatement(conn, request); |
if (stations != null){ |
98 |
if (stmt != null){ |
String xml = formatStations(stations); |
|
String xml = formatResultset(stmt); |
|
99 |
|
|
100 |
response.setContentType("text/xml"); |
response.setContentType("text/xml"); |
101 |
response.getWriter().print(xml); |
response.getWriter().print(xml); |
107 |
} catch (Exception e) { |
} catch (Exception e) { |
108 |
logger.log(Level.SEVERE, "Exception while finding stations", e); |
logger.log(Level.SEVERE, "Exception while finding stations", e); |
109 |
response.sendError(500); |
response.sendError(500); |
110 |
} finally { |
} |
|
try { |
|
|
if (conn != null) { |
|
|
conn.close(); |
|
|
} |
|
|
} catch (Throwable t) {} |
|
|
} |
|
111 |
} |
} |
112 |
|
|
113 |
} |
} |