Search in sources :

Example 1 with RequirementFactory

use of jetbrains.buildServer.serverSide.RequirementFactory in project teamcity-rest by JetBrains.

the class PropEntityAgentRequirement method addToMain.

@NotNull
public Requirement addToMain(@NotNull final BuildTypeSettingsEx buildType, @NotNull final ServiceLocator serviceLocator) {
    final Map<String, String> propertiesMap = properties == null ? Collections.emptyMap() : properties.getMap();
    String propertyName = propertiesMap.get(NAME_PROPERTY_NAME);
    if (StringUtil.isEmpty(propertyName)) {
        throw new BadRequestException("No name is specified. Make sure '" + NAME_PROPERTY_NAME + "' property is present and has not empty value");
    }
    Requirement similar = getInheritedOrSameIdSimilar(buildType, serviceLocator);
    if (inherited != null && inherited && similar != null) {
        return similar;
    }
    if (similar != null && id != null && id.equals(similar.getId())) {
        // todo
        return similar;
    }
    @NotNull final RequirementFactory factory = serviceLocator.getSingletonService(RequirementFactory.class);
    String forcedId = null;
    // special case for "overriden" entities
    if (id != null) {
        for (Requirement item : buildType.getRequirements()) {
            if (id.equals(item.getId())) {
                forcedId = id;
                break;
            }
        }
    }
    Requirement requirementToAdd;
    if (forcedId != null) {
        requirementToAdd = factory.createRequirement(forcedId, propertyName, propertiesMap.get(NAME_PROPERTY_VALUE), getSubmittedType());
    } else {
        requirementToAdd = factory.createRequirement(propertyName, propertiesMap.get(NAME_PROPERTY_VALUE), getSubmittedType());
    }
    String requirementId = requirementToAdd.getId();
    if (requirementId == null && disabled != null) {
        // throw exception before adding requirement to the model
        throw new OperationException("Cannot set disabled state for an entity without id");
    }
    buildType.addRequirement(requirementToAdd);
    if (disabled != null) {
        buildType.setEnabled(requirementId, !disabled);
    }
    return requirementToAdd;
}
Also used : Requirement(jetbrains.buildServer.requirements.Requirement) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) RequirementFactory(jetbrains.buildServer.serverSide.RequirementFactory) NotNull(org.jetbrains.annotations.NotNull) OperationException(jetbrains.buildServer.server.rest.errors.OperationException) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Requirement (jetbrains.buildServer.requirements.Requirement)1 BadRequestException (jetbrains.buildServer.server.rest.errors.BadRequestException)1 OperationException (jetbrains.buildServer.server.rest.errors.OperationException)1 RequirementFactory (jetbrains.buildServer.serverSide.RequirementFactory)1 NotNull (org.jetbrains.annotations.NotNull)1