View Javadoc
1   /**
2    * This file Copyright (c) 2014-2017 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.admincentral.shellapp.pulse.item.list;
35  
36  import info.magnolia.objectfactory.Components;
37  import info.magnolia.registry.RegistrationException;
38  import info.magnolia.ui.admincentral.shellapp.pulse.item.ConfiguredPulseListDefinition;
39  import info.magnolia.ui.admincentral.shellapp.pulse.item.PulseListDefinition;
40  import info.magnolia.ui.admincentral.shellapp.pulse.item.detail.PulseDetailPresenter;
41  import info.magnolia.ui.admincentral.shellapp.pulse.item.detail.PulseItemCategory;
42  import info.magnolia.ui.admincentral.shellapp.pulse.item.list.footer.PulseListFooterPresenter;
43  import info.magnolia.ui.api.action.ActionDefinition;
44  import info.magnolia.ui.api.action.ActionExecutionException;
45  import info.magnolia.ui.api.availability.AvailabilityChecker;
46  import info.magnolia.ui.api.availability.AvailabilityDefinition;
47  import info.magnolia.ui.api.view.View;
48  
49  import java.util.List;
50  import java.util.Set;
51  
52  import org.slf4j.Logger;
53  import org.slf4j.LoggerFactory;
54  
55  /**
56   * Abstract presenter for items displayed in pulse.
57   */
58  public abstract class AbstractPulseListPresenter implements PulseListPresenter, PulseDetailPresenter.Listener, PulseListView.Listener, PulseListFooterPresenter.Listener {
59  
60      private static final Logger log = LoggerFactory.getLogger(AbstractPulseListPresenter.class);
61  
62      protected final PulseListContainer container;
63      protected final AvailabilityChecker availabilityChecker;
64      protected final PulseListActionExecutor actionExecutor;
65      protected final PulseListFooterPresenter pulseListFooterPresenter;
66      protected PulseListDefinition definition;
67  
68      protected Listener listener;
69  
70      /**
71       * @deprecated since 5.5, use {@link AbstractPulseListPresenter#AbstractPulseListPresenter(PulseListContainer, ConfiguredPulseListDefinition, AvailabilityChecker, PulseListActionExecutor, PulseListFooterPresenter)} instead.
72       */
73      @Deprecated
74      protected AbstractPulseListPresenter(PulseListContainer container) {
75          this.container = container;
76          this.availabilityChecker = Components.getComponent(AvailabilityChecker.class);
77          this.actionExecutor = Components.getComponent(PulseListActionExecutor.class);
78          this.pulseListFooterPresenter = Components.getComponent(PulseListFooterPresenter.class);
79  
80          log.warn("You are using a deprecated not compatible constructor. Please update your implementation to use the new one!");
81      }
82  
83      protected AbstractPulseListPresenter(PulseListContainer container, ConfiguredPulseListDefinition definition,
84              AvailabilityChecker availabilityChecker, PulseListActionExecutor pulseListActionExecutor, PulseListFooterPresenter pulseListFooterPresenter) {
85          this.container = container;
86          this.availabilityChecker = availabilityChecker;
87          this.actionExecutor = pulseListActionExecutor;
88          this.pulseListFooterPresenter = pulseListFooterPresenter;
89          this.definition = definition;
90  
91          this.actionExecutor.setActionsDefinition(definition.getBulkActions());
92          this.pulseListFooterPresenter.setListener(this);
93      }
94  
95      @Override
96      public void setListener(Listener listener) {
97          this.listener = listener;
98      }
99  
100     @Override
101     public void showList() {
102         container.refresh();
103         listener.showList();
104     }
105 
106     @Override
107     public void setGrouping(boolean checked) {
108         container.setGrouping(checked);
109     }
110 
111     @Override
112     public void filterByItemCategory(PulseItemCategory category) {
113         container.filterByItemCategory(category);
114     }
115 
116     @Override
117     public abstract PulseItemCategory getCategory();
118 
119     @Override
120     public abstract View openItem(String itemId) throws RegistrationException;
121 
122     @Override
123     public abstract int getPendingItemCount();
124 
125     protected abstract List<Object> getSelectedItemIds();
126 
127     @Override
128     public void onSelectionChanged(Set<Object> itemIds) {
129         pulseListFooterPresenter.updateStatus(getTotalEntriesAmount(), itemIds.size());
130         // Evaluate availability of each action within the section
131         for (ActionDefinition actionDefinition : definition.getBulkActions()) {
132             AvailabilityDefinition availability = actionDefinition.getAvailability();
133             pulseListFooterPresenter.setActionEnabled(actionDefinition.getName(), availabilityChecker.isAvailable(availability, getSelectedItemIds()));
134         }
135 
136     }
137 
138     @Override
139     public void onItemSetChanged(long totalEntriesAmount) {
140         if (pulseListFooterPresenter != null) {
141             pulseListFooterPresenter.updateStatus(getTotalEntriesAmount(), 0);
142         }
143     }
144 
145     @Override
146     public void onBulkActionTriggered(String actionId) {
147         executeAction(actionId);
148     }
149 
150     protected void executeAction(String actionName) {
151         try {
152             AvailabilityDefinition availability = actionExecutor.getActionDefinition(actionName).getAvailability();
153             if (availabilityChecker.isAvailable(availability, getSelectedItemIds())) {
154                 actionExecutor.execute(actionName, new Object[]{getSelectedItemIds()});
155             }
156         } catch (ActionExecutionException e) {
157             log.error("An error occurred while executing action [{}]", actionName, e);
158         }
159     }
160 
161 }