/[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 1409 by torben, Mon May 2 11:59:40 2011 UTC revision 1505 by torben, Wed Jun 8 15:45:41 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;
                 return result;  
         }  
           
         public StationBean dumpAll() throws SQLException {  
                   
                 String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0,aliases " +  
                                 "FROM trainstations WHERE enabled = true ORDER BY id";  
                   
                 Connection conn = null;  
                 Statement stmt = null;  
                 ResultSet res = null;            
   
84                                    
85                  try {          }
                         conn = DBConnection.getConnection();  
86    
87                          stmt = conn.createStatement();          public StationEntry getById(int id) throws SQLException,NostationException {
88                          res = stmt.executeQuery(SQL);                            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";
                         // Does mostly the same as convertResultset()  
                         StationBean stations = new StationBean();  
                         while (res.next()) {  
                                 StationEntry entry = convertSingleRow(res);  
                                   
                                 Array arr = res.getArray(10);  
                                 if (arr != null) {  
                                         String[] aliases = (String[]) arr.getArray();  
                                         entry.setAliases(aliases);  
                                 }  
                                   
                                 stations.entries.add( entry );  
90                                                                    
91                          }                  StationBean stations =  fetchStations(SQL, new NullSetter() );
                         return stations;  
                           
   
                 } finally {  
                         if (res != null)  
                                 res.close();  
                         if (stmt != null)  
                                 stmt.close();  
                         if (conn != null)  
                                 conn.close();  
                 }  
92                                    
93                    if (stations.entries.size() > 0 ) {
94                            return stations.entries.get(0);
95                    } else {
96                            throw new NostationException();
97                    }
98          }          }
99            
100    
101    
102          /*          /*
103           * 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) )           * 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) )
# Line 133  public class StationDAO { Line 105  public class StationDAO {
105           *     'select $2 ilike $1' language sql strict immutable;           *     'select $2 ilike $1' language sql strict immutable;
106           *     create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~);           *     create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~);
107           */           */
108          public StationBean getByName(String name) throws SQLException {          public StationBean getByName(final String name) throws SQLException {
109                  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 " +
110                  "FROM trainstations " +                  "FROM trainstations " +
111                  "WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " +                  "WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " +
112                  "ORDER BY name ";                  "ORDER BY name ";
113    
114                    class NameSetter implements StatementParamSetter {
115                  StationBean result;                          @Override
116                  Connection conn = null;                          public void setParams(PreparedStatement stmt) throws SQLException {
117                  PreparedStatement stmt = null;                                  stmt.setString(1, name + "%");
118                  ResultSet res = null;                                  stmt.setString(2, name + "%");                          
119                  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();  
120                  }                  }
121                  return result;                  
122                    return fetchStations(SQL, new NameSetter() );
123          }          }
124    
125          //Latitude (horizonal), longitude(vertical) so          //Latitude (horizonal), longitude(vertical) so
# Line 176  public class StationDAO { Line 133  public class StationDAO {
133          // calculate the distance:          // calculate the distance:
134          //     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
135    
136          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 {
137                                    
138                  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 " : "";
139                                    
# Line 187  public class StationDAO { Line 144  public class StationDAO {
144                          "ORDER BY calcdist ASC " +                          "ORDER BY calcdist ASC " +
145                          "LIMIT " + LOCATION_LIMIT;                          "LIMIT " + LOCATION_LIMIT;
146    
   
147                                    
148                  StationBean result;                  class LatlongSetter implements StatementParamSetter {
149                  Connection conn = null;                          @Override
150                  PreparedStatement stmt = null;                          public void setParams(PreparedStatement stmt) throws SQLException {
151                  ResultSet res = null;                                  stmt.setDouble(1, latitude);
152                  try {                                  stmt.setDouble(2, longitude);
153                          conn = DBConnection.getConnection();                                  if (geolimit == true) {
154                          stmt = conn.prepareStatement(SQL);                                          stmt.setDouble(3, latitude);
155                          stmt.setDouble(1, latitude);                                          stmt.setDouble(4, longitude);
156                          stmt.setDouble(2, longitude);                                  }                              
157                          if (geolimit == true) {                          }                      
                                 stmt.setDouble(3, latitude);  
                                 stmt.setDouble(4, longitude);  
                         }  
                         res = stmt.executeQuery();  
                         result = convertResultset(res);  
                   
                 } finally {  
                         if (res != null)  
                                 res.close();  
                         if (stmt != null)  
                                 stmt.close();  
                         if (conn!= null)  
                                 conn.close();  
158                  }                  }
159                  return result;                  
160                    return fetchStations(SQL, new LatlongSetter() );
161          }          }
162                    
163          public StationBean getByLocation(double latitude, double longitude) throws SQLException {          public StationBean getByLocation(double latitude, double longitude) throws SQLException {
# Line 235  public class StationDAO { Line 179  public class StationDAO {
179                  "FROM trainstations " +                  "FROM trainstations " +
180                  "WHERE id IN " + list + " AND enabled = true " +                  "WHERE id IN " + list + " AND enabled = true " +
181                  "ORDER BY name ";                  "ORDER BY name ";
182                    
183                    return fetchStations(SQL, new NullSetter() );
184            }
185            
186    
187            
188            public StationEntry getSimpleByName(final String name) throws SQLException {
189                    String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " +
190                    "FROM trainstations " +
191                    "WHERE name = ?  AND enabled = true " +
192                    "LIMIT 1 ";
193    
194                    class NameSetter implements StatementParamSetter {
195                            @Override
196                            public void setParams(PreparedStatement stmt) throws SQLException {
197                                    stmt.setString(1, name );
198                            }                      
199                    }
200                    
201                    StationBean stations = fetchStations(SQL, new NameSetter() );
202                    
203                    if (stations.entries.size() == 1) {
204                            return stations.entries.get(0);
205                    } else {
206                            return null;
207                    }
208            }
209            
210            //used to create full dump in order to populate Google Appengine DB
211            @Deprecated
212            public StationBean dumpAll() throws SQLException {
213                    
214                    String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0,aliases " +
215                                    "FROM trainstations WHERE enabled = true ORDER BY id";
216                    
217                  Connection conn = null;                  Connection conn = null;
218                  Statement stmt = null;                  Statement stmt = null;
219                  ResultSet res = null;                  ResultSet res = null;          
                 StationBean result;  
220    
221                    
222                  try {                  try {
223                          conn = DBConnection.getConnection();                          conn = DBConnection.getConnection();
224    
225                          stmt = conn.createStatement();                          stmt = conn.createStatement();
226                          res = stmt.executeQuery(SQL);                          res = stmt.executeQuery(SQL);          
227                          result = convertResultset(res);                                                  
228                            // Does mostly the same as convertResultset()
229                            StationBean stations = new StationBean();
230                            while (res.next()) {
231                                    StationEntry entry = convertSingleRow(res);
232                                    
233                                    Array arr = res.getArray(10);
234                                    if (arr != null) {
235                                            String[] aliases = (String[]) arr.getArray();
236                                            entry.setAliases(aliases);
237                                    }
238                                    
239                                    stations.entries.add( entry );
240                                    
241                            }
242                            return stations;
243                            
244    
245                  } finally {                  } finally {
246                          if (res != null)                          if (res != null)
247                                  res.close();                                  res.close();
248                          if (stmt != null)                          if (stmt != null)
249                                  stmt.close();                                  stmt.close();
250                          if (conn!= null)                          if (conn != null)
251                                  conn.close();                                  conn.close();
252                  }                  }
253                    
                 return result;  
   
254          }          }
255    
256            @Deprecated
257          public static String getStationName(int stationID) {          public static String getStationName(int stationID) {
258                  String station = "";                  String station = "";
259    
# Line 280  public class StationDAO { Line 276  public class StationDAO {
276    
277                  return station;                  return station;
278          }          }
279            
280          public int getIdByName(String name) throws SQLException {          
281            
282            @Deprecated
283            public int getIdByName(final String name) throws SQLException {
284                  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 " +
285                  "FROM trainstations " +                  "FROM trainstations " +
286                  "WHERE name = ?  AND enabled = true " +                  "WHERE name = ?  AND enabled = true " +
287                  "LIMIT 1 ";                  "LIMIT 1 ";
288    
289                  StationBean result;                  class NameSetter implements StatementParamSetter {
290                  Connection conn = null;                          @Override
291                  PreparedStatement stmt = null;                          public void setParams(PreparedStatement stmt) throws SQLException {
292                  ResultSet res = null;                                  stmt.setString(1, name );
293                  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();  
294                  }                  }
295                    
296                  if (result.entries.size() == 1) {                  StationBean stations = fetchStations(SQL, new NameSetter() );
297                          return result.entries.get(0).getId();                  
298                    if (stations.entries.size() == 1) {
299                            return stations.entries.get(0).getId();
300                  } else {                  } else {
301                          return -1;                          return -1;
302                  }                  }

Legend:
Removed from v.1409  
changed lines
  Added in v.1505

  ViewVC Help
Powered by ViewVC 1.1.20