5 |
import java.sql.ResultSet; |
import java.sql.ResultSet; |
6 |
import java.sql.SQLException; |
import java.sql.SQLException; |
7 |
import java.sql.Statement; |
import java.sql.Statement; |
|
import java.util.ArrayList; |
|
|
import java.util.List; |
|
8 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
9 |
|
|
10 |
|
import javax.jdo.PersistenceManager; |
11 |
|
|
12 |
|
import dk.thoerup.android.traininfo.common.StationBean; |
13 |
|
import dk.thoerup.android.traininfo.common.StationBean.StationEntry; |
14 |
|
import dk.thoerup.traininfoservice.jdo.JdoStationBean; |
15 |
|
import dk.thoerup.traininfoservice.jdo.PMF; |
16 |
|
|
17 |
public class StationDAO { |
public class StationDAO { |
18 |
final static int LOCATION_LIMIT = 8; |
final static int LOCATION_LIMIT = 8; |
19 |
static final Logger logger = Logger.getLogger(StationDAO.class.getName()); |
static final Logger logger = Logger.getLogger(StationDAO.class.getName()); |
20 |
|
|
21 |
|
|
22 |
private StationBean convertSingleRow(ResultSet res) throws SQLException { |
private StationEntry convertSingleRow(ResultSet res) throws SQLException { |
23 |
StationBean station = new StationBean(); |
StationEntry station = new StationEntry(); |
24 |
|
|
25 |
station.setId( res.getInt(1) ); |
station.setId( res.getInt(1) ); |
26 |
station.setName( res.getString(2) ); |
station.setName( res.getString(2) ); |
31 |
station.setMetro( res.getString(7) ); |
station.setMetro( res.getString(7) ); |
32 |
station.setAddress( res.getString(8) ); |
station.setAddress( res.getString(8) ); |
33 |
station.setCalcdist( (int)res.getDouble(9) ); |
station.setCalcdist( (int)res.getDouble(9) ); |
34 |
|
|
35 |
|
station.setIsRegional( station.getRegional() != null ); |
36 |
|
station.setIsStrain( station.getStrain() != null ); |
37 |
|
station.setIsMetro( station.getMetro() != null ); |
38 |
|
|
39 |
return station; |
return station; |
40 |
} |
} |
41 |
|
|
42 |
private List<StationBean> convertResultset(ResultSet res) throws SQLException { |
private StationBean convertResultset(ResultSet res) throws SQLException { |
43 |
List<StationBean> stations = new ArrayList<StationBean>(); |
StationBean stations = new StationBean(); |
44 |
while (res.next()) { |
while (res.next()) { |
45 |
stations.add( convertSingleRow(res) ); |
stations.entries.add( convertSingleRow(res) ); |
46 |
} |
} |
47 |
return stations; |
return stations; |
48 |
|
|
49 |
} |
} |
50 |
|
|
51 |
|
|
52 |
public StationBean getById(int id) throws SQLException { |
public JdoStationBean getById(int id) throws SQLException { |
|
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0 " + |
|
|
"FROM trainstations WHERE id=" + id + " AND enabled=true"; |
|
|
|
|
|
Connection conn = null; |
|
|
Statement stmt = null; |
|
|
ResultSet res = null; |
|
|
StationBean result; |
|
53 |
|
|
54 |
|
PersistenceManager pm = null; |
55 |
|
|
56 |
try { |
try { |
57 |
conn = DBConnection.getConnection(); |
pm = PMF.get().getPersistenceManager(); |
58 |
|
return pm.getObjectById(JdoStationBean.class, new Integer(id) ); |
|
stmt = conn.createStatement(); |
|
|
res = stmt.executeQuery(SQL); |
|
|
res.next(); |
|
|
result = convertSingleRow(res); |
|
59 |
} finally { |
} finally { |
60 |
if (res != null) |
pm.close(); |
|
res.close(); |
|
|
if (stmt != null) |
|
|
stmt.close(); |
|
|
if (conn != null) |
|
|
conn.close(); |
|
61 |
} |
} |
|
|
|
|
return result; |
|
62 |
} |
} |
63 |
|
|
64 |
/* |
/* |
67 |
* 'select $2 ilike $1' language sql strict immutable; |
* 'select $2 ilike $1' language sql strict immutable; |
68 |
* create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~); |
* create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~); |
69 |
*/ |
*/ |
70 |
public List<StationBean> getByName(String name) throws SQLException { |
public StationBean getByName(String name) throws SQLException { |
71 |
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 " + |
72 |
"FROM trainstations " + |
"FROM trainstations " + |
73 |
"WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " + |
"WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " + |
74 |
"ORDER BY name "; |
"ORDER BY name "; |
75 |
|
|
76 |
|
|
77 |
List<StationBean> result; |
StationBean result; |
78 |
Connection conn = null; |
Connection conn = null; |
79 |
PreparedStatement stmt = null; |
PreparedStatement stmt = null; |
80 |
ResultSet res = null; |
ResultSet res = null; |
107 |
// calculate the distance: |
// calculate the distance: |
108 |
// 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 |
109 |
|
|
110 |
public List<StationBean> getByLocationWorker(double latitude, double longitude, boolean geolimit) throws SQLException { |
public StationBean getByLocationWorker(double latitude, double longitude, boolean geolimit) throws SQLException { |
111 |
|
|
112 |
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 " : ""; |
113 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
List<StationBean> result; |
StationBean result; |
124 |
Connection conn = null; |
Connection conn = null; |
125 |
PreparedStatement stmt = null; |
PreparedStatement stmt = null; |
126 |
ResultSet res = null; |
ResultSet res = null; |
147 |
return result; |
return result; |
148 |
} |
} |
149 |
|
|
150 |
public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException { |
public StationBean getByLocation(double latitude, double longitude) throws SQLException { |
151 |
List<StationBean> result = getByLocationWorker(latitude, longitude, true); |
StationBean result = getByLocationWorker(latitude, longitude, true); |
152 |
|
|
153 |
if (result.size() < LOCATION_LIMIT) { //failover |
if (result.entries.size() < LOCATION_LIMIT) { //failover |
154 |
logger.info("getByLocation failover: " +latitude + "," + longitude); |
logger.info("getByLocation failover: " +latitude + "," + longitude); |
155 |
|
|
156 |
result = getByLocationWorker(latitude, longitude, false); |
result = getByLocationWorker(latitude, longitude, false); |
161 |
|
|
162 |
|
|
163 |
|
|
164 |
public List<StationBean> getByList(String list) throws SQLException { |
public StationBean getByList(String list) throws SQLException { |
165 |
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 " + |
166 |
"FROM trainstations " + |
"FROM trainstations " + |
167 |
"WHERE id IN " + list + " AND enabled = true " + |
"WHERE id IN " + list + " AND enabled = true " + |
170 |
Connection conn = null; |
Connection conn = null; |
171 |
Statement stmt = null; |
Statement stmt = null; |
172 |
ResultSet res = null; |
ResultSet res = null; |
173 |
List<StationBean> result; |
StationBean result; |
174 |
|
|
175 |
try { |
try { |
176 |
conn = DBConnection.getConnection(); |
conn = DBConnection.getConnection(); |
218 |
"WHERE name = ? AND enabled = true " + |
"WHERE name = ? AND enabled = true " + |
219 |
"LIMIT 1 "; |
"LIMIT 1 "; |
220 |
|
|
221 |
List<StationBean> result; |
StationBean result; |
222 |
Connection conn = null; |
Connection conn = null; |
223 |
PreparedStatement stmt = null; |
PreparedStatement stmt = null; |
224 |
ResultSet res = null; |
ResultSet res = null; |
240 |
conn.close(); |
conn.close(); |
241 |
} |
} |
242 |
|
|
243 |
if (result.size() == 1) { |
if (result.entries.size() == 1) { |
244 |
return result.get(0).getId(); |
return result.entries.get(0).getId(); |
245 |
} else { |
} else { |
246 |
return -1; |
return -1; |
247 |
} |
} |