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

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

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

revision 589 by torben, Mon Feb 8 19:25:12 2010 UTC revision 836 by torben, Fri Jun 11 17:12:29 2010 UTC
# Line 11  import java.util.List; Line 11  import java.util.List;
11  public class StationDAO {  public class StationDAO {
12          private StationBean convertSingleRow(ResultSet res) throws SQLException {          private StationBean convertSingleRow(ResultSet res) throws SQLException {
13                  StationBean station = new StationBean();                  StationBean station = new StationBean();
14                    
15                  station.setId( res.getInt(1) );                  station.setId( res.getInt(1) );
16                  station.setName( res.getString(2) );                  station.setName( res.getString(2) );
17                  station.setLatitude( res.getDouble(3) );                  station.setLatitude( res.getDouble(3) );
# Line 21  public class StationDAO { Line 21  public class StationDAO {
21                  station.setMetro( res.getString(7) );                  station.setMetro( res.getString(7) );
22                  station.setAddress( res.getString(8) );                  station.setAddress( res.getString(8) );
23                  station.setCalcdist( (int)res.getDouble(9) );                  station.setCalcdist( (int)res.getDouble(9) );
24                    
25                  return station;                  return station;
26          }          }
27            
28          private List<StationBean> convertResultset(ResultSet res) throws SQLException {          private List<StationBean> convertResultset(ResultSet res) throws SQLException {
29                  List<StationBean> stations = new ArrayList<StationBean>();                  List<StationBean> stations = new ArrayList<StationBean>();
30                  while (res.next()) {                  while (res.next()) {
31                          stations.add( convertSingleRow(res) );                          stations.add( convertSingleRow(res) );
32                  }                  }
33                  return stations;                  return stations;
34                    
35          }          }
36            
37            
38          public StationBean getById(int id) throws SQLException {          public StationBean getById(int id) throws SQLException {
39                  String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0 " +                  String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0 " +
40                        "FROM trainstations WHERE id=" + id + " AND enabled=true";                  "FROM trainstations WHERE id=" + id + " AND enabled=true";
41                    
42                  Connection conn = null;                  Connection conn = null;
43                  Statement stmt = null;                  Statement stmt = null;
44                  ResultSet res = null;                  ResultSet res = null;
45                  StationBean result;                  StationBean result;
46                    
47                  try {                  try {
48                          conn = DBConnection.getConnection();                          conn = DBConnection.getConnection();
49                    
50                          stmt = conn.createStatement();                          stmt = conn.createStatement();
51                          res = stmt.executeQuery(SQL);                                    res = stmt.executeQuery(SQL);          
52                          res.next();                          res.next();
# Line 59  public class StationDAO { Line 59  public class StationDAO {
59                          if (conn != null)                          if (conn != null)
60                                  conn.close();                                  conn.close();
61                  }                  }
62                    
63                  return result;                  return result;
64          }          }
65            
66                    /*
67             * this code requires theses statements are run on database in order to do ILIKE searches against aliases (which is defines as array of varchar(64) )
68             *     create function rlike(text,text) returns bool as
69             *     'select $2 ilike $1' language sql strict immutable;
70             *     create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~);
71             */
72          public List<StationBean> getByName(String name) throws SQLException {          public List<StationBean> getByName(String name) throws SQLException {
73                  String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " +                  String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " +
74                  "FROM trainstations " +                  "FROM trainstations " +
75                  "WHERE name ILIKE ? AND enabled = true " +                  "WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " +
76                  "ORDER BY name ";                  "ORDER BY name ";
77    
78                    
79                  List<StationBean> result;                  List<StationBean> result;
80                  Connection conn = null;                  Connection conn = null;
81                  PreparedStatement stmt = null;                  PreparedStatement stmt = null;
# Line 78  public class StationDAO { Line 83  public class StationDAO {
83                  try {                  try {
84                          conn = DBConnection.getConnection();                          conn = DBConnection.getConnection();
85                          stmt = conn.prepareStatement(SQL);                          stmt = conn.prepareStatement(SQL);
86                            
87                          stmt.setString(1, name + "%");                          stmt.setString(1, name + "%");
88                                                    stmt.setString(2, name + "%");
89    
90                          res = stmt.executeQuery();                          res = stmt.executeQuery();
91                          result = convertResultset(res);                          result = convertResultset(res);
92                            
93                  } finally {                  } finally {
94                          if (res != null)                          if (res != null)
95                                  res.close();                                  res.close();
# Line 94  public class StationDAO { Line 100  public class StationDAO {
100                  }                  }
101                  return result;                  return result;
102          }          }
103            
104            //the "hack" with max 1.5 degrees latitude and 2.5 degrees longitude is only valid since we only service danish trains
105            // in denmark 1.5dg latitude ~ 165km, 2.5dg longitude ~ 155km
106    
107            // the ultra fast method  (and only slightly inaccurate as long as we only cover a limited geographically area)
108            // is using an aproximation of the length of 1 latitude degree and 1 longitude degree and just use pythagoras to
109            // calculate the distance:
110            //     sqrt( power(abs(latitude-?)*111320, 2) + power(abs(longitude-?)*63000,2) )::int as calcdist
111    
112          public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException {          public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException {
113                  String SQL = "SELECT * FROM ( "+                  String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address," +
114                  "               SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address," +                  "       earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " +
115                  "                     earth_distance( ll_to_earth(latitude,longitude), ll_to_earth(?,?))::int AS calcdist " +                  "FROM trainstations " +
116                  "               FROM trainstations " +                  "WHERE enabled = true AND abs(latitude-?)<1.5 AND abs(longitude-?)<2.5 " +
                 "               WHERE enabled = true " +  
                 "       ) AS trainstations2 " +  
117                  "ORDER BY calcdist ASC " +                  "ORDER BY calcdist ASC " +
118                  "LIMIT 4 ";                  "LIMIT 4 ";
119                  List<StationBean> result;                  List<StationBean> result;
# Line 113  public class StationDAO { Line 125  public class StationDAO {
125                          stmt = conn.prepareStatement(SQL);                          stmt = conn.prepareStatement(SQL);
126                          stmt.setDouble(1, latitude);                          stmt.setDouble(1, latitude);
127                          stmt.setDouble(2, longitude);                          stmt.setDouble(2, longitude);
128                            stmt.setDouble(3, latitude);
129                            stmt.setDouble(4, longitude);                  
130                          res = stmt.executeQuery();                          res = stmt.executeQuery();
131                          result = convertResultset(res);                          result = convertResultset(res);
132                            
133                    } finally {
134                            if (res != null)
135                                    res.close();
136                            if (stmt != null)
137                                    stmt.close();
138                            if (conn!= null)
139                                    conn.close();
140                    }
141                    return result;
142            }
143    
144            public List<StationBean> getByList(String list) throws SQLException {
145                    String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro, address,0.0 " +
146                    "FROM trainstations " +
147                    "WHERE id IN " + list + " AND enabled = true " +
148                    "ORDER BY name ";
149    
150                    Connection conn = null;
151                    Statement stmt = null;
152                    ResultSet res = null;
153                    List<StationBean> result;
154    
155                    try {
156                            conn = DBConnection.getConnection();
157                            stmt = conn.createStatement();
158                            res = stmt.executeQuery(SQL);
159                            result = convertResultset(res);
160                  } finally {                  } finally {
161                          if (res != null)                          if (res != null)
162                                  res.close();                                  res.close();
# Line 124  public class StationDAO { Line 165  public class StationDAO {
165                          if (conn!= null)                          if (conn!= null)
166                                  conn.close();                                  conn.close();
167                  }                  }
168    
169                  return result;                  return result;
170    
171          }          }
172                    public static String getStationName(int stationID) {
173           public List<StationBean> getByList(String list) throws SQLException {                  String station = "";
174                          String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro, address,0.0 " +  
175                          "FROM trainstations " +                  Connection conn = null;
176                          "WHERE id IN " + list + " AND enabled = true " +                  try {
177                          "ORDER BY name ";                          conn = DBConnection.getConnection();
178                                                    Statement stmt = conn.createStatement();
179                          Connection conn = null;                          ResultSet rs = stmt.executeQuery("SELECT name FROM trainstations WHERE id=" + stationID);
180                          Statement stmt = null;                          if (rs.next()) {
181                          ResultSet res = null;                                  station = rs.getString(1);
182                          List<StationBean> result;                          }
183                            
184                    } catch (Exception e) {
185                    } finally {
186                          try {                          try {
187                                  conn = DBConnection.getConnection();                                  if (conn != null && !conn.isClosed())
                                 stmt = conn.createStatement();  
                                 res = stmt.executeQuery(SQL);  
                                 result = convertResultset(res);  
                         } finally {  
                                 if (res != null)  
                                         res.close();  
                                 if (stmt != null)  
                                         stmt.close();  
                                 if (conn!= null)  
188                                          conn.close();                                          conn.close();
189                          }                          } catch (Exception e) {}
190                                            }
191                          return result;  
192                                            return station;
193           }          }
194            
195            public int getBySpecificName(String name) throws SQLException {
196                    String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " +
197                    "FROM trainstations " +
198                    "WHERE name = ?  AND enabled = true " +
199                    "LIMIT 1 ";
200    
201                    System.out.println(" getBySpecificName() ");
202    
203                    List<StationBean> result;
204                    Connection conn = null;
205                    PreparedStatement stmt = null;
206                    ResultSet res = null;
207                    try {
208                            conn = DBConnection.getConnection();
209                            stmt = conn.prepareStatement(SQL);
210    
211                            stmt.setString(1, name );
212    
213                            res = stmt.executeQuery();
214                            result = convertResultset(res);
215    
216                    } finally {
217                            if (res != null)
218                                    res.close();
219                            if (stmt != null)
220                                    stmt.close();
221                            if (conn!= null)
222                                    conn.close();
223                    }
224    
225                    if (result.size() == 1) {
226                            return result.get(0).getId();
227                    } else {
228                            return -1;
229                    }
230            }
231  }  }

Legend:
Removed from v.589  
changed lines
  Added in v.836

  ViewVC Help
Powered by ViewVC 1.1.20