Search in sources :

Example 6 with TransitAlert

use of org.opentripplanner.routing.alertpatch.TransitAlert in project OpenTripPlanner by opentripplanner.

the class AlertToLegMapper method getAlertsForStopAndTrip.

private static Collection<TransitAlert> getAlertsForStopAndTrip(Graph graph, FeedScopedId stopId, FeedScopedId tripId, boolean checkParentStop) {
    Stop stop = graph.index.getStopForId(stopId);
    if (stop == null) {
        return new ArrayList<>();
    }
    Collection<TransitAlert> alertsForStopAndTrip = graph.getTransitAlertService().getStopAndTripAlerts(stopId, tripId);
    if (checkParentStop) {
        if (alertsForStopAndTrip == null) {
            alertsForStopAndTrip = new HashSet<>();
        }
        if (stop.isPartOfStation()) {
            // Also check parent
            Collection<TransitAlert> alerts = graph.getTransitAlertService().getStopAndTripAlerts(stop.getParentStation().getId(), tripId);
            if (alerts != null) {
                alertsForStopAndTrip.addAll(alerts);
            }
        }
    // TODO SIRI: Add support for fetching alerts attached to MultiModal-stops
    // if (stop.getMultiModalStation() != null) {
    // //Also check multimodal parent
    // FeedScopedId multimodalStopId = new FeedScopedId(stopId.getAgencyId(), stop.getMultiModalStation());
    // Collection<AlertPatch> multimodalStopAlerts = graph.index.getAlertsForStopAndTrip(multimodalStopId, tripId);
    // if (multimodalStopAlerts != null) {
    // alertsForStopAndTrip.addAll(multimodalStopAlerts);
    // }
    // }
    }
    return alertsForStopAndTrip;
}
Also used : TransitAlert(org.opentripplanner.routing.alertpatch.TransitAlert) Stop(org.opentripplanner.model.Stop) ArrayList(java.util.ArrayList)

Example 7 with TransitAlert

use of org.opentripplanner.routing.alertpatch.TransitAlert in project OpenTripPlanner by opentripplanner.

the class AlertToLegMapper method getAlertsForStop.

private static Collection<TransitAlert> getAlertsForStop(Graph graph, FeedScopedId stopId, boolean checkParentStop) {
    Stop stop = graph.index.getStopForId(stopId);
    if (stop == null) {
        return new ArrayList<>();
    }
    Collection<TransitAlert> alertsForStop = graph.getTransitAlertService().getStopAlerts(stopId);
    if (checkParentStop) {
        if (alertsForStop == null) {
            alertsForStop = new HashSet<>();
        }
        if (stop.isPartOfStation()) {
            // Also check parent
            Collection<TransitAlert> parentStopAlerts = graph.getTransitAlertService().getStopAlerts(stop.getParentStation().getId());
            if (parentStopAlerts != null) {
                alertsForStop.addAll(parentStopAlerts);
            }
        }
    // TODO SIRI: Add support for fetching alerts attached to MultiModal-stops
    // if (stop.getMultiModalStation() != null) {
    // //Also check multimodal parent
    // FeedScopedId multimodalStopId = new FeedScopedId(stopId.getAgencyId(), stop.getMultiModalStation());
    // Collection<AlertPatch> multimodalStopAlerts = graph.index.getAlertsForStopId(multimodalStopId);
    // if (multimodalStopAlerts != null) {
    // alertsForStop.addAll(multimodalStopAlerts);
    // }
    // }
    }
    return alertsForStop;
}
Also used : TransitAlert(org.opentripplanner.routing.alertpatch.TransitAlert) Stop(org.opentripplanner.model.Stop) ArrayList(java.util.ArrayList)

Example 8 with TransitAlert

use of org.opentripplanner.routing.alertpatch.TransitAlert in project OpenTripPlanner by opentripplanner.

the class SiriAlertsUpdateHandler method handleAlert.

