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

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

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

android/TrainInfoService/src/dk/thoerup/traininfoservice/StationDAO.java revision 589 by torben, Mon Feb 8 19:25:12 2010 UTC android/TrainInfoServiceGoogle/src/dk/thoerup/traininfoservice/StationDAO.java revision 1106 by torben, Wed Sep 22 22:46:26 2010 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfoservice;  package dk.thoerup.traininfoservice;
2    
3  import java.sql.Connection;  import java.io.IOException;
 import java.sql.PreparedStatement;  
 import java.sql.ResultSet;  
 import java.sql.SQLException;  
 import java.sql.Statement;  
4  import java.util.ArrayList;  import java.util.ArrayList;
5    import java.util.Collections;
6    import java.util.Comparator;
7    import java.util.Iterator;
8  import java.util.List;  import java.util.List;
9    import java.util.logging.Logger;
10    
11    import javax.jdo.PersistenceManager;
12    import javax.jdo.Query;
13    
14    import dk.thoerup.android.traininfo.common.StationBean;
15    import dk.thoerup.android.traininfo.common.StationBean.StationEntry;
16    import dk.thoerup.traininfoservice.geo.Geo;
17    import dk.thoerup.traininfoservice.jdo.JdoStationBean;
18    import dk.thoerup.traininfoservice.jdo.PMF;
19    
20  public class StationDAO {  public class StationDAO {
21          private StationBean convertSingleRow(ResultSet res) throws SQLException {          final static int LOCATION_LIMIT = 8;
22                  StationBean station = new StationBean();          static final Logger logger = Logger.getLogger(StationDAO.class.getName());
23    
24    
25            public StationEntry getById(int id)  {
26    
27                    PersistenceManager pm = null;
28                                    
29                  station.setId( res.getInt(1) );                  try {
30                  station.setName( res.getString(2) );                          pm = PMF.get().getPersistenceManager();
31                  station.setLatitude( res.getDouble(3) );                          JdoStationBean bean =pm.getObjectById(JdoStationBean.class, new Integer(id) );
32                  station.setLongitude( res.getDouble(4) );                          return bean.toStationEntry();
33                  station.setRegional( res.getString(5) );                  } finally {
34                  station.setStrain( res.getString(6) );                          pm.close();
35                  station.setMetro( res.getString(7) );                  }
36                  station.setAddress( res.getString(8) );          }
37                  station.setCalcdist( (int)res.getDouble(9) );  
38            /* damn that JDO sucks so we to the filtering in java-code */
39            public StationBean getByName(String searchname) {
40                    PersistenceManager pm = null;
41                                    
42                  return station;                  try {
43                            pm = PMF.get().getPersistenceManager();
44                            
45                            
46                            
47                            Query q = pm.newQuery(JdoStationBean.class);
48                            q.setOrdering("name");                  
49                            List<JdoStationBean> beanList = (List<JdoStationBean>) q.execute();
50                            
51                            StationBean stationBean = new StationBean();
52                            
53                            searchname = searchname.toLowerCase();
54                            for(JdoStationBean bean : beanList) {
55                                    if (bean.getName().toLowerCase().startsWith(searchname)) {
56                                            stationBean.entries.add( bean.toStationEntry() );
57                                    } else {
58                                            for (String alias : bean.aliases ) {
59                                                    if (alias.toLowerCase().startsWith(searchname)) {
60                                                            stationBean.entries.add( bean.toStationEntry() );
61                                                    }
62                                            }
63                                    }
64                            }
65    
66                            return stationBean;                    
67                    } finally {
68                            pm.close();
69                    }
70          }          }
71                    
72          private List<StationBean> convertResultset(ResultSet res) throws SQLException {  
73                  List<StationBean> stations = new ArrayList<StationBean>();          //String limitExpression = (geolimit == true) ? "AND abs(latitude-?)<0.4 AND abs(longitude-?)<0.75 " : "";
74                  while (res.next()) {          public List<JdoStationBean> getByLocationList(double latitude, double longitude, boolean geolimit)  {
75                          stations.add( convertSingleRow(res) );          
76                    PersistenceManager pm = null;
77                    final double LAT = 0.4;
78                    final double LNG = 0.75;
79                    
80                    try {
81                            pm = PMF.get().getPersistenceManager();
82                            Query q = pm.newQuery(JdoStationBean.class);
83                            
84                            
85                            
86                            if (geolimit == true) {
87                                    double minLat = latitude - LAT;
88                                    double maxLat = latitude + LAT;
89                                    
90                                    //DAMN JDO implementation only allows us to compare on one parameter
91    
92                                    String filter = String.format("latitude > %f && latitude < %f", minLat, maxLat);
93                                    
94                                    q.setFilter( filter );
95                            }
96                            
97                            List<JdoStationBean> beanList = (List<JdoStationBean>) q.execute();                    
98                            
99                            logger.info("beanList size " + beanList.size());
100                            
101                            return beanList;
102                    } finally {
103                            pm.close();
104                  }                  }
105                  return stations;          }
106    
107    
108            public StationBean getByLocation(double latitude, double longitude)  {
109                    
110                    List<JdoStationBean> beanList = getByLocationList(latitude,longitude,true);
111                                    
112                    if (beanList.size() < LOCATION_LIMIT ) {
113                            logger.info("getByLocation failover: " +latitude + "," + longitude);
114                            beanList = getByLocationList(latitude,longitude, false);
115                    }
116    
117                    StationBean stationBean = new StationBean();                    
118    
119    
120                    Geo location = new Geo(latitude,longitude);
121                    for(JdoStationBean bean : beanList) {
122                            double meter = Geo.distanceKM( location, new Geo(bean.getLatitude(), bean.getLongitude() )) * 1000.0;
123    
124                            bean.distance = (int) meter;
125                    }
126    
127    
128                    Collections.sort(beanList, new Comparator<JdoStationBean>() {
129                            @Override
130                            public int compare(JdoStationBean o1, JdoStationBean o2) {
131                                    if (o1.distance < o2.distance) {
132                                            return -1;
133                                    } else if (o1.distance > o2.distance) {
134                                            return 1;
135                                    } else {
136                                            return 0;
137                                    }
138                            }
139                    });
140    
141                    for (int i=0; i<LOCATION_LIMIT && i<beanList.size(); i++) {
142                            stationBean.entries.add( beanList.get(i).toStationEntry() );
143                    }
144    
145                    return stationBean;                    
146          }          }
147                    
148            
149          public StationBean getById(int id) throws SQLException {          public StationBean getByList(String list)  {
150                  String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0 " +                  PersistenceManager pm = null;
                       "FROM trainstations WHERE id=" + id + " AND enabled=true";  
                   
                 Connection conn = null;  
                 Statement stmt = null;  
                 ResultSet res = null;  
                 StationBean result;  
151                                    
152                  try {                  try {
153                          conn = DBConnection.getConnection();                          String parts[] = list.split(",");
154                                            
155                          stmt = conn.createStatement();                          StringBuilder filter = new StringBuilder();
156                          res = stmt.executeQuery(SQL);                                    
157                          res.next();                          for(String part : parts)  {
158                          result = convertSingleRow(res);                                  if (filter.length() > 0) {
159                                            filter.append( " || " );
160                                    }
161                                    filter.append("id == ").append(part);
162                            }                      
163                            
164                            //String filter = "id == 10 || id == 82"; //TODO: build filter
165                            
166                            pm = PMF.get().getPersistenceManager();
167                            Query q = pm.newQuery(JdoStationBean.class);
168                            q.setFilter( filter.toString() );
169                            q.setOrdering("name");
170                            
171                            List<JdoStationBean> beanList = (List<JdoStationBean>) q.execute();
172                            
173                            StationBean stationBean = new StationBean();
174                            
175                            for(JdoStationBean bean : beanList) {
176                                    stationBean.entries.add( bean.toStationEntry() );
177                            }
178    
179                            return stationBean;                    
180                  } finally {                  } finally {
181                          if (res != null)                          pm.close();
                                 res.close();  
                         if (stmt != null)  
                                 stmt.close();  
                         if (conn != null)  
                                 conn.close();  
182                  }                  }
183    
184            }
185    
186    
187            public int getIdByName(String name)  {
188                    
189                    List<JdoStationBean> beanList = null;
190                    
191                    PersistenceManager pm = null;
192                                    
193                  return result;                  try {
194                            
195                            String filter = " name == '" + name + "'";
196                            
197                            pm = PMF.get().getPersistenceManager();
198                            Query q = pm.newQuery(JdoStationBean.class);
199                            q.setFilter(filter);
200                            
201                            beanList = (List<JdoStationBean>) q.execute();
202                            
203                            StationBean stationBean = new StationBean();
204                            
205                            for(JdoStationBean bean : beanList) {
206                                    stationBean.entries.add( bean.toStationEntry() );
207                            }
208                            
209                    } finally {
210                            pm.close();
211                    }
212    
213                    if ( beanList != null && beanList.size()  == 1) {
214                            return (int) beanList.get(0).getId();
215                    } else {
216                            return -1;
217                    }
218          }          }
219                    
220                    @SuppressWarnings("unchecked")
221          public List<StationBean> getByName(String name) throws SQLException {          public int saveStations(StationBean stationBean) throws IOException {
222                  String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " +                  PersistenceManager pm = null;
223                  "FROM trainstations " +                  
224                  "WHERE name ILIKE ? AND enabled = true " +                  try {
225                  "ORDER BY name ";                          pm = PMF.get().getPersistenceManager();
226                            
227                                            List oldEntries = (List) pm.newQuery(JdoStationBean.class).execute();
228                  List<StationBean> result;                          pm.deletePersistentAll(oldEntries);
229                  Connection conn = null;                          
230                  PreparedStatement stmt = null;                          List<JdoStationBean> jdoList = new ArrayList<JdoStationBean>();
231                  ResultSet res = null;                          for (StationEntry station : stationBean.entries) {
232                  try {                                  JdoStationBean jdoBean = JdoStationBean.fromStationEntry(station);
233                          conn = DBConnection.getConnection();                                  
234                          stmt = conn.prepareStatement(SQL);                                  jdoList.add(jdoBean);
235                                                            
                         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();  
                 }  
                 return result;  
         }  
           
         public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException {  
                 String SQL = "SELECT * FROM ( "+  
                 "               SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, 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 ";  
                 List<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);  
                         res = stmt.executeQuery();  
                         result = convertResultset(res);  
                           
                 } finally {  
                         if (res != null)  
                                 res.close();  
                         if (stmt != null)  
                                 stmt.close();  
                         if (conn!= null)  
                                 conn.close();  
                 }  
                 return result;  
         }  
           
          public List<StationBean> getByList(String list) throws SQLException {  
                         String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro, address,0.0 " +  
                         "FROM trainstations " +  
                         "WHERE id IN " + list + " AND enabled = true " +  
                         "ORDER BY name ";  
                           
                         Connection conn = null;  
                         Statement stmt = null;  
                         ResultSet res = null;  
                         List<StationBean> result;  
                           
                         try {  
                                 conn = DBConnection.getConnection();  
                                 stmt = conn.createStatement();  
                                 res = stmt.executeQuery(SQL);  
                                 result = convertResultset(res);  
                         } finally {  
                                 if (res != null)  
                                         res.close();  
                                 if (stmt != null)  
                                         stmt.close();  
                                 if (conn!= null)  
                                         conn.close();  
236                          }                          }
237                            pm.makePersistentAll(jdoList);
238                                                    
239                          return result;                          return jdoList.size();
240                                                    
241           }                  } finally {
242                                    pm.close();
243                    }
244            }
245            
246  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20