From 8e3c3e4b63fbaafb573a72173cf30428bac45f52 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Sat, 25 Jun 2016 21:48:53 +0200 Subject: [PATCH] improve modification events to pass the item before it was modified to the subscriber --- .../sonia/scm/ModificationHandlerEvent.java | 51 ++++++++++++++ .../sonia/scm/group/AbstractGroupManager.java | 23 +++++-- .../main/java/sonia/scm/group/GroupEvent.java | 2 +- .../scm/group/GroupModificationEvent.java | 66 ++++++++++++++++++ .../repository/AbstractRepositoryManager.java | 22 ++++-- .../sonia/scm/repository/RepositoryEvent.java | 2 +- .../RepositoryModificationEvent.java | 68 +++++++++++++++++++ .../sonia/scm/user/AbstractUserManager.java | 21 ++++-- .../main/java/sonia/scm/user/UserEvent.java | 2 +- .../sonia/scm/user/UserModificationEvent.java | 68 +++++++++++++++++++ .../sonia/scm/group/DefaultGroupManager.java | 7 +- .../repository/DefaultRepositoryManager.java | 4 +- .../sonia/scm/user/DefaultUserManager.java | 9 +-- 13 files changed, 317 insertions(+), 28 deletions(-) create mode 100644 scm-core/src/main/java/sonia/scm/ModificationHandlerEvent.java create mode 100644 scm-core/src/main/java/sonia/scm/group/GroupModificationEvent.java create mode 100644 scm-core/src/main/java/sonia/scm/repository/RepositoryModificationEvent.java create mode 100644 scm-core/src/main/java/sonia/scm/user/UserModificationEvent.java diff --git a/scm-core/src/main/java/sonia/scm/ModificationHandlerEvent.java b/scm-core/src/main/java/sonia/scm/ModificationHandlerEvent.java new file mode 100644 index 0000000000..18b9a9d6b3 --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/ModificationHandlerEvent.java @@ -0,0 +1,51 @@ +/** + * Copyright (c) 2014, Sebastian Sdorra + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of SCM-Manager; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ +package sonia.scm; + +import sonia.scm.event.HandlerEventBase; + +/** + * Extension to the {@link ModificationHandlerEvent}. + * + * @param type of changed item + * + * @author Sebastian Sdorra + * @since 1.48 + */ +public interface ModificationHandlerEvent extends HandlerEventBase +{ + /** + * Returns item, before it was modified. + * + * @return item before modification + */ + public T getItemBeforeModification(); +} diff --git a/scm-core/src/main/java/sonia/scm/group/AbstractGroupManager.java b/scm-core/src/main/java/sonia/scm/group/AbstractGroupManager.java index aea5258bf2..a4d4e4b355 100644 --- a/scm-core/src/main/java/sonia/scm/group/AbstractGroupManager.java +++ b/scm-core/src/main/java/sonia/scm/group/AbstractGroupManager.java @@ -90,21 +90,32 @@ public abstract class AbstractGroupManager implements GroupManager } /** - * Calls the {@link GroupListener#onEvent(Group,sonia.scm.HandlerEvent)} - * method of all registered listeners and send a {@link GroupEvent} to - * the {@link ScmEventBus}. - * + * Creates a new {@link GroupEvent} and calls {@link #fireEvent(sonia.scm.group.GroupEvent)}. + * * @param group group that has changed * @param event type of change event */ protected void fireEvent(Group group, HandlerEvent event) + { + fireEvent(new GroupEvent(group, event)); + } + + /** + * Calls the {@link GroupListener#onEvent(Group,sonia.scm.HandlerEvent)} + * method of all registered listeners and send a {@link GroupEvent} to + * the {@link ScmEventBus}. + * + * @param event type of change event + * @since 1.148 + */ + protected void fireEvent(GroupEvent event) { for (GroupListener listener : listenerSet) { - listener.onEvent(group, event); + listener.onEvent(event.getItem(), event.getEventType()); } - ScmEventBus.getInstance().post(new GroupEvent(group, event)); + ScmEventBus.getInstance().post(event); } //~--- fields --------------------------------------------------------------- diff --git a/scm-core/src/main/java/sonia/scm/group/GroupEvent.java b/scm-core/src/main/java/sonia/scm/group/GroupEvent.java index f13ed098d9..b22b7b2ae6 100644 --- a/scm-core/src/main/java/sonia/scm/group/GroupEvent.java +++ b/scm-core/src/main/java/sonia/scm/group/GroupEvent.java @@ -48,7 +48,7 @@ import sonia.scm.event.HandlerEventBase; * @since 1.23 */ @Event -public final class GroupEvent implements HandlerEventBase +public class GroupEvent implements HandlerEventBase { /** diff --git a/scm-core/src/main/java/sonia/scm/group/GroupModificationEvent.java b/scm-core/src/main/java/sonia/scm/group/GroupModificationEvent.java new file mode 100644 index 0000000000..50c9f143ce --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/group/GroupModificationEvent.java @@ -0,0 +1,66 @@ +/** + * Copyright (c) 2014, Sebastian Sdorra + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of SCM-Manager; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ +package sonia.scm.group; + +import sonia.scm.HandlerEvent; +import sonia.scm.ModificationHandlerEvent; + +/** + * Event which is fired whenever a group is modified. + * + * @author Sebastian Sdorra + * @since 1.48 + */ +public class GroupModificationEvent extends GroupEvent implements ModificationHandlerEvent +{ + + private final Group itemBeforeModification; + + /** + * Constructs a new {@link GroupModificationEvent}. + * + * @param item changed group + * @param itemBeforeModification changed group before it was modified + * @param eventType type of event + */ + public GroupModificationEvent(Group item, Group itemBeforeModification, HandlerEvent eventType) + { + super(item, eventType); + this.itemBeforeModification = itemBeforeModification; + } + + @Override + public Group getItemBeforeModification() + { + return itemBeforeModification; + } + +} diff --git a/scm-core/src/main/java/sonia/scm/repository/AbstractRepositoryManager.java b/scm-core/src/main/java/sonia/scm/repository/AbstractRepositoryManager.java index e2955d1e64..1468ac3ec1 100644 --- a/scm-core/src/main/java/sonia/scm/repository/AbstractRepositoryManager.java +++ b/scm-core/src/main/java/sonia/scm/repository/AbstractRepositoryManager.java @@ -231,22 +231,34 @@ public abstract class AbstractRepositoryManager implements RepositoryManager listenerSet.remove(listener); } + /** + * Creates an {@link RepositoryEvent} and calls {@link #fireEvent(sonia.scm.repository.RepositoryEvent)}. + * + * @param repository changed repository + * @param event event type + */ + protected void fireEvent(Repository repository, HandlerEvent event) + { + fireEvent(new RepositoryEvent(repository, event)); + } + /** * Calls the {@link RepositoryListener#onEvent(Repository,sonia.scm.HandlerEvent)} * method of all registered listeners and send a {@link RepositoryEvent} to * the {@link ScmEventBus}. * - * @param repository repository that has changed - * @param event type of change event + * @param event repository event + * + * @since 1.48 */ - protected void fireEvent(Repository repository, HandlerEvent event) + protected void fireEvent(RepositoryEvent event) { for (RepositoryListener listener : listenerSet) { - listener.onEvent(repository, event); + listener.onEvent(event.getItem(), event.getEventType()); } - ScmEventBus.getInstance().post(new RepositoryEvent(repository, event)); + ScmEventBus.getInstance().post(event); } /** diff --git a/scm-core/src/main/java/sonia/scm/repository/RepositoryEvent.java b/scm-core/src/main/java/sonia/scm/repository/RepositoryEvent.java index b6e8412696..3bf3a44c7a 100644 --- a/scm-core/src/main/java/sonia/scm/repository/RepositoryEvent.java +++ b/scm-core/src/main/java/sonia/scm/repository/RepositoryEvent.java @@ -47,7 +47,7 @@ import sonia.scm.event.HandlerEventBase; * @since 1.23 */ @Event -public final class RepositoryEvent implements HandlerEventBase +public class RepositoryEvent implements HandlerEventBase { /** diff --git a/scm-core/src/main/java/sonia/scm/repository/RepositoryModificationEvent.java b/scm-core/src/main/java/sonia/scm/repository/RepositoryModificationEvent.java new file mode 100644 index 0000000000..f8d571b4e5 --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/repository/RepositoryModificationEvent.java @@ -0,0 +1,68 @@ +/** + * Copyright (c) 2014, Sebastian Sdorra + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of SCM-Manager; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ +package sonia.scm.repository; + +import sonia.scm.HandlerEvent; +import sonia.scm.ModificationHandlerEvent; +import sonia.scm.event.Event; + +/** + * Event which is fired whenever a repository is modified. + * + * @author Sebastian Sdorra + * @since 1.48 + */ +@Event +public final class RepositoryModificationEvent extends RepositoryEvent implements ModificationHandlerEvent +{ + + private final Repository itemBeforeModification; + + /** + * Constructs a new {@link RepositoryModificationEvent}. + * + * @param item changed repository + * @param itemBeforeModification repository before it was modified + * @param eventType event type + */ + public RepositoryModificationEvent(Repository item, Repository itemBeforeModification, HandlerEvent eventType) + { + super(item, eventType); + this.itemBeforeModification = itemBeforeModification; + } + + @Override + public Repository getItemBeforeModification() + { + return itemBeforeModification; + } + +} diff --git a/scm-core/src/main/java/sonia/scm/user/AbstractUserManager.java b/scm-core/src/main/java/sonia/scm/user/AbstractUserManager.java index 2c3cb0b17d..c1020e0bb6 100644 --- a/scm-core/src/main/java/sonia/scm/user/AbstractUserManager.java +++ b/scm-core/src/main/java/sonia/scm/user/AbstractUserManager.java @@ -90,21 +90,32 @@ public abstract class AbstractUserManager implements UserManager } /** - * Calls the {@link UserListener#onEvent(User,sonia.scm.HandlerEvent)} - * method of all registered listeners and send a {@link UserEvent} to - * the {@link ScmEventBus}. + * Creates a new {@link UserEvent} and calls {@link #fireEvent(sonia.scm.user.UserEvent)}. * * @param user user that has changed * @param event type of change event */ protected void fireEvent(User user, HandlerEvent event) + { + fireEvent(new UserEvent(user, event)); + } + + /** + * Calls the {@link UserListener#onEvent(User,sonia.scm.HandlerEvent)} + * method of all registered listeners and send a {@link UserEvent} to + * the {@link ScmEventBus}. + * + * @param event user event + * @since 1.48 + */ + protected void fireEvent(UserEvent event) { for (UserListener listener : listenerSet) { - listener.onEvent(user, event); + listener.onEvent(event.getItem(), event.getEventType()); } - ScmEventBus.getInstance().post(new UserEvent(user, event)); + ScmEventBus.getInstance().post(event); } //~--- fields --------------------------------------------------------------- diff --git a/scm-core/src/main/java/sonia/scm/user/UserEvent.java b/scm-core/src/main/java/sonia/scm/user/UserEvent.java index 276cd5664a..ef1d99c000 100644 --- a/scm-core/src/main/java/sonia/scm/user/UserEvent.java +++ b/scm-core/src/main/java/sonia/scm/user/UserEvent.java @@ -47,7 +47,7 @@ import sonia.scm.event.HandlerEventBase; * @since 1.23 */ @Event -public final class UserEvent implements HandlerEventBase +public class UserEvent implements HandlerEventBase { /** diff --git a/scm-core/src/main/java/sonia/scm/user/UserModificationEvent.java b/scm-core/src/main/java/sonia/scm/user/UserModificationEvent.java new file mode 100644 index 0000000000..72c8e23e31 --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/user/UserModificationEvent.java @@ -0,0 +1,68 @@ +/** + * Copyright (c) 2014, Sebastian Sdorra + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of SCM-Manager; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ +package sonia.scm.user; + +import sonia.scm.HandlerEvent; +import sonia.scm.ModificationHandlerEvent; +import sonia.scm.event.Event; + +/** + * Event which is fired whenever a user is modified. + * + * @author Sebastian Sdorra + * @since 1.48 + */ +@Event +public class UserModificationEvent extends UserEvent implements ModificationHandlerEvent +{ + + private final User itemBeforeModification; + + /** + * Constructs a new {@link UserModificationEvent}. + * + * @param item changed user + * @param itemBeforeModification changed user before it was modified + * @param eventType type of event + */ + public UserModificationEvent(User item, User itemBeforeModification, HandlerEvent eventType) + { + super(item, eventType); + this.itemBeforeModification = itemBeforeModification; + } + + @Override + public User getItemBeforeModification() + { + return itemBeforeModification; + } + +} diff --git a/scm-webapp/src/main/java/sonia/scm/group/DefaultGroupManager.java b/scm-webapp/src/main/java/sonia/scm/group/DefaultGroupManager.java index b85bad40c5..542add65eb 100644 --- a/scm-webapp/src/main/java/sonia/scm/group/DefaultGroupManager.java +++ b/scm-webapp/src/main/java/sonia/scm/group/DefaultGroupManager.java @@ -222,13 +222,14 @@ public class DefaultGroupManager extends AbstractGroupManager String name = group.getName(); - if (groupDAO.contains(name)) + Group notModified = groupDAO.get(name); + if (notModified != null) { removeDuplicateMembers(group); + fireEvent(new GroupModificationEvent(group, notModified, HandlerEvent.BEFORE_MODIFY)); group.setLastModified(System.currentTimeMillis()); - fireEvent(group, HandlerEvent.BEFORE_MODIFY); groupDAO.modify(group); - fireEvent(group, HandlerEvent.MODIFY); + fireEvent(new GroupModificationEvent(group, notModified, HandlerEvent.MODIFY)); } else { diff --git a/scm-webapp/src/main/java/sonia/scm/repository/DefaultRepositoryManager.java b/scm-webapp/src/main/java/sonia/scm/repository/DefaultRepositoryManager.java index da5af847de..672a8d7a46 100644 --- a/scm-webapp/src/main/java/sonia/scm/repository/DefaultRepositoryManager.java +++ b/scm-webapp/src/main/java/sonia/scm/repository/DefaultRepositoryManager.java @@ -373,7 +373,7 @@ public class DefaultRepositoryManager extends AbstractRepositoryManager if (notModifiedRepository != null) { assertIsOwner(notModifiedRepository); - fireEvent(repository, HandlerEvent.BEFORE_MODIFY); + fireEvent(new RepositoryModificationEvent(repository, notModifiedRepository, HandlerEvent.BEFORE_MODIFY)); repository.setLastModified(System.currentTimeMillis()); getHandler(repository).modify(repository); repositoryDAO.modify(repository); @@ -384,7 +384,7 @@ public class DefaultRepositoryManager extends AbstractRepositoryManager "repository ".concat(repository.getName()).concat(" not found")); } - fireEvent(repository, HandlerEvent.MODIFY); + fireEvent(new RepositoryModificationEvent(repository, notModifiedRepository, HandlerEvent.MODIFY)); } /** diff --git a/scm-webapp/src/main/java/sonia/scm/user/DefaultUserManager.java b/scm-webapp/src/main/java/sonia/scm/user/DefaultUserManager.java index 4dce1df549..7eaf9a25b8 100644 --- a/scm-webapp/src/main/java/sonia/scm/user/DefaultUserManager.java +++ b/scm-webapp/src/main/java/sonia/scm/user/DefaultUserManager.java @@ -280,14 +280,15 @@ public class DefaultUserManager extends AbstractUserManager } String name = user.getName(); - - if (userDAO.contains(name)) + + User notModified = userDAO.get(name); + if (notModified != null) { AssertUtil.assertIsValid(user); + fireEvent(new UserModificationEvent(user, notModified, HandlerEvent.BEFORE_MODIFY)); user.setLastModified(System.currentTimeMillis()); - fireEvent(user, HandlerEvent.BEFORE_MODIFY); userDAO.modify(user); - fireEvent(user, HandlerEvent.MODIFY); + fireEvent(new UserModificationEvent(user, notModified, HandlerEvent.MODIFY)); } else {