All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opentripplanner.ext.legacygraphqlapi.datafetchers.LegacyGraphQLRouteImpl Maven / Gradle / Ivy

There is a newer version: 2.6.0
Show newest version
package org.opentripplanner.ext.legacygraphqlapi.datafetchers;

import graphql.relay.Relay;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import java.util.ArrayList;
import java.util.Collection;
import java.util.stream.Collectors;
import org.opentripplanner.ext.legacygraphqlapi.LegacyGraphQLRequestContext;
import org.opentripplanner.ext.legacygraphqlapi.LegacyGraphQLUtils;
import org.opentripplanner.ext.legacygraphqlapi.generated.LegacyGraphQLDataFetchers;
import org.opentripplanner.ext.legacygraphqlapi.generated.LegacyGraphQLTypes;
import org.opentripplanner.routing.alertpatch.EntitySelector;
import org.opentripplanner.routing.alertpatch.TransitAlert;
import org.opentripplanner.routing.services.TransitAlertService;
import org.opentripplanner.transit.model.network.Route;
import org.opentripplanner.transit.model.network.TripPattern;
import org.opentripplanner.transit.model.organization.Agency;
import org.opentripplanner.transit.model.site.StopLocation;
import org.opentripplanner.transit.model.timetable.Direction;
import org.opentripplanner.transit.model.timetable.Trip;
import org.opentripplanner.transit.service.TransitService;

public class LegacyGraphQLRouteImpl implements LegacyGraphQLDataFetchers.LegacyGraphQLRoute {

  @Override
  public DataFetcher agency() {
    return environment -> getSource(environment).getAgency();
  }

  @Override
  public DataFetcher> alerts() {
    return environment -> {
      TransitAlertService alertService = getAlertService(environment);
      var args = new LegacyGraphQLTypes.LegacyGraphQLRouteAlertsArgs(environment.getArguments());
      Iterable types = args.getLegacyGraphQLTypes();
      if (types != null) {
        Collection alerts = new ArrayList<>();
        types.forEach(type -> {
          switch (type) {
            case ROUTE:
              alerts.addAll(alertService.getRouteAlerts(getSource(environment).getId()));
              break;
            case ROUTE_TYPE:
              alerts.addAll(
                alertService.getRouteTypeAlerts(
                  getSource(environment).getGtfsType(),
                  getSource(environment).getId().getFeedId()
                )
              );
              alerts.addAll(
                alertService.getRouteTypeAndAgencyAlerts(
                  getSource(environment).getGtfsType(),
                  getSource(environment).getAgency().getId()
                )
              );
              break;
            case AGENCY:
              alerts.addAll(
                alertService.getAgencyAlerts(getSource(environment).getAgency().getId())
              );
              break;
            case TRIPS:
              getTrips(environment)
                .forEach(trip -> alerts.addAll(alertService.getTripAlerts(trip.getId(), null)));
              break;
            case STOPS_ON_ROUTE:
              alerts.addAll(
                alertService
                  .getAllAlerts()
                  .stream()
                  .filter(alert ->
                    alert
                      .getEntities()
                      .stream()
                      .anyMatch(entity ->
                        entity instanceof EntitySelector.StopAndRoute &&
                        ((EntitySelector.StopAndRoute) entity).stopAndRoute.routeOrTrip.equals(
                            getSource(environment).getId()
                          )
                      )
                  )
                  .toList()
              );
              getStops(environment)
                .forEach(stop ->
                  alerts.addAll(alertService.getStopAlerts(((StopLocation) stop).getId()))
                );
              break;
            case STOPS_ON_TRIPS:
              Iterable trips = getTrips(environment);
              trips.forEach(trip ->
                alerts.addAll(
                  alertService
                    .getAllAlerts()
                    .stream()
                    .filter(alert ->
                      alert
                        .getEntities()
                        .stream()
                        .anyMatch(entity ->
                          entity instanceof EntitySelector.StopAndTrip &&
                          ((EntitySelector.StopAndTrip) entity).stopAndTrip.routeOrTrip.equals(
                              trip.getId()
                            )
                        )
                    )
                    .toList()
                )
              );
              break;
            case PATTERNS:
              alerts.addAll(
                alertService.getDirectionAndRouteAlerts(
                  Direction.INBOUND,
                  getSource(environment).getId()
                )
              );
              alerts.addAll(
                alertService.getDirectionAndRouteAlerts(
                  Direction.OUTBOUND,
                  getSource(environment).getId()
                )
              );
              break;
          }
        });
        return alerts.stream().distinct().collect(Collectors.toList());
      } else {
        return getAlertService(environment).getRouteAlerts(getSource(environment).getId());
      }
    };
  }

  @Override
  public DataFetcher bikesAllowed() {
    return environment ->
      switch (getSource(environment).getBikesAllowed()) {
        case UNKNOWN -> "NO_INFORMATION";
        case ALLOWED -> "POSSIBLE";
        case NOT_ALLOWED -> "NOT_POSSIBLE";
      };
  }

  @Override
  public DataFetcher color() {
    return environment -> getSource(environment).getColor();
  }

  @Override
  public DataFetcher desc() {
    return environment -> getSource(environment).getDescription();
  }

  @Override
  public DataFetcher gtfsId() {
    return environment -> getSource(environment).getId().toString();
  }

  @Override
  public DataFetcher id() {
    return environment ->
      new Relay.ResolvedGlobalId("Route", getSource(environment).getId().toString());
  }

  @Override
  public DataFetcher longName() {
    return environment ->
      LegacyGraphQLUtils.getTranslation(getSource(environment).getLongName(), environment);
  }

  @Override
  public DataFetcher mode() {
    return environment -> getSource(environment).getMode().name();
  }

  @Override
  public DataFetcher> patterns() {
    return environment ->
      getTransitService(environment).getPatternsForRoute(getSource(environment));
  }

  @Override
  public DataFetcher shortName() {
    return environment -> getSource(environment).getShortName();
  }

  @Override
  public DataFetcher> stops() {
    return this::getStops;
  }

  @Override
  public DataFetcher textColor() {
    return environment -> getSource(environment).getTextColor();
  }

  @Override
  public DataFetcher> trips() {
    return this::getTrips;
  }

  @Override
  public DataFetcher type() {
    return environment -> getSource(environment).getGtfsType();
  }

  @Override
  public DataFetcher url() {
    return environment -> getSource(environment).getUrl();
  }

  private Iterable getStops(DataFetchingEnvironment environment) {
    return getTransitService(environment)
      .getPatternsForRoute(getSource(environment))
      .stream()
      .map(TripPattern::getStops)
      .flatMap(Collection::stream)
      .collect(Collectors.toSet());
  }

  private Iterable getTrips(DataFetchingEnvironment environment) {
    return getTransitService(environment)
      .getPatternsForRoute(getSource(environment))
      .stream()
      .flatMap(TripPattern::scheduledTripsAsStream)
      .collect(Collectors.toSet());
  }

  private TransitAlertService getAlertService(DataFetchingEnvironment environment) {
    return getTransitService(environment).getTransitAlertService();
  }

  private TransitService getTransitService(DataFetchingEnvironment environment) {
    return environment.getContext().getTransitService();
  }

  private Route getSource(DataFetchingEnvironment environment) {
    return environment.getSource();
  }
}