View Javadoc
1   /**
2    * This file Copyright (c) 2014-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.ui.admincentral.shellapp.pulse;
35  
36  import info.magnolia.event.EventBus;
37  import info.magnolia.event.EventHandler;
38  import info.magnolia.objectfactory.ComponentProvider;
39  import info.magnolia.registry.RegistrationException;
40  import info.magnolia.ui.admincentral.shellapp.pulse.item.PulseListDefinition;
41  import info.magnolia.ui.admincentral.shellapp.pulse.item.detail.PulseItemCategory;
42  import info.magnolia.ui.admincentral.shellapp.pulse.item.list.PulseListActionExecutor;
43  import info.magnolia.ui.admincentral.shellapp.pulse.item.list.PulseListPresenter;
44  import info.magnolia.ui.api.event.AdmincentralEventBus;
45  import info.magnolia.ui.api.shell.Shell;
46  import info.magnolia.ui.api.view.View;
47  import info.magnolia.ui.framework.shell.ShellImpl;
48  import info.magnolia.ui.vaadin.gwt.client.shared.magnoliashell.ShellAppType;
49  
50  import java.util.ArrayList;
51  import java.util.HashMap;
52  import java.util.List;
53  import java.util.Map;
54  import java.util.NavigableMap;
55  import java.util.TreeMap;
56  
57  import javax.inject.Inject;
58  import javax.inject.Named;
59  
60  import org.slf4j.Logger;
61  import org.slf4j.LoggerFactory;
62  
63  /**
64   * Presenter of {@link PulseView}.
65   */
66  public class PulsePresenter implements PulseListPresenter.Listener, PulseView.Listener, EventHandler {
67  
68      private static final Logger log = LoggerFactory.getLogger(PulsePresenter.class);
69  
70      private PulseView view;
71      private ShellImpl shell;
72      private PulseItemCategory selectedCategory;
73      private boolean isDisplayingDetailView;
74      private NavigableMap<String, PulseListPresenter> presenters = new TreeMap<>();
75      private Map<PulseListPresenter, View> presenterToView = new HashMap<>();
76      private PulseDefinition definition;
77      private ComponentProvider componentProvider;
78  
79      /**
80       * @deprecated since 5.4 - {@code admincentralEventBus} is not needed.
81       */
82      @Deprecated
83      public PulsePresenter(PulseDefinition definition, @Named(AdmincentralEventBus.NAME) final EventBus admincentralEventBus, final PulseView view, final Shell shell, ComponentProvider componentProvider) {
84          this(definition, view, shell, componentProvider);
85      }
86  
87      @Inject
88      public PulsePresenter(PulseDefinition definition, final PulseView view, final Shell shell, ComponentProvider componentProvider) {
89          this.view = view;
90          this.shell = (ShellImpl) shell;
91          this.componentProvider = componentProvider;
92          this.definition = definition;
93      }
94  
95      public View start() {
96          view.setListener(this);
97  
98          List<PulseItemCategory> categories = new ArrayList<>();
99          for (PulseListDefinition pulseListDefinition : definition.getPresenters()) {
100             if (pulseListDefinition.getPresenterClass() == null) {
101                 log.error("There is no presenterClass defined for pulse list '{}'.", pulseListDefinition.getName());
102             } else {
103                 PulseListActionExecutor actionExecutor = componentProvider.newInstance(PulseListActionExecutor.class);
104                 PulseListPresenter presenter = componentProvider.newInstance(pulseListDefinition.getPresenterClass(), pulseListDefinition, actionExecutor);
105                 presenter.setListener(this);
106                 presenters.put(pulseListDefinition.getName(), presenter);
107                 categories.add(presenter.getCategory());
108             }
109         }
110 
111         if (presenters.size() > 0) {
112             view.initNavigator(categories.toArray(new PulseItemCategory[categories.size()]));
113             final PulseListPresenter currentPresenter = presenters.lastEntry().getValue();
114 
115             selectedCategory = currentPresenter.getCategory();
116             view.setPulseSubView(startOrGetView(currentPresenter));
117             view.setTabActive(currentPresenter.getCategory());
118         }
119 
120         updatePulseCounter();
121         return view;
122     }
123 
124     @Override
125     public void onCategoryChange(PulseItemCategory category) {
126         selectedCategory = category;
127         showList();
128     }
129 
130     @Override
131     public void showList() {
132         for (PulseListPresenter presenter : presenters.values()) {
133             if (selectedCategory == presenter.getCategory()) {
134                 view.setPulseSubView(startOrGetView(presenter));
135                 break;
136             }
137         }
138         isDisplayingDetailView = false;
139     }
140 
141     public boolean isDisplayingDetailView() {
142         return isDisplayingDetailView;
143     }
144 
145     @Override
146     public void openItem(String identifier, String itemId) {
147         try {
148             view.setPulseSubView(presenters.get(identifier).openItem(itemId));
149             isDisplayingDetailView = true;
150 
151         } catch (RegistrationException e) {
152             log.error(e.getMessage());
153         }
154     }
155 
156     @Override
157     public void updatePulseCounter() {
158         int totalItem = 0;
159         for (PulseListPresenter presenter : presenters.values()) {
160             int pendingItem = presenter.getPendingItemCount();
161             view.updateCategoryBadgeCount(presenter.getCategory(), pendingItem);
162             totalItem += pendingItem;
163 
164         }
165 
166         shell.setIndication(ShellAppType.PULSE, totalItem);
167     }
168 
169     @Override
170     public void updateView(PulseItemCategory category) {
171         // update top navigation and load new tasks
172         selectedCategory = category;
173         view.setTabActive(category);
174         if (isDisplayingDetailView) {
175             showList();
176         }
177     }
178 
179     private View startOrGetView(PulseListPresenter presenter) {
180         View result = presenterToView.get(presenter);
181         if (result == null) {
182             result = presenter.start();
183             presenterToView.put(presenter, result);
184         }
185         return result;
186     }
187 }