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.actionbar.ActionbarView;
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.integration.NullItem;
55  import info.magnolia.ui.vaadin.integration.jcr.JcrItemId;
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         ComponentProvider provider) {
106         this.view = view;
107         this.actionbarPresenter = actionbarPresenter;
108         this.appContext = appContext;
109         this.eventBus = eventBus;
110         this.adminCentralEventBus = adminCentralEventBus;
111         this.executor = new SubAppActionExecutor(provider, appContext);
112         this.provider = provider;
113         this.eventBus.addHandler(EntrySelectedEvent.class, this);
114         this.actionbarPresenter.setListener(this);
115     }
116 
117     public SiteMapEditorView start() {
118         SiteMapEditorSubAppDescriptor descriptor = (SiteMapEditorSubAppDescriptor) appContext.getSubAppDescriptor();
119         this.targetWorkspace = descriptor.getTargetWorkspace();
120         this.contentPresenter = provider.newInstance(descriptor.getPresenterClass());
121 
122         ActionbarView actionbarView = actionbarPresenter.start(descriptor.getActionbar(), descriptor.getActions());
123         view.setActionbarView(actionbarView);
124 
125         SiteMapContentView contentView = contentPresenter.start();
126         view.setContentView(contentView);
127 
128         if (contentPresenter.getCurrentSelection() == null) {
129             actionbarPresenter.disableGroup(EDIT_ACTIONS_GROUP);
130         }
131 
132         adminCentralEventBus.addHandler(ContentChangedEvent.class, new ContentChangedEvent.Handler() {
133             @Override
134             public void onContentChanged(ContentChangedEvent event) {
135                 if (targetWorkspace.equals(((JcrItemId) event.getItemId()).getWorkspace())) {
136                     contentPresenter.edit(nodeAdapter.getJcrItem());
137                 }
138             }
139         });
140         return view;
141     }
142 
143     public void setSiteMapNode(JcrNodeAdapter nodeAdapter) {
144         this.nodeAdapter = nodeAdapter;
145         contentPresenter.edit(nodeAdapter.getJcrItem());
146     }
147 
148     @Override
149     public void onEntriesSelected(SiteMapEntry entry) {
150         if (entry == null) {
151             actionbarPresenter.disableGroup(EDIT_ACTIONS_GROUP);
152         } else {
153             actionbarPresenter.enableGroup(EDIT_ACTIONS_GROUP);
154         }
155     }
156 
157     @Override
158     public void onActionbarItemClicked(String itemName) {
159         try {
160             SiteMapEntry entry = contentPresenter.getCurrentSelection();
161             if (entry != null) {
162                 Node pageNode = MgnlContext.getJCRSession(targetWorkspace).getNode(entry.getPath());
163                 executor.execute(itemName, new JcrNodeAdapter(pageNode));
164             } else {
165                 executor.execute(itemName, new NullItem());
166             }
167         } catch (ActionExecutionException e) {
168             e.printStackTrace();
169         } catch (RepositoryException e) {
170             e.printStackTrace();
171         }
172     }
173 
174 
175     public String getCaptionForLocation(DetailLocation location) {
176         return contentPresenter.getLabel() + ": " + getSiteMapName(location);
177     }
178 
179     private String getSiteMapName(DetailLocation location) {
180         String caption = StringUtils.EMPTY;
181         try {
182             Session session = MgnlContext.getJCRSession(GoogleSiteMapConfiguration.WORKSPACE);
183             Node node = session.getNode(location.getNodePath());
184             caption = PropertyUtil.getString(node, GoogleSiteMapConfiguration.SITE_MAP_DISPLAY_NAME_PROPERTY_NAME, node.getName());
185         } catch (RepositoryException e) {
186             log.warn("Could not set page Tab Title for item : {}", location.getNodePath(), e);
187         }
188         return caption;
189     }
190 }