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