/[projects]/android/TrainInfoService/src/dk/thoerup/traininfoservice/LocateStations.java
ViewVC logotype

Diff of /android/TrainInfoService/src/dk/thoerup/traininfoservice/LocateStations.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 301 by torben, Mon Sep 7 12:23:35 2009 UTC revision 344 by torben, Thu Sep 24 21:04:34 2009 UTC
# Line 26  public class LocateStations extends Http Line 26  public class LocateStations extends Http
26          super();          super();
27          // TODO Auto-generated constructor stub          // TODO Auto-generated constructor stub
28      }      }
29        
30    
31          protected String getStations(Connection conn, double latitude, double longitude) throws SQLException {  
32            protected String getStations(Connection conn, double latitude, double longitude, String name) throws SQLException {
33                    String SQL = "";
34                    
35                    if (latitude >= 0.0 && longitude >= 0.0)
36                    {
37                  //inner select is workaround from not being able to use a calculated column directly in where clause                  //inner select is workaround from not being able to use a calculated column directly in where clause
38                  final String SQL = "SELECT * FROM ( "+                          SQL = "SELECT * FROM ( "+
39                                                     "               SELECT name,latitude,longitude,stationcode, " +                                                     "               SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, " +
40                                                     "                     earth_distance( ll_to_earth(latitude,longitude), ll_to_earth(?,?))::int AS calcdist " +                                                     "                     earth_distance( ll_to_earth(latitude,longitude), ll_to_earth(?,?))::int AS calcdist " +
41                                                     "               FROM trainstations " +                                                     "               FROM trainstations " +
42                                                     "               WHERE latitude IS NOT NULL AND longitude IS NOT NULL " +                                                     "               WHERE latitude IS NOT NULL AND longitude IS NOT NULL " +
43                                                     "       ) AS trainstations2 " +                                                     "       ) AS trainstations2 " +
44                                                 "ORDER BY calcdist ASC " +                                                 "ORDER BY calcdist ASC " +
45                                                 "LIMIT 4 ";                                                 "LIMIT 4 ";
46                    } else if (name != null) {
47                            SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,0.0 " +
48                                      "FROM trainstations " +
49                                      "WHERE name ILIKE ? AND latitude IS NOT NULL AND longitude IS NOT NULL " +
50                                      "ORDER BY name ";
51                    } else throw new SQLException("not enough parameters");
52                                    
                 System.out.println(SQL);  
53                                    
54                  PreparedStatement stmt = null;                  PreparedStatement stmt = null;
55                  ResultSet res = null;                  ResultSet res = null;
# Line 50  public class LocateStations extends Http Line 61  public class LocateStations extends Http
61                  try                  try
62                  {                  {
63                          stmt = conn.prepareStatement(SQL);                          stmt = conn.prepareStatement(SQL);
64                          stmt.setDouble(1, latitude);                          if (latitude >= 0 && longitude >= 0) {
65                          stmt.setDouble(2, longitude);                                  stmt.setDouble(1, latitude);
66                                    stmt.setDouble(2, longitude);
67                            }
68                            if (name != null) {
69                                    stmt.setString(1, "%" + name + "%");
70                            }
71                                                    
72                          res = stmt.executeQuery();                          res = stmt.executeQuery();
73                                                    
74                          while (res.next()) {                          while (res.next()) {
75                                  buff.append("<station>\n");                                  buff.append("<station>\n");
76    
77                                  buff.append("<name>").append( res.getString(1) ) .append("</name>\n");                                  buff.append("<id>").append( res.getInt(1) ).append("</id>\n");
78                                  buff.append("<latitude>").append( res.getDouble(2) ) .append("</latitude>\n");                                  buff.append("<name>").append( res.getString(2) ) .append("</name>\n");
79                                  buff.append("<longitude>").append( res.getDouble(3) ) .append("</longitude>\n");                                                                          buff.append("<latitude>").append( res.getDouble(3) ) .append("</latitude>\n");
80                                  buff.append("<stationcode>").append( res.getString(4) ) .append("</stationcode>\n");                                  buff.append("<longitude>").append( res.getDouble(4) ) .append("</longitude>\n");
81                                  buff.append("<calcdist>").append( res.getInt(5) ) .append("</calcdist>\n");                                  res.getString(5);
82                                    buff.append("<fjerntog>").append( !res.wasNull() ) .append("</fjerntog>\n");
83                                    res.getString(6);
84                                    buff.append("<stog>").append( !res.wasNull() ) .append("</stog>\n");
85                                    buff.append("<calcdist>").append( res.getInt(7) ) .append("</calcdist>\n");
86                                                                    
87                                  buff.append("</station>\n");                                                              buff.append("</station>\n");                            
88                          }                          }
# Line 78  public class LocateStations extends Http Line 98  public class LocateStations extends Http
98    
99          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
100                                    
101                  double latitude = Double.parseDouble( request.getParameter("latitude") );                  double latitude = -1.0;
102                  double longitude = Double.parseDouble( request.getParameter("longitude") );                  if (request.getParameter("latitude") != null)
103                            latitude = Double.parseDouble( request.getParameter("latitude") );
104                    
105                    double longitude = -1.0;
106                    if (request.getParameter("latitude") != null)
107                            longitude = Double.parseDouble( request.getParameter("longitude") );
108                    
109                    String name = request.getParameter("name");
110                                                                    
111                  Connection conn = null;                  Connection conn = null;
112                  try {                  try {
113                          conn = DBConnection.getConnection();                          conn = DBConnection.getConnection();
114                                                    
115                          String xml = getStations(conn, latitude, longitude);                          String xml = getStations(conn, latitude, longitude, name);
116                                                    
117                          response.setContentType("text/xml");                          response.setContentType("text/xml");
118                          response.getWriter().print(xml);                          response.getWriter().print(xml);

Legend:
Removed from v.301  
changed lines
  Added in v.344

  ViewVC Help
Powered by ViewVC 1.1.20