Ejemplo n.º 1
0
  public static WrappedGTFSEntity<Pattern> fromTrip(DataFetchingEnvironment env) {
    WrappedGTFSEntity<Trip> trip = (WrappedGTFSEntity<Trip>) env.getSource();
    FeedSource fs = ApiMain.getFeedSource(trip.feedUniqueId);
    if (fs == null) return null;

    Pattern patt = fs.feed.patterns.get(fs.feed.tripPatternMap.get(trip.entity.trip_id));
    return new WrappedGTFSEntity<>(fs.id, patt);
  }
 @Override
 public Object get(DataFetchingEnvironment environment) {
   if (!(environment.getSource() instanceof EObject)) {
     return null;
   }
   EObject context = (EObject) environment.getSource();
   try {
     IServiceExecutor serviceExecutor = serviceExecutorProvider.getServiceExecutor();
     serviceExecutor.init(context);
     serviceExecutor.execute(serviceName, environment.getArguments());
     Object[] values = serviceExecutor.getObjects();
     if (environment.getFieldType() instanceof GraphQLList) {
       return Arrays.asList(values);
     }
     return (values != null && values.length > 0 ? values[0] : null);
   } catch (Exception e) {
     return null;
   }
 }
Ejemplo n.º 3
0
  public static Long fromRouteCount(DataFetchingEnvironment env) {
    WrappedGTFSEntity<Route> route = (WrappedGTFSEntity<Route>) env.getSource();
    FeedSource fs = ApiMain.getFeedSource(route.feedUniqueId);
    if (fs == null) return null;

    return fs.feed
        .patterns
        .values()
        .stream()
        .filter(p -> p.route_id.equals(route.entity.route_id))
        .count();
  }
Ejemplo n.º 4
0
  public static List<WrappedGTFSEntity<Pattern>> fromRoute(DataFetchingEnvironment env) {
    WrappedGTFSEntity<Route> route = (WrappedGTFSEntity<Route>) env.getSource();
    FeedSource fs = ApiMain.getFeedSource(route.feedUniqueId);
    if (fs == null) return null;

    List<String> stopIds = env.getArgument("stop_id");
    List<String> patternId = env.getArgument("pattern_id");
    Long limit = env.getArgument("limit");

    List<WrappedGTFSEntity<Pattern>> patterns =
        fs.feed
            .patterns
            .values()
            .stream()
            .filter(p -> p.route_id.equals(route.entity.route_id))
            .map(p -> new WrappedGTFSEntity<>(fs.id, p))
            .collect(Collectors.toList());
    if (patternId != null) {
      patterns
          .stream()
          .filter(p -> patternId.contains(p.entity.pattern_id))
          .collect(Collectors.toList());
    }
    if (stopIds != null) {
      patterns
          .stream()
          .filter(
              p ->
                  !Collections.disjoint(
                      p.entity.orderedStops,
                      stopIds)) // disjoint returns true if no elements in common
          .collect(Collectors.toList());
    }
    if (limit != null) {
      return patterns.stream().limit(limit).collect(Collectors.toList());
    }
    return patterns;
  }
Ejemplo n.º 5
0
  public static List<WrappedGTFSEntity<Pattern>> apex(DataFetchingEnvironment env) {
    Collection<FeedSource> feeds;

    List<String> feedId = (List<String>) env.getArgument("feed_id");
    feeds = ApiMain.getFeedSources(feedId);
    Map<String, Object> args = env.getArguments();
    List<WrappedGTFSEntity<Pattern>> patterns = new ArrayList<>();

    for (FeedSource fs : feeds) {
      if (env.getArgument("pattern_id") != null) {
        List<String> patternId = (List<String>) env.getArgument("pattern_id");
        patternId
            .stream()
            .filter(fs.feed.patterns::containsKey)
            .map(fs.feed.patterns::get)
            .map(pattern -> new WrappedGTFSEntity(fs.id, pattern))
            .forEach(patterns::add);
      } else if (env.getArgument("route_id") != null) {
        List<String> routeId = (List<String>) env.getArgument("route_id");
        fs.feed
            .patterns
            .values()
            .stream()
            .filter(p -> routeId.contains(p.route_id))
            .map(pattern -> new WrappedGTFSEntity(fs.id, pattern))
            .forEach(patterns::add);
      }
      // get patterns by lat/lon/radius
      else if (argumentDefined(env, "lat") && argumentDefined(env, "lon")) {
        Double lat = (Double) args.get("lat");
        Double lon = (Double) args.get("lon");
        Double radius = args.get("radius") == null ? DEFAULT_RADIUS : (Double) args.get("radius");
        Coordinate latLng = new Coordinate(lon, lat);
        Envelope searchEnvelope = GeomUtil.getBoundingBox(latLng, radius);

        List<Pattern> results = fs.routeIndex.query(searchEnvelope);
        results
            .stream()
            .map(pattern -> new WrappedGTFSEntity(fs.id, pattern))
            .forEach(patterns::add);
      }
      // get patterns by bounding box
      else if (argumentDefined(env, "min_lat")
          && argumentDefined(env, "max_lat")
          && argumentDefined(env, "min_lon")
          && argumentDefined(env, "max_lon")) {
        Coordinate maxCoordinate =
            new Coordinate((Double) args.get("max_lon"), (Double) args.get("max_lat"));
        Coordinate minCoordinate =
            new Coordinate((Double) args.get("min_lon"), (Double) args.get("min_lat"));
        Envelope searchEnvelope = new Envelope(maxCoordinate, minCoordinate);

        List<Pattern> results = (List<Pattern>) fs.routeIndex.query(searchEnvelope);
        results
            .stream()
            .filter(p -> p != null)
            .map(pattern -> new WrappedGTFSEntity(fs.id, pattern))
            .forEach(patterns::add);
      }
    }

    return patterns;
  }