|
| 1 | +package api.external.transit; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import org.json.JSONArray; |
| 6 | +import org.json.JSONObject; |
| 7 | + |
| 8 | +import de.schildbach.pte.dto.Fare; |
| 9 | +import de.schildbach.pte.dto.Location; |
| 10 | +import de.schildbach.pte.dto.Trip; |
| 11 | +import de.schildbach.pte.dto.Trip.Leg; |
| 12 | +import de.schildbach.pte.dto.Line; |
| 13 | + |
| 14 | +public class JSONConverter { |
| 15 | + public static JSONObject tripToJSON(Trip trip) { |
| 16 | + JSONObject tripObj = new JSONObject(); |
| 17 | + tripObj.put("durationMinutes", trip.getDuration() / 60000.0); |
| 18 | + tripObj.put("departureTime", trip.getFirstDepartureTime()); |
| 19 | + JSONObject legs = new JSONObject(); |
| 20 | + for (Integer i = 0; i < trip.legs.size(); i++) { |
| 21 | + legs.put(i.toString(), JSONConverter.legToJSON(trip.legs.get(i))); |
| 22 | + } |
| 23 | + tripObj.put("legs", legs); |
| 24 | + tripObj.put("numChanges", trip.numChanges); |
| 25 | + JSONArray fares = new JSONArray(); |
| 26 | + if (trip.fares != null) { |
| 27 | + for (Fare fare : trip.fares) { |
| 28 | + fares.put(JSONConverter.fareToJson(fare)); |
| 29 | + } |
| 30 | + tripObj.put("fares", fares); |
| 31 | + } |
| 32 | + tripObj.put("description", JSONConverter.getDescription(trip.legs)); |
| 33 | + return tripObj; |
| 34 | + } |
| 35 | + |
| 36 | + public static JSONObject fareToJson(Fare fare) { |
| 37 | + JSONObject fareObj = new JSONObject(); |
| 38 | + if (fare == null) return fareObj; |
| 39 | + fareObj.put("currency", fare.currency.getCurrencyCode()); |
| 40 | + fareObj.put("fare", fare.fare); |
| 41 | + fareObj.put("network", fare.network); |
| 42 | + fareObj.put("units", fare.units); |
| 43 | + fareObj.put("unitName", fare.unitName); |
| 44 | + fareObj.put("class", fare.getClass().getName()); |
| 45 | + return fareObj; |
| 46 | + } |
| 47 | + |
| 48 | + public static JSONObject legToJSON(Leg leg) { |
| 49 | + JSONObject legObj = new JSONObject(); |
| 50 | + if (leg == null) return legObj; |
| 51 | + legObj.put("departure", JSONConverter.locationToJSON(leg.departure)); |
| 52 | + legObj.put("arrival", JSONConverter.locationToJSON(leg.arrival)); |
| 53 | + legObj.put("departureTime", leg.getDepartureTime()); |
| 54 | + legObj.put("arrivalTime", leg.getArrivalTime()); |
| 55 | + if (leg instanceof Trip.Public) { |
| 56 | + Trip.Public pub = (Trip.Public)leg; |
| 57 | + legObj.putOpt("departureDelay", pub.getDepartureDelay() / 60000.0); |
| 58 | + legObj.putOpt("arrivalDelay", pub.getArrivalDelay() / 60000.0); |
| 59 | + legObj.putOpt("line", JSONConverter.lineToJSON(pub.line)); |
| 60 | + } |
| 61 | + return legObj; |
| 62 | + } |
| 63 | + |
| 64 | + public static JSONObject lineToJSON(Line line) { |
| 65 | + JSONObject lineObj = new JSONObject(); |
| 66 | + if (line == null) return lineObj; |
| 67 | + lineObj.put("id", line.id); |
| 68 | + lineObj.put("name", line.name); |
| 69 | + lineObj.put("label", line.label); |
| 70 | + lineObj.put("message", line.message); |
| 71 | + lineObj.put("network", line.network); |
| 72 | + lineObj.put("product", line.product.toString()); |
| 73 | + return lineObj; |
| 74 | + } |
| 75 | + |
| 76 | + public static JSONObject locationToJSON(Location location) { |
| 77 | + JSONObject locationObj = new JSONObject(); |
| 78 | + if (location == null) return locationObj; |
| 79 | + JSONObject coordinateObj = new JSONObject(); |
| 80 | + coordinateObj.put("lat", location.lat); |
| 81 | + coordinateObj.put("lon", location.lon); |
| 82 | + locationObj.put("coords", coordinateObj); |
| 83 | + locationObj.put("name", location.name); |
| 84 | + locationObj.put("place", location.place); |
| 85 | + locationObj.put("id", location.id); |
| 86 | + return locationObj; |
| 87 | + } |
| 88 | + |
| 89 | + @SuppressWarnings("deprecation") |
| 90 | + public static String getDescription(List<Leg> legs) { |
| 91 | + String response = ""; |
| 92 | + for (int i = 0; i < legs.size(); i++) { |
| 93 | + if (!(legs.get(i) instanceof Trip.Public)) continue; |
| 94 | + if (i == 0) { |
| 95 | + response += "Take the "; |
| 96 | + } else { |
| 97 | + response += " From there, take the "; |
| 98 | + } |
| 99 | + Trip.Public pub = (Trip.Public)legs.get(i); |
| 100 | + response += pub.line.label; |
| 101 | + response += " (departing at " + pub.getDepartureTime().getHours() + ":" + pub.getDepartureTime().getMinutes(); |
| 102 | + response += ", +" + (pub.getDepartureDelay() / 60000.0) + " mins delay) to "; |
| 103 | + response += pub.arrival.name; |
| 104 | + response += " (arriving at " + pub.getArrivalTime().getHours() + ":" + pub.getArrivalTime().getMinutes(); |
| 105 | + response += ", +" + (pub.getArrivalDelay() / 60000.0) + " mins delay)."; |
| 106 | + } |
| 107 | + return response; |
| 108 | + } |
| 109 | +} |
0 commit comments