6 |
import java.sql.ResultSet; |
import java.sql.ResultSet; |
7 |
import java.sql.SQLException; |
import java.sql.SQLException; |
8 |
import java.sql.Statement; |
import java.sql.Statement; |
9 |
|
import java.util.Collections; |
10 |
|
import java.util.Comparator; |
11 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
12 |
|
|
13 |
import dk.thoerup.android.traininfo.common.StationBean; |
import dk.thoerup.android.traininfo.common.StationBean; |
47 |
station.setIsRegional( station.getRegional() != null ); |
station.setIsRegional( station.getRegional() != null ); |
48 |
station.setIsStrain( station.getStrain() != null ); |
station.setIsStrain( station.getStrain() != null ); |
49 |
station.setIsMetro( station.getMetro() != null ); |
station.setIsMetro( station.getMetro() != null ); |
50 |
|
|
51 |
|
Array aliases = res.getArray( 10); |
52 |
|
if (aliases != null) { |
53 |
|
station.setAliases( (String[]) aliases.getArray() ); |
54 |
|
} else { |
55 |
|
String[] emptyArray = {}; |
56 |
|
station.setAliases( emptyArray ); |
57 |
|
} |
58 |
|
|
59 |
|
station.setTritStation( res.getInt(11) ); |
60 |
|
if (res.wasNull()) |
61 |
|
station.setTritStation( -1 ); |
62 |
|
|
63 |
return station; |
return station; |
64 |
} |
} |
99 |
} |
} |
100 |
|
|
101 |
public StationEntry getById(int id) throws SQLException,NostationException { |
public StationEntry getById(int id) throws SQLException,NostationException { |
102 |
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,aliases,tritstation " + |
103 |
"FROM trainstations WHERE id=" + id + " AND enabled=true"; |
"FROM trainstations WHERE id=" + id + " AND enabled=true"; |
104 |
|
|
105 |
StationBean stations = fetchStations(SQL, new NullSetter() ); |
StationBean stations = fetchStations(SQL, new NullSetter() ); |
120 |
* create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~); |
* create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~); |
121 |
*/ |
*/ |
122 |
public StationBean getByNameNormal(final String name) throws SQLException { |
public StationBean getByNameNormal(final String name) throws SQLException { |
123 |
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,aliases,tritstation " + |
124 |
"FROM trainstations " + |
"FROM trainstations " + |
125 |
"WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " + |
"WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " + |
126 |
"ORDER BY name "; |
"ORDER BY name "; |
138 |
|
|
139 |
|
|
140 |
public StationBean getByNameFuzzy(final String name) throws SQLException { |
public StationBean getByNameFuzzy(final String name) throws SQLException { |
141 |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0, " + |
String SQL = "SELECT * FROM (" + |
142 |
"levenshtein(lower(name),lower(?) ) as leven " + |
" SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0, aliases, tritstation, " + |
143 |
"FROM trainstations " + |
" levenshtein(lower(name),lower(?) ) as leven " + |
144 |
"WHERE (levenshtein(lower(name),lower(?) ) <= 3) AND enabled = true " + |
" FROM trainstations " + |
145 |
"AND enabled = true " + |
" WHERE enabled = true ) as lev2 " + |
146 |
"ORDER BY leven " + |
"WHERE (leven <= 3) " + |
147 |
"LIMIT 1"; |
"ORDER BY leven " + |
148 |
|
"LIMIT 1"; |
149 |
|
|
150 |
class NameSetter implements StatementParamSetter { |
class NameSetter implements StatementParamSetter { |
151 |
@Override |
@Override |
152 |
public void setParams(PreparedStatement stmt) throws SQLException { |
public void setParams(PreparedStatement stmt) throws SQLException { |
153 |
stmt.setString(1, name ); |
stmt.setString(1, name ); |
|
stmt.setString(2, name ); |
|
154 |
} |
} |
155 |
} |
} |
156 |
|
|
159 |
return stations; |
return stations; |
160 |
} |
} |
161 |
|
|
162 |
public StationBean getByName(final String name) throws SQLException { |
private String removeSuffix(String str, String suffix) { |
163 |
|
if (str.endsWith(suffix)) { |
164 |
|
return str.substring(0, str.length() - suffix.length() ); |
165 |
|
} else { |
166 |
|
return str; |
167 |
|
} |
168 |
|
} |
169 |
|
|
170 |
|
public StationBean getByName(String name) throws SQLException { |
171 |
|
name = removeSuffix(name, " st."); |
172 |
|
name = removeSuffix(name, " st"); |
173 |
|
name = removeSuffix(name, " station"); |
174 |
|
|
175 |
StationBean stations = getByNameNormal(name); |
StationBean stations = getByNameNormal(name); |
176 |
|
|
177 |
if (stations.entries.size() == 0) { |
if (stations.entries.size() == 0) { |
|
logger.info("getByName failover: " + name); |
|
178 |
stations = getByNameFuzzy(name); |
stations = getByNameFuzzy(name); |
179 |
|
|
180 |
|
logger.info("getByName failover: " + name + "(" + (stations.entries.size() >0) + ")" ); |
181 |
} |
} |
182 |
return stations; |
return stations; |
183 |
} |
} |
187 |
//Latitude (horizonal), longitude(vertical) so |
//Latitude (horizonal), longitude(vertical) so |
188 |
// 1 degree latitude is ~ 111320 meters, since the distance between the horizonal lines is always the same |
// 1 degree latitude is ~ 111320 meters, since the distance between the horizonal lines is always the same |
189 |
// 1 degree longitude is ~111320 meters at equator but gets shorter as we get closer to the poles. |
// 1 degree longitude is ~111320 meters at equator but gets shorter as we get closer to the poles. |
190 |
// the "hack" with max 0.4 degrees latitude and 0.75 degrees longitude is only valid since we only service danish trains, |
// so 1 degree longitude is 64.5 km at denmarks southern point (gedser=54.55,11.95) |
191 |
|
// and is 59.4km at northern point (skagen = 57.75,10.65) |
192 |
|
// The "hack" with max 0.4 degrees latitude and 0.75 degrees longitude is only valid since we only service danish trains, |
193 |
// in denmark 0.4dg latitude ~ 44km, 0.75dg longitude ~ 47km |
// in denmark 0.4dg latitude ~ 44km, 0.75dg longitude ~ 47km |
194 |
|
|
195 |
// 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) |
202 |
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 " : ""; |
203 |
|
|
204 |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, " + |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, " + |
205 |
"earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " + |
"earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist,aliases,tritstation " + |
206 |
"FROM trainstations " + |
"FROM trainstations " + |
207 |
"WHERE enabled = true " + limitExpression + |
"WHERE enabled = true " + limitExpression + |
208 |
"ORDER BY calcdist ASC " + |
"ORDER BY calcdist ASC " + |
239 |
|
|
240 |
|
|
241 |
public StationBean getByList(String list) throws SQLException { |
public StationBean getByList(String list) throws SQLException { |
242 |
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,aliases,tritstation " + |
243 |
"FROM trainstations " + |
"FROM trainstations " + |
244 |
"WHERE id IN " + list + " AND enabled = true " + |
"WHERE id IN " + list + " AND enabled = true " + |
245 |
"ORDER BY name "; |
"ORDER BY name "; |
250 |
|
|
251 |
|
|
252 |
public StationEntry getSimpleByName(final String name) throws SQLException { |
public StationEntry getSimpleByName(final String name) throws SQLException { |
253 |
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,aliases,tritstation " + |
254 |
"FROM trainstations " + |
"FROM trainstations " + |
255 |
"WHERE name = ? AND enabled = true " + |
"WHERE name = ? AND enabled = true " + |
256 |
"LIMIT 1 "; |
"LIMIT 1 "; |
271 |
} |
} |
272 |
} |
} |
273 |
|
|
274 |
|
Comparator<StationEntry> nameComparator = new Comparator<StationEntry>() { |
275 |
|
@Override |
276 |
|
public int compare(StationEntry arg0, StationEntry arg1) { |
277 |
|
return arg0.getName().compareTo( arg1.getName() ); |
278 |
|
} |
279 |
|
}; |
280 |
|
|
281 |
//used to create full dump in order to populate Google Appengine DB |
//used to create full dump in order to populate Google Appengine DB |
282 |
@Deprecated |
//after 1.1.0 also used to populate client-side station list |
283 |
public StationBean dumpAll() throws SQLException { |
public StationBean dumpAll() throws SQLException { |
284 |
|
|
285 |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0,aliases " + |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0,aliases,tritstation " + |
286 |
"FROM trainstations WHERE enabled = true ORDER BY id"; |
"FROM trainstations WHERE enabled = true"; |
287 |
|
|
288 |
|
|
289 |
|
StationBean stations = fetchStations(SQL, new NullSetter() ); |
290 |
|
Collections.sort( stations.entries,nameComparator ); |
291 |
|
|
292 |
|
return stations; |
293 |
|
|
294 |
|
/* |
295 |
Connection conn = null; |
Connection conn = null; |
296 |
Statement stmt = null; |
Statement stmt = null; |
297 |
ResultSet res = null; |
ResultSet res = null; |
317 |
stations.entries.add( entry ); |
stations.entries.add( entry ); |
318 |
|
|
319 |
} |
} |
320 |
|
Collections.sort( stations.entries,nameComparator ); |
321 |
return stations; |
return stations; |
322 |
|
|
323 |
|
|
328 |
stmt.close(); |
stmt.close(); |
329 |
if (conn != null) |
if (conn != null) |
330 |
conn.close(); |
conn.close(); |
331 |
} |
}*/ |
332 |
|
|
333 |
} |
} |
334 |
|
|
335 |
|
/* |
336 |
@Deprecated |
@Deprecated |
337 |
public static String getStationName(int stationID) { |
public static String getStationName(int stationID) { |
338 |
String station = ""; |
String station = ""; |
355 |
} |
} |
356 |
|
|
357 |
return station; |
return station; |
358 |
} |
}*/ |
|
|
|
359 |
|
|
360 |
|
public boolean hasDisabledStation(final String name) throws SQLException { |
361 |
|
String SQL = "Select count(*) as antal from trainstations where name = '" + name + "' and enabled=false " ; |
362 |
|
|
363 |
|
Connection conn = DBConnection.getConnection(); |
364 |
|
Statement stmt = conn.createStatement(); |
365 |
|
ResultSet rs = stmt.executeQuery( SQL ); |
366 |
|
|
367 |
|
rs.next(); |
368 |
|
|
369 |
|
int antal = rs.getInt(1); |
370 |
|
|
371 |
|
rs.close(); |
372 |
|
stmt.close(); |
373 |
|
conn.close(); |
374 |
|
|
375 |
|
return (antal > 0); |
376 |
|
} |
377 |
|
|
378 |
|
/* |
379 |
@Deprecated |
@Deprecated |
380 |
public int getIdByName(final String name) throws SQLException { |
public int getIdByName(final String name) throws SQLException { |
381 |
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 " + |
397 |
} else { |
} else { |
398 |
return -1; |
return -1; |
399 |
} |
} |
400 |
} |
}*/ |
401 |
} |
} |