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

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

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

revision 1503 by torben, Mon May 2 16:21:37 2011 UTC revision 1504 by torben, Wed Jun 8 15:38:11 2011 UTC
# Line 13  import dk.thoerup.android.traininfo.comm Line 13  import dk.thoerup.android.traininfo.comm
13    
14  public class StationDAO {  public class StationDAO {
15                    
16            private interface StatementParamSetter {
17                    public void setParams(PreparedStatement stmt) throws SQLException ;
18            }
19            private class NullSetter implements StatementParamSetter {
20                    @Override
21                    public void setParams(PreparedStatement stmt) throws SQLException {}
22            }
23            
24          public static class NostationException extends Exception {          public static class NostationException extends Exception {
25                  private static final long serialVersionUID = 1L;                  private static final long serialVersionUID = 1L;
26          }          }
# Line 50  public class StationDAO { Line 58  public class StationDAO {
58    
59          }          }
60    
61            private StationBean fetchStations(String SQL, StatementParamSetter setter) throws SQLException {
62          public StationEntry getById(int id) throws SQLException,NostationException {                  StationBean stations;
                 String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0 " +  
                 "FROM trainstations WHERE id=" + id + " AND enabled=true";  
   
63                  Connection conn = null;                  Connection conn = null;
64                  Statement stmt = null;                  PreparedStatement stmt = null;
65                  ResultSet res = null;                  ResultSet res = null;
                 StationEntry result;  
   
66                  try {                  try {
67                          conn = DBConnection.getConnection();                          conn = DBConnection.getConnection();
68                            stmt = conn.prepareStatement(SQL);
                         stmt = conn.createStatement();  
                         res = stmt.executeQuery(SQL);  
69                                                    
70                          if (res.next()) {                          setter.setParams(stmt);
71                                  result = convertSingleRow(res);  
72                          } else {                          res = stmt.executeQuery();
73                                  throw new NostationException();                          stations = convertResultset(res);
74                          }  
75                  } finally {                  } finally {
76                          if (res != null)                          if (res != null)
77                                  res.close();                                  res.close();
78                          if (stmt != null)                          if (stmt != null)
79                                  stmt.close();                                  stmt.close();
80                          if (conn != null)                          if (conn!= null)
81                                  conn.close();                                  conn.close();
82                  }                  }
83                    return stations;
84                    
85            }
86    
87                  return result;          public StationEntry getById(int id) throws SQLException,NostationException {
88                    String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0 " +
89                    "FROM trainstations WHERE id=" + id + " AND enabled=true";
90                                    
91                    StationBean stations =  fetchStations(SQL, new NullSetter() );
92                    
93                    if (stations.entries.size() > 0 ) {
94                            return stations.entries.get(0);
95                    } else {
96                            throw new NostationException();
97                    }
98          }          }
99                    
100          public StationBean dumpAll() throws SQLException {          public StationBean dumpAll() throws SQLException {
# Line 133  public class StationDAO { Line 147  public class StationDAO {
147           *     'select $2 ilike $1' language sql strict immutable;           *     'select $2 ilike $1' language sql strict immutable;
148           *     create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~);           *     create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~);
149           */           */
150          public StationBean getByName(String name) throws SQLException {          public StationBean getByName(final String name) throws SQLException {
151                  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 " +
152                  "FROM trainstations " +                  "FROM trainstations " +
153                  "WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " +                  "WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " +
154                  "ORDER BY name ";                  "ORDER BY name ";
155    
156                    class NameSetter implements StatementParamSetter {
157                  StationBean result;                          @Override
158                  Connection conn = null;                          public void setParams(PreparedStatement stmt) throws SQLException {
159                  PreparedStatement stmt = null;                                  stmt.setString(1, name + "%");
160                  ResultSet res = null;                                  stmt.setString(2, name + "%");                          
161                  try {                          }                      
                         conn = DBConnection.getConnection();  
                         stmt = conn.prepareStatement(SQL);  
   
                         stmt.setString(1, name + "%");  
                         stmt.setString(2, name + "%");  
   
                         res = stmt.executeQuery();  
                         result = convertResultset(res);  
   
                 } finally {  
                         if (res != null)  
                                 res.close();  
                         if (stmt != null)  
                                 stmt.close();  
                         if (conn!= null)  
                                 conn.close();  
162                  }                  }
163                  return result;                  
164                    return fetchStations(SQL, new NameSetter() );
165          }          }
166    
167          //Latitude (horizonal), longitude(vertical) so          //Latitude (horizonal), longitude(vertical) so
# Line 176  public class StationDAO { Line 175  public class StationDAO {
175          // calculate the distance:          // calculate the distance:
176          //     sqrt( power(abs(latitude-?)*111320, 2) + power(abs(longitude-?)*63000,2) )::int as calcdist          //     sqrt( power(abs(latitude-?)*111320, 2) + power(abs(longitude-?)*63000,2) )::int as calcdist
177    
178          public StationBean getByLocationWorker(double latitude, double longitude, boolean geolimit) throws SQLException {          public StationBean getByLocationWorker(final double latitude, final double longitude, final boolean geolimit) throws SQLException {
179                                    
180                  String limitExpression = (geolimit == true) ? "AND abs(latitude-?)<0.4 AND abs(longitude-?)<0.75 " : "";                  String limitExpression = (geolimit == true) ? "AND abs(latitude-?)<0.4 AND abs(longitude-?)<0.75 " : "";
181                                    
# Line 187  public class StationDAO { Line 186  public class StationDAO {
186                          "ORDER BY calcdist ASC " +                          "ORDER BY calcdist ASC " +
187                          "LIMIT " + LOCATION_LIMIT;                          "LIMIT " + LOCATION_LIMIT;
188    
   
                   
                 StationBean result;  
                 Connection conn = null;  
                 PreparedStatement stmt = null;  
                 ResultSet res = null;  
                 try {  
                         conn = DBConnection.getConnection();  
                         stmt = conn.prepareStatement(SQL);  
                         stmt.setDouble(1, latitude);  
                         stmt.setDouble(2, longitude);  
                         if (geolimit == true) {  
                                 stmt.setDouble(3, latitude);  
                                 stmt.setDouble(4, longitude);  
                         }  
                         res = stmt.executeQuery();  
                         result = convertResultset(res);  
189                                    
190                  } finally {                  class LatlongSetter implements StatementParamSetter {
191                          if (res != null)                          @Override
192                                  res.close();                          public void setParams(PreparedStatement stmt) throws SQLException {
193                          if (stmt != null)                                  stmt.setDouble(1, latitude);
194                                  stmt.close();                                  stmt.setDouble(2, longitude);
195                          if (conn!= null)                                  if (geolimit == true) {
196                                  conn.close();                                          stmt.setDouble(3, latitude);
197                                            stmt.setDouble(4, longitude);
198                                    }                              
199                            }                      
200                  }                  }
201                  return result;                  
202                    return fetchStations(SQL, new LatlongSetter() );
203          }          }
204                    
205          public StationBean getByLocation(double latitude, double longitude) throws SQLException {          public StationBean getByLocation(double latitude, double longitude) throws SQLException {
# Line 235  public class StationDAO { Line 221  public class StationDAO {
221                  "FROM trainstations " +                  "FROM trainstations " +
222                  "WHERE id IN " + list + " AND enabled = true " +                  "WHERE id IN " + list + " AND enabled = true " +
223                  "ORDER BY name ";                  "ORDER BY name ";
224                    
225                    return fetchStations(SQL, new NullSetter() );
226            }
227            
228    
229                  Connection conn = null;          
230                  Statement stmt = null;          public StationEntry getSimpleByName(final String name) throws SQLException {
231                  ResultSet res = null;                  String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " +
232                  StationBean result;                  "FROM trainstations " +
233                    "WHERE name = ?  AND enabled = true " +
234                    "LIMIT 1 ";
235    
236                  try {                  class NameSetter implements StatementParamSetter {
237                          conn = DBConnection.getConnection();                          @Override
238                          stmt = conn.createStatement();                          public void setParams(PreparedStatement stmt) throws SQLException {
239                          res = stmt.executeQuery(SQL);                                  stmt.setString(1, name );
240                          result = convertResultset(res);                          }                      
241                  } finally {                  }
242                          if (res != null)                  
243                                  res.close();                  StationBean stations = fetchStations(SQL, new NameSetter() );
244                          if (stmt != null)                  
245                                  stmt.close();                  if (stations.entries.size() == 1) {
246                          if (conn!= null)                          return stations.entries.get(0);
247                                  conn.close();                  } else {
248                            return null;
249                  }                  }
   
                 return result;  
   
250          }          }
251    
252          @Deprecated          @Deprecated
253          public static String getStationName(int stationID) {          public static String getStationName(int stationID) {
254                  String station = "";                  String station = "";
# Line 282  public class StationDAO { Line 273  public class StationDAO {
273                  return station;                  return station;
274          }          }
275                    
276          public StationEntry getSimpleByName(String name) throws SQLException {          
277                  String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " +          
                 "FROM trainstations " +  
                 "WHERE name = ?  AND enabled = true " +  
                 "LIMIT 1 ";  
   
                 StationBean result;  
                 Connection conn = null;  
                 PreparedStatement stmt = null;  
                 ResultSet res = null;  
                 try {  
                         conn = DBConnection.getConnection();  
                         stmt = conn.prepareStatement(SQL);  
   
                         stmt.setString(1, name );  
   
                         res = stmt.executeQuery();  
                         result = convertResultset(res);  
   
                 } finally {  
                         if (res != null)  
                                 res.close();  
                         if (stmt != null)  
                                 stmt.close();  
                         if (conn!= null)  
                                 conn.close();  
                 }  
   
                 if (result.entries.size() == 1) {  
                         return result.entries.get(0);  
                 } else {  
                         return null;  
                 }  
         }  
   
