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

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

package org.opentripplanner.ext.legacygraphqlapi.datafetchers;

import graphql.relay.Relay;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.opentripplanner.ext.legacygraphqlapi.LegacyGraphQLRequestContext;
import org.opentripplanner.ext.legacygraphqlapi.generated.LegacyGraphQLDataFetchers;
import org.opentripplanner.model.Agency;
import org.opentripplanner.model.Route;
import org.opentripplanner.model.Trip;
import org.opentripplanner.model.TripPattern;
import org.opentripplanner.routing.RoutingService;
import org.opentripplanner.routing.alertpatch.TransitAlert;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class LegacyGraphQLRouteImpl implements LegacyGraphQLDataFetchers.LegacyGraphQLRoute {

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

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

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

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

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

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

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

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

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

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

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

  @Override
  public DataFetcher bikesAllowed() {
    return environment -> {
      switch (getSource(environment).getBikesAllowed()) {
        case 0: return "NO_INFORMATION";
        case 1: return "POSSIBLE";
        case 2: return "NOT_POSSIBLE";
        default: return null;
      }
    };
  }

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

  @Override
  public DataFetcher> stops() {
    return environment -> getRoutingService(environment)
        .getPatternsForRoute()
        .get(getSource(environment))
        .stream()
        .map(TripPattern::getStops)
        .flatMap(Collection::stream)
        .collect(Collectors.toSet());
  }

  @Override
  public DataFetcher> trips() {
    return environment -> getRoutingService(environment)
        .getPatternsForRoute()
        .get(getSource(environment))
        .stream()
        .map(TripPattern::getTrips)
        .flatMap(Collection::stream)
        .collect(Collectors.toSet());
  }

  //TODO
  @Override
  public DataFetcher> alerts() {
    return environment -> List.of();
  }

  private RoutingService getRoutingService(DataFetchingEnvironment environment) {
    return environment.getContext().getRoutingService();
  }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy