1 |
package dk.thoerup.traininfoservice; |
package dk.thoerup.traininfoservice; |
2 |
|
|
3 |
|
import java.io.ByteArrayOutputStream; |
4 |
import java.io.IOException; |
import java.io.IOException; |
5 |
import java.sql.SQLException; |
import java.sql.SQLException; |
|
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; |
14 |
|
|
15 |
|
import org.simpleframework.xml.Serializer; |
16 |
|
import org.simpleframework.xml.core.Persister; |
17 |
|
|
18 |
|
import dk.thoerup.android.traininfo.common.StationBean; |
19 |
|
|
20 |
/** |
/** |
21 |
* Servlet implementation class LocateStations |
* Servlet implementation class LocateStations |
22 |
*/ |
*/ |
23 |
|
@WebServlet(urlPatterns={"/LocateStations"}) |
24 |
public class LocateStations extends HttpServlet { |
public class LocateStations extends HttpServlet { |
25 |
private static final long serialVersionUID = 1L; |
private static final long serialVersionUID = 1L; |
26 |
|
|
45 |
} |
} |
46 |
|
|
47 |
|
|
48 |
protected List<StationBean> getStations(HttpServletRequest req) throws SQLException { |
protected StationBean getStations(HttpServletRequest req) throws SQLException { |
49 |
List<StationBean> stations = null; |
StationBean stations = null; |
50 |
if (req.getParameter("latitude") != null && req.getParameter("latitude") != null) { |
if (req.getParameter("latitude") != null && req.getParameter("latitude") != null) { |
51 |
|
|
52 |
|
Statistics.getInstance().incrementStationLookupsLocation(); |
53 |
|
|
54 |
double latitude = Double.parseDouble( req.getParameter("latitude") ); |
double latitude = Double.parseDouble( req.getParameter("latitude") ); |
55 |
double longitude = Double.parseDouble( req.getParameter("longitude") ); |
double longitude = Double.parseDouble( req.getParameter("longitude") ); |
56 |
stations = stationDao.getByLocation(latitude, longitude); |
stations = stationDao.getByLocation(latitude, longitude); |
57 |
|
|
58 |
} else if (req.getParameter("name") != null) { |
} else if (req.getParameter("name") != null) { |
59 |
|
Statistics.getInstance().incrementStationLookupsName(); |
60 |
String name = req.getParameter("name").trim(); |
String name = req.getParameter("name").trim(); |
61 |
stations = stationDao.getByName(name); |
stations = stationDao.getByName(name); |
62 |
|
|
63 |
} else if (req.getParameter("list") != null) { |
} else if (req.getParameter("list") != null) { |
64 |
|
Statistics.getInstance().incrementStationLookupsFavorites(); |
65 |
String list = transformToIntList( req.getParameter("list")); |
String list = transformToIntList( req.getParameter("list")); |
66 |
stations = stationDao.getByList(list); |
stations = stationDao.getByList(list); |
67 |
} |
} |
69 |
} |
} |
70 |
|
|
71 |
|
|
72 |
protected String formatStations(List<StationBean> stations) { |
protected String formatStations(StationBean stations) throws ServletException { |
|
|
|
|
StringBuffer buff = new StringBuffer(); |
|
|
|
|
|
buff.append("<?xml version=\"1.0\" ?>\n"); |
|
|
buff.append("<stations>\n"); |
|
73 |
|
|
74 |
|
Serializer serializer = new Persister(); |
75 |
|
|
76 |
for (int i=0; i<stations.size(); i++) { |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
|
StationBean station = stations.get(i); |
|
77 |
|
|
78 |
buff.append("<station>\n"); |
try { |
79 |
|
serializer.write(stations, out); |
80 |
buff.append("<id>").append( station.getId() ).append("</id>\n"); |
} catch (Exception e) { |
81 |
buff.append("<name>").append( station.getName() ) .append("</name>\n"); |
throw new ServletException(e); |
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"); |
return out.toString(); |
|
buff.append("<strain>").append( station.getStrain() != null ) .append("</strain>\n"); |
|
|
buff.append("<metro>").append( station.getMetro() != null ).append("</metro>\n"); |
|
|
buff.append("<address>").append( station.getAddress() != null ? station.getAddress() : "").append("</address>"); |
|
|
buff.append("<calcdist>").append( station.getCalcdist() ) .append("</calcdist>\n"); |
|
|
|
|
|
buff.append("</station>\n"); |
|
|
} |
|
|
buff.append("</stations>\n"); |
|
|
return buff.toString(); |
|
85 |
} |
} |
86 |
|
|
87 |
@Override |
@Override |
89 |
|
|
90 |
|
|
91 |
try { |
try { |
92 |
List<StationBean> stations = getStations(request); |
StationBean stations = getStations(request); |
93 |
|
|
94 |
|
|
95 |
if (stations != null){ |
if (stations != null){ |