View Javadoc
1   /**
2    * This file Copyright (c) 2018 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.admincentral.badge;
35  
36  import static info.magnolia.task.Task.Status.*;
37  
38  import info.magnolia.context.Context;
39  import info.magnolia.event.EventBus;
40  import info.magnolia.event.HandlerRegistration;
41  import info.magnolia.i18nsystem.SimpleTranslator;
42  import info.magnolia.task.Task;
43  import info.magnolia.task.TasksManager;
44  import info.magnolia.task.event.TaskEvent;
45  import info.magnolia.task.event.TaskEventHandler;
46  import info.magnolia.ui.api.event.AdmincentralEventBus;
47  import info.magnolia.ui.api.location.DefaultLocation;
48  import info.magnolia.ui.api.location.Location;
49  import info.magnolia.ui.api.location.LocationController;
50  import info.magnolia.ui.api.shell.CloseAppEvent;
51  
52  import java.util.Arrays;
53  import java.util.Collections;
54  import java.util.List;
55  
56  import javax.inject.Inject;
57  import javax.inject.Named;
58  
59  /**
60   * Task badge implementation.
61   */
62  public class TaskBadge extends AbstractBadge implements TaskEventHandler {
63  
64      private final TasksManager tasksManager;
65      private final HandlerRegistration registration;
66      static final List<Task.Status> CREATED = Collections.singletonList(Created);
67      static final List<Task.Status> ASSIGNED = Arrays.asList(InProgress, Scheduled);
68      private final EventBus eventBus;
69  
70      @Inject
71      TaskBadge(LocationController controller, Context context, SimpleTranslator i18n, TasksManager tasksManager, @Named(AdmincentralEventBus.NAME) EventBus eventBus) {
72          super(controller, context, i18n);
73          this.tasksManager = tasksManager;
74          this.registration = eventBus.addHandler(TaskEvent.class,this);
75          this.eventBus = eventBus;
76  
77          update();
78      }
79  
80      @Override
81      Status getStatus() {
82          long newItems = tasksManager.getTasksAmountByUserAndStatus(getUserName(), CREATED);
83          int assignedItems = tasksManager.findTasksByAssigneeAndStatus(getUserName(), ASSIGNED).size();
84  
85          String description = getI18n().translate("badge.task.description", newItems, assignedItems);
86          // the number of all tasks in the workflow for the current user which have not been archived.
87          long grandTotal = newItems + assignedItems;
88          return new Status(newItems > 0, (int) grandTotal, description);
89      }
90  
91      @Override
92      public String getCaption() {
93          return getI18n().translate("tasks.app.caption");
94      }
95  
96      @Override
97      Location getLocation() {
98          return new DefaultLocation("app", "tasks-app");
99      }
100 
101     @Override
102     public void taskClaimed(TaskEvent taskEvent) {
103         update();
104     }
105 
106     @Override
107     public void taskAdded(TaskEvent taskEvent) {
108         update();
109     }
110 
111     @Override
112     public void taskArchived(TaskEvent taskEvent) {
113         update();
114     }
115 
116     @Override
117     public void taskResolved(TaskEvent taskEvent) {
118         update();
119     }
120 
121     @Override
122     public void taskFailed(TaskEvent taskEvent) {
123         update();
124     }
125 
126     @Override
127     public void taskRemoved(TaskEvent taskEvent) {
128         update();
129     }
130 
131     @Override
132     public void taskScheduled(TaskEvent taskEvent) {
133         update();
134     }
135 
136     @Override
137     public void detach() {
138         registration.removeHandler();
139         super.detach();
140     }
141 
142     @Override
143     protected void onBadgeDeselected() {
144         eventBus.fireEvent(new CloseAppEvent());
145     }
146 }