private TransitAlert handleAlert(PtSituationElement situation) {
    TransitAlert alert = createAlertWithTexts(situation);
    // If situation is closed, it must be allowed - it will remove already existing alerts
    if ((alert.alertHeaderText == null || alert.alertHeaderText.toString().isEmpty()) && (alert.alertDescriptionText == null || alert.alertDescriptionText.toString().isEmpty()) && (alert.alertDetailText == null || alert.alertDetailText.toString().isEmpty())) {
        LOG.debug("Empty Alert - ignoring situationNumber: {}", situation.getSituationNumber() != null ? situation.getSituationNumber().getValue() : null);
        return null;
    }
    ArrayList<TimePeriod> periods = new ArrayList<>();
    if (situation.getValidityPeriods().size() > 0) {
        for (HalfOpenTimestampOutputRangeStructure activePeriod : situation.getValidityPeriods()) {
            final long realStart = activePeriod.getStartTime() != null ? activePeriod.getStartTime().toInstant().toEpochMilli() : 0;
            final long start = activePeriod.getStartTime() != null ? realStart - earlyStart : 0;
            final long realEnd = activePeriod.getEndTime() != null ? activePeriod.getEndTime().toInstant().toEpochMilli() : 0;
            final long end = activePeriod.getEndTime() != null ? realEnd : 0;
            periods.add(new TimePeriod(start / 1000, end / 1000));
        }
    } else {
        // Per the GTFS-rt spec, if an alert has no TimeRanges, than it should always be shown.
        periods.add(new TimePeriod(0, Long.MAX_VALUE));
    }
    alert.setTimePeriods(periods);
    AffectsScopeStructure affectsStructure = situation.getAffects();
    if (affectsStructure != null) {
        AffectsScopeStructure.Operators operators = affectsStructure.getOperators();
        if (operators != null && !isListNullOrEmpty(operators.getAffectedOperators())) {
            for (AffectedOperatorStructure affectedOperator : operators.getAffectedOperators()) {
                OperatorRefStructure operatorRef = affectedOperator.getOperatorRef();
                if (operatorRef == null || operatorRef.getValue() == null) {
                    continue;
                }
                // SIRI Operators are mapped to OTP Agency, this i probably wrong - but
                // I leave this for now.
                String agencyId = operatorRef.getValue();
                alert.addEntity(new EntitySelector.Agency(new FeedScopedId(feedId, agencyId)));
            }
        }
        AffectsScopeStructure.Networks networks = affectsStructure.getNetworks();
        Set<Route> stopRoutes = new HashSet<>();
        if (networks != null) {
            for (AffectsScopeStructure.Networks.AffectedNetwork affectedNetwork : networks.getAffectedNetworks()) {
                List<AffectedLineStructure> affectedLines = affectedNetwork.getAffectedLines();
                if (affectedLines != null && !isListNullOrEmpty(affectedLines)) {
                    for (AffectedLineStructure line : affectedLines) {
                        LineRef lineRef = line.getLineRef();
                        if (lineRef == null || lineRef.getValue() == null) {
                            continue;
                        }
                        stopRoutes.addAll(siriFuzzyTripMatcher.getRoutes(lineRef.getValue()));
                    }
                }
            }
        }
        AffectsScopeStructure.StopPoints stopPoints = affectsStructure.getStopPoints();
        AffectsScopeStructure.StopPlaces stopPlaces = affectsStructure.getStopPlaces();
        if (stopPoints != null && !isListNullOrEmpty(stopPoints.getAffectedStopPoints())) {
            for (AffectedStopPointStructure stopPoint : stopPoints.getAffectedStopPoints()) {
                StopPointRef stopPointRef = stopPoint.getStopPointRef();
                if (stopPointRef == null || stopPointRef.getValue() == null) {
                    continue;
                }
                FeedScopedId stopId = siriFuzzyTripMatcher.getStop(stopPointRef.getValue());
                if (stopId != null) {
                    if (stopRoutes.isEmpty()) {
                        alert.addEntity(new EntitySelector.Stop(stopId));
                        // TODO: is this correct? Should the stop conditions be in the entity selector?
                        updateStopConditions(alert, stopPoint.getStopConditions());
                    } else {
                        // Adding combination of stop & route
                        for (Route route : stopRoutes) {
                            alert.addEntity(new EntitySelector.StopAndRoute(stopId, route.getId()));
                            // TODO: is this correct? Should the stop conditions be in the entity selector?
                            updateStopConditions(alert, stopPoint.getStopConditions());
                        }
                    }
                }
            }
        } else if (stopPlaces != null && !isListNullOrEmpty(stopPlaces.getAffectedStopPlaces())) {
            for (AffectedStopPlaceStructure stopPoint : stopPlaces.getAffectedStopPlaces()) {
                StopPlaceRef stopPlace = stopPoint.getStopPlaceRef();
                if (stopPlace == null || stopPlace.getValue() == null) {
                    continue;
                }
                FeedScopedId stopId = siriFuzzyTripMatcher.getStop(stopPlace.getValue());
                if (stopId != null) {
                    if (stopRoutes.isEmpty()) {
                        alert.addEntity(new EntitySelector.Stop(stopId));
                    } else {
                        // Adding combination of stop & route
                        for (Route route : stopRoutes) {
                            alert.addEntity(new EntitySelector.StopAndRoute(stopId, route.getId()));
                        }
                    }
                }
            }
        } else if (networks != null && !isListNullOrEmpty(networks.getAffectedNetworks())) {
            for (AffectsScopeStructure.Networks.AffectedNetwork affectedNetwork : networks.getAffectedNetworks()) {
                List<AffectedLineStructure> affectedLines = affectedNetwork.getAffectedLines();
                if (affectedLines != null && !isListNullOrEmpty(affectedLines)) {
                    for (AffectedLineStructure line : affectedLines) {
                        LineRef lineRef = line.getLineRef();
                        if (lineRef == null || lineRef.getValue() == null) {
                            continue;
                        }
                        List<AffectedStopPointStructure> affectedStops = new ArrayList<>();
                        AffectedLineStructure.Routes routes = line.getRoutes();
                        // Resolve AffectedStop-ids
                        if (routes != null) {
                            for (AffectedRouteStructure route : routes.getAffectedRoutes()) {
                                if (route.getStopPoints() != null) {
                                    List<Serializable> stopPointsList = route.getStopPoints().getAffectedStopPointsAndLinkProjectionToNextStopPoints();
                                    for (Serializable serializable : stopPointsList) {
                                        if (serializable instanceof AffectedStopPointStructure) {
                                            AffectedStopPointStructure stopPointStructure = (AffectedStopPointStructure) serializable;
                                            affectedStops.add(stopPointStructure);
                                        }
                                    }
                                }
                            }
                        }
                        Set<Route> affectedRoutes = siriFuzzyTripMatcher.getRoutes(lineRef.getValue());
                        for (Route route : affectedRoutes) {
                            if (!affectedStops.isEmpty()) {
                                for (AffectedStopPointStructure affectedStop : affectedStops) {
                                    FeedScopedId stop = siriFuzzyTripMatcher.getStop(affectedStop.getStopPointRef().getValue());
                                    if (stop == null) {
                                        continue;
                                    }
                                    alert.addEntity(new EntitySelector.StopAndRoute(stop, route.getId()));
                                    // TODO: is this correct? Should the stop conditions be in the entity selector?
                                    updateStopConditions(alert, affectedStop.getStopConditions());
                                }
                            } else {
                                alert.addEntity(new EntitySelector.Route(route.getId()));
                            }
                        }
                    }
                } else {
                    NetworkRefStructure networkRef = affectedNetwork.getNetworkRef();
                    if (networkRef == null || networkRef.getValue() == null) {
                        continue;
                    }
                    String networkId = networkRef.getValue();
                // TODO: What to do here?
                }
            }
        }
        AffectsScopeStructure.VehicleJourneys vjs = affectsStructure.getVehicleJourneys();
        if (vjs != null && !isListNullOrEmpty(vjs.getAffectedVehicleJourneies())) {
            for (AffectedVehicleJourneyStructure affectedVehicleJourney : vjs.getAffectedVehicleJourneies()) {
                String lineRef = null;
                if (affectedVehicleJourney.getLineRef() != null) {
                    lineRef = affectedVehicleJourney.getLineRef().getValue();
                }
                List<AffectedStopPointStructure> affectedStops = new ArrayList<>();
                List<AffectedRouteStructure> routes = affectedVehicleJourney.getRoutes();
                // Resolve AffectedStop-ids
                if (routes != null) {
                    for (AffectedRouteStructure route : routes) {
                        if (route.getStopPoints() != null) {
                            List<Serializable> stopPointsList = route.getStopPoints().getAffectedStopPointsAndLinkProjectionToNextStopPoints();
                            for (Serializable serializable : stopPointsList) {
                                if (serializable instanceof AffectedStopPointStructure) {
                                    AffectedStopPointStructure stopPointStructure = (AffectedStopPointStructure) serializable;
                                    affectedStops.add(stopPointStructure);
                                }
                            }
                        }
                    }
                }
                List<VehicleJourneyRef> vehicleJourneyReves = affectedVehicleJourney.getVehicleJourneyReves();
                if (!isListNullOrEmpty(vehicleJourneyReves)) {
                    for (VehicleJourneyRef vehicleJourneyRef : vehicleJourneyReves) {
                        List<FeedScopedId> tripIds = new ArrayList<>();
                        FeedScopedId tripIdFromVehicleJourney = siriFuzzyTripMatcher.getTripId(vehicleJourneyRef.getValue());
                        Date effectiveStartDate;
                        Date effectiveEndDate;
                        // Need to know if validity is set explicitly or calculated based on ServiceDate
                        boolean effectiveValiditySetExplicitly = false;
                        ZonedDateTime originAimedDepartureTime = (affectedVehicleJourney.getOriginAimedDepartureTime() != null ? affectedVehicleJourney.getOriginAimedDepartureTime() : ZonedDateTime.now());
                        ServiceDate serviceDate = new ServiceDate(originAimedDepartureTime.getYear(), originAimedDepartureTime.getMonthValue(), originAimedDepartureTime.getDayOfMonth());
                        if (tripIdFromVehicleJourney != null) {
                            tripIds.add(tripIdFromVehicleJourney);
                            // ServiceJourneyId matches - use provided validity
                            effectiveStartDate = alert.getEffectiveStartDate();
                            effectiveEndDate = alert.getEffectiveEndDate();
                            effectiveValiditySetExplicitly = true;
                        } else {
                            // TODO - SIRI: Support submode when fuzzy-searching for trips
                            tripIds = siriFuzzyTripMatcher.getTripIdForTripShortNameServiceDateAndMode(vehicleJourneyRef.getValue(), serviceDate, TraverseMode.RAIL);
                            // ServiceJourneyId does NOT match - calculate validity based on originAimedDepartureTime
                            effectiveStartDate = serviceDate.getAsDate();
                            effectiveEndDate = serviceDate.next().getAsDate();
                        }
                        for (FeedScopedId tripId : tripIds) {
                            if (!effectiveValiditySetExplicitly) {
                                // Effective validity is set based on ServiceDate - need to calculate correct validity based on departuretimes
                                // Calculate validity based on actual, planned departure/arrival for trip
                                int tripDepartureTime = siriFuzzyTripMatcher.getTripDepartureTime(tripId);
                                int tripArrivalTime = siriFuzzyTripMatcher.getTripArrivalTime(tripId);
                                // ServiceJourneyId does NOT match - calculate validity based on serviceDate (calculated from originalAimedDepartureTime)
                                effectiveStartDate = new Date(serviceDate.getAsDate().getTime() + tripDepartureTime * 1000);
                                // Appending 6 hours to end-validity in case of delays.
                                effectiveEndDate = new Date(effectiveStartDate.getTime() + (tripArrivalTime - tripDepartureTime + 6 * 3600) * 1000);
                                // Verify that calculated validity does not exceed explicitly set validity
                                if (effectiveStartDate.before(alert.getEffectiveStartDate())) {
                                    effectiveStartDate = alert.getEffectiveStartDate();
                                }
                                if (effectiveEndDate.after(alert.getEffectiveEndDate())) {
                                    effectiveEndDate = alert.getEffectiveEndDate();
                                }
                                if (effectiveStartDate.after(effectiveEndDate) | effectiveStartDate.equals(effectiveEndDate)) {
                                    // Ignore this as situation is no longer valid
                                    continue;
                                }
                            }
                            if (effectiveEndDate == null) {
                                effectiveEndDate = new Date(Long.MAX_VALUE);
                            }
                            if (!affectedStops.isEmpty()) {
                                for (AffectedStopPointStructure affectedStop : affectedStops) {
                                    FeedScopedId stop = siriFuzzyTripMatcher.getStop(affectedStop.getStopPointRef().getValue());
                                    if (stop == null) {
                                        continue;
                                    }
                                    // Creating unique, deterministic id for the alert
                                    alert.addEntity(new EntitySelector.StopAndTrip(stop, tripId));
                                    // TODO: is this correct? Should the stop conditions be in the entity selector?
                                    updateStopConditions(alert, affectedStop.getStopConditions());
                                    // A tripId for a given date may be reused for other dates not affected by this alert.
                                    List<TimePeriod> timePeriodList = new ArrayList<>();
                                    timePeriodList.add(new TimePeriod(effectiveStartDate.getTime() / 1000, effectiveEndDate.getTime() / 1000));
                                // TODO: Make it possible to add time periods for trip selectors
                                // alert.setTimePeriods(timePeriodList);
                                }
                            } else {
                                alert.addEntity(new EntitySelector.Trip(tripId));
                                // A tripId for a given date may be reused for other dates not affected by this alert.
                                List<TimePeriod> timePeriodList = new ArrayList<>();
                                timePeriodList.add(new TimePeriod(effectiveStartDate.getTime() / 1000, effectiveEndDate.getTime() / 1000));
                            // TODO: Make it possible to add time periods for trip selectors
                            // alert.setTimePeriods(timePeriodList);
                            }
                        }
                    }
                }
                if (lineRef != null) {
                    Set<Route> affectedRoutes = siriFuzzyTripMatcher.getRoutes(lineRef);
                    for (Route route : affectedRoutes) {
                        alert.addEntity(new EntitySelector.Route(route.getId()));
                    }
                }
            }
        }
    }
    if (alert.getStopConditions().isEmpty()) {
        updateStopConditions(alert, null);
    }
    alert.alertType = situation.getReportType();
    if (situation.getSeverity() != null) {
        alert.severity = situation.getSeverity().value();
    } else {
        // When severity is not set - use default
        alert.severity = SeverityEnumeration.NORMAL.value();
    }
    if (situation.getParticipantRef() != null) {
        String codespace = situation.getParticipantRef().getValue();
        // TODO - SIRI: Should probably not assume this codespace -> authority rule
        alert.setFeedId(codespace + ":Authority:" + codespace);
    }
    return alert;
}
Also used : Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) NonLocalizedString(org.opentripplanner.util.NonLocalizedString) TranslatedString(org.opentripplanner.util.TranslatedString) I18NString(org.opentripplanner.util.I18NString) ServiceDate(org.opentripplanner.model.calendar.ServiceDate) ZonedDateTime(java.time.ZonedDateTime) Route(org.opentripplanner.model.Route) HashSet(java.util.HashSet) TimePeriod(org.opentripplanner.routing.alertpatch.TimePeriod) TransitAlert(org.opentripplanner.routing.alertpatch.TransitAlert) EntitySelector(org.opentripplanner.routing.alertpatch.EntitySelector) StopPlaceRef(uk.org.ifopt.siri20.StopPlaceRef) Date(java.util.Date) ServiceDate(org.opentripplanner.model.calendar.ServiceDate) FeedScopedId(org.opentripplanner.model.FeedScopedId)

