1 |
package dk.thoerup.traininfoservice; |
2 |
|
3 |
import java.sql.Connection; |
4 |
import java.sql.PreparedStatement; |
5 |
import java.sql.ResultSet; |
6 |
import java.sql.SQLException; |
7 |
import java.sql.Statement; |
8 |
import java.util.ArrayList; |
9 |
import java.util.List; |
10 |
|
11 |
public class StationDAO { |
12 |
private StationBean convertSingleRow(ResultSet res) throws SQLException { |
13 |
StationBean station = new StationBean(); |
14 |
|
15 |
station.setId( res.getInt(1) ); |
16 |
station.setName( res.getString(2) ); |
17 |
station.setLatitude( res.getDouble(3) ); |
18 |
station.setLongitude( res.getDouble(4) ); |
19 |
station.setRegional( res.getString(5) ); |
20 |
station.setStrain( res.getString(6) ); |
21 |
station.setMetro( res.getString(7) ); |
22 |
station.setAddress( res.getString(8) ); |
23 |
station.setCalcdist( (int)res.getDouble(9) ); |
24 |
|
25 |
return station; |
26 |
} |
27 |
|
28 |
private List<StationBean> convertResultset(ResultSet res) throws SQLException { |
29 |
List<StationBean> stations = new ArrayList<StationBean>(); |
30 |
while (res.next()) { |
31 |
stations.add( convertSingleRow(res) ); |
32 |
} |
33 |
return stations; |
34 |
|
35 |
} |
36 |
|
37 |
|
38 |
public StationBean getById(int id) throws SQLException { |
39 |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0 " + |
40 |
"FROM trainstations WHERE id=" + id + " AND enabled=true"; |
41 |
|
42 |
Connection conn = null; |
43 |
Statement stmt = null; |
44 |
ResultSet res = null; |
45 |
StationBean result; |
46 |
|
47 |
try { |
48 |
conn = DBConnection.getConnection(); |
49 |
|
50 |
stmt = conn.createStatement(); |
51 |
res = stmt.executeQuery(SQL); |
52 |
res.next(); |
53 |
result = convertSingleRow(res); |
54 |
} finally { |
55 |
if (res != null) |
56 |
res.close(); |
57 |
if (stmt != null) |
58 |
stmt.close(); |
59 |
if (conn != null) |
60 |
conn.close(); |
61 |
} |
62 |
|
63 |
return result; |
64 |
} |
65 |
|
66 |
|
67 |
public List<StationBean> getByName(String name) throws SQLException { |
68 |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " + |
69 |
"FROM trainstations " + |
70 |
"WHERE name ILIKE ? AND enabled = true " + |
71 |
"ORDER BY name "; |
72 |
|
73 |
|
74 |
List<StationBean> result; |
75 |
Connection conn = null; |
76 |
PreparedStatement stmt = null; |
77 |
ResultSet res = null; |
78 |
try { |
79 |
conn = DBConnection.getConnection(); |
80 |
stmt = conn.prepareStatement(SQL); |
81 |
|
82 |
stmt.setString(1, name + "%"); |
83 |
|
84 |
res = stmt.executeQuery(); |
85 |
result = convertResultset(res); |
86 |
|
87 |
} finally { |
88 |
if (res != null) |
89 |
res.close(); |
90 |
if (stmt != null) |
91 |
stmt.close(); |
92 |
if (conn!= null) |
93 |
conn.close(); |
94 |
} |
95 |
return result; |
96 |
} |
97 |
|
98 |
public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException { |
99 |
String SQL = "SELECT * FROM ( "+ |
100 |
" SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address," + |
101 |
" earth_distance( ll_to_earth(latitude,longitude), ll_to_earth(?,?))::int AS calcdist " + |
102 |
" FROM trainstations " + |
103 |
" WHERE enabled = true " + |
104 |
" ) AS trainstations2 " + |
105 |
"ORDER BY calcdist ASC " + |
106 |
"LIMIT 4 "; |
107 |
List<StationBean> result; |
108 |
Connection conn = null; |
109 |
PreparedStatement stmt = null; |
110 |
ResultSet res = null; |
111 |
try { |
112 |
conn = DBConnection.getConnection(); |
113 |
stmt = conn.prepareStatement(SQL); |
114 |
stmt.setDouble(1, latitude); |
115 |
stmt.setDouble(2, longitude); |
116 |
res = stmt.executeQuery(); |
117 |
result = convertResultset(res); |
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 |
return result; |
128 |
} |
129 |
|
130 |
public List<StationBean> getByList(String list) throws SQLException { |
131 |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro, address,0.0 " + |
132 |
"FROM trainstations " + |
133 |
"WHERE id IN " + list + " AND enabled = true " + |
134 |
"ORDER BY name "; |
135 |
|
136 |
Connection conn = null; |
137 |
Statement stmt = null; |
138 |
ResultSet res = null; |
139 |
List<StationBean> result; |
140 |
|
141 |
try { |
142 |
conn = DBConnection.getConnection(); |
143 |
stmt = conn.createStatement(); |
144 |
res = stmt.executeQuery(SQL); |
145 |
result = convertResultset(res); |
146 |
} finally { |
147 |
if (res != null) |
148 |
res.close(); |
149 |
if (stmt != null) |
150 |
stmt.close(); |
151 |
if (conn!= null) |
152 |
conn.close(); |
153 |
} |
154 |
|
155 |
return result; |
156 |
|
157 |
} |
158 |
|
159 |
} |