/[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 346 by torben, Thu Sep 24 21:04:34 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;
         
     /**  
      * @see HttpServlet#HttpServlet()  
      */  
     public LocateStations() {  
         super();  
         // TODO Auto-generated constructor stub  
     }  
       
21    
22            /**
23             * @see HttpServlet#HttpServlet()
24             */
25            public LocateStations() {
26                    super();
27                    // TODO Auto-generated constructor stub
28            }
29    
30            public enum Requested {
31                    BY_NAME,
32                    BY_LOCATION,
33                    NONE
34            }
35    
36          protected String getStations(Connection conn, double latitude, double longitude, String name) throws SQLException {  
37            protected String getStations(Connection conn, double latitude, double longitude, String name, Requested method) throws SQLException {
38                  String SQL = "";                  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");  
                   
                   
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                          if (latitude >= 0 && longitude >= 0) {                          switch (method)
51                            {
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);                                  stmt.setDouble(1, latitude);
64                                  stmt.setDouble(2, longitude);                                  stmt.setDouble(2, longitude);
65                          }  
66                          if (name != null) {                                  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 + "%");                                  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 83  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 97  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 = -1.0;                  double longitude = 0.0;
112                  if (request.getParameter("latitude") != null)                  double latitude = 0.0;
113                    String name = "";
114    
115                    Requested method = Requested.NONE;
116    
117                    if (request.getParameter("latitude") != null && request.getParameter("latitude") != null) {
118                          latitude = Double.parseDouble( request.getParameter("latitude") );                          latitude = Double.parseDouble( request.getParameter("latitude") );
                   
                 double longitude = -1.0;  
                 if (request.getParameter("latitude") != null)  
119                          longitude = Double.parseDouble( request.getParameter("longitude") );                          longitude = Double.parseDouble( request.getParameter("longitude") );
120                                            method = Requested.BY_LOCATION;
121                  String name = request.getParameter("name");                  }
122                                    
123                  Connection conn = null;                  if (request.getParameter("name") != null) {
124                  try {                          name = request.getParameter("name");
125                          conn = DBConnection.getConnection();                          method = Requested.BY_NAME;
126                                            }
127                          String xml = getStations(conn, latitude, longitude, name);  
128                            
129                          response.setContentType("text/xml");                  if (method != Requested.NONE) {
130                          response.getWriter().print(xml);  
131                                                    Connection conn = null;
                         conn.close();  
                         conn = null;  
                           
                 } catch (Exception e) {  
                         throw new ServletException(e);  
                 } finally {  
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.346  
changed lines
  Added in v.347

  ViewVC Help
Powered by ViewVC 1.1.20