Line data Source code
1 : // Copyright (C) 2014 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.api.changes; 16 : 17 : import static com.google.gerrit.server.api.ApiUtil.asRestApiException; 18 : 19 : import com.google.gerrit.extensions.api.changes.DeleteReviewerInput; 20 : import com.google.gerrit.extensions.api.changes.DeleteVoteInput; 21 : import com.google.gerrit.extensions.api.changes.ReviewerApi; 22 : import com.google.gerrit.extensions.restapi.RestApiException; 23 : import com.google.gerrit.server.change.ReviewerResource; 24 : import com.google.gerrit.server.change.VoteResource; 25 : import com.google.gerrit.server.restapi.change.DeleteReviewer; 26 : import com.google.gerrit.server.restapi.change.DeleteVote; 27 : import com.google.gerrit.server.restapi.change.Votes; 28 : import com.google.inject.Inject; 29 : import com.google.inject.assistedinject.Assisted; 30 : import java.util.Map; 31 : 32 : public class ReviewerApiImpl implements ReviewerApi { 33 : interface Factory { 34 : ReviewerApiImpl create(ReviewerResource r); 35 : } 36 : 37 : private final ReviewerResource reviewer; 38 : private final Votes.List listVotes; 39 : private final DeleteVote deleteVote; 40 : private final DeleteReviewer deleteReviewer; 41 : 42 : @Inject 43 : ReviewerApiImpl( 44 : Votes.List listVotes, 45 : DeleteVote deleteVote, 46 : DeleteReviewer deleteReviewer, 47 15 : @Assisted ReviewerResource reviewer) { 48 15 : this.listVotes = listVotes; 49 15 : this.deleteVote = deleteVote; 50 15 : this.deleteReviewer = deleteReviewer; 51 15 : this.reviewer = reviewer; 52 15 : } 53 : 54 : @Override 55 : public Map<String, Short> votes() throws RestApiException { 56 : try { 57 5 : return listVotes.apply(reviewer).value(); 58 0 : } catch (Exception e) { 59 0 : throw asRestApiException("Cannot list votes", e); 60 : } 61 : } 62 : 63 : @Override 64 : public void deleteVote(String label) throws RestApiException { 65 : try { 66 6 : deleteVote.apply(new VoteResource(reviewer, label), null); 67 1 : } catch (Exception e) { 68 1 : throw asRestApiException("Cannot delete vote", e); 69 6 : } 70 6 : } 71 : 72 : @Override 73 : public void deleteVote(DeleteVoteInput input) throws RestApiException { 74 : try { 75 2 : deleteVote.apply(new VoteResource(reviewer, input.label), input); 76 0 : } catch (Exception e) { 77 0 : throw asRestApiException("Cannot delete vote", e); 78 2 : } 79 2 : } 80 : 81 : @Override 82 : public void remove() throws RestApiException { 83 13 : remove(new DeleteReviewerInput()); 84 13 : } 85 : 86 : @Override 87 : public void remove(DeleteReviewerInput input) throws RestApiException { 88 : try { 89 13 : deleteReviewer.apply(reviewer, input); 90 2 : } catch (Exception e) { 91 2 : throw asRestApiException("Cannot remove reviewer", e); 92 13 : } 93 13 : } 94 : }