mirror of
https://gitlab.com/oeffi/public-transport-enabler.git
synced 2025-07-19 00:39:58 +00:00
AbstractEfaProvider: Make control flow clearer in jsonStopfinderRequest().
This commit is contained in:
parent
6a4f2a6b9b
commit
22d552f07f
1 changed files with 43 additions and 26 deletions
|
@ -247,45 +247,50 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
|
|||
|
||||
try {
|
||||
final List<SuggestedLocation> locations = new ArrayList<>();
|
||||
|
||||
final JSONObject head = new JSONObject(page.toString());
|
||||
final JSONObject stopFinder = head.optJSONObject("stopFinder");
|
||||
final JSONArray stops;
|
||||
if (stopFinder == null) {
|
||||
stops = head.getJSONArray("stopFinder");
|
||||
} else {
|
||||
if (stopFinder != null) {
|
||||
final JSONArray messages = stopFinder.optJSONArray("message");
|
||||
if (messages != null) {
|
||||
for (int i = 0; i < messages.length(); i++) {
|
||||
final JSONObject message = messages.optJSONObject(i);
|
||||
final String messageName = message.getString("name");
|
||||
final String messageValue = Strings.emptyToNull(message.getString("value"));
|
||||
if ("code".equals(messageName) && !"-8010".equals(messageValue)
|
||||
&& !"-8011".equals(messageValue))
|
||||
return new SuggestLocationsResult(header, SuggestLocationsResult.Status.SERVICE_DOWN);
|
||||
}
|
||||
final SuggestLocationsResult.Status status = parseJsonMessages(messages);
|
||||
if (status != null)
|
||||
return new SuggestLocationsResult(header, status);
|
||||
}
|
||||
|
||||
final JSONObject points = stopFinder.optJSONObject("points");
|
||||
if (points != null) {
|
||||
final JSONObject stop = points.getJSONObject("point");
|
||||
final SuggestedLocation location = parseJsonStop(stop);
|
||||
final JSONObject point = points.getJSONObject("point");
|
||||
final SuggestedLocation location = parseJsonStop(point);
|
||||
locations.add(location);
|
||||
return new SuggestLocationsResult(header, locations);
|
||||
}
|
||||
|
||||
stops = stopFinder.optJSONArray("points");
|
||||
if (stops == null)
|
||||
return new SuggestLocationsResult(header, locations);
|
||||
final JSONArray pointsArray = stopFinder.optJSONArray("points");
|
||||
if (pointsArray != null) {
|
||||
final int nPoints = pointsArray.length();
|
||||
for (int i = 0; i < nPoints; i++) {
|
||||
final JSONObject point = pointsArray.optJSONObject(i);
|
||||
final SuggestedLocation location = parseJsonStop(point);
|
||||
locations.add(location);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
final JSONArray messages = head.optJSONArray("message");
|
||||
if (messages != null) {
|
||||
final SuggestLocationsResult.Status status = parseJsonMessages(messages);
|
||||
if (status != null)
|
||||
return new SuggestLocationsResult(header, status);
|
||||
}
|
||||
|
||||
final int nStops = stops.length();
|
||||
|
||||
for (int i = 0; i < nStops; i++) {
|
||||
final JSONObject stop = stops.optJSONObject(i);
|
||||
final SuggestedLocation location = parseJsonStop(stop);
|
||||
final JSONArray pointsArray = head.optJSONArray("stopFinder");
|
||||
if (pointsArray != null) {
|
||||
final int nPoints = pointsArray.length();
|
||||
for (int i = 0; i < nPoints; i++) {
|
||||
final JSONObject point = pointsArray.optJSONObject(i);
|
||||
final SuggestedLocation location = parseJsonStop(point);
|
||||
locations.add(location);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new SuggestLocationsResult(header, locations);
|
||||
} catch (final JSONException x) {
|
||||
|
@ -293,6 +298,18 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
|
|||
}
|
||||
}
|
||||
|
||||
private SuggestLocationsResult.Status parseJsonMessages(final JSONArray messages) throws JSONException {
|
||||
final int messagesSize = messages.length();
|
||||
for (int i = 0; i < messagesSize; i++) {
|
||||
final JSONObject message = messages.optJSONObject(i);
|
||||
final String messageName = message.getString("name");
|
||||
final String messageValue = Strings.emptyToNull(message.getString("value"));
|
||||
if ("code".equals(messageName) && !"-8010".equals(messageValue) && !"-8011".equals(messageValue))
|
||||
return SuggestLocationsResult.Status.SERVICE_DOWN;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private SuggestedLocation parseJsonStop(final JSONObject stop) throws JSONException {
|
||||
String type = stop.getString("type");
|
||||
if ("any".equals(type))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue