/[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 309 by torben, Thu Sep 10 19:00:31 2009 UTC revision 347 by torben, Fri Sep 25 13:54:16 2009 UTC
# Line 18  import dk.thoerup.traininfoservice.DBCon Line 18  import dk.thoerup.traininfoservice.DBCon
18   */   */
19  public class LocateStations extends HttpServlet {  public class LocateStations extends HttpServlet {
20          private static final long serialVersionUID = 1L;          private static final long serialVersionUID = 1L;
21          
22      /**          /**
23       * @see HttpServlet#HttpServlet()           * @see HttpServlet#HttpServlet()
24       */           */
25      public LocateStations() {          public LocateStations() {
26          super();                  super();
27          // TODO Auto-generated constructor stub                  // TODO Auto-generated constructor stub
28      }          }
29    
30          protected String getStations(Connection conn, double latitude, double longitude) throws SQLException {          public enum Requested {
31                  //inner select is workaround from not being able to use a calculated column directly in where clause                  BY_NAME,
32                  final String SQL = "SELECT * FROM ( "+                  BY_LOCATION,
33                                                     "               SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, " +                  NONE
34                                                     "                     earth_distance( ll_to_earth(latitude,longitude), ll_to_earth(?,?))::int AS calcdist " +          }
35                                                     "               FROM trainstations " +  
36                                                     "               WHERE latitude IS NOT NULL AND longitude IS NOT NULL " +  
37                                                     "       ) AS trainstations2 " +          protected String getStations(Connection conn, double latitude, double longitude, String name, Requested method) throws SQLException {
38                                                 "ORDER BY calcdist ASC " +                  String SQL = "";
                                                "LIMIT 4 ";  
                   
                 System.out.println(SQL);  
                   
39                  PreparedStatement stmt = null;                  PreparedStatement stmt = null;
40                  ResultSet res = null;                  ResultSet res = null;
41                    
42                  StringBuffer buff = new StringBuffer();                  StringBuffer buff = new StringBuffer();
43                    
44                  buff.append("<?xml version=\"1.0\" ?>\n");                  buff.append("<?xml version=\"1.0\" ?>\n");
45                  buff.append("<stations>\n");                  buff.append("<stations>\n");    
46                    
47                  try                  try
48                  {                  {
49                          stmt = conn.prepareStatement(SQL);  
50                          stmt.setDouble(1, latitude);                          switch (method)
51                          stmt.setDouble(2, longitude);                          {
52                                                    case BY_LOCATION:
53                                    //inner select is workaround from not being able to use a calculated column directly in where clause
54                                    SQL = "SELECT * FROM ( "+
55                                                    "               SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, " +
56                                                    "                     earth_distance( ll_to_earth(latitude,longitude), ll_to_earth(?,?))::int AS calcdist " +
57                                                    "               FROM trainstations " +
58                                                    "               WHERE latitude IS NOT NULL AND longitude IS NOT NULL " +
59                                                    "       ) AS trainstations2 " +
60                                                    "ORDER BY calcdist ASC " +
61                                                    "LIMIT 4 ";
62                                    stmt = conn.prepareStatement(SQL);
63                                    stmt.setDouble(1, latitude);
64                                    stmt.setDouble(2, longitude);
65    
66                                    break;
67                            case BY_NAME:
68                                    SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,0.0 " +
69                                            "FROM trainstations " +
70                                            "WHERE name ILIKE ? AND latitude IS NOT NULL AND longitude IS NOT NULL " +
71                                            "ORDER BY name ";
72                                    stmt = conn.prepareStatement(SQL);
73                                    stmt.setString(1, "%" + name + "%");
74                                    break;
75                            default:
76                                    throw new SQLException("not enough parameters");
77                            }
78    
79    
80    
81    
82                          res = stmt.executeQuery();                          res = stmt.executeQuery();
83                            
84                          while (res.next()) {                          while (res.next()) {
85                                  buff.append("<station>\n");                                  buff.append("<station>\n");
86    
# Line 67  public class LocateStations extends Http Line 93  public class LocateStations extends Http
93                                  res.getString(6);                                  res.getString(6);
94                                  buff.append("<stog>").append( !res.wasNull() ) .append("</stog>\n");                                  buff.append("<stog>").append( !res.wasNull() ) .append("</stog>\n");
95                                  buff.append("<calcdist>").append( res.getInt(7) ) .append("</calcdist>\n");                                  buff.append("<calcdist>").append( res.getInt(7) ) .append("</calcdist>\n");
96                                    
97                                  buff.append("</station>\n");                                                              buff.append("</station>\n");                            
98                          }                          }
99                  } finally {                  } finally {
# Line 81  public class LocateStations extends Http Line 107  public class LocateStations extends Http
107          }          }
108    
109          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
110                    
111                  double latitude = Double.parseDouble( request.getParameter("latitude") );                  double longitude = 0.0;
112                  double longitude = Double.parseDouble( request.getParameter("longitude") );                  double latitude = 0.0;
113                                                    String name = "";
114                  Connection conn = null;  
115                  try {                  Requested method = Requested.NONE;
116                          conn = DBConnection.getConnection();  
117                                            if (request.getParameter("latitude") != null && request.getParameter("latitude") != null) {
118                          String xml = getStations(conn, latitude, longitude);                          latitude = Double.parseDouble( request.getParameter("latitude") );
119                                                    longitude = Double.parseDouble( request.getParameter("longitude") );
120                          response.setContentType("text/xml");                          method = Requested.BY_LOCATION;
121                          response.getWriter().print(xml);                  }
122                            
123                          conn.close();                  if (request.getParameter("name") != null) {
124                          conn = null;                          name = request.getParameter("name");
125                                                    method = Requested.BY_NAME;
126                  } catch (Exception e) {                  }
127                          throw new ServletException(e);  
128                  } finally {  
129                    if (method != Requested.NONE) {
130    
131                            Connection conn = null;
132                          try {                          try {
133                                  if (conn != null)                                  conn = DBConnection.getConnection();
134                                          conn.close();  
135                          } catch (Throwable t) {}                                  String xml = getStations(conn, latitude, longitude, name, method);
136    
137                                    response.setContentType("text/xml");
138                                    response.getWriter().print(xml);
139    
140                                    conn.close();
141                                    conn = null;
142    
143                            } catch (Exception e) {
144                                    throw new ServletException(e);
145                            } finally {
146                                    try {
147                                            if (conn != null)
148                                                    conn.close();
149                                    } catch (Throwable t) {}
150                            }
151                    } else {
152                            response.sendError(400, "not enough parameters");
153                  }                  }
154          }          }
155    

Legend:
Removed from v.309  
changed lines
  Added in v.347

  ViewVC Help
Powered by ViewVC 1.1.20