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.Comment; 18 : import com.google.gerrit.entities.HumanComment; 19 : import com.google.gerrit.entities.PatchSet; 20 : import com.google.gerrit.extensions.common.CommentInfo; 21 : import com.google.gerrit.extensions.common.Input; 22 : import com.google.gerrit.extensions.restapi.ResourceNotFoundException; 23 : import com.google.gerrit.extensions.restapi.Response; 24 : import com.google.gerrit.extensions.restapi.RestApiException; 25 : import com.google.gerrit.extensions.restapi.RestModifyView; 26 : import com.google.gerrit.server.CommentsUtil; 27 : import com.google.gerrit.server.PatchSetUtil; 28 : import com.google.gerrit.server.change.DraftCommentResource; 29 : import com.google.gerrit.server.update.BatchUpdate; 30 : import com.google.gerrit.server.update.BatchUpdateOp; 31 : import com.google.gerrit.server.update.ChangeContext; 32 : import com.google.gerrit.server.update.UpdateException; 33 : import com.google.gerrit.server.util.time.TimeUtil; 34 : import com.google.inject.Inject; 35 : import com.google.inject.Singleton; 36 : import java.util.Collections; 37 : import java.util.Optional; 38 : 39 : @Singleton 40 : public class DeleteDraftComment implements RestModifyView<DraftCommentResource, Input> { 41 : private final BatchUpdate.Factory updateFactory; 42 : private final CommentsUtil commentsUtil; 43 : private final PatchSetUtil psUtil; 44 : 45 : @Inject 46 : DeleteDraftComment( 47 142 : BatchUpdate.Factory updateFactory, CommentsUtil commentsUtil, PatchSetUtil psUtil) { 48 142 : this.updateFactory = updateFactory; 49 142 : this.commentsUtil = commentsUtil; 50 142 : this.psUtil = psUtil; 51 142 : } 52 : 53 : @Override 54 : public Response<CommentInfo> apply(DraftCommentResource rsrc, Input input) 55 : throws RestApiException, UpdateException { 56 9 : try (BatchUpdate bu = 57 9 : updateFactory.create(rsrc.getChange().getProject(), rsrc.getUser(), TimeUtil.now())) { 58 9 : Op op = new Op(rsrc.getComment().key); 59 9 : bu.addOp(rsrc.getChange().getId(), op); 60 9 : bu.execute(); 61 : } 62 9 : return Response.none(); 63 : } 64 : 65 : private class Op implements BatchUpdateOp { 66 : private final Comment.Key key; 67 : 68 9 : private Op(Comment.Key key) { 69 9 : this.key = key; 70 9 : } 71 : 72 : @Override 73 : public boolean updateChange(ChangeContext ctx) throws ResourceNotFoundException { 74 9 : Optional<HumanComment> maybeComment = 75 9 : commentsUtil.getDraft(ctx.getNotes(), ctx.getIdentifiedUser(), key); 76 9 : if (!maybeComment.isPresent()) { 77 0 : return false; // Nothing to do. 78 : } 79 9 : PatchSet.Id psId = PatchSet.id(ctx.getChange().getId(), key.patchSetId); 80 9 : PatchSet ps = psUtil.get(ctx.getNotes(), psId); 81 9 : if (ps == null) { 82 0 : throw new ResourceNotFoundException("patch set not found: " + psId); 83 : } 84 9 : HumanComment c = maybeComment.get(); 85 9 : commentsUtil.setCommentCommitId(c, ctx.getChange(), ps); 86 9 : commentsUtil.deleteHumanComments(ctx.getUpdate(psId), Collections.singleton(c)); 87 9 : return true; 88 : } 89 : } 90 : }