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; |
import java.util.logging.Level; |
8 |
import java.util.logging.Logger; |
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; |
30 |
|
|
31 |
Logger logger = Logger.getLogger( LocateStations.class.toString() ); |
Logger logger = Logger.getLogger( LocateStations.class.toString() ); |
|
|
|
|
/** |
|
|
* @see HttpServlet#HttpServlet() |
|
|
*/ |
|
|
public LocateStations() { |
|
|
super(); |
|
|
// TODO Auto-generated constructor stub |
|
|
} |
|
|
|
|
|
public enum Requested { |
|
|
BY_NAME, |
|
|
BY_LOCATION, |
|
|
NONE |
|
|
} |
|
|
|
|
32 |
|
|
33 |
protected String getStations(Connection conn, double latitude, double longitude, String name, Requested method) throws SQLException { |
StationDAO stationDao = new StationDAO(); |
|
String SQL = ""; |
|
|
PreparedStatement stmt = null; |
|
|
ResultSet res = null; |
|
34 |
|
|
|
StringBuffer buff = new StringBuffer(); |
|
35 |
|
|
36 |
buff.append("<?xml version=\"1.0\" ?>\n"); |
protected String transformToIntList(String input) { |
37 |
buff.append("<stations>\n"); |
String strings[] = input.split(","); |
|
|
|
|
try |
|
|
{ |
|
38 |
|
|
39 |
switch (method) |
StringBuffer sb = new StringBuffer(); |
40 |
{ |
sb.append("("); |
41 |
case BY_LOCATION: |
for (int i = 0; i<strings.length; i++) { |
42 |
//inner select is workaround from not being able to use a calculated column directly in where clause |
if (i>0) { |
43 |
SQL = "SELECT * FROM ( "+ |
sb.append(","); |
|
" 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 latitude IS NOT NULL AND longitude IS NOT NULL " + |
|
|
" ) AS trainstations2 " + |
|
|
"ORDER BY calcdist ASC " + |
|
|
"LIMIT 4 "; |
|
|
stmt = conn.prepareStatement(SQL); |
|
|
stmt.setDouble(1, latitude); |
|
|
stmt.setDouble(2, longitude); |
|
|
|
|
|
break; |
|
|
case BY_NAME: |
|
|
SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,0.0 " + |
|
|
"FROM trainstations " + |
|
|
"WHERE name ILIKE ? AND latitude IS NOT NULL AND longitude IS NOT NULL " + |
|
|
"ORDER BY name "; |
|
|
stmt = conn.prepareStatement(SQL); |
|
|
stmt.setString(1, "%" + name + "%"); |
|
|
break; |
|
|
default: |
|
|
// This should not be possible |
|
|
logger.severe("getStations(): default switch case"); |
|
44 |
} |
} |
45 |
|
sb.append( Integer.parseInt(strings[i])); //by doing the integer conversion we ensure that it really is a integer |
46 |
|
} |
47 |
|
sb.append(")"); |
48 |
|
return sb.toString(); |
49 |
|
} |
50 |
|
|
51 |
|
|
52 |
|
protected StationBean getStations(HttpServletRequest req) throws SQLException { |
53 |
|
StationBean stations = null; |
54 |
|
if (req.getParameter("latitude") != null && req.getParameter("latitude") != null) { |
55 |
|
|
56 |
|
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 |
res = stmt.executeQuery(); |
protected String formatStations(StationBean stations) throws ServletException { |
77 |
|
|
78 |
while (res.next()) { |
Serializer serializer = new Persister(); |
|
buff.append("<station>\n"); |
|
79 |
|
|
80 |
buff.append("<id>").append( res.getInt(1) ).append("</id>\n"); |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
|
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"); |
|
81 |
|
|
82 |
buff.append("</station>\n"); |
try { |
83 |
} |
serializer.write(stations, out); |
84 |
} finally { |
} catch (Exception e) { |
85 |
if (res != null && !res.isClosed()) |
throw new ServletException(e); |
|
res.close(); |
|
|
if (stmt != null && !stmt.isClosed()) |
|
|
stmt.close(); |
|
86 |
} |
} |
87 |
buff.append("</stations>\n"); |
|
88 |
return buff.toString(); |
return out.toString(); |
89 |
} |
} |
90 |
|
|
91 |
|
@SuppressWarnings("unchecked") |
92 |
|
@Override |
93 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
94 |
|
|
95 |
double longitude = 0.0; |
if (true) { |
96 |
double latitude = 0.0; |
PersistenceManager pm = null; |
97 |
String name = ""; |
try { |
98 |
|
pm = PMF.get().getPersistenceManager(); |
99 |
Requested method = Requested.NONE; |
|
100 |
|
|
|
if (request.getParameter("latitude") != null && request.getParameter("latitude") != null) { |
|
|
latitude = Double.parseDouble( request.getParameter("latitude") ); |
|
|
longitude = Double.parseDouble( request.getParameter("longitude") ); |
|
|
method = Requested.BY_LOCATION; |
|
|
} |
|
101 |
|
|
102 |
if (request.getParameter("name") != null) { |
|
103 |
name = request.getParameter("name"); |
JdoStationBean b = new JdoStationBean(); |
104 |
method = Requested.BY_NAME; |
b.setId(1000); |
105 |
|
b.setName("TestStation"); |
106 |
|
b.setNameLower( b.getName().toLowerCase() ); |
107 |
|
b.setMetro("12"); |
108 |
|
pm.makePersistent(b); |
109 |
|
|
110 |
|
|
111 |
|
JdoStationBean b2 = new JdoStationBean(); |
112 |
|
b2.setId(1001); |
113 |
|
b2.setName("teststation 2"); |
114 |
|
b2.setNameLower( b2.getName().toLowerCase() ); |
115 |
|
b2.setMetro("12"); |
116 |
|
pm.makePersistent(b2); |
117 |
|
|
118 |
|
JdoStationBean b3 = new JdoStationBean(); |
119 |
|
b3.setId(1002); |
120 |
|
b3.setName("Horsens"); |
121 |
|
b3.setNameLower( b3.getName().toLowerCase() ); |
122 |
|
b3.setMetro("13"); |
123 |
|
pm.makePersistent(b3); |
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
//String query = "select from " + JdoStationBean.class.getName();// + " where nameLower.startsWith('test')"; |
129 |
|
|
130 |
|
String query = "select from " + JdoStationBean.class.getName() + " where (id == 1001 || id == 1002) "; |
131 |
|
|
132 |
|
List<JdoStationBean> stations = (List<JdoStationBean>) pm.newQuery( query ).execute(); |
133 |
|
|
134 |
|
logger.info("size=" + stations.size() ); |
135 |
|
for(JdoStationBean bean : stations) { |
136 |
|
logger.info("Station: " + bean.getId() + "/" + bean.getName()); |
137 |
|
} |
138 |
|
|
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
} finally { |
143 |
|
if (pm != null) |
144 |
|
pm.close(); |
145 |
|
} |
146 |
|
|
147 |
|
|
148 |
|
return; |
149 |
} |
} |
150 |
|
|
151 |
|
try { |
152 |
|
StationBean stations = getStations(request); |
153 |
|
|
|
if (method != Requested.NONE) { |
|
154 |
|
|
155 |
Connection conn = null; |
if (stations != null){ |
156 |
try { |
String xml = formatStations(stations); |
|
conn = DBConnection.getConnection(); |
|
|
|
|
|
String xml = getStations(conn, latitude, longitude, name, method); |
|
157 |
|
|
158 |
response.setContentType("text/xml"); |
response.setContentType("text/xml"); |
159 |
response.getWriter().print(xml); |
response.getWriter().print(xml); |
160 |
|
} else { |
161 |
conn.close(); |
response.sendError(400, "not enough parameters"); |
|
conn = null; |
|
|
|
|
|
} catch (Exception e) { |
|
|
logger.log(Level.SEVERE, "Exception while finding stations", e); |
|
|
response.sendError(500); |
|
|
} finally { |
|
|
try { |
|
|
if (conn != null) |
|
|
conn.close(); |
|
|
} catch (Throwable t) {} |
|
162 |
} |
} |
163 |
} else { |
|
164 |
response.sendError(400, "not enough parameters"); |
|
165 |
} |
} catch (Exception e) { |
166 |
|
logger.log(Level.SEVERE, "Exception while finding stations", e); |
167 |
|
response.sendError(500); |
168 |
|
} |
169 |
} |
} |
170 |
|
|
171 |
} |
} |