mirror of
https://gitlab.com/oeffi/public-transport-enabler.git
synced 2025-07-08 10:08:48 +00:00
NetworkProvider: Add types parameter to suggestLocations().
This commit is contained in:
parent
0ea83a1ad3
commit
8e52035ce5
11 changed files with 70 additions and 57 deletions
|
@ -241,10 +241,10 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
|
|||
url.addEncodedQueryParameter("coordOutputFormatTail", Integer.toString(COORD_FORMAT_TAIL));
|
||||
}
|
||||
|
||||
protected SuggestLocationsResult jsonStopfinderRequest(final Location constraint, final int maxLocations)
|
||||
throws IOException {
|
||||
protected SuggestLocationsResult jsonStopfinderRequest(final CharSequence constraint,
|
||||
final @Nullable Set<LocationType> types, final int maxLocations) throws IOException {
|
||||
final HttpUrl.Builder url = stopFinderEndpoint.newBuilder();
|
||||
appendStopfinderRequestParameters(url, constraint, "JSON", maxLocations);
|
||||
appendStopfinderRequestParameters(url, constraint, "JSON", types, maxLocations);
|
||||
final CharSequence page = httpClient.get(url.build());
|
||||
final ResultHeader header = new ResultHeader(network, SERVER_PRODUCT);
|
||||
|
||||
|
@ -350,30 +350,35 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
|
|||
return new SuggestedLocation(location, quality);
|
||||
}
|
||||
|
||||
private void appendStopfinderRequestParameters(final HttpUrl.Builder url, final Location constraint,
|
||||
final String outputFormat, final int maxLocations) {
|
||||
private void appendStopfinderRequestParameters(final HttpUrl.Builder url, final CharSequence constraint,
|
||||
final String outputFormat, final @Nullable Set<LocationType> types, final int maxLocations) {
|
||||
appendCommonRequestParams(url, outputFormat);
|
||||
url.addEncodedQueryParameter("locationServerActive", "1");
|
||||
if (includeRegionId)
|
||||
url.addEncodedQueryParameter("regionID_sf", "1"); // prefer own region
|
||||
appendLocationParams(url, constraint, "sf");
|
||||
if (constraint.type == LocationType.ANY) {
|
||||
url.addEncodedQueryParameter("type_sf", "any");
|
||||
url.addEncodedQueryParameter("name_sf", ParserUtils.urlEncode(constraint.toString(), requestUrlEncoding));
|
||||
if (needsSpEncId)
|
||||
url.addEncodedQueryParameter("SpEncId", "0");
|
||||
// 1=place 2=stop 4=street 8=address 16=crossing 32=poi 64=postcode
|
||||
url.addEncodedQueryParameter("anyObjFilter_sf", Integer.toString(2 + 4 + 8 + 16 + 32 + 64));
|
||||
int filter = 0;
|
||||
if (types == null || types.contains(LocationType.STATION))
|
||||
filter += 2; // stop
|
||||
if (types == null || types.contains(LocationType.POI))
|
||||
filter += 32; // poi
|
||||
if (types == null || types.contains(LocationType.ADDRESS))
|
||||
filter += 4 + 8 + 16 + 64; // street + address + crossing + postcode
|
||||
url.addEncodedQueryParameter("anyObjFilter_sf", Integer.toString(filter));
|
||||
url.addEncodedQueryParameter("reducedAnyPostcodeObjFilter_sf", "64");
|
||||
url.addEncodedQueryParameter("reducedAnyTooManyObjFilter_sf", "2");
|
||||
url.addEncodedQueryParameter("useHouseNumberList", "true");
|
||||
if (maxLocations > 0)
|
||||
url.addEncodedQueryParameter("anyMaxSizeHitList", Integer.toString(maxLocations));
|
||||
}
|
||||
}
|
||||
|
||||
protected SuggestLocationsResult mobileStopfinderRequest(final Location constraint, final int maxLocations)
|
||||
throws IOException {
|
||||
protected SuggestLocationsResult mobileStopfinderRequest(final CharSequence constraint,
|
||||
final @Nullable Set<LocationType> types, final int maxLocations) throws IOException {
|
||||
final HttpUrl.Builder url = stopFinderEndpoint.newBuilder();
|
||||
appendStopfinderRequestParameters(url, constraint, "XML", maxLocations);
|
||||
appendStopfinderRequestParameters(url, constraint, "XML", types, maxLocations);
|
||||
final AtomicReference<SuggestLocationsResult> result = new AtomicReference<>();
|
||||
|
||||
final HttpClient.Callback callback = new HttpClient.Callback() {
|
||||
|
@ -607,9 +612,9 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
|
|||
}
|
||||
|
||||
@Override
|
||||
public SuggestLocationsResult suggestLocations(final CharSequence constraint, final int maxLocations)
|
||||
throws IOException {
|
||||
return jsonStopfinderRequest(new Location(LocationType.ANY, null, null, constraint.toString()), maxLocations);
|
||||
public SuggestLocationsResult suggestLocations(final CharSequence constraint,
|
||||
final @Nullable Set<LocationType> types, final int maxLocations) throws IOException {
|
||||
return jsonStopfinderRequest(constraint, types, maxLocations);
|
||||
}
|
||||
|
||||
private interface ProcessItdOdvCallback {
|
||||
|
|
|
@ -196,9 +196,9 @@ public abstract class AbstractHafasClientInterfaceProvider extends AbstractHafas
|
|||
}
|
||||
|
||||
@Override
|
||||
public SuggestLocationsResult suggestLocations(final CharSequence constraint, final int maxLocations)
|
||||
throws IOException {
|
||||
return jsonLocMatch(constraint, null, maxLocations);
|
||||
public SuggestLocationsResult suggestLocations(final CharSequence constraint,
|
||||
final @Nullable Set<LocationType> types, final int maxLocations) throws IOException {
|
||||
return jsonLocMatch(constraint, types, maxLocations);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -295,8 +295,8 @@ public abstract class AbstractHafasLegacyProvider extends AbstractHafasProvider
|
|||
}
|
||||
|
||||
@Override
|
||||
public SuggestLocationsResult suggestLocations(final CharSequence constraint, final int maxLocations)
|
||||
throws IOException {
|
||||
public SuggestLocationsResult suggestLocations(final CharSequence constraint,
|
||||
final @Nullable Set<LocationType> types, final int maxLocations) throws IOException {
|
||||
final HttpUrl.Builder url = getStopEndpoint.newBuilder().addPathSegment(apiLanguage);
|
||||
appendJsonGetStopsParameters(url, checkNotNull(constraint), maxLocations);
|
||||
return jsonGetStops(url.build());
|
||||
|
@ -701,7 +701,7 @@ public abstract class AbstractHafasLegacyProvider extends AbstractHafasProvider
|
|||
final ResultHeader header = new ResultHeader(network, SERVER_PRODUCT);
|
||||
|
||||
if (!from.isIdentified()) {
|
||||
final List<Location> locations = suggestLocations(from.name, 0).getLocations();
|
||||
final List<Location> locations = suggestLocations(from.name, null, 0).getLocations();
|
||||
if (locations.isEmpty())
|
||||
return new QueryTripsResult(header, QueryTripsResult.Status.NO_TRIPS); // TODO
|
||||
if (locations.size() > 1)
|
||||
|
@ -710,7 +710,7 @@ public abstract class AbstractHafasLegacyProvider extends AbstractHafasProvider
|
|||
}
|
||||
|
||||
if (via != null && !via.isIdentified()) {
|
||||
final List<Location> locations = suggestLocations(via.name, 0).getLocations();
|
||||
final List<Location> locations = suggestLocations(via.name, null, 0).getLocations();
|
||||
if (locations.isEmpty())
|
||||
return new QueryTripsResult(header, QueryTripsResult.Status.NO_TRIPS); // TODO
|
||||
if (locations.size() > 1)
|
||||
|
@ -719,7 +719,7 @@ public abstract class AbstractHafasLegacyProvider extends AbstractHafasProvider
|
|||
}
|
||||
|
||||
if (!to.isIdentified()) {
|
||||
final List<Location> locations = suggestLocations(to.name, 0).getLocations();
|
||||
final List<Location> locations = suggestLocations(to.name, null, 0).getLocations();
|
||||
if (locations.isEmpty())
|
||||
return new QueryTripsResult(header, QueryTripsResult.Status.NO_TRIPS); // TODO
|
||||
if (locations.size() > 1)
|
||||
|
@ -1360,7 +1360,7 @@ public abstract class AbstractHafasLegacyProvider extends AbstractHafasProvider
|
|||
final ResultHeader header = new ResultHeader(network, SERVER_PRODUCT);
|
||||
|
||||
if (!from.isIdentified()) {
|
||||
final List<Location> locations = suggestLocations(from.name, 0).getLocations();
|
||||
final List<Location> locations = suggestLocations(from.name, null, 0).getLocations();
|
||||
if (locations.isEmpty())
|
||||
return new QueryTripsResult(header, QueryTripsResult.Status.NO_TRIPS); // TODO
|
||||
if (locations.size() > 1)
|
||||
|
@ -1369,7 +1369,7 @@ public abstract class AbstractHafasLegacyProvider extends AbstractHafasProvider
|
|||
}
|
||||
|
||||
if (via != null && !via.isIdentified()) {
|
||||
final List<Location> locations = suggestLocations(via.name, 0).getLocations();
|
||||
final List<Location> locations = suggestLocations(via.name, null, 0).getLocations();
|
||||
if (locations.isEmpty())
|
||||
return new QueryTripsResult(header, QueryTripsResult.Status.NO_TRIPS); // TODO
|
||||
if (locations.size() > 1)
|
||||
|
@ -1378,7 +1378,7 @@ public abstract class AbstractHafasLegacyProvider extends AbstractHafasProvider
|
|||
}
|
||||
|
||||
if (!to.isIdentified()) {
|
||||
final List<Location> locations = suggestLocations(to.name, 0).getLocations();
|
||||
final List<Location> locations = suggestLocations(to.name, null, 0).getLocations();
|
||||
if (locations.isEmpty())
|
||||
return new QueryTripsResult(header, QueryTripsResult.Status.NO_TRIPS); // TODO
|
||||
if (locations.size() > 1)
|
||||
|
|
|
@ -845,14 +845,17 @@ public abstract class AbstractNavitiaProvider extends AbstractNetworkProvider {
|
|||
}
|
||||
|
||||
@Override
|
||||
public SuggestLocationsResult suggestLocations(final CharSequence constraint, final int maxLocations)
|
||||
throws IOException {
|
||||
public SuggestLocationsResult suggestLocations(final CharSequence constraint,
|
||||
final @Nullable Set<LocationType> types, final int maxLocations) throws IOException {
|
||||
final String nameCstr = constraint.toString();
|
||||
|
||||
final HttpUrl.Builder url = url().addPathSegment("places");
|
||||
url.addQueryParameter("q", nameCstr);
|
||||
if (types == null || types.contains(LocationType.ANY) || types.contains(LocationType.STATION))
|
||||
url.addQueryParameter("type[]", "stop_area");
|
||||
if (types == null || types.contains(LocationType.ANY) || types.contains(LocationType.ADDRESS))
|
||||
url.addQueryParameter("type[]", "address");
|
||||
if (types == null || types.contains(LocationType.ANY) || types.contains(LocationType.POI))
|
||||
url.addQueryParameter("type[]", "poi");
|
||||
url.addQueryParameter("type[]", "administrative_region");
|
||||
url.addQueryParameter("depth", "1");
|
||||
|
@ -1009,7 +1012,7 @@ public abstract class AbstractNavitiaProvider extends AbstractNetworkProvider {
|
|||
Location newFrom = null, newTo = null;
|
||||
|
||||
if (!from.isIdentified() && from.hasName()) {
|
||||
ambiguousFrom = suggestLocations(from.name, 0).getLocations();
|
||||
ambiguousFrom = suggestLocations(from.name, null, 0).getLocations();
|
||||
if (ambiguousFrom.isEmpty())
|
||||
return new QueryTripsResult(resultHeader, QueryTripsResult.Status.UNKNOWN_FROM);
|
||||
if (ambiguousFrom.size() == 1 && ambiguousFrom.get(0).isIdentified())
|
||||
|
@ -1017,7 +1020,7 @@ public abstract class AbstractNavitiaProvider extends AbstractNetworkProvider {
|
|||
}
|
||||
|
||||
if (!to.isIdentified() && to.hasName()) {
|
||||
ambiguousTo = suggestLocations(to.name, 0).getLocations();
|
||||
ambiguousTo = suggestLocations(to.name, null, 0).getLocations();
|
||||
if (ambiguousTo.isEmpty())
|
||||
return new QueryTripsResult(resultHeader, QueryTripsResult.Status.UNKNOWN_TO);
|
||||
if (ambiguousTo.size() == 1 && ambiguousTo.get(0).isIdentified())
|
||||
|
|
|
@ -81,7 +81,7 @@ public abstract class AbstractNetworkProvider implements NetworkProvider {
|
|||
@Deprecated
|
||||
@Override
|
||||
public final SuggestLocationsResult suggestLocations(final CharSequence constraint) throws IOException {
|
||||
return suggestLocations(constraint, 0);
|
||||
return suggestLocations(constraint, null, 0);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
|
|
|
@ -130,9 +130,9 @@ public class BayernProvider extends AbstractEfaProvider {
|
|||
}
|
||||
|
||||
@Override
|
||||
public SuggestLocationsResult suggestLocations(final CharSequence constraint, final int maxLocations)
|
||||
throws IOException {
|
||||
return mobileStopfinderRequest(new Location(LocationType.ANY, null, null, constraint.toString()), maxLocations);
|
||||
public SuggestLocationsResult suggestLocations(final CharSequence constraint,
|
||||
final @Nullable Set<LocationType> types, final int maxLocations) throws IOException {
|
||||
return mobileStopfinderRequest(constraint, types, maxLocations);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -827,7 +827,8 @@ public class NegentweeProvider extends AbstractNetworkProvider {
|
|||
}
|
||||
|
||||
@Override
|
||||
public SuggestLocationsResult suggestLocations(CharSequence constraint, int maxLocations) throws IOException {
|
||||
public SuggestLocationsResult suggestLocations(CharSequence constraint, @Nullable Set<LocationType> types,
|
||||
int maxLocations) throws IOException {
|
||||
HttpUrl url = buildApiUrl("locations", Arrays.asList(new QueryParameter("q", constraint.toString())));
|
||||
final CharSequence page;
|
||||
try {
|
||||
|
|
|
@ -112,12 +112,15 @@ public interface NetworkProvider {
|
|||
*
|
||||
* @param constraint
|
||||
* input by user so far
|
||||
* @param types
|
||||
* types of locations to suggest, or {@code null} for any
|
||||
* @param maxLocations
|
||||
* maximum number of locations to suggest or {@code 0}
|
||||
* @return location suggestions
|
||||
* @throws IOException
|
||||
*/
|
||||
SuggestLocationsResult suggestLocations(CharSequence constraint, int maxLocations) throws IOException;
|
||||
SuggestLocationsResult suggestLocations(CharSequence constraint, @Nullable Set<LocationType> types,
|
||||
int maxLocations) throws IOException;
|
||||
|
||||
@Deprecated
|
||||
SuggestLocationsResult suggestLocations(CharSequence constraint) throws IOException;
|
||||
|
|
|
@ -72,9 +72,9 @@ public class StvProvider extends AbstractEfaProvider {
|
|||
}
|
||||
|
||||
@Override
|
||||
public SuggestLocationsResult suggestLocations(final CharSequence constraint, final int maxLocations)
|
||||
throws IOException {
|
||||
return mobileStopfinderRequest(new Location(LocationType.ANY, null, null, constraint.toString()), maxLocations);
|
||||
public SuggestLocationsResult suggestLocations(final CharSequence constraint,
|
||||
final @Nullable Set<LocationType> types, final int maxLocations) throws IOException {
|
||||
return mobileStopfinderRequest(constraint, types, maxLocations);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -28,6 +28,7 @@ import java.util.Collections;
|
|||
import java.util.Comparator;
|
||||
import java.util.Currency;
|
||||
import java.util.Date;
|
||||
import java.util.EnumSet;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
|
@ -573,14 +574,14 @@ public class VrsProvider extends AbstractNetworkProvider {
|
|||
}
|
||||
|
||||
@Override
|
||||
public SuggestLocationsResult suggestLocations(final CharSequence constraint, final int maxLocations)
|
||||
throws IOException {
|
||||
public SuggestLocationsResult suggestLocations(final CharSequence constraint,
|
||||
final @Nullable Set<LocationType> types, final int maxLocations) throws IOException {
|
||||
// sc = station count
|
||||
final int sc = maxLocations / 2;
|
||||
final int sc = EnumSet.of(LocationType.STATION).equals(types) ? maxLocations : maxLocations / 2;
|
||||
// ac = address count
|
||||
final int ac = maxLocations / 4;
|
||||
final int ac = EnumSet.of(LocationType.ADDRESS).equals(types) ? maxLocations : maxLocations / 4;
|
||||
// pc = points of interest count
|
||||
final int pc = maxLocations / 4;
|
||||
final int pc = EnumSet.of(LocationType.POI).equals(types) ? maxLocations : maxLocations / 4;
|
||||
// t = sap (stops and/or addresses and/or pois)
|
||||
final HttpUrl.Builder url = API_BASE.newBuilder();
|
||||
url.addQueryParameter("eID", "tx_vrsinfo_ass2_objects");
|
||||
|
@ -1127,7 +1128,7 @@ public class VrsProvider extends AbstractNetworkProvider {
|
|||
} else if (loc.coord != null) {
|
||||
return String.format(Locale.ENGLISH, "%f,%f", loc.getLatAsDouble(), loc.getLonAsDouble());
|
||||
} else {
|
||||
SuggestLocationsResult suggestLocationsResult = suggestLocations(loc.name, 0);
|
||||
SuggestLocationsResult suggestLocationsResult = suggestLocations(loc.name, null, 0);
|
||||
final List<Location> suggestedLocations = suggestLocationsResult.getLocations();
|
||||
if (suggestedLocations.size() == 1) {
|
||||
return suggestedLocations.get(0).id;
|
||||
|
|
|
@ -115,7 +115,7 @@ public abstract class AbstractProviderLiveTest {
|
|||
}
|
||||
|
||||
protected final SuggestLocationsResult suggestLocations(final CharSequence constraint) throws IOException {
|
||||
return provider.suggestLocations(constraint, 0);
|
||||
return provider.suggestLocations(constraint, null, 0);
|
||||
}
|
||||
|
||||
protected final QueryTripsResult queryTrips(final Location from, final @Nullable Location via, final Location to,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue