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

org.opentripplanner.ext.legacygraphqlapi.datafetchers.LegacyGraphQLAgencyImpl 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.List;
import java.util.stream.Collectors;
import org.opentripplanner.ext.legacygraphqlapi.LegacyGraphQLRequestContext;
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.organization.Agency;
import org.opentripplanner.transit.service.TransitService;

public class LegacyGraphQLAgencyImpl implements LegacyGraphQLDataFetchers.LegacyGraphQLAgency {

  @Override
  public DataFetcher> alerts() {
    return environment -> {
      TransitAlertService alertService = getTransitService(environment).getTransitAlertService();
      var args = new LegacyGraphQLTypes.LegacyGraphQLAgencyAlertsArgs(environment.getArguments());
      Iterable types = args.getLegacyGraphQLTypes();
      if (types != null) {
        Collection alerts = new ArrayList<>();
        types.forEach(type -> {
          switch (type) {
            case AGENCY:
              alerts.addAll(alertService.getAgencyAlerts(getSource(environment).getId()));
              break;
            case ROUTE_TYPES:
              alertService
                .getAllAlerts()
                .stream()
                .filter(alert ->
                  alert
                    .getEntities()
                    .stream()
                    .filter(entitySelector ->
                      entitySelector instanceof EntitySelector.RouteTypeAndAgency
                    )
                    .map(EntitySelector.RouteTypeAndAgency.class::cast)
                    .anyMatch(entity -> entity.agencyId.equals(getSource(environment).getId()))
                )
                .forEach(alert -> alerts.add(alert));
              break;
            case ROUTES:
              getRoutes(environment)
                .forEach(route -> alerts.addAll(alertService.getRouteAlerts(route.getId())));
              break;
          }
        });
        return alerts.stream().distinct().collect(Collectors.toList());
      } else {
        return alertService.getAgencyAlerts(getSource(environment).getId());
      }
    };
  }

  @Override
  public DataFetcher fareUrl() {
    return environment -> getSource(environment).getFareUrl();
  }

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

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

  @Override
  public DataFetcher lang() {
    return environment -> getSource(environment).getLang();
  }

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

  @Override
  public DataFetcher phone() {
    return environment -> getSource(environment).getPhone();
  }

  @Override
  public DataFetcher> routes() {
    return environment -> getRoutes(environment);
  }

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

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

  private List getRoutes(DataFetchingEnvironment environment) {
    return getTransitService(environment)
      .getAllRoutes()
      .stream()
      .filter(route -> route.getAgency().equals(getSource(environment)))
      .collect(Collectors.toList());
  }

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

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy