1 |
package dk.thoerup.traininfoservice; |
package dk.thoerup.traininfoservice; |
2 |
|
|
3 |
|
|
4 |
import java.io.IOException; |
import java.io.IOException; |
5 |
|
import java.io.StringWriter; |
6 |
import java.sql.SQLException; |
import java.sql.SQLException; |
|
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.servlet.ServletException; |
import javax.servlet.ServletException; |
11 |
|
import javax.servlet.annotation.WebServlet; |
12 |
import javax.servlet.http.HttpServlet; |
import javax.servlet.http.HttpServlet; |
13 |
import javax.servlet.http.HttpServletRequest; |
import javax.servlet.http.HttpServletRequest; |
14 |
import javax.servlet.http.HttpServletResponse; |
import javax.servlet.http.HttpServletResponse; |
15 |
|
|
16 |
|
import org.simpleframework.xml.Serializer; |
17 |
|
import org.simpleframework.xml.core.Persister; |
18 |
|
|
19 |
|
import dk.thoerup.android.traininfo.common.StationBean; |
20 |
|
import dk.thoerup.traininfoservice.db.StationDAO; |
21 |
|
|
22 |
/** |
/** |
23 |
* Servlet implementation class LocateStations |
* Servlet implementation class LocateStations |
24 |
*/ |
*/ |
25 |
|
@WebServlet(urlPatterns={"/LocateStations"}) |
26 |
public class LocateStations extends HttpServlet { |
public class LocateStations extends HttpServlet { |
27 |
private static final long serialVersionUID = 1L; |
private static final long serialVersionUID = 1L; |
28 |
|
|
47 |
} |
} |
48 |
|
|
49 |
|
|
50 |
protected List<StationBean> getStations(HttpServletRequest req) throws SQLException { |
protected StationBean getStations(HttpServletRequest req) throws SQLException { |
51 |
List<StationBean> stations = null; |
StationBean stations = null; |
52 |
if (req.getParameter("latitude") != null && req.getParameter("latitude") != null) { |
if (req.getParameter("latitude") != null && req.getParameter("latitude") != null) { |
53 |
|
|
54 |
Statistics.getInstance().incrementStationLookupsLocation(); |
Statistics.getInstance().incrementStationLookupsLocation(); |
66 |
Statistics.getInstance().incrementStationLookupsFavorites(); |
Statistics.getInstance().incrementStationLookupsFavorites(); |
67 |
String list = transformToIntList( req.getParameter("list")); |
String list = transformToIntList( req.getParameter("list")); |
68 |
stations = stationDao.getByList(list); |
stations = stationDao.getByList(list); |
69 |
|
} else if (req.getParameter("dump") != null) { |
70 |
|
stations = stationDao.dumpAll(); |
71 |
} |
} |
72 |
return stations; |
return stations; |
73 |
} |
} |
74 |
|
|
75 |
|
|
76 |
protected String formatStations(List<StationBean> stations) { |
protected String formatStations(StationBean stations) throws ServletException { |
|
|
|
|
StringBuffer buff = new StringBuffer(); |
|
|
|
|
|
//buff.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"); |
|
|
buff.append("<stations>\n"); |
|
77 |
|
|
78 |
|
Serializer serializer = new Persister(); |
79 |
|
|
80 |
for (int i=0; i<stations.size(); i++) { |
StringWriter out = new StringWriter(); |
|
StationBean station = stations.get(i); |
|
81 |
|
|
82 |
buff.append("<station>\n"); |
try { |
83 |
|
serializer.write(stations, out); |
84 |
buff.append("<id>").append( station.getId() ).append("</id>\n"); |
} catch (Exception e) { |
85 |
buff.append("<name>").append( station.getName() ) .append("</name>\n"); |
throw new ServletException(e); |
86 |
buff.append("<latitude>").append( station.getLatitude() ) .append("</latitude>\n"); |
} |
87 |
buff.append("<longitude>").append( station.getLongitude() ) .append("</longitude>\n"); |
|
88 |
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(); |
|
89 |
} |
} |
90 |
|
|
91 |
@Override |
@Override |
93 |
|
|
94 |
|
|
95 |
try { |
try { |
96 |
List<StationBean> stations = getStations(request); |
StationBean stations = getStations(request); |
97 |
|
|
98 |
|
|
99 |
if (stations != null){ |
if (stations != null){ |