mirror of
https://gitlab.com/oeffi/public-transport-enabler.git
synced 2025-07-15 09:00:36 +00:00
Navitia: More efficient product parsing
According to CanalTP/navitia#1386, Navitia has finally added the product to departure query results, so all information we need can now be retrieved by one network query only. The AbstractNavitiaProvider was adapted accordingly and all legacy code including the Product Cache was removed.
This commit is contained in:
parent
62a5a54d73
commit
6d99e63db8
1 changed files with 6 additions and 59 deletions
|
@ -669,15 +669,18 @@ public abstract class AbstractNavitiaProvider extends AbstractNetworkProvider
|
|||
}
|
||||
}
|
||||
|
||||
private Line parseLine(final JSONObject jsonLine) throws IOException
|
||||
private Line parseLine(final JSONObject jsonRoute) throws IOException
|
||||
{
|
||||
try
|
||||
{
|
||||
final JSONObject jsonLine = jsonRoute.getJSONObject("line");
|
||||
final String lineId = jsonLine.getString("id");
|
||||
String network = null;
|
||||
if (jsonLine.has("network"))
|
||||
network = Strings.emptyToNull(jsonLine.getJSONObject("network").optString("name"));
|
||||
final Product product = parseLineProduct(jsonLine);
|
||||
final JSONObject mode = jsonRoute.getJSONArray("physical_modes").getJSONObject(0);
|
||||
final String modeId = mode.getString("id");
|
||||
final Product product = parseLineProductFromMode(modeId);
|
||||
final String code = jsonLine.getString("code");
|
||||
final String name = Strings.emptyToNull(jsonLine.optString("name"));
|
||||
final String color = Strings.emptyToNull(jsonLine.getString("color"));
|
||||
|
@ -692,8 +695,6 @@ public abstract class AbstractNavitiaProvider extends AbstractNetworkProvider
|
|||
}
|
||||
}
|
||||
|
||||
private Map<String, Product> lineProductCache = new WeakHashMap<String, Product>();
|
||||
|
||||
private Product parseLineProductFromMode(final String modeId)
|
||||
{
|
||||
final String modeType = modeId.replace("physical_mode:", "");
|
||||
|
@ -726,59 +727,6 @@ public abstract class AbstractNavitiaProvider extends AbstractNetworkProvider
|
|||
}
|
||||
}
|
||||
|
||||
private Product parseLineProduct(final JSONObject line) throws IOException
|
||||
{
|
||||
try
|
||||
{
|
||||
final String lineId = line.getString("id");
|
||||
final JSONObject mode;
|
||||
|
||||
if (line.has("physical_modes"))
|
||||
{
|
||||
mode = line.getJSONArray("physical_modes").getJSONObject(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
final Product cachedProduct = lineProductCache.get(lineId);
|
||||
if (cachedProduct != null)
|
||||
return cachedProduct;
|
||||
|
||||
// this makes a network request and is sometimes necessary
|
||||
mode = getLinePhysicalMode(lineId);
|
||||
}
|
||||
|
||||
final String modeId = mode.getString("id");
|
||||
final Product product = parseLineProductFromMode(modeId);
|
||||
|
||||
lineProductCache.put(lineId, product);
|
||||
|
||||
return product;
|
||||
}
|
||||
catch (final JSONException jsonExc)
|
||||
{
|
||||
throw new ParserException(jsonExc);
|
||||
}
|
||||
}
|
||||
|
||||
private JSONObject getLinePhysicalMode(final String lineId) throws IOException
|
||||
{
|
||||
final String uri = uri() + "lines/" + ParserUtils.urlEncode(lineId) + "/physical_modes";
|
||||
final CharSequence page = httpClient.get(uri);
|
||||
|
||||
try
|
||||
{
|
||||
final JSONObject head = new JSONObject(page.toString());
|
||||
final JSONArray physicalModes = head.getJSONArray("physical_modes");
|
||||
final JSONObject physicalMode = physicalModes.getJSONObject(0);
|
||||
|
||||
return physicalMode;
|
||||
}
|
||||
catch (final JSONException jsonExc)
|
||||
{
|
||||
throw new ParserException(jsonExc);
|
||||
}
|
||||
}
|
||||
|
||||
private LineDestination getStationLine(final Line line, final JSONObject jsonDeparture) throws IOException
|
||||
{
|
||||
try
|
||||
|
@ -971,8 +919,7 @@ public abstract class AbstractNavitiaProvider extends AbstractNetworkProvider
|
|||
|
||||
// Build line.
|
||||
final JSONObject route = jsonDeparture.getJSONObject("route");
|
||||
final JSONObject jsonLine = route.getJSONObject("line");
|
||||
final Line line = parseLine(jsonLine);
|
||||
final Line line = parseLine(route);
|
||||
|
||||
final JSONObject stopPoint = jsonDeparture.getJSONObject("stop_point");
|
||||
final Location location = parsePlace(stopPoint, PlaceType.STOP_POINT);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue