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.pages.app.editor.statusbar.activationstatus;
35  
36  import info.magnolia.cms.beans.config.ServerConfiguration;
37  import info.magnolia.context.MgnlContext;
38  import info.magnolia.event.EventBus;
39  import info.magnolia.i18nsystem.SimpleTranslator;
40  import info.magnolia.jcr.util.NodeTypes;
41  import info.magnolia.jcr.util.NodeUtil;
42  import info.magnolia.pages.app.editor.PageEditorPresenter;
43  import info.magnolia.pages.app.editor.extension.AbstractExtension;
44  import info.magnolia.repository.RepositoryConstants;
45  import info.magnolia.ui.api.app.SubAppEventBus;
46  import info.magnolia.ui.api.event.AdmincentralEventBus;
47  import info.magnolia.ui.api.event.ContentChangedEvent;
48  import info.magnolia.ui.api.view.View;
49  import info.magnolia.ui.contentapp.detail.DetailLocation;
50  import info.magnolia.ui.vaadin.integration.contentconnector.ContentConnector;
51  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeItemId;
52  
53  import javax.inject.Inject;
54  import javax.inject.Named;
55  import javax.jcr.ItemNotFoundException;
56  import javax.jcr.Node;
57  import javax.jcr.RepositoryException;
58  
59  /**
60   * An extension displaying the current activation status of the page inside the status bar. Returns null on start, in
61   * case this is not the admin instance.
62   */
63  public class ActivationStatus extends AbstractExtension {
64  
65      private final SimpleTranslator i18n;
66      private final ServerConfiguration serverConfiguration;
67      private final ContentConnector contentConnector;
68      private final EventBus admincentralEventBus;
69      private final EventBus subAppEventBus;
70      private PageEditorPresenter pageEditorPresenter;
71  
72      private ActivationStatusView view;
73  
74      @Inject
75      public ActivationStatus(ActivationStatusView view, SimpleTranslator i18n, ServerConfiguration serverConfiguration, ContentConnector contentConnector,
76                              final @Named(AdmincentralEventBus.NAME) EventBus admincentralEventBus, final @Named(SubAppEventBus.NAME) EventBus subAppEventBus, PageEditorPresenter pageEditorPresenter) {
77          this.view = view;
78          this.i18n = i18n;
79          this.serverConfiguration = serverConfiguration;
80          this.contentConnector = contentConnector;
81          this.admincentralEventBus = admincentralEventBus;
82          this.subAppEventBus = subAppEventBus;
83          this.pageEditorPresenter = pageEditorPresenter;
84      }
85  
86      @Override
87      public View start(DetailLocation location) {
88          if (serverConfiguration.isAdmin()) {
89              registerHandlers();
90              updateActivationStatus(location.getNodePath());
91          } else {
92              view.setVisible(false);
93          }
94          return view;
95      }
96  
97      private void updateActivationStatus(String nodePath) {
98          int status = NodeTypes.Activatable.ACTIVATION_STATUS_NOT_ACTIVATED;
99          try {
100             Node node = MgnlContext.getJCRSession(RepositoryConstants.WEBSITE).getNode(nodePath);
101             if (!node.isNodeType(NodeTypes.Page.NAME)) {
102                 node = NodeUtil.getNearestAncestorOfType(node, NodeTypes.Page.NAME);
103             }
104             status = NodeTypes.Activatable.getActivationStatus(node);
105         } catch (RepositoryException e) {
106             // page has no activation status
107         }
108         setActivationStatus(status);
109     }
110 
111 
112     private void updateActivationStatus(JcrNodeItemId itemId) {
113         int status = NodeTypes.Activatable.ACTIVATION_STATUS_NOT_ACTIVATED;
114         try {
115             Node node = MgnlContext.getJCRSession(itemId.getWorkspace()).getNodeByIdentifier(itemId.getUuid());
116             if (!node.isNodeType(NodeTypes.Page.NAME)) {
117                 node = NodeUtil.getNearestAncestorOfType(node, NodeTypes.Page.NAME);
118             }
119             status = NodeTypes.Activatable.getActivationStatus(node);
120         } catch (ItemNotFoundException e) {
121             // Should update status by page path instead
122             updateActivationStatus(pageEditorPresenter.getStatus().getNodePath());
123             return;
124         } catch (RepositoryException e) {
125             // page has no activation status
126         }
127         setActivationStatus(status);
128 
129     }
130 
131     protected void setActivationStatus(int status) {
132         if (view.isVisible()) {
133             String icon = "activation-status ";
134             String text = i18n.translate("pages.editPage.statusBar.unpublished");
135             switch (status) {
136             case NodeTypes.Activatable.ACTIVATION_STATUS_MODIFIED:
137                 icon += "color-yellow icon-status-orange";
138                 text = i18n.translate("pages.editPage.statusBar.modified");
139                 break;
140             case NodeTypes.Activatable.ACTIVATION_STATUS_ACTIVATED:
141                 icon += "color-green icon-status-green";
142                 text = i18n.translate("pages.editPage.statusBar.published");
143                 break;
144             default:
145                 icon += "color-red icon-status-red";
146             }
147 
148             view.setIconStyle(icon);
149             view.setActivationStatus(text);
150         }
151     }
152 
153     private void registerHandlers() {
154         subAppEventBus.addHandler(ContentChangedEvent.class, new ContentChangedEvent.Handler() {
155 
156             @Override
157             public void onContentChanged(ContentChangedEvent event) {
158                 if (event.getItemId() instanceof JcrNodeItemId) {
159                     updateActivationStatus((JcrNodeItemId) event.getItemId());
160                 }
161             }
162         });
163 
164         admincentralEventBus.addHandler(ContentChangedEvent.class, new ContentChangedEvent.Handler() {
165 
166             @Override
167             public void onContentChanged(ContentChangedEvent event) {
168                 final Object itemId = event.getItemId();
169                 final String nodePath = pageEditorPresenter.getStatus().getNodePath();
170                 final String itemUrlFragment = contentConnector.getItemUrlFragment(itemId);
171 
172                 if (contentConnector.canHandleItem(itemId) && itemId instanceof JcrNodeItemId && itemUrlFragment != null && itemUrlFragment.equals(nodePath)) {
173                     updateActivationStatus((JcrNodeItemId) itemId);
174                 }
175             }
176         });
177     }
178 
179     @Override
180     public void onLocationUpdate(DetailLocation location) {
181         if (serverConfiguration.isAdmin()) {
182             if (!view.isVisible()) {
183                 view.setVisible(true);
184             }
185             updateActivationStatus(location.getNodePath());
186         }
187     }
188 
189     @Override
190     public void deactivate() {
191         view.setVisible(false);
192     }
193 }