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.List; |
7 |
|
import java.util.logging.Level; |
8 |
|
import java.util.logging.Logger; |
9 |
|
|
10 |
|
import javax.jdo.PersistenceManager; |
11 |
|
import javax.jdo.Query; |
12 |
import javax.servlet.ServletException; |
import javax.servlet.ServletException; |
13 |
import javax.servlet.http.HttpServlet; |
import javax.servlet.http.HttpServlet; |
14 |
import javax.servlet.http.HttpServletRequest; |
import javax.servlet.http.HttpServletRequest; |
15 |
import javax.servlet.http.HttpServletResponse; |
import javax.servlet.http.HttpServletResponse; |
16 |
|
|
17 |
import dk.thoerup.traininfoservice.DBConnection; |
import org.simpleframework.xml.Serializer; |
18 |
|
import org.simpleframework.xml.core.Persister; |
19 |
|
|
20 |
|
import dk.thoerup.android.traininfo.common.StationBean; |
21 |
|
import dk.thoerup.traininfoservice.jdo.JdoStationBean; |
22 |
|
import dk.thoerup.traininfoservice.jdo.PMF; |
23 |
|
|
24 |
/** |
/** |
25 |
* Servlet implementation class LocateStations |
* Servlet implementation class LocateStations |
26 |
*/ |
*/ |
27 |
|
|
28 |
public class LocateStations extends HttpServlet { |
public class LocateStations extends HttpServlet { |
29 |
private static final long serialVersionUID = 1L; |
private static final long serialVersionUID = 1L; |
|
|
|
|
/** |
|
|
* @see HttpServlet#HttpServlet() |
|
|
*/ |
|
|
public LocateStations() { |
|
|
super(); |
|
|
// TODO Auto-generated constructor stub |
|
|
} |
|
|
|
|
|
protected String getStations(Connection conn, double latitude, double longitude) throws SQLException { |
|
|
//inner select is workaround from not being able to use a calculated column directly in where clause |
|
|
final String SQL = "SELECT * FROM ( "+ |
|
|
" SELECT name,latitude,longitude,address,stationcode, " + |
|
|
" earth_distance( ll_to_earth(latitude,longitude), ll_to_earth(?,?))::int AS calcdist " + |
|
|
" FROM trainstations " + |
|
|
" ) AS trainstations2 " + |
|
|
"ORDER BY calcdist ASC " + |
|
|
"LIMIT 4 "; |
|
|
|
|
|
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); |
|
|
stmt.setDouble(1, latitude); |
|
|
stmt.setDouble(2, longitude); |
|
|
|
|
|
res = stmt.executeQuery(); |
|
|
|
|
|
while (res.next()) { |
|
|
buff.append("<station>\n"); |
|
30 |
|
|
31 |
buff.append("<name>").append( res.getString(1) ) .append("</name>\n"); |
Logger logger = Logger.getLogger( LocateStations.class.toString() ); |
32 |
buff.append("<latitude>").append( res.getDouble(2) ) .append("</latitude>\n"); |
|
33 |
buff.append("<longitude>").append( res.getDouble(3) ) .append("</longitude>\n"); |
StationDAO stationDao = new StationDAO(); |
34 |
buff.append("<address>").append( res.getString(4) ) .append("</address>\n"); |
|
35 |
buff.append("<stationcode>").append( res.getString(5) ) .append("</stationcode>\n"); |
|
36 |
buff.append("<calcdist>").append( res.getDouble(6) ) .append("</calcdist>\n"); |
protected String transformToIntList(String input) { |
37 |
|
String strings[] = input.split(","); |
38 |
buff.append("</station>\n"); |
|
39 |
|
StringBuffer sb = new StringBuffer(); |
40 |
|
sb.append("("); |
41 |
|
for (int i = 0; i<strings.length; i++) { |
42 |
|
if (i>0) { |
43 |
|
sb.append(","); |
44 |
} |
} |
45 |
} 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(); |
|
46 |
} |
} |
47 |
buff.append("</stations>\n"); |
sb.append(")"); |
48 |
return buff.toString(); |
return sb.toString(); |
49 |
} |
} |
50 |
|
|
51 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
|
52 |
|
protected StationBean getStations(HttpServletRequest req) throws SQLException { |
53 |
double latitude = Double.parseDouble( request.getParameter("latitude") ); |
StationBean stations = null; |
54 |
double longitude = Double.parseDouble( request.getParameter("longitude") ); |
if (req.getParameter("latitude") != null && req.getParameter("latitude") != null) { |
55 |
|
|
56 |
Connection conn = null; |
Statistics.getInstance().incrementStationLookupsLocation(); |
57 |
|
|
58 |
|
double latitude = Double.parseDouble( req.getParameter("latitude") ); |
59 |
|
double longitude = Double.parseDouble( req.getParameter("longitude") ); |
60 |
|
stations = stationDao.getByLocation(latitude, longitude); |
61 |
|
|
62 |
|
} else if (req.getParameter("name") != null) { |
63 |
|
Statistics.getInstance().incrementStationLookupsName(); |
64 |
|
String name = req.getParameter("name").trim(); |
65 |
|
stations = stationDao.getByName(name); |
66 |
|
|
67 |
|
} else if (req.getParameter("list") != null) { |
68 |
|
Statistics.getInstance().incrementStationLookupsFavorites(); |
69 |
|
String list = transformToIntList( req.getParameter("list")); |
70 |
|
stations = stationDao.getByList(list); |
71 |
|
} |
72 |
|
return stations; |
73 |
|
} |
74 |
|
|
75 |
|
|
76 |
|
protected String formatStations(StationBean stations) throws ServletException { |
77 |
|
|
78 |
|
Serializer serializer = new Persister(); |
79 |
|
|
80 |
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
81 |
|
|
82 |
try { |
try { |
83 |
conn = DBConnection.getConnection(); |
serializer.write(stations, out); |
|
|
|
|
String xml = getStations(conn, latitude, longitude); |
|
|
|
|
|
response.setContentType("text/xml"); |
|
|
response.getWriter().print(xml); |
|
|
|
|
|
conn.close(); |
|
|
conn = null; |
|
|
|
|
84 |
} catch (Exception e) { |
} catch (Exception e) { |
85 |
throw new ServletException(e); |
throw new ServletException(e); |
86 |
} finally { |
} |
87 |
|
|
88 |
|
return out.toString(); |
89 |
|
} |
90 |
|
|
91 |
|
@SuppressWarnings("unchecked") |
92 |
|
@Override |
93 |
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
94 |
|
|
95 |
|
if (true) { |
96 |
|
PersistenceManager pm = null; |
97 |
try { |
try { |
98 |
if (conn != null) |
pm = PMF.get().getPersistenceManager(); |
99 |
conn.close(); |
|
100 |
} catch (Throwable t) {} |
|
101 |
|
|
102 |
|
|
103 |
|
String query = "select from " + JdoStationBean.class.getName() + " where name.matches('Test.*')"; |
104 |
|
List<JdoStationBean> stations = (List<JdoStationBean>) pm.newQuery(query).execute(); |
105 |
|
|
106 |
|
logger.info("size=" + stations.size() ); |
107 |
|
for(JdoStationBean bean : stations) { |
108 |
|
logger.info("Station: " + bean.getId() + "/" + bean.getName()); |
109 |
|
} |
110 |
|
|
111 |
|
/*if (stations.size() == 0) { |
112 |
|
JdoStationBean b = new JdoStationBean(); |
113 |
|
b.setId(1000); |
114 |
|
b.setName("TestStation"); |
115 |
|
b.setMetro("12"); |
116 |
|
pm.makePersistent(b); |
117 |
|
|
118 |
|
JdoStationBean b2 = new JdoStationBean(); |
119 |
|
b2.setId(1001); |
120 |
|
b2.setName("teststation 2"); |
121 |
|
b2.setMetro("12"); |
122 |
|
pm.makePersistent(b2); |
123 |
|
}*/ |
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
} finally { |
128 |
|
if (pm != null) |
129 |
|
pm.close(); |
130 |
|
} |
131 |
|
|
132 |
|
|
133 |
|
return; |
134 |
} |
} |
135 |
|
|
136 |
|
try { |
137 |
|
StationBean stations = getStations(request); |
138 |
|
|
139 |
|
|
140 |
|
if (stations != null){ |
141 |
|
String xml = formatStations(stations); |
142 |
|
|
143 |
|
response.setContentType("text/xml"); |
144 |
|
response.getWriter().print(xml); |
145 |
|
} else { |
146 |
|
response.sendError(400, "not enough parameters"); |
147 |
|
} |
148 |
|
|
149 |
|
|
150 |
|
} catch (Exception e) { |
151 |
|
logger.log(Level.SEVERE, "Exception while finding stations", e); |
152 |
|
response.sendError(500); |
153 |
|
} |
154 |
} |
} |
155 |
|
|
156 |
} |
} |