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 |
|
int tritStation = res.getInt(11); |
60 |
|
if (res.wasNull()) { |
61 |
|
tritStation = -1; |
62 |
|
} |
63 |
|
station.setTritStation( tritStation ); |
64 |
|
|
65 |
return station; |
return station; |
66 |
} |
} |
101 |
} |
} |
102 |
|
|
103 |
public StationEntry getById(int id) throws SQLException,NostationException { |
public StationEntry getById(int id) throws SQLException,NostationException { |
104 |
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 " + |
105 |
"FROM trainstations WHERE id=" + id + " AND enabled=true"; |
"FROM trainstations WHERE id=" + id + " AND enabled=true"; |
106 |
|
|
107 |
StationBean stations = fetchStations(SQL, new NullSetter() ); |
StationBean stations = fetchStations(SQL, new NullSetter() ); |
122 |
* create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~); |
* create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~); |
123 |
*/ |
*/ |
124 |
public StationBean getByNameNormal(final String name) throws SQLException { |
public StationBean getByNameNormal(final String name) throws SQLException { |
125 |
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 " + |
126 |
"FROM trainstations " + |
"FROM trainstations " + |
127 |
"WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " + |
"WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " + |
128 |
"ORDER BY name "; |
"ORDER BY name "; |
140 |
|
|
141 |
|
|
142 |
public StationBean getByNameFuzzy(final String name) throws SQLException { |
public StationBean getByNameFuzzy(final String name) throws SQLException { |
143 |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0, " + |
String SQL = "SELECT * FROM (" + |
144 |
"levenshtein(lower(name),lower(?) ) as leven " + |
" SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0, aliases, tritstation, " + |
145 |
"FROM trainstations " + |
" levenshtein(lower(name),lower(?) ) as leven " + |
146 |
"WHERE (levenshtein(lower(name),lower(?) ) <= 3) " + |
" FROM trainstations " + |
147 |
"AND enabled = true " + |
" WHERE enabled = true ) as lev2 " + |
148 |
"ORDER BY leven " + |
"WHERE (leven <= 3) " + |
149 |
"LIMIT 1"; |
"ORDER BY leven " + |
150 |
|
"LIMIT 1"; |
151 |
|
|
152 |
class NameSetter implements StatementParamSetter { |
class NameSetter implements StatementParamSetter { |
153 |
@Override |
@Override |
154 |
public void setParams(PreparedStatement stmt) throws SQLException { |
public void setParams(PreparedStatement stmt) throws SQLException { |
155 |
stmt.setString(1, name ); |
stmt.setString(1, name ); |
|
stmt.setString(2, name ); |
|
156 |
} |
} |
157 |
} |
} |
158 |
|
|
161 |
return stations; |
return stations; |
162 |
} |
} |
163 |
|
|
164 |
public StationBean getByName(final String name) throws SQLException { |
private String removeSuffix(String str, String suffix) { |
165 |
|
if (str.endsWith(suffix)) { |
166 |
|
return str.substring(0, str.length() - suffix.length() ); |
167 |
|
} else { |
168 |
|
return str; |
169 |
|
} |
170 |
|
} |
171 |
|
|
172 |
|
public StationBean getByName(String name) throws SQLException { |
173 |
|
name = removeSuffix(name, " st."); |
174 |
|
name = removeSuffix(name, " st"); |
175 |
|
name = removeSuffix(name, " station"); |
176 |
|
|
177 |
StationBean stations = getByNameNormal(name); |
StationBean stations = getByNameNormal(name); |
178 |
|
|
179 |
if (stations.entries.size() == 0) { |
if (stations.entries.size() == 0) { |
|
logger.info("getByName failover: " + name); |
|
180 |
stations = getByNameFuzzy(name); |
stations = getByNameFuzzy(name); |
181 |
|
|
182 |
|
logger.info("getByName failover: " + name + "(" + (stations.entries.size() >0) + ")" ); |
183 |
} |
} |
184 |
return stations; |
return stations; |
185 |
} |
} |
204 |
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 " : ""; |
205 |
|
|
206 |
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, " + |
207 |
"earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " + |
"earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist,aliases,tritstation " + |
208 |
"FROM trainstations " + |
"FROM trainstations " + |
209 |
"WHERE enabled = true " + limitExpression + |
"WHERE enabled = true " + limitExpression + |
210 |
"ORDER BY calcdist ASC " + |
"ORDER BY calcdist ASC " + |
241 |
|
|
242 |
|
|
243 |
public StationBean getByList(String list) throws SQLException { |
public StationBean getByList(String list) throws SQLException { |
244 |
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 " + |
245 |
"FROM trainstations " + |
"FROM trainstations " + |
246 |
"WHERE id IN " + list + " AND enabled = true " + |
"WHERE id IN " + list + " AND enabled = true " + |
247 |
"ORDER BY name "; |
"ORDER BY name "; |
252 |
|
|
253 |
|
|
254 |
public StationEntry getSimpleByName(final String name) throws SQLException { |
public StationEntry getSimpleByName(final String name) throws SQLException { |
255 |
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 " + |
256 |
"FROM trainstations " + |
"FROM trainstations " + |
257 |
"WHERE name = ? AND enabled = true " + |
"WHERE name = ? AND enabled = true " + |
258 |
"LIMIT 1 "; |
"LIMIT 1 "; |
273 |
} |
} |
274 |
} |
} |
275 |
|
|
276 |
|
Comparator<StationEntry> nameComparator = new Comparator<StationEntry>() { |
277 |
|
@Override |
278 |
|
public int compare(StationEntry arg0, StationEntry arg1) { |
279 |
|
return arg0.getName().compareTo( arg1.getName() ); |
280 |
|
} |
281 |
|
}; |
282 |
|
|
283 |
//used to create full dump in order to populate Google Appengine DB |
//used to create full dump in order to populate Google Appengine DB |
284 |
@Deprecated |
//after 1.1.0 also used to populate client-side station list |
285 |
public StationBean dumpAll() throws SQLException { |
public StationBean dumpAll() throws SQLException { |
286 |
|
|
287 |
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 " + |
288 |
"FROM trainstations WHERE enabled = true ORDER BY id"; |
"FROM trainstations WHERE enabled = true"; |
289 |
|
|
290 |
|
|
291 |
|
StationBean stations = fetchStations(SQL, new NullSetter() ); |
292 |
|
Collections.sort( stations.entries,nameComparator ); |
293 |
|
|
294 |
|
return stations; |
295 |
|
|
296 |
|
/* |
297 |
Connection conn = null; |
Connection conn = null; |
298 |
Statement stmt = null; |
Statement stmt = null; |
299 |
ResultSet res = null; |
ResultSet res = null; |
319 |
stations.entries.add( entry ); |
stations.entries.add( entry ); |
320 |
|
|
321 |
} |
} |
322 |
|
Collections.sort( stations.entries,nameComparator ); |
323 |
return stations; |
return stations; |
324 |
|
|
325 |
|
|
330 |
stmt.close(); |
stmt.close(); |
331 |
if (conn != null) |
if (conn != null) |
332 |
conn.close(); |
conn.close(); |
333 |
} |
}*/ |
334 |
|
|
335 |
} |
} |
336 |
|
|
337 |
|
/* |
338 |
@Deprecated |
@Deprecated |
339 |
public static String getStationName(int stationID) { |
public static String getStationName(int stationID) { |
340 |
String station = ""; |
String station = ""; |
357 |
} |
} |
358 |
|
|
359 |
return station; |
return station; |
360 |
} |
}*/ |
|
|
|
361 |
|
|
362 |
|
public boolean hasDisabledStation(final String name) throws SQLException { |
363 |
|
String SQL = "Select count(*) as antal from trainstations where name = '" + name + "' and enabled=false " ; |
364 |
|
|
365 |
|
Connection conn = DBConnection.getConnection(); |
366 |
|
Statement stmt = conn.createStatement(); |
367 |
|
ResultSet rs = stmt.executeQuery( SQL ); |
368 |
|
|
369 |
|
rs.next(); |
370 |
|
|
371 |
|
int antal = rs.getInt(1); |
372 |
|
|
373 |
|
rs.close(); |
374 |
|
stmt.close(); |
375 |
|
conn.close(); |
376 |
|
|
377 |
|
return (antal > 0); |
378 |
|
} |
379 |
|
|
380 |
|
/* |
381 |
@Deprecated |
@Deprecated |
382 |
public int getIdByName(final String name) throws SQLException { |
public int getIdByName(final String name) throws SQLException { |
383 |
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 " + |
399 |
} else { |
} else { |
400 |
return -1; |
return -1; |
401 |
} |
} |
402 |
} |
}*/ |
403 |
} |
} |