--- android/TrainInfoService/src/dk/thoerup/traininfoservice/LocateStations.java 2009/09/27 19:14:39 348
+++ android/TrainInfoServiceGoogle/src/dk/thoerup/traininfoservice/LocateStations.java 2010/09/21 20:10:46 1093
@@ -1,162 +1,171 @@
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;
Logger logger = Logger.getLogger( LocateStations.class.toString() );
-
- /**
- * @see HttpServlet#HttpServlet()
- */
- public LocateStations() {
- super();
- // TODO Auto-generated constructor stub
- }
-
- public enum Requested {
- BY_NAME,
- BY_LOCATION,
- NONE
- }
-
- protected String getStations(Connection conn, double latitude, double longitude, String name, Requested method) throws SQLException {
- String SQL = "";
- PreparedStatement stmt = null;
- ResultSet res = null;
+ StationDAO stationDao = new StationDAO();
- StringBuffer buff = new StringBuffer();
- buff.append("\n");
- buff.append("\n");
-
- try
- {
+ protected String transformToIntList(String input) {
+ String strings[] = input.split(",");
- switch (method)
- {
- case BY_LOCATION:
- //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 ";
- stmt = conn.prepareStatement(SQL);
- stmt.setDouble(1, latitude);
- stmt.setDouble(2, longitude);
-
- break;
- case BY_NAME:
- 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 ";
- stmt = conn.prepareStatement(SQL);
- stmt.setString(1, "%" + name + "%");
- break;
- default:
- // This should not be possible
- logger.severe("getStations(): default switch case");
+ 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;
+ }
- res = stmt.executeQuery();
+ protected String formatStations(StationBean stations) throws ServletException {
- while (res.next()) {
- buff.append("\n");
+ Serializer serializer = new Persister();
- 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");
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
- buff.append("\n");
- }
- } finally {
- if (res != null && !res.isClosed())
- res.close();
- if (stmt != null && !stmt.isClosed())
- stmt.close();
+ try {
+ serializer.write(stations, out);
+ } catch (Exception e) {
+ throw new ServletException(e);
}
- buff.append("\n");
- return buff.toString();
+
+ return out.toString();
}
+ @SuppressWarnings("unchecked")
+ @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- double longitude = 0.0;
- double latitude = 0.0;
- String name = "";
-
- Requested method = Requested.NONE;
-
- if (request.getParameter("latitude") != null && request.getParameter("latitude") != null) {
- latitude = Double.parseDouble( request.getParameter("latitude") );
- longitude = Double.parseDouble( request.getParameter("longitude") );
- method = Requested.BY_LOCATION;
- }
+ if (true) {
+ PersistenceManager pm = null;
+ try {
+ pm = PMF.get().getPersistenceManager();
+
+
- if (request.getParameter("name") != null) {
- name = request.getParameter("name");
- method = Requested.BY_NAME;
+
+ JdoStationBean b = new JdoStationBean();
+ b.setId(1000);
+ b.setName("TestStation");
+ b.setNameLower( b.getName().toLowerCase() );
+ b.setMetro("12");
+ pm.makePersistent(b);
+
+
+ JdoStationBean b2 = new JdoStationBean();
+ b2.setId(1001);
+ b2.setName("teststation 2");
+ b2.setNameLower( b2.getName().toLowerCase() );
+ b2.setMetro("12");
+ pm.makePersistent(b2);
+
+ JdoStationBean b3 = new JdoStationBean();
+ b3.setId(1002);
+ b3.setName("Horsens");
+ b3.setNameLower( b3.getName().toLowerCase() );
+ b3.setMetro("13");
+ pm.makePersistent(b3);
+
+
+
+
+ //String query = "select from " + JdoStationBean.class.getName();// + " where nameLower.startsWith('test')";
+
+ String query = "select from " + JdoStationBean.class.getName() + " where (id == 1001 || id == 1002) ";
+
+ List stations = (List) pm.newQuery( query ).execute();
+
+ logger.info("size=" + stations.size() );
+ for(JdoStationBean bean : stations) {
+ logger.info("Station: " + bean.getId() + "/" + bean.getName());
+ }
+
+
+
+
+ } finally {
+ if (pm != null)
+ pm.close();
+ }
+
+
+ return;
}
+ try {
+ StationBean stations = getStations(request);
- if (method != Requested.NONE) {
- Connection conn = null;
- try {
- conn = DBConnection.getConnection();
-
- String xml = getStations(conn, latitude, longitude, name, method);
+ if (stations != null){
+ String xml = formatStations(stations);
response.setContentType("text/xml");
response.getWriter().print(xml);
-
- conn.close();
- conn = null;
-
- } catch (Exception e) {
- logger.log(Level.SEVERE, "Exception while finding stations", e);
- response.sendError(500);
- } finally {
- try {
- if (conn != null)
- conn.close();
- } catch (Throwable t) {}
+ } else {
+ response.sendError(400, "not enough parameters");
}
- } else {
- response.sendError(400, "not enough parameters");
- }
+
+
+ } catch (Exception e) {
+ logger.log(Level.SEVERE, "Exception while finding stations", e);
+ response.sendError(500);
+ }
}
}