public ItemFilter getFilter()

in rest-api/src/jetbrains/buildServer/server/rest/data/finder/impl/ProjectFinder.java [238:362]


  public ItemFilter<SProject> getFilter(@NotNull final Locator locator) {
    // todo: Introduce filtering by SELECTED_BY_USER, otherwise this filter does not fully support all dimensions thus not adheres to contract.

    final MultiCheckerFilter<SProject> result = new MultiCheckerFilter<SProject>();

    final String id = locator.getSingleDimensionValue(DIMENSION_ID);
    if (id != null) {
      if (TeamCityProperties.getBoolean(APIController.REST_COMPATIBILITY_ALLOW_EXTERNAL_ID_AS_INTERNAL)) {
        result.add(item -> id.equalsIgnoreCase(item.getExternalId()) || id.equalsIgnoreCase(item.getProjectId()));
      } else {
        result.add(item -> id.equalsIgnoreCase(item.getExternalId()));
      }
    }

    final String name = locator.getSingleDimensionValue(DIMENSION_NAME);
    if (name != null) {
      result.add(item -> name.equals(item.getName()));
    }

    final Boolean archived = locator.getSingleDimensionValueAsBoolean(DIMENSION_ARCHIVED);
    if (archived != null) {
      result.add(item -> FilterUtil.isIncludedByBooleanFilter(archived, item.isArchived()));
    }

    // In a case of a single value locator (that's looking up by external id, name or id) virtual projects should not be filtered out
    if (!locator.isSingleValue()) {
      final Boolean virtual = locator.getSingleDimensionValueAsBoolean(DIMENSION_VIRTUAL);
      if (virtual != null) {
        result.add(project -> virtual.equals(project.isVirtual()));
      } else if (!locator.isAnyPresent(DIMENSION_VIRTUAL)) {
        result.add(project -> !project.isVirtual());
      }
    }


    final Boolean readOnlyUI = locator.getSingleDimensionValueAsBoolean(DIMENSION_READ_ONLY_UI);
    if (readOnlyUI != null) {
      result.add(item -> FilterUtil.isIncludedByBooleanFilter(readOnlyUI, item.isReadOnly()));
    }

    final String parameterDimension = locator.getSingleDimensionValue(DIMENSION_PARAMETER);
    if (parameterDimension != null) {
      final ParameterCondition parameterCondition = ParameterCondition.create(parameterDimension);
      result.add(item -> {
          final boolean canView = !Project.shouldRestrictSettingsViewing(item, myPermissionChecker);
          if (!canView) {
            LOG.debug("While filtering projects by " + DIMENSION_PARAMETER + " user does not have enough permissions to see settings. Excluding project: " + item.describe(false));
            return false;
          }
          return parameterCondition.matches(item);
      });
    }

    if (locator.isUnused(DIMENSION_PROJECT)) {
      final String directParentLocator = locator.getSingleDimensionValue(DIMENSION_PROJECT);
      if (directParentLocator != null) {
        final SProject directParent = getItem(directParentLocator);
        result.add(item -> directParent.getProjectId().equals(item.getParentProjectId()));
      }
    }

    if (locator.isUnused(DIMENSION_AFFECTED_PROJECT)) {
      final SProject parentProject = getParentProject(locator);
      if (parentProject != null) {
        result.add(item -> isSameOrParent(parentProject, item));
      }
    }

    final String featureDimension = locator.getSingleDimensionValue(FEATURE);
    if (featureDimension != null) {
      result.add(item -> {
          final boolean canView = !Project.shouldRestrictSettingsViewing(item, myPermissionChecker);
          if (!canView) {
            LOG.debug("While filtering projects by " + DIMENSION_PARAMETER + " user does not have enough permissions to see settings. Excluding project: " + item.describe(false));
            return false;
          }
          return new PropEntityProjectFeature.ProjectFeatureFinder(item).getItems(featureDimension).getEntries().size() > 0;
      });
    }

    final String defaultTemplateDimension = locator.getSingleDimensionValue(DEFAULT_TEMPLATE);
    if (defaultTemplateDimension != null) {
      Set<String> defaultTemplateIds = myServiceLocator
        .getSingletonService(BuildTypeFinder.class)
        .getItems(defaultTemplateDimension)
        .getEntries().stream().map(bt -> bt.getInternalId()).collect(Collectors.toSet());
      result.add(item -> {
        final boolean canView = !Project.shouldRestrictSettingsViewing(item, myPermissionChecker);
        if (!canView) {
          LOG.debug("While filtering projects by " + DIMENSION_PARAMETER + " user does not have enough permissions to see settings. Excluding project: " + item.describe(false));
          return false;
        }
        String defaultTemplateId = item.getDefaultTemplateId();
        return defaultTemplateId != null && defaultTemplateIds.contains(defaultTemplateId);
      });
    }

    final String userPermissionDimension = locator.getSingleDimensionValue(USER_PERMISSION);
    if (userPermissionDimension != null) {
      result.add(new PermissionCheck().matches(userPermissionDimension));
    }

    final List<String> poolDimensions = locator.getDimensionValue(AGENT_POOL);
    if (!poolDimensions.isEmpty()) {
      AgentPoolFinder agentPoolFinder = myServiceLocator.getSingletonService(AgentPoolFinder.class);
      // streams API alternative
      //Optional<Set<String>> filterProjectInternalIds = poolDimensions.stream().
      //  map(poolLocator -> agentPoolFinder.getItems(poolLocator).myEntries.stream().flatMap(pool -> pool.getProjectIds().stream()).collect(Collectors.toSet())).
      //  reduce((projectIds, projectIds2) -> projectIds.stream().filter(projectIds2::contains).collect(Collectors.toSet()));
      //if (filterProjectInternalIds.isPresent()){
      //  result.add(item -> filterProjectInternalIds.get().contains(item.getProjectId()));
      //}

      for (String poolDimension : poolDimensions) {
        List<AgentPool> pools = agentPoolFinder.getItems(poolDimension).getEntries();
        Set<String> filterProjectInternalIds = new HashSet<>();
        for (AgentPool pool : pools) {
          filterProjectInternalIds.addAll(pool.getProjectIds());
        }
        result.add(item -> filterProjectInternalIds.contains(item.getProjectId()));
      }
    }

    return result.toItemFilter();
  }