public List getFromPostedAndApply()

in rest-api/src/jetbrains/buildServer/server/rest/model/buildType/Investigation.java [133:207]


  public List<InvestigationWrapper> getFromPostedAndApply(@NotNull final ServiceLocator serviceLocator, final boolean allowMultipleResult) {
    checkIsValid();

    if (target == null) {
      throw new BadRequestException("Invalid 'investigation' entity: 'target' should be specified");
    }
    ProblemTarget.ProblemTargetData targetData;
    try {
      targetData = target.getFromPosted(serviceLocator);
    } catch (BadRequestException e) {
      throw new BadRequestException("Invalid 'investigation' entity: " + e.getMessage());
    }

    ResponsibilityEntry entry = new ResponsibilityEntryEx(TypedFinderBuilder.getEnumValue(state, ResponsibilityEntry.State.class),
                                                            assignee.getFromPosted(serviceLocator.getSingletonService(UserFinder.class)),
                                                            serviceLocator.getSingletonService(UserFinder.class).getCurrentUser(),
                                                            new Date(),
                                                            assignment == null || assignment.getTextFromPosted() == null ? "" : assignment.getTextFromPosted(),
                                                            resolution.getFromPostedForInvestigation(serviceLocator));

    ResponsibilityFacadeEx responsibilityFacade = serviceLocator.getSingletonService(ResponsibilityFacadeEx.class);

    InvestigationFinder investigationFinder = serviceLocator.findSingletonService(InvestigationFinder.class);
    assert investigationFinder != null;
    List<InvestigationWrapper> resultEntries = new ArrayList<>(1);

    if (targetData.isAnyProblem()) {
      List<BuildType> buildTypesFromPosted = scope.getBuildTypesFromPosted(serviceLocator);
      if (!allowMultipleResult && buildTypesFromPosted.size() > 1) {
        throw new OnlySingleEntitySupportedException("Invalid 'scope' entity: for this request only single buildType is supported within 'buildTypes' entity");
      }
      for (BuildType buildType : buildTypesFromPosted) {
        responsibilityFacade.setBuildTypeResponsibility(buildType, entry);
        resultEntries.add(investigationFinder.getItem(InvestigationFinder.getLocator((SBuildType)buildType)));
      }
    } else {
      if (scope.buildTypes != null) {
        throw new BadRequestException("Invalid 'investigation' entity: Invalid 'scope' entity: 'buildTypes' should not be specified for not buildType-level investigation");
      }

      SProject project = scope.getProjectFromPosted(serviceLocator);

      List<STest> tests = targetData.getTests();
      if (!tests.isEmpty()) {
        if (!allowMultipleResult && tests.size() > 1) {
          throw new OnlySingleEntitySupportedException("Invalid 'target' entity: for this request only single test is supported within 'tests' entity");
        }
        responsibilityFacade.setTestNameResponsibility(
          tests.stream().map(sTest -> sTest.getName()).distinct().collect(Collectors.toList()),
          project.getProjectId(),
          entry);
        tests.stream().map(test -> investigationFinder //only one item should be found in the project
          .getItem(InvestigationFinder.getLocatorForTest(test.getTestNameId(), project))).distinct().forEachOrdered(resultEntries::add);
      }

      List<Long> problems = targetData.getProblemIds();
      if (!problems.isEmpty()) {
        if (!allowMultipleResult && problems.size() > 1) {
          throw new OnlySingleEntitySupportedException("Invalid 'target' entity: for this request only single problem is supported within 'problems' entity");
        }
        responsibilityFacade.setBuildProblemResponsibility(
          problems.stream().distinct().map(problemId -> ProblemWrapper.getBuildProblemInfo(problemId.intValue(), project.getProjectId())).collect(Collectors.toList()), //seems like only id is used inside
          project.getProjectId(),
          entry);
        problems.stream().distinct().map(problemId -> investigationFinder //only one item should be found in the project
          .getItem(InvestigationFinder.getLocatorForProblem(problemId.intValue(), project))).forEachOrdered(resultEntries::add);
      }
    }

    if (!allowMultipleResult && resultEntries.size() != 1) {
      throw new BadRequestException("Invalid 'investigation' entity: Invalid 'target' entity: found " + resultEntries.size() + " result entities, while exactly one is required");
    }

    return resultEntries;
  }