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