Line data Source code
1 : // Copyright (C) 2015 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.extensions.events; 16 : 17 : import com.google.common.flogger.FluentLogger; 18 : import com.google.gerrit.exceptions.StorageException; 19 : import com.google.gerrit.extensions.api.changes.NotifyHandling; 20 : import com.google.gerrit.extensions.common.AccountInfo; 21 : import com.google.gerrit.extensions.common.ChangeInfo; 22 : import com.google.gerrit.extensions.events.TopicEditedListener; 23 : import com.google.gerrit.server.account.AccountState; 24 : import com.google.gerrit.server.plugincontext.PluginSetContext; 25 : import com.google.gerrit.server.query.change.ChangeData; 26 : import com.google.inject.Inject; 27 : import com.google.inject.Singleton; 28 : import java.time.Instant; 29 : 30 : /** Helper class to fire an event when the topic of a change has been edited. */ 31 : @Singleton 32 : public class TopicEdited { 33 142 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 34 : 35 : private final PluginSetContext<TopicEditedListener> listeners; 36 : private final EventUtil util; 37 : 38 : @Inject 39 142 : TopicEdited(PluginSetContext<TopicEditedListener> listeners, EventUtil util) { 40 142 : this.listeners = listeners; 41 142 : this.util = util; 42 142 : } 43 : 44 : public void fire(ChangeData changeData, AccountState account, String oldTopicName, Instant when) { 45 30 : if (listeners.isEmpty()) { 46 4 : return; 47 : } 48 : try { 49 26 : Event event = 50 26 : new Event(util.changeInfo(changeData), util.accountInfo(account), oldTopicName, when); 51 26 : listeners.runEach(l -> l.onTopicEdited(event)); 52 0 : } catch (StorageException e) { 53 0 : logger.atSevere().withCause(e).log("Couldn't fire event"); 54 26 : } 55 26 : } 56 : 57 : /** Event to be fired when the topic of a change has been edited. */ 58 : private static class Event extends AbstractChangeEvent implements TopicEditedListener.Event { 59 : private final String oldTopic; 60 : 61 : Event(ChangeInfo change, AccountInfo editor, String oldTopic, Instant when) { 62 26 : super(change, editor, when, NotifyHandling.ALL); 63 26 : this.oldTopic = oldTopic; 64 26 : } 65 : 66 : @Override 67 : public String getOldTopic() { 68 26 : return oldTopic; 69 : } 70 : } 71 : }