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.DeleteCommentInput; 20 : import com.google.gerrit.extensions.api.changes.DraftApi; 21 : import com.google.gerrit.extensions.api.changes.DraftInput; 22 : import com.google.gerrit.extensions.common.CommentInfo; 23 : import com.google.gerrit.extensions.restapi.NotImplementedException; 24 : import com.google.gerrit.extensions.restapi.RestApiException; 25 : import com.google.gerrit.server.change.DraftCommentResource; 26 : import com.google.gerrit.server.restapi.change.DeleteDraftComment; 27 : import com.google.gerrit.server.restapi.change.GetDraftComment; 28 : import com.google.gerrit.server.restapi.change.PutDraftComment; 29 : import com.google.inject.Inject; 30 : import com.google.inject.assistedinject.Assisted; 31 : 32 : class DraftApiImpl implements DraftApi { 33 : interface Factory { 34 : DraftApiImpl create(DraftCommentResource d); 35 : } 36 : 37 : private final DeleteDraftComment deleteDraft; 38 : private final GetDraftComment getDraft; 39 : private final PutDraftComment putDraft; 40 : private final DraftCommentResource draft; 41 : 42 : @Inject 43 : DraftApiImpl( 44 : DeleteDraftComment deleteDraft, 45 : GetDraftComment getDraft, 46 : PutDraftComment putDraft, 47 18 : @Assisted DraftCommentResource draft) { 48 18 : this.deleteDraft = deleteDraft; 49 18 : this.getDraft = getDraft; 50 18 : this.putDraft = putDraft; 51 18 : this.draft = draft; 52 18 : } 53 : 54 : @Override 55 : public CommentInfo get() throws RestApiException { 56 : try { 57 7 : return getDraft.apply(draft).value(); 58 0 : } catch (Exception e) { 59 0 : throw asRestApiException("Cannot retrieve draft", e); 60 : } 61 : } 62 : 63 : @Override 64 : public CommentInfo update(DraftInput in) throws RestApiException { 65 : try { 66 2 : return putDraft.apply(draft, in).value(); 67 1 : } catch (Exception e) { 68 1 : throw asRestApiException("Cannot update draft", e); 69 : } 70 : } 71 : 72 : @Override 73 : public void delete() throws RestApiException { 74 : try { 75 8 : deleteDraft.apply(draft, null); 76 0 : } catch (Exception e) { 77 0 : throw asRestApiException("Cannot delete draft", e); 78 8 : } 79 8 : } 80 : 81 : @Override 82 : public CommentInfo delete(DeleteCommentInput input) { 83 0 : throw new NotImplementedException(); 84 : } 85 : }