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

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

package org.opentripplanner.ext.legacygraphqlapi.datafetchers;

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.plan.Place;
import org.opentripplanner.model.plan.StopArrival;
import org.opentripplanner.model.plan.VertexType;
import org.opentripplanner.routing.RoutingService;
import org.opentripplanner.routing.bike_park.BikePark;
import org.opentripplanner.routing.bike_rental.BikeRentalStation;
import org.opentripplanner.routing.bike_rental.BikeRentalStationService;

public class LegacyGraphQLPlaceImpl implements LegacyGraphQLDataFetchers.LegacyGraphQLPlace {

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

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

  @Override
  public DataFetcher lat() {
    return environment -> getSource(environment).place.coordinate.latitude();
  }

  @Override
  public DataFetcher lon() {
    return environment -> getSource(environment).place.coordinate.longitude();
  }

  @Override
  public DataFetcher arrivalTime() {
    return environment -> getSource(environment).arrival.getTime().getTime();
  }

  @Override
  public DataFetcher departureTime() {
    return environment -> getSource(environment).departure.getTime().getTime();
  }

  @Override
  public DataFetcher stop() {
    return environment -> {
      Place place = getSource(environment).place;
      return place.vertexType.equals(VertexType.TRANSIT) ?
          getRoutingService(environment).getStopForId(place.stopId) : null;
    };
  }

  @Override
  public DataFetcher bikeRentalStation() {
    return environment -> {
      Place place = getSource(environment).place;

      if (!place.vertexType.equals(VertexType.BIKESHARE)) { return null; }

      BikeRentalStationService bikerentalStationService = getRoutingService(environment)
          .getBikerentalStationService();

      if (bikerentalStationService == null) { return null; }

      return bikerentalStationService
          .getBikeRentalStations()
          .stream()
          .filter(bikeRentalStation -> bikeRentalStation.id.equals(place.bikeShareId))
          .findAny()
          .orElse(null);
    };
  }

  // TODO
  @Override
  public DataFetcher bikePark() {
    return environment -> null;
  }

  // TODO
  @Override
  public DataFetcher carPark() {
    return environment -> null;
  }

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

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