Line data Source code
1 : // Copyright (C) 2013 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.change; 16 : 17 : import com.google.common.collect.ImmutableList; 18 : import com.google.common.flogger.FluentLogger; 19 : import com.google.gerrit.entities.Project; 20 : import com.google.gerrit.extensions.common.ProjectInfo; 21 : import com.google.gerrit.index.query.OrPredicate; 22 : import com.google.gerrit.index.query.Predicate; 23 : import com.google.gerrit.server.permissions.PermissionBackendException; 24 : import com.google.gerrit.server.project.ChildProjects; 25 : import com.google.gerrit.server.project.ProjectCache; 26 : import com.google.gerrit.server.project.ProjectState; 27 : import java.util.Optional; 28 : 29 : public class ParentProjectPredicate extends OrPredicate<ChangeData> { 30 4 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 31 : 32 : protected final String value; 33 : 34 : public ParentProjectPredicate( 35 : ProjectCache projectCache, ChildProjects childProjects, String value) { 36 4 : super(predicates(projectCache, childProjects, value)); 37 4 : this.value = value; 38 4 : } 39 : 40 : protected static ImmutableList<Predicate<ChangeData>> predicates( 41 : ProjectCache projectCache, ChildProjects childProjects, String value) { 42 4 : Optional<ProjectState> projectState = projectCache.get(Project.nameKey(value)); 43 4 : if (!projectState.isPresent()) { 44 0 : return ImmutableList.of(); 45 : } 46 : 47 4 : ImmutableList.Builder<Predicate<ChangeData>> r = ImmutableList.builder(); 48 4 : r.add(ChangePredicates.project(projectState.get().getNameKey())); 49 : try { 50 4 : for (ProjectInfo p : childProjects.list(projectState.get().getNameKey())) { 51 4 : r.add(ChangePredicates.project(Project.nameKey(p.name))); 52 4 : } 53 0 : } catch (PermissionBackendException e) { 54 0 : logger.atWarning().withCause(e).log("cannot check permissions to expand child projects"); 55 4 : } 56 4 : return r.build(); 57 : } 58 : 59 : @Override 60 : public String toString() { 61 4 : return ChangeQueryBuilder.FIELD_PARENTPROJECT + ":" + value; 62 : } 63 : }