Line data Source code
1 : // Copyright (C) 2010 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.gerrit.index.query.AndPredicate; 19 : import com.google.gerrit.index.query.Predicate; 20 : import com.google.gerrit.index.query.QueryBuilder; 21 : import com.google.gerrit.index.query.QueryParseException; 22 : import com.google.gerrit.server.CurrentUser; 23 : import com.google.gerrit.server.account.ProjectWatches.ProjectWatchKey; 24 : import java.util.ArrayList; 25 : import java.util.Collection; 26 : import java.util.Collections; 27 : import java.util.List; 28 : 29 : public class IsWatchedByPredicate extends AndPredicate<ChangeData> { 30 : protected static String describe(CurrentUser user) { 31 4 : if (user.isIdentifiedUser()) { 32 4 : return user.getAccountId().toString(); 33 : } 34 0 : return user.toString(); 35 : } 36 : 37 : protected final CurrentUser user; 38 : 39 : public IsWatchedByPredicate(ChangeQueryBuilder.Arguments args) throws QueryParseException { 40 4 : super(filters(args)); 41 4 : this.user = args.getUser(); 42 4 : } 43 : 44 : protected static List<Predicate<ChangeData>> filters(ChangeQueryBuilder.Arguments args) 45 : throws QueryParseException { 46 4 : List<Predicate<ChangeData>> r = new ArrayList<>(); 47 4 : ChangeQueryBuilder builder = new ChangeQueryBuilder(args); 48 4 : for (ProjectWatchKey w : getWatches(args)) { 49 4 : Predicate<ChangeData> f = null; 50 4 : if (w.filter() != null) { 51 : try { 52 0 : f = builder.parse(w.filter()); 53 0 : if (QueryBuilder.find(f, IsWatchedByPredicate.class) != null) { 54 : // If the query is going to infinite loop, assume it 55 : // will never match and return null. Yes this test 56 : // prevents you from having a filter that matches what 57 : // another user is filtering on. :-) 58 0 : continue; 59 : } 60 0 : } catch (QueryParseException e) { 61 0 : continue; 62 0 : } 63 : } 64 : 65 : Predicate<ChangeData> p; 66 4 : if (w.project().equals(args.allProjectsName)) { 67 0 : p = null; 68 : } else { 69 4 : p = builder.project(w.project().get()); 70 : } 71 : 72 4 : if (p != null && f != null) { 73 0 : r.add(and(p, f)); 74 4 : } else if (p != null) { 75 4 : r.add(p); 76 0 : } else if (f != null) { 77 0 : r.add(f); 78 : } else { 79 0 : r.add(builder.statusOpen()); 80 : } 81 4 : } 82 4 : if (r.isEmpty()) { 83 4 : return ImmutableList.of(ChangeIndexPredicate.none()); 84 : } 85 4 : return ImmutableList.of(or(r)); 86 : } 87 : 88 : protected static Collection<ProjectWatchKey> getWatches(ChangeQueryBuilder.Arguments args) 89 : throws QueryParseException { 90 4 : CurrentUser user = args.getUser(); 91 4 : if (user.isIdentifiedUser()) { 92 4 : return user.asIdentifiedUser().state().projectWatches().keySet(); 93 : } 94 0 : return Collections.emptySet(); 95 : } 96 : 97 : @Override 98 : public int getCost() { 99 0 : return 1; 100 : } 101 : 102 : @Override 103 : public String toString() { 104 4 : String val = describe(user); 105 4 : if (val.indexOf(' ') < 0) { 106 4 : return ChangeQueryBuilder.FIELD_WATCHEDBY + ":" + val; 107 : } 108 0 : return ChangeQueryBuilder.FIELD_WATCHEDBY + ":\"" + val + "\""; 109 : } 110 : }