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,44 +247,49 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final List<SuggestedLocation> locations = new ArrayList<>();
|
final List<SuggestedLocation> locations = new ArrayList<>();
|
||||||
|
|
||||||
final JSONObject head = new JSONObject(page.toString());
|
final JSONObject head = new JSONObject(page.toString());
|
||||||
final JSONObject stopFinder = head.optJSONObject("stopFinder");
|
final JSONObject stopFinder = head.optJSONObject("stopFinder");
|
||||||
final JSONArray stops;
|
if (stopFinder != null) {
|
||||||
if (stopFinder == null) {
|
|
||||||
stops = head.getJSONArray("stopFinder");
|
|
||||||
} else {
|
|
||||||
final JSONArray messages = stopFinder.optJSONArray("message");
|
final JSONArray messages = stopFinder.optJSONArray("message");
|
||||||
if (messages != null) {
|
if (messages != null) {
|
||||||
for (int i = 0; i < messages.length(); i++) {
|
final SuggestLocationsResult.Status status = parseJsonMessages(messages);
|
||||||
final JSONObject message = messages.optJSONObject(i);
|
if (status != null)
|
||||||
final String messageName = message.getString("name");
|
return new SuggestLocationsResult(header, status);
|
||||||
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 JSONObject points = stopFinder.optJSONObject("points");
|
final JSONObject points = stopFinder.optJSONObject("points");
|
||||||
if (points != null) {
|
if (points != null) {
|
||||||
final JSONObject stop = points.getJSONObject("point");
|
final JSONObject point = points.getJSONObject("point");
|
||||||
final SuggestedLocation location = parseJsonStop(stop);
|
final SuggestedLocation location = parseJsonStop(point);
|
||||||
locations.add(location);
|
locations.add(location);
|
||||||
return new SuggestLocationsResult(header, locations);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
stops = stopFinder.optJSONArray("points");
|
final JSONArray pointsArray = stopFinder.optJSONArray("points");
|
||||||
if (stops == null)
|
if (pointsArray != null) {
|
||||||
return new SuggestLocationsResult(header, locations);
|
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();
|
final JSONArray pointsArray = head.optJSONArray("stopFinder");
|
||||||
|
if (pointsArray != null) {
|
||||||
for (int i = 0; i < nStops; i++) {
|
final int nPoints = pointsArray.length();
|
||||||
final JSONObject stop = stops.optJSONObject(i);
|
for (int i = 0; i < nPoints; i++) {
|
||||||
final SuggestedLocation location = parseJsonStop(stop);
|
final JSONObject point = pointsArray.optJSONObject(i);
|
||||||
locations.add(location);
|
final SuggestedLocation location = parseJsonStop(point);
|
||||||
|
locations.add(location);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new SuggestLocationsResult(header, locations);
|
return new SuggestLocationsResult(header, locations);
|
||||||
|
@ -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 {
|
private SuggestedLocation parseJsonStop(final JSONObject stop) throws JSONException {
|
||||||
String type = stop.getString("type");
|
String type = stop.getString("type");
|
||||||
if ("any".equals(type))
|
if ("any".equals(type))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue