From d49e5fa6316ad3862a3a70e6d3b4b4a8f3f88e34 Mon Sep 17 00:00:00 2001 From: Leonard Ehrenfried Date: Fri, 27 Sep 2024 09:38:30 +0200 Subject: [PATCH] Add proper mapper for realtime state --- .../apis/gtfs/datafetchers/LegImpl.java | 5 +++-- .../apis/gtfs/datafetchers/StoptimeImpl.java | 9 ++++---- .../gtfs/generated/GraphQLDataFetchers.java | 5 +++-- .../apis/gtfs/generated/graphql-codegen.yml | 2 +- .../gtfs/mapping/RealtimeStateMapper.java | 22 +++++++++++++++++++ 5 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 src/main/java/org/opentripplanner/apis/gtfs/mapping/RealtimeStateMapper.java diff --git a/src/main/java/org/opentripplanner/apis/gtfs/datafetchers/LegImpl.java b/src/main/java/org/opentripplanner/apis/gtfs/datafetchers/LegImpl.java index 950e3d9bba5..8d84c7f0b03 100644 --- a/src/main/java/org/opentripplanner/apis/gtfs/datafetchers/LegImpl.java +++ b/src/main/java/org/opentripplanner/apis/gtfs/datafetchers/LegImpl.java @@ -10,6 +10,7 @@ import org.opentripplanner.apis.gtfs.generated.GraphQLDataFetchers; import org.opentripplanner.apis.gtfs.generated.GraphQLTypes; import org.opentripplanner.apis.gtfs.mapping.NumberMapper; +import org.opentripplanner.apis.gtfs.mapping.RealtimeStateMapper; import org.opentripplanner.ext.restapi.mapping.LocalDateMapper; import org.opentripplanner.ext.ridehailing.model.RideEstimate; import org.opentripplanner.ext.ridehailing.model.RideHailingLeg; @@ -191,10 +192,10 @@ public DataFetcher realTime() { } @Override - public DataFetcher realtimeState() { + public DataFetcher realtimeState() { return environment -> { var state = getSource(environment).getRealTimeState(); - return (state != null) ? state.name() : null; + return RealtimeStateMapper.map(state); }; } diff --git a/src/main/java/org/opentripplanner/apis/gtfs/datafetchers/StoptimeImpl.java b/src/main/java/org/opentripplanner/apis/gtfs/datafetchers/StoptimeImpl.java index faf59ef9d6e..bc14d15fbf2 100644 --- a/src/main/java/org/opentripplanner/apis/gtfs/datafetchers/StoptimeImpl.java +++ b/src/main/java/org/opentripplanner/apis/gtfs/datafetchers/StoptimeImpl.java @@ -3,10 +3,11 @@ import graphql.schema.DataFetcher; import graphql.schema.DataFetchingEnvironment; import org.opentripplanner.apis.gtfs.generated.GraphQLDataFetchers; +import org.opentripplanner.apis.gtfs.generated.GraphQLTypes; +import org.opentripplanner.apis.gtfs.mapping.RealtimeStateMapper; import org.opentripplanner.framework.graphql.GraphQLUtils; import org.opentripplanner.model.StopTime; import org.opentripplanner.model.TripTimeOnDate; -import org.opentripplanner.transit.model.timetable.RealTimeState; import org.opentripplanner.transit.model.timetable.Trip; public class StoptimeImpl implements GraphQLDataFetchers.GraphQLStoptime { @@ -67,11 +68,11 @@ public DataFetcher realtimeDeparture() { } @Override - public DataFetcher realtimeState() { + public DataFetcher realtimeState() { return environment -> getSource(environment).isCanceledEffectively() - ? RealTimeState.CANCELED.name() - : getSource(environment).getRealTimeState().name(); + ? GraphQLTypes.GraphQLRealtimeState.CANCELED + : RealtimeStateMapper.map(getSource(environment).getRealTimeState()); } @Override diff --git a/src/main/java/org/opentripplanner/apis/gtfs/generated/GraphQLDataFetchers.java b/src/main/java/org/opentripplanner/apis/gtfs/generated/GraphQLDataFetchers.java index 2dbcf7d1998..7532faf28bd 100644 --- a/src/main/java/org/opentripplanner/apis/gtfs/generated/GraphQLDataFetchers.java +++ b/src/main/java/org/opentripplanner/apis/gtfs/generated/GraphQLDataFetchers.java @@ -18,6 +18,7 @@ import org.opentripplanner.apis.gtfs.generated.GraphQLTypes.GraphQLBikesAllowed; import org.opentripplanner.apis.gtfs.generated.GraphQLTypes.GraphQLInputField; import org.opentripplanner.apis.gtfs.generated.GraphQLTypes.GraphQLOccupancyStatus; +import org.opentripplanner.apis.gtfs.generated.GraphQLTypes.GraphQLRealtimeState; import org.opentripplanner.apis.gtfs.generated.GraphQLTypes.GraphQLRelativeDirection; import org.opentripplanner.apis.gtfs.generated.GraphQLTypes.GraphQLRoutingErrorCode; import org.opentripplanner.apis.gtfs.generated.GraphQLTypes.GraphQLTransitMode; @@ -504,7 +505,7 @@ public interface GraphQLLeg { public DataFetcher realTime(); - public DataFetcher realtimeState(); + public DataFetcher realtimeState(); public DataFetcher rentedBike(); @@ -1082,7 +1083,7 @@ public interface GraphQLStoptime { public DataFetcher realtimeDeparture(); - public DataFetcher realtimeState(); + public DataFetcher realtimeState(); public DataFetcher scheduledArrival(); diff --git a/src/main/java/org/opentripplanner/apis/gtfs/generated/graphql-codegen.yml b/src/main/java/org/opentripplanner/apis/gtfs/generated/graphql-codegen.yml index 29490a28b78..2a7f2a95ba0 100644 --- a/src/main/java/org/opentripplanner/apis/gtfs/generated/graphql-codegen.yml +++ b/src/main/java/org/opentripplanner/apis/gtfs/generated/graphql-codegen.yml @@ -84,7 +84,7 @@ config: PlanConnection: graphql.execution.DataFetcherResult PlanEdge: graphql.relay.DefaultEdge#DefaultEdge PlanPageInfo: org.opentripplanner.apis.gtfs.model.PlanPageInfo#PlanPageInfo - RealtimeState: String + RealtimeState: org.opentripplanner.apis.gtfs.generated.GraphQLTypes.GraphQLRealtimeState#GraphQLRealtimeState RelativeDirection: org.opentripplanner.apis.gtfs.generated.GraphQLTypes.GraphQLRelativeDirection#GraphQLRelativeDirection Route: org.opentripplanner.transit.model.network.Route#Route RoutingError: org.opentripplanner.routing.api.response.RoutingError#RoutingError diff --git a/src/main/java/org/opentripplanner/apis/gtfs/mapping/RealtimeStateMapper.java b/src/main/java/org/opentripplanner/apis/gtfs/mapping/RealtimeStateMapper.java new file mode 100644 index 00000000000..6bd46466546 --- /dev/null +++ b/src/main/java/org/opentripplanner/apis/gtfs/mapping/RealtimeStateMapper.java @@ -0,0 +1,22 @@ +package org.opentripplanner.apis.gtfs.mapping; + +import javax.annotation.Nullable; +import org.opentripplanner.apis.gtfs.generated.GraphQLTypes; +import org.opentripplanner.transit.model.timetable.RealTimeState; + +/** + * Maps from the internal model to the API one. + */ +public class RealtimeStateMapper { + + public static GraphQLTypes.GraphQLRealtimeState map(@Nullable RealTimeState state) { + if (state == null) return null; + return switch (state) { + case SCHEDULED -> GraphQLTypes.GraphQLRealtimeState.SCHEDULED; + case UPDATED -> GraphQLTypes.GraphQLRealtimeState.UPDATED; + case CANCELED, DELETED -> GraphQLTypes.GraphQLRealtimeState.CANCELED; + case ADDED -> GraphQLTypes.GraphQLRealtimeState.ADDED; + case MODIFIED -> GraphQLTypes.GraphQLRealtimeState.MODIFIED; + }; + } +}