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.change; 16 : 17 : import com.google.common.base.Strings; 18 : import com.google.gerrit.common.Nullable; 19 : import com.google.gerrit.entities.Change; 20 : import com.google.gerrit.extensions.restapi.BadRequestException; 21 : import com.google.gerrit.server.ChangeMessagesUtil; 22 : import com.google.gerrit.server.extensions.events.TopicEdited; 23 : import com.google.gerrit.server.notedb.ChangeUpdate; 24 : import com.google.gerrit.server.update.BatchUpdateOp; 25 : import com.google.gerrit.server.update.ChangeContext; 26 : import com.google.gerrit.server.update.PostUpdateContext; 27 : import com.google.gerrit.server.validators.ValidationException; 28 : import com.google.inject.Inject; 29 : import com.google.inject.assistedinject.Assisted; 30 : 31 : public class SetTopicOp implements BatchUpdateOp { 32 : public interface Factory { 33 : SetTopicOp create(@Nullable String topic); 34 : } 35 : 36 : private final String topic; 37 : private final TopicEdited topicEdited; 38 : private final ChangeMessagesUtil cmUtil; 39 : 40 : private Change change; 41 : private String oldTopicName; 42 : private String newTopicName; 43 : 44 : @Inject 45 : public SetTopicOp( 46 30 : TopicEdited topicEdited, ChangeMessagesUtil cmUtil, @Nullable @Assisted String topic) { 47 30 : this.topic = topic; 48 30 : this.topicEdited = topicEdited; 49 30 : this.cmUtil = cmUtil; 50 30 : } 51 : 52 : @Override 53 : public boolean updateChange(ChangeContext ctx) throws BadRequestException { 54 30 : change = ctx.getChange(); 55 30 : ChangeUpdate update = ctx.getUpdate(change.currentPatchSetId()); 56 30 : newTopicName = Strings.nullToEmpty(topic); 57 30 : oldTopicName = Strings.nullToEmpty(change.getTopic()); 58 30 : if (oldTopicName.equals(newTopicName)) { 59 23 : return false; 60 : } 61 : 62 : String summary; 63 12 : if (oldTopicName.isEmpty()) { 64 12 : summary = "Topic set to " + newTopicName; 65 4 : } else if (newTopicName.isEmpty()) { 66 2 : summary = "Topic " + oldTopicName + " removed"; 67 : } else { 68 3 : summary = String.format("Topic changed from %s to %s", oldTopicName, newTopicName); 69 : } 70 12 : change.setTopic(Strings.emptyToNull(newTopicName)); 71 : try { 72 12 : update.setTopic(change.getTopic()); 73 1 : } catch (ValidationException ex) { 74 1 : throw new BadRequestException(ex.getMessage()); 75 12 : } 76 12 : cmUtil.setChangeMessage(ctx, summary, ChangeMessagesUtil.TAG_SET_TOPIC); 77 12 : return true; 78 : } 79 : 80 : @Override 81 : public void postUpdate(PostUpdateContext ctx) { 82 30 : if (change != null) { 83 30 : topicEdited.fire(ctx.getChangeData(change), ctx.getAccount(), oldTopicName, ctx.getWhen()); 84 : } 85 30 : } 86 : }