View Javadoc
1   /**
2    * This file Copyright (c) 2014-2015 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.ui.framework.task;
35  
36  import info.magnolia.cms.security.SecuritySupport;
37  import info.magnolia.event.EventBus;
38  import info.magnolia.event.SystemEventBus;
39  import info.magnolia.task.Task;
40  import info.magnolia.task.event.TaskEvent;
41  import info.magnolia.task.event.TaskEventHandler;
42  
43  import java.util.Collection;
44  import java.util.HashSet;
45  import java.util.List;
46  import java.util.Set;
47  
48  import javax.inject.Inject;
49  import javax.inject.Named;
50  import javax.inject.Provider;
51  import javax.inject.Singleton;
52  
53  import org.apache.commons.lang3.StringUtils;
54  import org.slf4j.Logger;
55  import org.slf4j.LoggerFactory;
56  
57  import com.google.common.collect.ArrayListMultimap;
58  import com.google.common.collect.ListMultimap;
59  import com.google.common.collect.Multimaps;
60  
61  /**
62   * LocalTaskDispatcherManager.
63   */
64  @Singleton
65  public class LocalTaskDispatcherManager implements TaskEventHandler {
66  
67      private static final Logger log = LoggerFactory.getLogger(LocalTaskDispatcherManager.class);
68  
69      private final ListMultimap<String, TaskEventDispatcher> listeners = Multimaps.synchronizedListMultimap(ArrayListMultimap.<String, TaskEventDispatcher>create());
70  
71      private Provider<SecuritySupport> securitySupport;
72  
73      @Inject
74      public LocalTaskDispatcherManager(@Named(SystemEventBus.NAME) final EventBus systemEventBus, Provider<SecuritySupport> securitySupport) {
75          this.securitySupport = securitySupport;
76          systemEventBus.addHandler(TaskEvent.class, this);
77      }
78  
79      @Override
80      public void taskClaimed(TaskEvent taskEvent) {
81          Set<String> users = getAllRecipients(taskEvent.getTask());
82          sendTaskEvent(taskEvent, users);
83      }
84  
85      @Override
86      public void taskAdded(TaskEvent taskEvent) {
87          Set<String> users = getAllRecipients(taskEvent.getTask());
88          sendTaskEvent(taskEvent, users);
89      }
90  
91      @Override
92      public void taskResolved(TaskEvent taskEvent) {
93          Set<String> users = getAllRecipients(taskEvent.getTask());
94          sendTaskEvent(taskEvent, users);
95      }
96  
97      @Override
98      public void taskFailed(TaskEvent taskEvent) {
99          Set<String> users = getAllRecipients(taskEvent.getTask());
100         sendTaskEvent(taskEvent, users);
101     }
102 
103     @Override
104     public void taskArchived(TaskEvent taskEvent) {
105         Set<String> users = getAllRecipients(taskEvent.getTask());
106         sendTaskEvent(taskEvent, users);
107     }
108 
109     @Override
110     public void taskRemoved(TaskEvent taskEvent) {
111         Set<String> users = getAllRecipients(taskEvent.getTask());
112         sendTaskEvent(taskEvent, users);
113     }
114 
115     @Override
116     public void taskScheduled(TaskEvent taskEvent) {
117         Set<String> users = getAllRecipients(taskEvent.getTask());
118         sendTaskEvent(taskEvent, users);
119     }
120 
121     private Set<String> getAllRecipients(final Task task) {
122 
123         HashSet<String> users = new HashSet<String>();
124 
125         log.debug("Found actorId [{}]", task.getActorId());
126         if (StringUtils.isNotBlank(task.getActorId())) {
127             users.add(task.getActorId());
128         }
129 
130         if (task.getActorIds() != null) {
131             log.debug("Found actorIds {}", task.getActorIds());
132             users.addAll(task.getActorIds());
133         }
134 
135 
136         log.debug("Found groups {}", task.getGroupIds());
137         if (task.getGroupIds() != null) {
138             for (String group : task.getGroupIds()) {
139                 Collection<String> usersOfGroupTransitive = securitySupport.get().getUserManager().getUsersWithGroup(group, true);
140                 users.addAll(usersOfGroupTransitive);
141             }
142         }
143 
144         return users;
145     }
146 
147     private void sendTaskEvent(TaskEvent taskEvent, Set<String> users) {
148 
149         log.debug("Sending a task event to the following users {}", users);
150         for (String userId : users) {
151             final List<TaskEventDispatcher> listenerList = listeners.get(userId);
152             if (listenerList != null) {
153                 for (final TaskEventDispatcher listener : listenerList) {
154                     listener.onTaskEvent(taskEvent);
155                 }
156             }
157         }
158     }
159 
160     public void registerLocalTasksListener(String userName, TaskEventDispatcher listener) {
161         listeners.put(userName, listener);
162     }
163 
164     public void unregisterLocalTasksListener(String userName, TaskEventDispatcher listener) {
165         listeners.remove(userName, listener);
166     }
167 
168 }