Line data Source code
1 : // Copyright (C) 2012 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.restapi.change; 16 : 17 : import com.google.gerrit.entities.Account; 18 : import com.google.gerrit.entities.Address; 19 : import com.google.gerrit.extensions.api.changes.ReviewerInfo; 20 : import com.google.gerrit.extensions.restapi.Response; 21 : import com.google.gerrit.extensions.restapi.RestReadView; 22 : import com.google.gerrit.server.approval.ApprovalsUtil; 23 : import com.google.gerrit.server.change.ChangeResource; 24 : import com.google.gerrit.server.change.ReviewerJson; 25 : import com.google.gerrit.server.change.ReviewerResource; 26 : import com.google.gerrit.server.permissions.PermissionBackendException; 27 : import com.google.inject.Inject; 28 : import com.google.inject.Singleton; 29 : import java.util.LinkedHashMap; 30 : import java.util.List; 31 : import java.util.Map; 32 : 33 : @Singleton 34 : public class ListReviewers implements RestReadView<ChangeResource> { 35 : private final ApprovalsUtil approvalsUtil; 36 : private final ReviewerJson json; 37 : private final ReviewerResource.Factory resourceFactory; 38 : 39 : @Inject 40 : ListReviewers( 41 145 : ApprovalsUtil approvalsUtil, ReviewerResource.Factory resourceFactory, ReviewerJson json) { 42 145 : this.approvalsUtil = approvalsUtil; 43 145 : this.resourceFactory = resourceFactory; 44 145 : this.json = json; 45 145 : } 46 : 47 : @Override 48 : public Response<List<ReviewerInfo>> apply(ChangeResource rsrc) throws PermissionBackendException { 49 4 : Map<String, ReviewerResource> reviewers = new LinkedHashMap<>(); 50 4 : for (Account.Id accountId : approvalsUtil.getReviewers(rsrc.getNotes()).all()) { 51 2 : if (!reviewers.containsKey(accountId.toString())) { 52 2 : reviewers.put(accountId.toString(), resourceFactory.create(rsrc, accountId)); 53 : } 54 2 : } 55 4 : for (Address adr : rsrc.getNotes().getReviewersByEmail().all()) { 56 1 : if (!reviewers.containsKey(adr.toString())) { 57 1 : reviewers.put(adr.toString(), new ReviewerResource(rsrc, adr)); 58 : } 59 1 : } 60 4 : return Response.ok(json.format(reviewers.values())); 61 : } 62 : }