/[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 492 by torben, Sun Nov 1 16:35:48 2009 UTC revision 858 by torben, Wed Jun 16 08:11:33 2010 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfoservice;  package dk.thoerup.traininfoservice;
2    
3  import java.io.IOException;  import java.io.IOException;
 import java.sql.Connection;  
 import java.sql.PreparedStatement;  
 import java.sql.ResultSet;  
4  import java.sql.SQLException;  import java.sql.SQLException;
5    import java.util.List;
6  import java.util.logging.Level;  import java.util.logging.Level;
7  import java.util.logging.Logger;  import java.util.logging.Logger;
8    
# Line 22  public class LocateStations extends Http Line 19  public class LocateStations extends Http
19    
20          Logger logger = Logger.getLogger( LocateStations.class.toString() );          Logger logger = Logger.getLogger( LocateStations.class.toString() );
21    
22            StationDAO stationDao = new StationDAO();
23    
24    
           
25          protected String transformToIntList(String input) {                      protected String transformToIntList(String input) {            
26                  String strings[] = input.split(",");                  String strings[] = input.split(",");
27                    
28                  StringBuffer sb = new StringBuffer();                  StringBuffer sb = new StringBuffer();
29                  sb.append("(");                  sb.append("(");
30                  for (int i = 0; i<strings.length; i++) {                  for (int i = 0; i<strings.length; i++) {
# Line 40  public class LocateStations extends Http Line 38  public class LocateStations extends Http
38          }          }
39    
40    
41          protected PreparedStatement createStatement(Connection conn, HttpServletRequest req) throws SQLException {          protected List<StationBean> getStations(HttpServletRequest req) throws SQLException {
42                    List<StationBean> stations = null;
                 PreparedStatement stmt = null;  
                 String SQL;  
   
43                  if (req.getParameter("latitude") != null && req.getParameter("latitude") != null) {                  if (req.getParameter("latitude") != null && req.getParameter("latitude") != null) {
44                          //inner select is workaround from not being able to use a calculated column directly in where clause                          
45                          SQL = "SELECT * FROM ( "+                          Statistics.getInstance().incrementStationLookupsLocation();
46                          "               SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, address," +                          
                         "                     earth_distance( ll_to_earth(latitude,longitude), ll_to_earth(?,?))::int AS calcdist " +  
                         "               FROM trainstations " +  
                         "               WHERE enabled = true " +  
                         "       ) AS trainstations2 " +  
                         "ORDER BY calcdist ASC " +  
                         "LIMIT 4 ";  
47                          double latitude = Double.parseDouble( req.getParameter("latitude") );                          double latitude = Double.parseDouble( req.getParameter("latitude") );
48                          double longitude = Double.parseDouble( req.getParameter("longitude") );                          double longitude = Double.parseDouble( req.getParameter("longitude") );
49                          stmt = conn.prepareStatement(SQL);                          stations = stationDao.getByLocation(latitude, longitude);
                         stmt.setDouble(1, latitude);  
                         stmt.setDouble(2, longitude);  
                 } else if (req.getParameter("name") != null) {  
                         SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, address, 0.0 " +  
                         "FROM trainstations " +  
                         "WHERE name ILIKE ? AND enabled = true " +  
                         "ORDER BY name ";  
50    
51                    } else if (req.getParameter("name") != null) {
52                            Statistics.getInstance().incrementStationLookupsName();
53                          String name = req.getParameter("name").trim();                          String name = req.getParameter("name").trim();
54                          stmt = conn.prepareStatement(SQL);                          stations = stationDao.getByName(name);
55                          stmt.setString(1, name + "%");  
56                  } else if (req.getParameter("list") != null) {                  } else if (req.getParameter("list") != null) {
57                            Statistics.getInstance().incrementStationLookupsFavorites();
58                          String list = transformToIntList( req.getParameter("list"));                          String list = transformToIntList( req.getParameter("list"));
59                          SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,address,0.0 " +                          stations = stationDao.getByList(list);
60                          "FROM trainstations " +                  }
61                          "WHERE id IN " + list + " AND enabled = true " +                  return stations;
                         "ORDER BY name ";  
                         stmt = conn.prepareStatement(SQL);  
                 }  
   
                 return stmt;  
62          }          }
63    
64    
65          protected String formatResultset(PreparedStatement stmt) throws SQLException {          protected String formatStations(List<StationBean> stations)  {
                 ResultSet res = null;  
66    
67                  StringBuffer buff = new StringBuffer();                  StringBuffer buff = new StringBuffer();
68    
69                  buff.append("<?xml version=\"1.0\" ?>\n");                  buff.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
70                  buff.append("<stations>\n");                      buff.append("<stations>\n");    
71    
                 try  
                 {  
                         res = stmt.executeQuery();  
   
                         while (res.next()) {  
                                 buff.append("<station>\n");  
   
                                 buff.append("<id>").append( res.getInt(1) ).append("</id>\n");  
                                 buff.append("<name>").append( res.getString(2) ) .append("</name>\n");  
                                 buff.append("<latitude>").append( res.getDouble(3) ) .append("</latitude>\n");  
                                 buff.append("<longitude>").append( res.getDouble(4) ) .append("</longitude>\n");  
                                 res.getString(5);  
                                 buff.append("<fjerntog>").append( !res.wasNull() ) .append("</fjerntog>\n");  
                                 res.getString(6);  
                                 buff.append("<stog>").append( !res.wasNull() ) .append("</stog>\n");  
                                   
                                 String address = res.getString(7);  
                                 buff.append("<address>").append( res.wasNull() ? "" : address ).append("</address>");  
                                 buff.append("<calcdist>").append( res.getInt(8) ) .append("</calcdist>\n");  
72    
73                                  buff.append("</station>\n");                                              for (int i=0; i<stations.size(); i++) {                
74                          }                          StationBean station = stations.get(i);
75                  } finally {  
76                          if (res != null && !res.isClosed())                          buff.append("<station>\n");
77                                  res.close();  
78                          if (stmt != null && !stmt.isClosed())                          buff.append("<id>").append( station.getId() ).append("</id>\n");
79                                  stmt.close();                          buff.append("<name>").append( station.getName() ) .append("</name>\n");
80                  }                          buff.append("<latitude>").append( station.getLatitude() ) .append("</latitude>\n");
81                            buff.append("<longitude>").append( station.getLongitude() ) .append("</longitude>\n");                  
82                            buff.append("<regional>").append( station.getRegional() != null ) .append("</regional>\n");
83                            buff.append("<strain>").append( station.getStrain() != null ) .append("</strain>\n");
84                            buff.append("<metro>").append( station.getMetro() != null ).append("</metro>\n");
85                            buff.append("<address>").append( station.getAddress() != null ?  station.getAddress() : "").append("</address>");                              
86                            buff.append("<calcdist>").append( station.getCalcdist() ) .append("</calcdist>\n");
87    
88                            buff.append("</station>\n");                            
89                    }
90                  buff.append("</stations>\n");                  buff.append("</stations>\n");
91                  return buff.toString();                  return buff.toString();
92          }          }
# Line 125  public class LocateStations extends Http Line 94  public class LocateStations extends Http
94          @Override          @Override
95          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
96    
97                  Connection conn = null;  
98                  try {                  try {
99                          conn = DBConnection.getConnection();                          List<StationBean> stations = getStations(request);
100    
101    
102                          PreparedStatement stmt = createStatement(conn, request);                          if (stations != null){
103                          if (stmt != null){                                  String xml = formatStations(stations);
                                 String xml = formatResultset(stmt);  
104    
105                                  response.setContentType("text/xml");                                  response.setContentType("text/xml");
106                                  response.getWriter().print(xml);                                  response.getWriter().print(xml);
# Line 143  public class LocateStations extends Http Line 112  public class LocateStations extends Http
112                  } catch (Exception e) {                                          } catch (Exception e) {                        
113                          logger.log(Level.SEVERE, "Exception while finding stations", e);                          logger.log(Level.SEVERE, "Exception while finding stations", e);
114                          response.sendError(500);                                                                          response.sendError(500);                                                
115                  } finally {                  }
                         try {  
                                 if (conn != null) {  
                                         conn.close();  
                                 }  
                         } catch (Exception t) {}  
                 }  
116          }          }
117    
118  }  }

Legend:
Removed from v.492  
changed lines
  Added in v.858

  ViewVC Help
Powered by ViewVC 1.1.20