/[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

android/TrainInfoService/src/dk/thoerup/traininfoservice/StationDAO.java revision 1060 by torben, Thu Sep 16 13:32:10 2010 UTC android/TrainInfoService/src/dk/thoerup/traininfoservice/db/StationDAO.java revision 1417 by torben, Mon May 2 16:21:37 2011 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfoservice;  package dk.thoerup.traininfoservice.db;
2    
3    import java.sql.Array;
4  import java.sql.Connection;  import java.sql.Connection;
5  import java.sql.PreparedStatement;  import java.sql.PreparedStatement;
6  import java.sql.ResultSet;  import java.sql.ResultSet;
# Line 7  import java.sql.SQLException; Line 8  import java.sql.SQLException;
8  import java.sql.Statement;  import java.sql.Statement;
9  import java.util.logging.Logger;  import java.util.logging.Logger;
10    
11  import dk.thoerup.traininfoservice.StationBean.StationEntry;  import dk.thoerup.android.traininfo.common.StationBean;
12    import dk.thoerup.android.traininfo.common.StationEntry;
13    
14  public class StationDAO {  public class StationDAO {
15            
16            public static class NostationException extends Exception {
17                    private static final long serialVersionUID = 1L;
18            }
19            
20          final static int LOCATION_LIMIT = 8;          final static int LOCATION_LIMIT = 8;
21          static final Logger logger = Logger.getLogger(StationDAO.class.getName());          static final Logger logger = Logger.getLogger(StationDAO.class.getName());
22                    
# Line 44  public class StationDAO { Line 51  public class StationDAO {
51          }          }
52    
53    
54          public StationEntry getById(int id) throws SQLException {          public StationEntry getById(int id) throws SQLException,NostationException {
55                  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 " +
56                  "FROM trainstations WHERE id=" + id + " AND enabled=true";                  "FROM trainstations WHERE id=" + id + " AND enabled=true";
57    
# Line 57  public class StationDAO { Line 64  public class StationDAO {
64                          conn = DBConnection.getConnection();                          conn = DBConnection.getConnection();
65    
66                          stmt = conn.createStatement();                          stmt = conn.createStatement();
67                          res = stmt.executeQuery(SQL);                                    res = stmt.executeQuery(SQL);
68                          res.next();                          
69                          result = convertSingleRow(res);                          if (res.next()) {
70                                    result = convertSingleRow(res);
71                            } else {
72                                    throw new NostationException();
73                            }
74                  } finally {                  } finally {
75                          if (res != null)                          if (res != null)
76                                  res.close();                                  res.close();
# Line 71  public class StationDAO { Line 82  public class StationDAO {
82    
83                  return result;                  return result;
84          }          }
85            
86            public StationBean dumpAll() throws SQLException {
87                    
88                    String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0,aliases " +
89                                    "FROM trainstations WHERE enabled = true ORDER BY id";
90                    
91                    Connection conn = null;
92                    Statement stmt = null;
93                    ResultSet res = null;          
94    
95                    
96                    try {
97                            conn = DBConnection.getConnection();
98    
99                            stmt = conn.createStatement();
100                            res = stmt.executeQuery(SQL);          
101                                                    
102                            // Does mostly the same as convertResultset()
103                            StationBean stations = new StationBean();
104                            while (res.next()) {
105                                    StationEntry entry = convertSingleRow(res);
106                                    
107                                    Array arr = res.getArray(10);
108                                    if (arr != null) {
109                                            String[] aliases = (String[]) arr.getArray();
110                                            entry.setAliases(aliases);
111                                    }
112                                    
113                                    stations.entries.add( entry );
114                                    
115                            }
116                            return stations;
117                            
118    
119                    } finally {
120                            if (res != null)
121                                    res.close();
122                            if (stmt != null)
123                                    stmt.close();
124                            if (conn != null)
125                                    conn.close();
126                    }
127                    
128            }
129    
130          /*          /*
131           * 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 110  public class StationDAO { Line 165  public class StationDAO {
165                  return result;                  return result;
166          }          }
167    
168          //the "hack" with max 0.4 degrees latitude and 0.75 degrees longitude is only valid since we only service danish trains,          //Latitude (horizonal), longitude(vertical) so
169            // 1 degree latitude is ~ 111320 meters, since the distance between the horizonal lines is always the same
170            // 1 degree longitude is ~111320 meters at equator but gets shorter as we get closer to the poles.
171            // the "hack" with max 0.4 degrees latitude and 0.75 degrees longitude is only valid since we only service danish trains,
172          // in denmark 0.4dg latitude ~ 44km, 0.75dg longitude ~ 47km          // in denmark 0.4dg latitude ~ 44km, 0.75dg longitude ~ 47km
173    
174          // the ultra fast method  (and only slightly inaccurate as long as we only cover a limited geographically area)          // the ultra fast method  (and only slightly inaccurate as long as we only cover a limited geographically area)
# Line 200  public class StationDAO { Line 258  public class StationDAO {
258                  return result;                  return result;
259    
260          }          }
261            @Deprecated
262          public static String getStationName(int stationID) {          public static String getStationName(int stationID) {
263                  String station = "";                  String station = "";
264    
# Line 222  public class StationDAO { Line 281  public class StationDAO {
281    
282                  return station;                  return station;
283          }          }
284            
285            public StationEntry getSimpleByName(String name) throws SQLException {
286                    String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " +
287                    "FROM trainstations " +
288                    "WHERE name = ?  AND enabled = true " +
289                    "LIMIT 1 ";
290    
291                    StationBean result;
292                    Connection conn = null;
293                    PreparedStatement stmt = null;
294                    ResultSet res = null;
295                    try {
296                            conn = DBConnection.getConnection();
297                            stmt = conn.prepareStatement(SQL);
298    
299                            stmt.setString(1, name );
300    
301                            res = stmt.executeQuery();
302                            result = convertResultset(res);
303    
304                    } finally {
305                            if (res != null)
306                                    res.close();
307                            if (stmt != null)
308                                    stmt.close();
309                            if (conn!= null)
310                                    conn.close();
311                    }
312    
313                    if (result.entries.size() == 1) {
314                            return result.entries.get(0);
315                    } else {
316                            return null;
317                    }
318            }
319    
320            @Deprecated
321          public int getIdByName(String name) throws SQLException {          public int getIdByName(String name) throws SQLException {
322                  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 " +
323                  "FROM trainstations " +                  "FROM trainstations " +

Legend:
Removed from v.1060  
changed lines
  Added in v.1417

  ViewVC Help
Powered by ViewVC 1.1.20