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 |
return station; |
return station; |
60 |
} |
} |
95 |
} |
} |
96 |
|
|
97 |
public StationEntry getById(int id) throws SQLException,NostationException { |
public StationEntry getById(int id) throws SQLException,NostationException { |
98 |
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 " + |
99 |
"FROM trainstations WHERE id=" + id + " AND enabled=true"; |
"FROM trainstations WHERE id=" + id + " AND enabled=true"; |
100 |
|
|
101 |
StationBean stations = fetchStations(SQL, new NullSetter() ); |
StationBean stations = fetchStations(SQL, new NullSetter() ); |
116 |
* create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~); |
* create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~); |
117 |
*/ |
*/ |
118 |
public StationBean getByNameNormal(final String name) throws SQLException { |
public StationBean getByNameNormal(final String name) throws SQLException { |
119 |
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 " + |
120 |
"FROM trainstations " + |
"FROM trainstations " + |
121 |
"WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " + |
"WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " + |
122 |
"ORDER BY name "; |
"ORDER BY name "; |
134 |
|
|
135 |
|
|
136 |
public StationBean getByNameFuzzy(final String name) throws SQLException { |
public StationBean getByNameFuzzy(final String name) throws SQLException { |
137 |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0, " + |
String SQL = "SELECT * FROM (" + |
138 |
"levenshtein(lower(name),lower(?) ) as leven " + |
" SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0, aliases, " + |
139 |
"FROM trainstations " + |
" levenshtein(lower(name),lower(?) ) as leven " + |
140 |
"WHERE (levenshtein(lower(name),lower(?) ) <= 3) " + |
" FROM trainstations " + |
141 |
"AND enabled = true " + |
" WHERE enabled = true ) as lev2 " + |
142 |
"ORDER BY leven " + |
"WHERE (leven <= 3) " + |
143 |
"LIMIT 1"; |
"ORDER BY leven " + |
144 |
|
"LIMIT 1"; |
145 |
|
|
146 |
class NameSetter implements StatementParamSetter { |
class NameSetter implements StatementParamSetter { |
147 |
@Override |
@Override |
148 |
public void setParams(PreparedStatement stmt) throws SQLException { |
public void setParams(PreparedStatement stmt) throws SQLException { |
149 |
stmt.setString(1, name ); |
stmt.setString(1, name ); |
|
stmt.setString(2, name ); |
|
150 |
} |
} |
151 |
} |
} |
152 |
|
|
155 |
return stations; |
return stations; |
156 |
} |
} |
157 |
|
|
158 |
public StationBean getByName(final String name) throws SQLException { |
private String removeSuffix(String str, String suffix) { |
159 |
|
if (str.endsWith(suffix)) { |
160 |
|
return str.substring(0, str.length() - suffix.length() ); |
161 |
|
} else { |
162 |
|
return str; |
163 |
|
} |
164 |
|
} |
165 |
|
|
166 |
|
public StationBean getByName(String name) throws SQLException { |
167 |
|
name = removeSuffix(name, " st."); |
168 |
|
name = removeSuffix(name, " st"); |
169 |
|
name = removeSuffix(name, " station"); |
170 |
|
|
171 |
StationBean stations = getByNameNormal(name); |
StationBean stations = getByNameNormal(name); |
172 |
|
|
173 |
if (stations.entries.size() == 0) { |
if (stations.entries.size() == 0) { |
|
logger.info("getByName failover: " + name); |
|
174 |
stations = getByNameFuzzy(name); |
stations = getByNameFuzzy(name); |
175 |
|
|
176 |
|
logger.info("getByName failover: " + name + "(" + (stations.entries.size() >0) + ")" ); |
177 |
} |
} |
178 |
return stations; |
return stations; |
179 |
} |
} |
198 |
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 " : ""; |
199 |
|
|
200 |
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, " + |
201 |
"earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " + |
"earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist,aliases " + |
202 |
"FROM trainstations " + |
"FROM trainstations " + |
203 |
"WHERE enabled = true " + limitExpression + |
"WHERE enabled = true " + limitExpression + |
204 |
"ORDER BY calcdist ASC " + |
"ORDER BY calcdist ASC " + |
235 |
|
|
236 |
|
|
237 |
public StationBean getByList(String list) throws SQLException { |
public StationBean getByList(String list) throws SQLException { |
238 |
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 " + |
239 |
"FROM trainstations " + |
"FROM trainstations " + |
240 |
"WHERE id IN " + list + " AND enabled = true " + |
"WHERE id IN " + list + " AND enabled = true " + |
241 |
"ORDER BY name "; |
"ORDER BY name "; |
246 |
|
|
247 |
|
|
248 |
public StationEntry getSimpleByName(final String name) throws SQLException { |
public StationEntry getSimpleByName(final String name) throws SQLException { |
249 |
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 " + |
250 |
"FROM trainstations " + |
"FROM trainstations " + |
251 |
"WHERE name = ? AND enabled = true " + |
"WHERE name = ? AND enabled = true " + |
252 |
"LIMIT 1 "; |
"LIMIT 1 "; |
267 |
} |
} |
268 |
} |
} |
269 |
|
|
270 |
|
Comparator<StationEntry> nameComparator = new Comparator<StationEntry>() { |
271 |
|
@Override |
272 |
|
public int compare(StationEntry arg0, StationEntry arg1) { |
273 |
|
return arg0.getName().compareTo( arg1.getName() ); |
274 |
|
} |
275 |
|
}; |
276 |
|
|
277 |
//used to create full dump in order to populate Google Appengine DB |
//used to create full dump in order to populate Google Appengine DB |
278 |
@Deprecated |
//after 1.1.0 also used to populate client-side station list |
279 |
public StationBean dumpAll() throws SQLException { |
public StationBean dumpAll() throws SQLException { |
280 |
|
|
281 |
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 " + |
282 |
"FROM trainstations WHERE enabled = true ORDER BY id"; |
"FROM trainstations WHERE enabled = true"; |
283 |
|
|
284 |
|
|
285 |
|
StationBean stations = fetchStations(SQL, new NullSetter() ); |
286 |
|
Collections.sort( stations.entries,nameComparator ); |
287 |
|
|
288 |
|
return stations; |
289 |
|
|
290 |
|
/* |
291 |
Connection conn = null; |
Connection conn = null; |
292 |
Statement stmt = null; |
Statement stmt = null; |
293 |
ResultSet res = null; |
ResultSet res = null; |
313 |
stations.entries.add( entry ); |
stations.entries.add( entry ); |
314 |
|
|
315 |
} |
} |
316 |
|
Collections.sort( stations.entries,nameComparator ); |
317 |
return stations; |
return stations; |
318 |
|
|
319 |
|
|
324 |
stmt.close(); |
stmt.close(); |
325 |
if (conn != null) |
if (conn != null) |
326 |
conn.close(); |
conn.close(); |
327 |
} |
}*/ |
328 |
|
|
329 |
} |
} |
330 |
|
|
352 |
return station; |
return station; |
353 |
} |
} |
354 |
|
|
355 |
|
public boolean hasDisabledStation(final String name) throws SQLException { |
356 |
|
String SQL = "Select count(*) as antal from trainstations where name = '" + name + "' and enabled=false " ; |
357 |
|
|
358 |
|
Connection conn = DBConnection.getConnection(); |
359 |
|
Statement stmt = conn.createStatement(); |
360 |
|
ResultSet rs = stmt.executeQuery( SQL ); |
361 |
|
|
362 |
|
rs.next(); |
363 |
|
|
364 |
|
int antal = rs.getInt(1); |
365 |
|
|
366 |
|
rs.close(); |
367 |
|
stmt.close(); |
368 |
|
conn.close(); |
369 |
|
|
370 |
|
return (antal > 0); |
371 |
|
} |
372 |
|
|
373 |
|
|
374 |
@Deprecated |
@Deprecated |