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.project; 16 : 17 : import com.google.common.base.Strings; 18 : import com.google.common.collect.Lists; 19 : import com.google.common.primitives.Ints; 20 : import com.google.gerrit.entities.Project; 21 : import com.google.gerrit.extensions.client.ProjectState; 22 : import com.google.gerrit.index.project.ProjectData; 23 : import com.google.gerrit.index.query.LimitPredicate; 24 : import com.google.gerrit.index.query.Predicate; 25 : import com.google.gerrit.index.query.QueryBuilder; 26 : import com.google.gerrit.index.query.QueryParseException; 27 : import com.google.inject.Inject; 28 : import java.util.List; 29 : 30 : /** Parses a query string meant to be applied to project objects. */ 31 : public class ProjectQueryBuilder extends QueryBuilder<ProjectData, ProjectQueryBuilder> { 32 : public static final String FIELD_LIMIT = "limit"; 33 : 34 6 : private static final QueryBuilder.Definition<ProjectData, ProjectQueryBuilder> mydef = 35 : new QueryBuilder.Definition<>(ProjectQueryBuilder.class); 36 : 37 : @Inject 38 : ProjectQueryBuilder() { 39 6 : super(mydef, null); 40 6 : } 41 : 42 : @Operator 43 : public Predicate<ProjectData> name(String name) { 44 3 : return ProjectPredicates.name(Project.nameKey(name)); 45 : } 46 : 47 : @Operator 48 : public Predicate<ProjectData> parent(String parentName) { 49 4 : return ProjectPredicates.parent(Project.nameKey(parentName)); 50 : } 51 : 52 : @Operator 53 : public Predicate<ProjectData> inname(String namePart) { 54 3 : if (namePart.isEmpty()) { 55 0 : return name(namePart); 56 : } 57 3 : return ProjectPredicates.inname(namePart); 58 : } 59 : 60 : @Operator 61 : public Predicate<ProjectData> description(String description) throws QueryParseException { 62 3 : if (Strings.isNullOrEmpty(description)) { 63 2 : throw error("description operator requires a value"); 64 : } 65 : 66 3 : return ProjectPredicates.description(description); 67 : } 68 : 69 : @Operator 70 : public Predicate<ProjectData> state(String state) throws QueryParseException { 71 3 : if (Strings.isNullOrEmpty(state)) { 72 2 : throw error("state operator requires a value"); 73 : } 74 : ProjectState parsedState; 75 : try { 76 3 : parsedState = ProjectState.valueOf(state.replace('-', '_').toUpperCase()); 77 2 : } catch (IllegalArgumentException e) { 78 2 : throw error("state operator must be either 'active' or 'read-only'", e); 79 3 : } 80 3 : if (parsedState == ProjectState.HIDDEN) { 81 0 : throw error("state operator must be either 'active' or 'read-only'"); 82 : } 83 3 : return ProjectPredicates.state(parsedState); 84 : } 85 : 86 : @Override 87 : protected Predicate<ProjectData> defaultField(String query) throws QueryParseException { 88 : // Adapt the capacity of this list when adding more default predicates. 89 3 : List<Predicate<ProjectData>> preds = Lists.newArrayListWithCapacity(3); 90 3 : preds.add(name(query)); 91 3 : preds.add(inname(query)); 92 3 : if (!Strings.isNullOrEmpty(query)) { 93 3 : preds.add(description(query)); 94 : } 95 3 : return Predicate.or(preds); 96 : } 97 : 98 : @Operator 99 : public Predicate<ProjectData> limit(String query) throws QueryParseException { 100 0 : Integer limit = Ints.tryParse(query); 101 0 : if (limit == null) { 102 0 : throw error("Invalid limit: " + query); 103 : } 104 0 : return new LimitPredicate<>(FIELD_LIMIT, limit); 105 : } 106 : }