278          @Deprecated          @Deprecated
279          public int getIdByName(String name) throws SQLException {          public int getIdByName(final String name) throws SQLException {
280                  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 " +
281                  "FROM trainstations " +                  "FROM trainstations " +
282                  "WHERE name = ?  AND enabled = true " +                  "WHERE name = ?  AND enabled = true " +
283                  "LIMIT 1 ";                  "LIMIT 1 ";
284    
285                  StationBean result;                  class NameSetter implements StatementParamSetter {
286                  Connection conn = null;                          @Override
287                  PreparedStatement stmt = null;                          public void setParams(PreparedStatement stmt) throws SQLException {
288                  ResultSet res = null;                                  stmt.setString(1, name );
289                  try {                          }                      
                         conn = DBConnection.getConnection();  
                         stmt = conn.prepareStatement(SQL);  
   
                         stmt.setString(1, name );  
   
                         res = stmt.executeQuery();  
                         result = convertResultset(res);  
   
                 } finally {  
                         if (res != null)  
                                 res.close();  
                         if (stmt != null)  
                                 stmt.close();  
                         if (conn!= null)  
                                 conn.close();  
290                  }                  }
291                    
292                  if (result.entries.size() == 1) {                  StationBean stations = fetchStations(SQL, new NameSetter() );
293                          return result.entries.get(0).getId();                  
294                    if (stations.entries.size() == 1) {
295                            return stations.entries.get(0).getId();
296                  } else {                  } else {
297                          return -1;                          return -1;
298                  }                  }

Legend:
Removed from v.1503  
changed lines
  Added in v.1504

  ViewVC Help
Powered by ViewVC 1.1.20