105 |
* 'select $2 ilike $1' language sql strict immutable; |
* 'select $2 ilike $1' language sql strict immutable; |
106 |
* create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~); |
* create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~); |
107 |
*/ |
*/ |
108 |
public StationBean getByName(final String name) throws SQLException { |
public StationBean getByNameNormal(final String name) throws SQLException { |
109 |
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 " + |
110 |
"FROM trainstations " + |
"FROM trainstations " + |
111 |
"WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " + |
"WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " + |
121 |
|
|
122 |
return fetchStations(SQL, new NameSetter() ); |
return fetchStations(SQL, new NameSetter() ); |
123 |
} |
} |
124 |
|
|
125 |
|
|
126 |
|
public StationBean getByNameFuzzy(final String name) throws SQLException { |
127 |
|
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0, " + |
128 |
|
"levenshtein(lower(name),lower(?) ) as leven " + |
129 |
|
"FROM trainstations " + |
130 |
|
"WHERE (levenshtein(lower(name),lower(?) ) <= 3) " + |
131 |
|
"AND enabled = true " + |
132 |
|
"ORDER BY leven " + |
133 |
|
"LIMIT 1"; |
134 |
|
|
135 |
|
class NameSetter implements StatementParamSetter { |
136 |
|
@Override |
137 |
|
public void setParams(PreparedStatement stmt) throws SQLException { |
138 |
|
stmt.setString(1, name ); |
139 |
|
stmt.setString(2, name ); |
140 |
|
} |
141 |
|
} |
142 |
|
|
143 |
|
StationBean stations = fetchStations(SQL, new NameSetter() ); |
144 |
|
stations.fuzzystrmatch = true; |
145 |
|
return stations; |
146 |
|
} |
147 |
|
|
148 |
|
public StationBean getByName(final String name) throws SQLException { |
149 |
|
StationBean stations = getByNameNormal(name); |
150 |
|
|
151 |
|
if (stations.entries.size() == 0) { |
152 |
|
logger.info("getByName failover: " + name); |
153 |
|
stations = getByNameFuzzy(name); |
154 |
|
} |
155 |
|
return stations; |
156 |
|
} |
157 |
|
|
158 |
|
|
159 |
|
|
160 |
//Latitude (horizonal), longitude(vertical) so |
//Latitude (horizonal), longitude(vertical) so |
161 |
// 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 |