Example 9 with TransitAlert

use of org.opentripplanner.routing.alertpatch.TransitAlert in project OpenTripPlanner by opentripplanner.

the class EstimatedCallType method getAllRelevantAlerts.

/**
 * Resolves all AlertPatches that are relevant for the supplied TripTimeShort.
 */
private static Collection<TransitAlert> getAllRelevantAlerts(TripTimeShort tripTimeShort, RoutingService routingService) {
    FeedScopedId tripId = tripTimeShort.tripId;
    Trip trip = routingService.getTripForId().get(tripId);
    FeedScopedId routeId = trip.getRoute().getId();
    FeedScopedId stopId = tripTimeShort.stopId;
    Stop stop = routingService.getStopForId(stopId);
    FeedScopedId parentStopId = stop.getParentStation().getId();
    Collection<TransitAlert> allAlerts = new HashSet<>();
    TransitAlertService alertPatchService = routingService.getTransitAlertService();
    // Quay
    allAlerts.addAll(alertPatchService.getStopAlerts(stopId));
    allAlerts.addAll(alertPatchService.getStopAndTripAlerts(stopId, tripId));
    allAlerts.addAll(alertPatchService.getStopAndRouteAlerts(stopId, routeId));
    // StopPlace
    allAlerts.addAll(alertPatchService.getStopAlerts(parentStopId));
    allAlerts.addAll(alertPatchService.getStopAndTripAlerts(parentStopId, tripId));
    allAlerts.addAll(alertPatchService.getStopAndRouteAlerts(parentStopId, routeId));
    // Trip
    allAlerts.addAll(alertPatchService.getTripAlerts(tripId));
    // Route
    allAlerts.addAll(alertPatchService.getRouteAlerts(routeId));
    // Agency
    // TODO OTP2 This should probably have a FeedScopeId argument instead of string
    allAlerts.addAll(alertPatchService.getAgencyAlerts(trip.getRoute().getAgency().getId()));
    // TripPattern
    allAlerts.addAll(alertPatchService.getTripPatternAlerts(routingService.getPatternForTrip().get(trip).getId()));
    long serviceDayMillis = 1000 * tripTimeShort.serviceDay;
    long arrivalMillis = 1000 * tripTimeShort.realtimeArrival;
    long departureMillis = 1000 * tripTimeShort.realtimeDeparture;
    filterSituationsByDateAndStopConditions(allAlerts, new Date(serviceDayMillis + arrivalMillis), new Date(serviceDayMillis + departureMillis), Arrays.asList(StopCondition.STOP, StopCondition.START_POINT, StopCondition.EXCEPTIONAL_STOP));
    return allAlerts;
}
Also used : Trip(org.opentripplanner.model.Trip) TransitAlert(org.opentripplanner.routing.alertpatch.TransitAlert) TransitAlertService(org.opentripplanner.routing.services.TransitAlertService) Stop(org.opentripplanner.model.Stop) FeedScopedId(org.opentripplanner.model.FeedScopedId) Date(java.util.Date) HashSet(java.util.HashSet)

