124 |
|
|
125 |
|
|
126 |
public StationBean getByNameFuzzy(final String name) throws SQLException { |
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, " + |
String SQL = "SELECT * FROM (" + |
128 |
"levenshtein(lower(name),lower(?) ) as leven " + |
" SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0, " + |
129 |
"FROM trainstations " + |
" levenshtein(lower(name),lower(?) ) as leven " + |
130 |
"WHERE (levenshtein(lower(name),lower(?) ) <= 3) " + |
" FROM trainstations " + |
131 |
"AND enabled = true " + |
" WHERE enabled = true ) as lev2 " + |
132 |
"ORDER BY leven " + |
"WHERE (leven <= 3) " + |
133 |
"LIMIT 1"; |
"ORDER BY leven " + |
134 |
|
"LIMIT 1"; |
135 |
|
|
136 |
class NameSetter implements StatementParamSetter { |
class NameSetter implements StatementParamSetter { |
137 |
@Override |
@Override |
138 |
public void setParams(PreparedStatement stmt) throws SQLException { |
public void setParams(PreparedStatement stmt) throws SQLException { |
139 |
stmt.setString(1, name ); |
stmt.setString(1, name ); |
|
stmt.setString(2, name ); |
|
140 |
} |
} |
141 |
} |
} |
142 |
|
|
145 |
return stations; |
return stations; |
146 |
} |
} |
147 |
|
|
148 |
public StationBean getByName(final String name) throws SQLException { |
private String removeSuffix(String str, String suffix) { |
149 |
|
if (str.endsWith(suffix)) { |
150 |
|
return str.substring(0, str.length() - suffix.length() ); |
151 |
|
} else { |
152 |
|
return str; |
153 |
|
} |
154 |
|
} |
155 |
|
|
156 |
|
public StationBean getByName(String name) throws SQLException { |
157 |
|
name = removeSuffix(name, " st."); |
158 |
|
name = removeSuffix(name, " st"); |
159 |
|
name = removeSuffix(name, " station"); |
160 |
|
|
161 |
StationBean stations = getByNameNormal(name); |
StationBean stations = getByNameNormal(name); |
162 |
|
|
163 |
if (stations.entries.size() == 0) { |
if (stations.entries.size() == 0) { |
|
logger.info("getByName failover: " + name); |
|
164 |
stations = getByNameFuzzy(name); |
stations = getByNameFuzzy(name); |
165 |
|
|
166 |
|
logger.info("getByName failover: " + name + "(" + (stations.entries.size() >0) + ")" ); |
167 |
} |
} |
168 |
return stations; |
return stations; |
169 |
} |
} |