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 java.util.Locale;
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.LegacyGraphQLVertexType;
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.vehicle_parking.VehicleParking;
import org.opentripplanner.routing.vehicle_rental.VehicleRentalPlace;
import org.opentripplanner.routing.vehicle_rental.VehicleRentalStation;
import org.opentripplanner.routing.vehicle_rental.VehicleRentalVehicle;

public class LegacyGraphQLPlaceImpl implements LegacyGraphQLDataFetchers.LegacyGraphQLPlace {

  @Override
  public DataFetcher name() {
    return environment -> {
      Locale locale = LegacyGraphQLUtils.getLocale(environment);
      return getSource(environment).place.name.toString(locale);
    };
  }

  @Override
  public DataFetcher vertexType() {
    return environment -> {
      var place = getSource(environment).place;
      switch (place.vertexType) {
        case NORMAL:
          return LegacyGraphQLVertexType.NORMAL.name();
        case TRANSIT:
          return LegacyGraphQLVertexType.TRANSIT.name();
        case VEHICLERENTAL:
          return LegacyGraphQLVertexType.BIKESHARE.name();
        case VEHICLEPARKING:
          return LegacyGraphQLVertexType.BIKEPARK.name();
        default:
          throw new IllegalStateException("Unhandled vertexType: " + 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 -> getSource(environment).place.stop;
  }

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

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

      return place.vehicleRentalPlace;
    };
  }

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

      if (!place.vertexType.equals(VertexType.VEHICLERENTAL)
              || !(place.vehicleRentalPlace instanceof VehicleRentalStation)) {
        return null;
      }

      return (VehicleRentalStation) place.vehicleRentalPlace;
    };
  }

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

      if (!place.vertexType.equals(VertexType.VEHICLERENTAL)
              || !(place.vehicleRentalPlace instanceof VehicleRentalVehicle)) {
        return null;
      }

      return (VehicleRentalVehicle) place.vehicleRentalPlace;
    };
  }

  @Override
  public DataFetcher bikePark() {
    return this::getVehicleParking;
  }

  @Override
  public DataFetcher carPark() {
    return this::getVehicleParking;
  }

  @Override
  public DataFetcher vehicleParking() {
    return this::getVehicleParking;
  }

  private VehicleParking getVehicleParking(DataFetchingEnvironment environment) {
    var vehicleParkingWithEntrance = getSource(environment).place.vehicleParkingWithEntrance;
    if (vehicleParkingWithEntrance == null) {
      return null;
    }

    return vehicleParkingWithEntrance.getVehicleParking();
  }

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

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