View Javadoc

1   /**
2    * This file Copyright (c) 2013 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.module.googlesitemap.app.subapp.sitemapdetail;
35  
36  import info.magnolia.context.MgnlContext;
37  import info.magnolia.event.EventBus;
38  import info.magnolia.jcr.util.PropertyUtil;
39  import info.magnolia.module.googlesitemap.GoogleSiteMapConfiguration;
40  import info.magnolia.module.googlesitemap.app.subapp.sitemapdetail.base.SiteMapContentView;
41  import info.magnolia.module.googlesitemap.app.subapp.sitemapdetail.base.SiteMapDetailPresenter;
42  import info.magnolia.module.googlesitemap.app.subapp.sitemapdetail.event.EntrySelectedEvent;
43  import info.magnolia.module.googlesitemap.bean.SiteMapEntry;
44  import info.magnolia.objectfactory.ComponentProvider;
45  import info.magnolia.ui.actionbar.ActionbarPresenter;
46  import info.magnolia.ui.api.action.ActionDefinition;
47  import info.magnolia.ui.api.action.ActionExecutionException;
48  import info.magnolia.ui.api.app.SubAppContext;
49  import info.magnolia.ui.api.app.SubAppEventBus;
50  import info.magnolia.ui.api.event.AdmincentralEventBus;
51  import info.magnolia.ui.api.event.ContentChangedEvent;
52  import info.magnolia.ui.contentapp.detail.DetailLocation;
53  import info.magnolia.ui.framework.app.SubAppActionExecutor;
54  import info.magnolia.ui.vaadin.actionbar.ActionbarView;
55  import info.magnolia.ui.vaadin.integration.NullItem;
56  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
57  
58  import javax.inject.Inject;
59  import javax.inject.Named;
60  import javax.jcr.Node;
61  import javax.jcr.RepositoryException;
62  import javax.jcr.Session;
63  
64  import org.apache.commons.lang.StringUtils;
65  import org.slf4j.Logger;
66  import org.slf4j.LoggerFactory;
67  
68  /**
69   * Presenter class, serves as a delegate for {@link SiteMapEditorSubApp}.
70   * Manages {@link SiteMapEditorView}.
71   */
72  public class SiteMapEditorPresenter implements EntrySelectedEvent.Handler, ActionbarPresenter.Listener {
73  
74      private Logger log = LoggerFactory.getLogger(getClass());
75  
76      public static final String EDIT_ACTIONS_GROUP = "editActions";
77  
78      private SiteMapEditorView view;
79  
80      private SiteMapDetailPresenter contentPresenter;
81  
82      private ActionbarPresenter actionbarPresenter;
83  
84      private SubAppContext appContext;
85  
86      private EventBus eventBus;
87  
88      private EventBus adminCentralEventBus;
89  
90      private SubAppActionExecutor executor;
91  
92      private JcrNodeAdapter nodeAdapter;
93  
94      private ComponentProvider provider;
95  
96      private String targetWorkspace;
97  
98      @Inject
99      public SiteMapEditorPresenter(
100         SiteMapEditorView view,
101         ActionbarPresenter actionbarPresenter,
102         SubAppContext appContext,
103         @Named(SubAppEventBus.NAME) EventBus eventBus,
104         @Named(AdmincentralEventBus.NAME) final EventBus adminCentralEventBus,
105         SubAppActionExecutor executor,
106         ComponentProvider provider) {
107         this.view = view;
108         this.actionbarPresenter = actionbarPresenter;
109         this.appContext = appContext;
110         this.eventBus = eventBus;
111         this.adminCentralEventBus = adminCentralEventBus;
112         this.executor = executor;
113         this.provider = provider;
114         this.eventBus.addHandler(EntrySelectedEvent.class, this);
115         this.actionbarPresenter.setListener(this);
116     }
117 
118     public SiteMapEditorView start() {
119         SiteMapEditorSubAppDescriptor descriptor = (SiteMapEditorSubAppDescriptor) appContext.getSubAppDescriptor();
120         this.targetWorkspace = descriptor.getTargetWorkspace();
121         this.contentPresenter = provider.newInstance(descriptor.getPresenterClass());
122 
123         ActionbarView actionbarView = actionbarPresenter.start(descriptor.getActionbar());
124         view.setActionbarView(actionbarView);
125 
126         SiteMapContentView contentView = contentPresenter.start();
127         view.setContentView(contentView);
128 
129         if (contentPresenter.getCurrentSelection() == null) {
130             actionbarPresenter.disableGroup(EDIT_ACTIONS_GROUP);
131         }
132 
133         adminCentralEventBus.addHandler(ContentChangedEvent.class, new ContentChangedEvent.Handler() {
134             @Override
135             public void onContentChanged(ContentChangedEvent event) {
136                 if (targetWorkspace.equals(event.getWorkspace())) {
137                     contentPresenter.edit(nodeAdapter.getJcrItem());
138                 }
139             }
140         });
141         return view;
142     }
143 
144     public void setSiteMapNode(JcrNodeAdapter nodeAdapter) {
145         this.nodeAdapter = nodeAdapter;
146         contentPresenter.edit(nodeAdapter.getJcrItem());
147     }
148 
149     @Override
150     public void onEntriesSelected(SiteMapEntry entry) {
151         if (entry == null) {
152             actionbarPresenter.disableGroup(EDIT_ACTIONS_GROUP);
153         } else {
154             actionbarPresenter.enableGroup(EDIT_ACTIONS_GROUP);
155         }
156     }
157 
158     @Override
159     public void onActionbarItemClicked(String itemName) {
160         try {
161             SiteMapEntry entry = contentPresenter.getCurrentSelection();
162             if (entry != null) {
163                 Node pageNode = MgnlContext.getJCRSession(targetWorkspace).getNode(entry.getPath());
164                 executor.execute(itemName, new JcrNodeAdapter(pageNode));
165             } else {
166                 executor.execute(itemName, new NullItem());
167             }
168         } catch (ActionExecutionException e) {
169             e.printStackTrace();
170         } catch (RepositoryException e) {
171             e.printStackTrace();
172         }
173     }
174 
175     @Override
176     public String getLabel(String itemName) {
177         ActionDefinition actionDefinition = executor.getActionDefinition(itemName);
178         return actionDefinition != null ? actionDefinition.getLabel() : null;
179     }
180 
181     @Override
182     public String getIcon(String itemName) {
183         try {
184             ActionDefinition actionDefinition = executor.getActionDefinition(itemName);
185             return actionDefinition != null ? actionDefinition.getIcon() : null;
186         } catch (Exception e) {
187             e.printStackTrace();
188             return null;
189         }
190     }
191 
192     public String getCaptionForLocation(DetailLocation location) {
193         return contentPresenter.getLabel() + ": " + getSiteMapName(location);
194     }
195 
196     private String getSiteMapName(DetailLocation location) {
197         String caption = StringUtils.EMPTY;
198         try {
199             Session session = MgnlContext.getJCRSession(GoogleSiteMapConfiguration.WORKSPACE);
200             Node node = session.getNode(location.getNodePath());
201             caption = PropertyUtil.getString(node, GoogleSiteMapConfiguration.SITE_MAP_DISPLAY_NAME_PROPERTY_NAME, node.getName());
202         } catch (RepositoryException e) {
203             log.warn("Could not set page Tab Title for item : {}", location.getNodePath(), e);
204         }
205         return caption;
206     }
207 }