1 |
torben |
292 |
package dk.thoerup.traininfoservice; |
2 |
|
|
|
3 |
torben |
1060 |
import java.io.ByteArrayOutputStream; |
4 |
torben |
292 |
import java.io.IOException; |
5 |
|
|
import java.sql.SQLException; |
6 |
torben |
1080 |
import java.util.List; |
7 |
torben |
348 |
import java.util.logging.Level; |
8 |
|
|
import java.util.logging.Logger; |
9 |
torben |
292 |
|
10 |
torben |
1080 |
import javax.jdo.PersistenceManager; |
11 |
|
|
import javax.jdo.Query; |
12 |
torben |
292 |
import javax.servlet.ServletException; |
13 |
|
|
import javax.servlet.http.HttpServlet; |
14 |
|
|
import javax.servlet.http.HttpServletRequest; |
15 |
|
|
import javax.servlet.http.HttpServletResponse; |
16 |
|
|
|
17 |
torben |
1060 |
import org.simpleframework.xml.Serializer; |
18 |
|
|
import org.simpleframework.xml.core.Persister; |
19 |
|
|
|
20 |
torben |
1061 |
import dk.thoerup.android.traininfo.common.StationBean; |
21 |
torben |
1080 |
import dk.thoerup.traininfoservice.jdo.JdoStationBean; |
22 |
|
|
import dk.thoerup.traininfoservice.jdo.PMF; |
23 |
torben |
1061 |
|
24 |
torben |
292 |
/** |
25 |
|
|
* Servlet implementation class LocateStations |
26 |
|
|
*/ |
27 |
torben |
1080 |
|
28 |
torben |
292 |
public class LocateStations extends HttpServlet { |
29 |
|
|
private static final long serialVersionUID = 1L; |
30 |
|
|
|
31 |
torben |
348 |
Logger logger = Logger.getLogger( LocateStations.class.toString() ); |
32 |
torben |
431 |
|
33 |
torben |
588 |
StationDAO stationDao = new StationDAO(); |
34 |
torben |
431 |
|
35 |
torben |
588 |
|
36 |
torben |
431 |
protected String transformToIntList(String input) { |
37 |
|
|
String strings[] = input.split(","); |
38 |
torben |
588 |
|
39 |
torben |
431 |
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 |
|
|
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 |
torben |
347 |
} |
50 |
torben |
322 |
|
51 |
torben |
431 |
|
52 |
torben |
1060 |
protected StationBean getStations(HttpServletRequest req) throws SQLException { |
53 |
|
|
StationBean stations = null; |
54 |
torben |
431 |
if (req.getParameter("latitude") != null && req.getParameter("latitude") != null) { |
55 |
torben |
711 |
|
56 |
|
|
Statistics.getInstance().incrementStationLookupsLocation(); |
57 |
|
|
|
58 |
torben |
431 |
double latitude = Double.parseDouble( req.getParameter("latitude") ); |
59 |
|
|
double longitude = Double.parseDouble( req.getParameter("longitude") ); |
60 |
torben |
588 |
stations = stationDao.getByLocation(latitude, longitude); |
61 |
|
|
|
62 |
torben |
431 |
} else if (req.getParameter("name") != null) { |
63 |
torben |
711 |
Statistics.getInstance().incrementStationLookupsName(); |
64 |
torben |
588 |
String name = req.getParameter("name").trim(); |
65 |
|
|
stations = stationDao.getByName(name); |
66 |
torben |
431 |
|
67 |
|
|
} else if (req.getParameter("list") != null) { |
68 |
torben |
711 |
Statistics.getInstance().incrementStationLookupsFavorites(); |
69 |
torben |
431 |
String list = transformToIntList( req.getParameter("list")); |
70 |
torben |
588 |
stations = stationDao.getByList(list); |
71 |
|
|
} |
72 |
|
|
return stations; |
73 |
torben |
347 |
} |
74 |
|
|
|
75 |
|
|
|
76 |
torben |
1060 |
protected String formatStations(StationBean stations) throws ServletException { |
77 |
torben |
347 |
|
78 |
torben |
1060 |
Serializer serializer = new Persister(); |
79 |
torben |
347 |
|
80 |
torben |
1060 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
81 |
torben |
431 |
|
82 |
torben |
1060 |
try { |
83 |
|
|
serializer.write(stations, out); |
84 |
|
|
} catch (Exception e) { |
85 |
|
|
throw new ServletException(e); |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
return out.toString(); |
89 |
torben |
292 |
} |
90 |
|
|
|
91 |
torben |
1080 |
@SuppressWarnings("unchecked") |
92 |
torben |
425 |
@Override |
93 |
torben |
292 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
94 |
torben |
347 |
|
95 |
torben |
1080 |
if (true) { |
96 |
|
|
PersistenceManager pm = null; |
97 |
|
|
try { |
98 |
|
|
pm = PMF.get().getPersistenceManager(); |
99 |
|
|
|
100 |
|
|
|
101 |
torben |
1093 |
|
102 |
|
|
|
103 |
|
|
JdoStationBean b = new JdoStationBean(); |
104 |
|
|
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 |
torben |
1080 |
|
118 |
torben |
1093 |
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 |
torben |
1080 |
|
127 |
|
|
|
128 |
torben |
1093 |
//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 |
torben |
1080 |
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 |
torben |
588 |
|
151 |
torben |
431 |
try { |
152 |
torben |
1060 |
StationBean stations = getStations(request); |
153 |
torben |
347 |
|
154 |
|
|
|
155 |
torben |
588 |
if (stations != null){ |
156 |
|
|
String xml = formatStations(stations); |
157 |
|
|
|
158 |
torben |
431 |
response.setContentType("text/xml"); |
159 |
|
|
response.getWriter().print(xml); |
160 |
|
|
} else { |
161 |
|
|
response.sendError(400, "not enough parameters"); |
162 |
|
|
} |
163 |
torben |
347 |
|
164 |
|
|
|
165 |
torben |
431 |
} catch (Exception e) { |
166 |
|
|
logger.log(Level.SEVERE, "Exception while finding stations", e); |
167 |
|
|
response.sendError(500); |
168 |
torben |
588 |
} |
169 |
torben |
292 |
} |
170 |
|
|
|
171 |
|
|
} |