--- android/TrainInfoService/src/dk/thoerup/traininfoservice/LocateStations.java 2009/09/24 21:04:34 344 +++ android/TrainInfoServiceGoogle/src/dk/thoerup/traininfoservice/LocateStations.java 2010/09/20 20:11:55 1080 @@ -1,133 +1,156 @@ package dk.thoerup.traininfoservice; +import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; 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 dk.thoerup.traininfoservice.DBConnection; +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; - - /** - * @see HttpServlet#HttpServlet() - */ - public LocateStations() { - super(); - // TODO Auto-generated constructor stub - } - + Logger logger = Logger.getLogger( LocateStations.class.toString() ); - protected String getStations(Connection conn, double latitude, double longitude, String name) throws SQLException { - String SQL = ""; - - if (latitude >= 0.0 && longitude >= 0.0) - { - //inner select is workaround from not being able to use a calculated column directly in where clause - SQL = "SELECT * FROM ( "+ - " 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 "; - } else if (name != null) { - 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 "; - } else throw new SQLException("not enough parameters"); - - - PreparedStatement stmt = null; - ResultSet res = null; - - StringBuffer buff = new StringBuffer(); - - buff.append("\n"); - buff.append("\n"); - try - { - stmt = conn.prepareStatement(SQL); - if (latitude >= 0 && longitude >= 0) { - stmt.setDouble(1, latitude); - stmt.setDouble(2, longitude); - } - if (name != null) { - stmt.setString(1, "%" + name + "%"); - } - - res = stmt.executeQuery(); - - while (res.next()) { - buff.append("\n"); + StationDAO stationDao = new StationDAO(); - buff.append("").append( res.getInt(1) ).append("\n"); - buff.append("").append( res.getString(2) ) .append("\n"); - buff.append("").append( res.getDouble(3) ) .append("\n"); - buff.append("").append( res.getDouble(4) ) .append("\n"); - res.getString(5); - buff.append("").append( !res.wasNull() ) .append("\n"); - res.getString(6); - buff.append("").append( !res.wasNull() ) .append("\n"); - buff.append("").append( res.getInt(7) ) .append("\n"); - - buff.append("\n"); + + protected String transformToIntList(String input) { + String strings[] = input.split(","); + + StringBuffer sb = new StringBuffer(); + sb.append("("); + for (int i = 0; i0) { + sb.append(","); } - } finally { - if (res != null && !res.isClosed()) - res.close(); - if (stmt != null && !stmt.isClosed()) - stmt.close(); + sb.append( Integer.parseInt(strings[i])); //by doing the integer conversion we ensure that it really is a integer } - buff.append("\n"); - return buff.toString(); + sb.append(")"); + return sb.toString(); } - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - - double latitude = -1.0; - if (request.getParameter("latitude") != null) - latitude = Double.parseDouble( request.getParameter("latitude") ); - - double longitude = -1.0; - if (request.getParameter("latitude") != null) - longitude = Double.parseDouble( request.getParameter("longitude") ); - - String name = request.getParameter("name"); - - Connection conn = null; + + 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 { - conn = DBConnection.getConnection(); - - String xml = getStations(conn, latitude, longitude, name); - - response.setContentType("text/xml"); - response.getWriter().print(xml); - - conn.close(); - conn = null; - + serializer.write(stations, out); } catch (Exception e) { throw new ServletException(e); - } finally { + } + + return out.toString(); + } + + @SuppressWarnings("unchecked") + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + if (true) { + PersistenceManager pm = null; try { - if (conn != null) - conn.close(); - } catch (Throwable t) {} + 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); + } } }