package dk.thoerup.traininfoservice; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.sql.SQLException; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.jdo.PersistenceManager; import javax.jdo.Query; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.simpleframework.xml.Serializer; import org.simpleframework.xml.core.Persister; import dk.thoerup.android.traininfo.common.StationBean; import dk.thoerup.traininfoservice.jdo.JdoStationBean; import dk.thoerup.traininfoservice.jdo.PMF; /** * Servlet implementation class LocateStations */ public class LocateStations extends HttpServlet { private static final long serialVersionUID = 1L; Logger logger = Logger.getLogger( LocateStations.class.toString() ); StationDAO stationDao = new StationDAO(); protected String transformToIntList(String input) { String strings[] = input.split(","); StringBuffer sb = new StringBuffer(); sb.append("("); for (int i = 0; i0) { sb.append(","); } sb.append( Integer.parseInt(strings[i])); //by doing the integer conversion we ensure that it really is a integer } sb.append(")"); return sb.toString(); } protected StationBean getStations(HttpServletRequest req) throws SQLException { StationBean stations = null; if (req.getParameter("latitude") != null && req.getParameter("latitude") != null) { Statistics.getInstance().incrementStationLookupsLocation(); double latitude = Double.parseDouble( req.getParameter("latitude") ); double longitude = Double.parseDouble( req.getParameter("longitude") ); stations = stationDao.getByLocation(latitude, longitude); } else if (req.getParameter("name") != null) { Statistics.getInstance().incrementStationLookupsName(); String name = req.getParameter("name").trim(); stations = stationDao.getByName(name); } else if (req.getParameter("list") != null) { Statistics.getInstance().incrementStationLookupsFavorites(); String list = transformToIntList( req.getParameter("list")); stations = stationDao.getByList(list); } return stations; } protected String formatStations(StationBean stations) throws ServletException { Serializer serializer = new Persister(); ByteArrayOutputStream out = new ByteArrayOutputStream(); try { serializer.write(stations, out); } catch (Exception e) { throw new ServletException(e); } return out.toString(); } @SuppressWarnings("unchecked") @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (true) { PersistenceManager pm = null; try { pm = PMF.get().getPersistenceManager(); String query = "select from " + JdoStationBean.class.getName() + " where name.matches('Test.*')"; List stations = (List) pm.newQuery(query).execute(); logger.info("size=" + stations.size() ); for(JdoStationBean bean : stations) { logger.info("Station: " + bean.getId() + "/" + bean.getName()); } /*if (stations.size() == 0) { JdoStationBean b = new JdoStationBean(); b.setId(1000); b.setName("TestStation"); b.setMetro("12"); pm.makePersistent(b); JdoStationBean b2 = new JdoStationBean(); b2.setId(1001); b2.setName("teststation 2"); b2.setMetro("12"); pm.makePersistent(b2); }*/ } finally { if (pm != null) pm.close(); } return; } try { StationBean stations = getStations(request); if (stations != null){ String xml = formatStations(stations); response.setContentType("text/xml"); response.getWriter().print(xml); } else { response.sendError(400, "not enough parameters"); } } catch (Exception e) { logger.log(Level.SEVERE, "Exception while finding stations", e); response.sendError(500); } } }