Line data Source code
1 : // Copyright (C) 2017 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.query.group; 16 : 17 : import static com.google.common.collect.ImmutableList.toImmutableList; 18 : 19 : import com.google.common.collect.ImmutableList; 20 : import com.google.common.collect.Iterables; 21 : import com.google.common.flogger.FluentLogger; 22 : import com.google.gerrit.entities.Account; 23 : import com.google.gerrit.entities.AccountGroup; 24 : import com.google.gerrit.entities.InternalGroup; 25 : import com.google.gerrit.index.IndexConfig; 26 : import com.google.gerrit.index.query.InternalQuery; 27 : import com.google.gerrit.index.query.Predicate; 28 : import com.google.gerrit.server.index.group.GroupIndexCollection; 29 : import com.google.inject.Inject; 30 : import java.util.List; 31 : import java.util.Optional; 32 : 33 : /** 34 : * Query wrapper for the group index. 35 : * 36 : * <p>Instances are one-time-use. Other singleton classes should inject a Provider rather than 37 : * holding on to a single instance. 38 : */ 39 : public class InternalGroupQuery extends InternalQuery<InternalGroup, InternalGroupQuery> { 40 150 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 41 : 42 : @Inject 43 : InternalGroupQuery( 44 : GroupQueryProcessor queryProcessor, GroupIndexCollection indexes, IndexConfig indexConfig) { 45 150 : super(queryProcessor, indexes, indexConfig); 46 150 : } 47 : 48 : public Optional<InternalGroup> byName(AccountGroup.NameKey groupName) { 49 150 : return getOnlyGroup(GroupPredicates.name(groupName.get()), "group name '" + groupName + "'"); 50 : } 51 : 52 : public Optional<InternalGroup> byId(AccountGroup.Id groupId) { 53 7 : return getOnlyGroup(GroupPredicates.id(groupId), "group id '" + groupId + "'"); 54 : } 55 : 56 : public List<InternalGroup> byMember(Account.Id memberId) { 57 20 : return query(GroupPredicates.member(memberId)); 58 : } 59 : 60 : public List<InternalGroup> bySubgroup(AccountGroup.UUID subgroupId) { 61 18 : return query(GroupPredicates.subgroup(subgroupId)); 62 : } 63 : 64 : private Optional<InternalGroup> getOnlyGroup( 65 : Predicate<InternalGroup> predicate, String groupDescription) { 66 150 : List<InternalGroup> groups = query(predicate); 67 150 : if (groups.isEmpty()) { 68 7 : return Optional.empty(); 69 : } 70 : 71 150 : if (groups.size() == 1) { 72 150 : return Optional.of(Iterables.getOnlyElement(groups)); 73 : } 74 : 75 0 : ImmutableList<AccountGroup.UUID> groupUuids = 76 0 : groups.stream().map(InternalGroup::getGroupUUID).collect(toImmutableList()); 77 0 : logger.atWarning().log("Ambiguous %s for groups %s.", groupDescription, groupUuids); 78 0 : return Optional.empty(); 79 : } 80 : }