/* * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jetbrains.buildServer.server.rest.data; import java.util.ArrayList; import java.util.List; import jetbrains.buildServer.parameters.impl.MapParametersProviderImpl; import jetbrains.buildServer.server.rest.errors.BadRequestException; import jetbrains.buildServer.server.rest.errors.NotFoundException; import jetbrains.buildServer.server.rest.model.PagerData; import jetbrains.buildServer.serverSide.BuildAgentManager; import jetbrains.buildServer.serverSide.SBuildAgent; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; /** * @author Yegor.Yarko * Date: 25.12.13 */ public class AgentFinder extends AbstractFinder { public static final String CONNECTED = "connected"; public static final String AUTHORIZED = "authorized"; public static final String PARAMETER = "parameter"; @NotNull private final BuildAgentManager myAgentManager; public AgentFinder(final @NotNull BuildAgentManager agentManager) { super(new String[]{DIMENSION_ID, CONNECTED, AUTHORIZED, PARAMETER, Locator.LOCATOR_SINGLE_VALUE_UNUSED_NAME, PagerData.START, PagerData.COUNT}); myAgentManager = agentManager; } @NotNull @Override public List getAllItems() { final List result = myAgentManager.getRegisteredAgents(true); result.addAll(myAgentManager.getUnregisteredAgents()); return result; } @Override @Nullable protected SBuildAgent findSingleItem(@NotNull final Locator locator) { if (locator.isSingleValue()) { // no dimensions found, assume it's name final SBuildAgent agent = myAgentManager.findAgentByName(locator.getSingleValue(), true); if (agent == null) { throw new NotFoundException("No agent can be found by name '" + locator.getSingleValue() + "'."); } locator.checkLocatorFullyProcessed(); return agent; } Long id = locator.getSingleDimensionValueAsLong("id"); if (id != null) { final SBuildAgent agent = myAgentManager.findAgentById(id.intValue(), true); if (agent == null) { throw new NotFoundException("No agent can be found by id '" + locator.getSingleDimensionValue("id") + "'."); } locator.checkLocatorFullyProcessed(); return agent; } String name = locator.getSingleDimensionValue("name"); if (name != null) { final SBuildAgent agent = myAgentManager.findAgentByName(name, true); if (agent != null) { locator.checkLocatorFullyProcessed(); return agent; } throw new NotFoundException("No agent can be found by name '" + name + "'."); } return null; } @Override protected AbstractFilter getFilter(final Locator locator) { if (locator.isSingleValue()) { throw new BadRequestException("Single value locator '" + locator.getSingleValue() + "' is not supported for several items query."); } final Long countFromFilter = locator.getSingleDimensionValueAsLong(PagerData.COUNT); final MultiCheckerFilter result = new MultiCheckerFilter(locator.getSingleDimensionValueAsLong(PagerData.START), countFromFilter != null ? countFromFilter.intValue() : null, null); final Boolean authorizedDimension = locator.getSingleDimensionValueAsBoolean(AUTHORIZED); if (authorizedDimension != null) { result.add(new FilterConditionChecker() { public boolean isIncluded(@NotNull final SBuildAgent item) { return FilterUtil.isIncludedByBooleanFilter(authorizedDimension, item.isAuthorized()); } }); } final Boolean connectedDimension = locator.getSingleDimensionValueAsBoolean(CONNECTED); if (connectedDimension != null) { result.add(new FilterConditionChecker() { public boolean isIncluded(@NotNull final SBuildAgent item) { return FilterUtil.isIncludedByBooleanFilter(connectedDimension, item.isRegistered()); } }); } final String parameterDimension = locator.getSingleDimensionValue(PARAMETER); if (parameterDimension != null) { final ParameterCondition parameterCondition = ParameterCondition.create(parameterDimension); result.add(new FilterConditionChecker() { public boolean isIncluded(@NotNull final SBuildAgent item) { return parameterCondition.matches(new MapParametersProviderImpl(item.getAvailableParameters())); } }); } return result; } @Override protected List getPrefilteredItems(@NotNull final Locator locator) { List result = new ArrayList(); final Boolean authorizedDimension = locator.getSingleDimensionValueAsBoolean(AUTHORIZED); if (authorizedDimension == null || authorizedDimension){ result.addAll(myAgentManager.getRegisteredAgents(true)); } else{ result.addAll(myAgentManager.getRegisteredAgents(false)); } final Boolean connectedDimension = locator.getSingleDimensionValueAsBoolean(CONNECTED); if (connectedDimension == null || !connectedDimension){ result.addAll(myAgentManager.getUnregisteredAgents()); } return result; } }