Example 10 with TransitAlert

use of org.opentripplanner.routing.alertpatch.TransitAlert in project OpenTripPlanner by opentripplanner.

the class PtSituationElementType method create.

public static GraphQLObjectType create(GraphQLOutputType authorityType, GraphQLOutputType quayType, GraphQLOutputType lineType, GraphQLOutputType serviceJourneyType, GraphQLOutputType multilingualStringType, GraphQLObjectType validityPeriodType, GraphQLObjectType infoLinkType, Relay relay) {
    return GraphQLObjectType.newObject().name(NAME).description("Simple public transport situation element").field(GraphQLFieldDefinition.newFieldDefinition().name("id").type(new GraphQLNonNull(Scalars.GraphQLID)).dataFetcher(environment -> relay.toGlobalId(NAME, ((TransitAlert) environment.getSource()).getId())).build()).field(GraphQLFieldDefinition.newFieldDefinition().name("authority").type(authorityType).description("Get affected authority for this situation element").dataFetcher(environment -> {
        return GqlUtil.getRoutingService(environment).getAgencyForId(((TransitAlert) environment.getSource()).getEntities().stream().filter(EntitySelector.Agency.class::isInstance).map(EntitySelector.Agency.class::cast).findAny().map(entitySelector -> entitySelector.agencyId).orElse(null));
    }).build()).field(GraphQLFieldDefinition.newFieldDefinition().name("lines").type(new GraphQLNonNull(new GraphQLList(lineType))).dataFetcher(environment -> {
        RoutingService routingService = GqlUtil.getRoutingService(environment);
        return ((TransitAlert) environment.getSource()).getEntities().stream().filter(EntitySelector.Route.class::isInstance).map(EntitySelector.Route.class::cast).map(entitySelector -> entitySelector.routeId).map(routeId -> routingService.getRouteForId(routeId)).collect(Collectors.toList());
    }).build()).field(GraphQLFieldDefinition.newFieldDefinition().name("serviceJourneys").type(new GraphQLNonNull(new GraphQLList(serviceJourneyType))).dataFetcher(environment -> {
        RoutingService routingService = GqlUtil.getRoutingService(environment);
        return ((TransitAlert) environment.getSource()).getEntities().stream().filter(EntitySelector.Trip.class::isInstance).map(EntitySelector.Trip.class::cast).map(entitySelector -> entitySelector.tripId).map(tripId -> routingService.getTripForId().get(tripId)).collect(Collectors.toList());
    }).build()).field(GraphQLFieldDefinition.newFieldDefinition().name("quays").type(new GraphQLNonNull(new GraphQLList(quayType))).dataFetcher(environment -> {
        RoutingService routingService = GqlUtil.getRoutingService(environment);
        return ((TransitAlert) environment.getSource()).getEntities().stream().filter(EntitySelector.Stop.class::isInstance).map(EntitySelector.Stop.class::cast).map(entitySelector -> entitySelector.stopId).map(stopId -> routingService.getStopForId(stopId)).collect(Collectors.toList());
    }).build()).field(GraphQLFieldDefinition.newFieldDefinition().name("summary").type(new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(multilingualStringType)))).description("Summary of situation in all different translations available").dataFetcher(environment -> {
        TransitAlert alert = environment.getSource();
        if (alert.alertHeaderText instanceof TranslatedString) {
            return ((TranslatedString) alert.alertHeaderText).getTranslations();
        } else if (alert.alertHeaderText != null) {
            return List.of(new AbstractMap.SimpleEntry<>(null, alert.alertHeaderText.toString()));
        } else {
            return emptyList();
        }
    }).build()).field(GraphQLFieldDefinition.newFieldDefinition().name("description").type(new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(multilingualStringType)))).description("Description of situation in all different translations available").dataFetcher(environment -> {
        TransitAlert alert = environment.getSource();
        if (alert.alertDescriptionText instanceof TranslatedString) {
            return ((TranslatedString) alert.alertDescriptionText).getTranslations();
        } else if (alert.alertDescriptionText != null) {
            return List.of(new AbstractMap.SimpleEntry<>(null, alert.alertDescriptionText.toString()));
        } else {
            return emptyList();
        }
    }).build()).field(GraphQLFieldDefinition.newFieldDefinition().name("advice").type(new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(multilingualStringType)))).description("Advice of situation in all different translations available").dataFetcher(environment -> {
        TransitAlert alert = environment.getSource();
        if (alert.alertAdviceText instanceof TranslatedString) {
            return ((TranslatedString) alert.alertAdviceText).getTranslations();
        } else if (alert.alertAdviceText != null) {
            return List.of(new AbstractMap.SimpleEntry<>(null, alert.alertAdviceText.toString()));
        } else {
            return emptyList();
        }
    }).build()).field(GraphQLFieldDefinition.newFieldDefinition().name("infoLinks").type(new GraphQLList(infoLinkType)).description("Optional links to more information.").dataFetcher(environment -> null).build()).field(GraphQLFieldDefinition.newFieldDefinition().name("validityPeriod").type(validityPeriodType).description("Period this situation is in effect").dataFetcher(environment -> {
        TransitAlert alert = environment.getSource();
        Long startTime = alert.getEffectiveStartDate() != null ? alert.getEffectiveEndDate().getTime() : null;
        Long endTime = alert.getEffectiveEndDate() != null ? alert.getEffectiveEndDate().getTime() : null;
        return Pair.of(startTime, endTime);
    }).build()).field(GraphQLFieldDefinition.newFieldDefinition().name("reportType").type(EnumTypes.REPORT_TYPE).description("ReportType of this situation").dataFetcher(environment -> ((TransitAlert) environment.getSource()).alertType).build()).field(GraphQLFieldDefinition.newFieldDefinition().name("situationNumber").type(Scalars.GraphQLString).description("Operator's internal id for this situation").dataFetcher(environment -> null).build()).field(GraphQLFieldDefinition.newFieldDefinition().name("reportAuthority").type(authorityType).description("Authority that reported this situation").deprecate("Not yet officially supported. May be removed or renamed.").dataFetcher(environment -> {
        return GqlUtil.getRoutingService(environment).getAgencyForId(((TransitAlert) environment.getSource()).getEntities().stream().filter(EntitySelector.Agency.class::isInstance).map(EntitySelector.Agency.class::cast).findAny().map(entitySelector -> entitySelector.agencyId).orElse(null));
    }).build()).build();
}
Also used : GraphQLObjectType(graphql.schema.GraphQLObjectType) EnumTypes(org.opentripplanner.ext.transmodelapi.model.EnumTypes) RoutingService(org.opentripplanner.routing.RoutingService) GqlUtil(org.opentripplanner.ext.transmodelapi.support.GqlUtil) Collections.emptyList(java.util.Collections.emptyList) GraphQLNonNull(graphql.schema.GraphQLNonNull) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) EntitySelector(org.opentripplanner.routing.alertpatch.EntitySelector) GraphQLOutputType(graphql.schema.GraphQLOutputType) Collectors(java.util.stream.Collectors) TranslatedString(org.opentripplanner.util.TranslatedString) TransitAlert(org.opentripplanner.routing.alertpatch.TransitAlert) Scalars(graphql.Scalars) AbstractMap(java.util.AbstractMap) List(java.util.List) GraphQLList(graphql.schema.GraphQLList) GraphQLTypeReference(graphql.schema.GraphQLTypeReference) Pair(org.apache.commons.lang3.tuple.Pair) Relay(graphql.relay.Relay) GraphQLList(graphql.schema.GraphQLList) TranslatedString(org.opentripplanner.util.TranslatedString) EntitySelector(org.opentripplanner.routing.alertpatch.EntitySelector) TransitAlert(org.opentripplanner.routing.alertpatch.TransitAlert) RoutingService(org.opentripplanner.routing.RoutingService) AbstractMap(java.util.AbstractMap) GraphQLNonNull(graphql.schema.GraphQLNonNull)

Aggregations

TransitAlert (org.opentripplanner.routing.alertpatch.TransitAlert)14 ArrayList (java.util.ArrayList)7 Stop (org.opentripplanner.model.Stop)5 HashSet (java.util.HashSet)4 FeedScopedId (org.opentripplanner.model.FeedScopedId)4 TranslatedString (org.opentripplanner.util.TranslatedString)4 Date (java.util.Date)3 Route (org.opentripplanner.model.Route)3 EntitySelector (org.opentripplanner.routing.alertpatch.EntitySelector)3 I18NString (org.opentripplanner.util.I18NString)3 GtfsRealtime (com.google.transit.realtime.GtfsRealtime)2 Scalars (graphql.Scalars)2 Relay (graphql.relay.Relay)2 GraphQLFieldDefinition (graphql.schema.GraphQLFieldDefinition)2 GraphQLList (graphql.schema.GraphQLList)2 GraphQLNonNull (graphql.schema.GraphQLNonNull)2 GraphQLObjectType (graphql.schema.GraphQLObjectType)2 GraphQLOutputType (graphql.schema.GraphQLOutputType)2 GraphQLSchema (graphql.schema.GraphQLSchema)2 GraphQLTypeReference (graphql.schema.GraphQLTypeReference)2