View Javadoc
1   /**
2    * This file Copyright (c) 2012-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.detail;
35  
36  import info.magnolia.i18nsystem.SimpleTranslator;
37  import info.magnolia.jcr.util.NodeTypes;
38  import info.magnolia.jcr.util.NodeUtil;
39  import info.magnolia.pages.app.detail.context.MoveComponentContext;
40  import info.magnolia.pages.app.detail.context.NavigationContext;
41  import info.magnolia.ui.ValueContext;
42  import info.magnolia.ui.api.action.ActionExecutionException;
43  import info.magnolia.ui.api.action.ActionExecutor;
44  import info.magnolia.ui.api.app.SubAppContext;
45  import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
46  import info.magnolia.ui.api.message.Message;
47  import info.magnolia.ui.api.message.MessageType;
48  import info.magnolia.ui.contentapp.detail.ContentDetailSubApp;
49  import info.magnolia.ui.datasource.jcr.JcrDatasource;
50  import info.magnolia.ui.editor.LocaleContext;
51  import info.magnolia.ui.framework.ioc.Destructible;
52  import info.magnolia.ui.observation.DatasourceObservation;
53  import info.magnolia.ui.vaadin.editor.PageEditorListener;
54  import info.magnolia.ui.vaadin.editor.PageEditorView;
55  import info.magnolia.ui.vaadin.gwt.client.shared.AbstractElement;
56  import info.magnolia.ui.vaadin.gwt.client.shared.ErrorType;
57  
58  import java.util.stream.Stream;
59  
60  import javax.inject.Inject;
61  import javax.jcr.Node;
62  
63  import com.machinezoo.noexception.Exceptions;
64  import com.vaadin.shared.Registration;
65  import com.vaadin.ui.Notification;
66  
67  import lombok.extern.slf4j.Slf4j;
68  
69  /**
70   * Presenter for the server side {@link info.magnolia.ui.vaadin.editor.PageEditorView}.
71   * Serves multiple methods for actions triggered from the page editor.
72   */
73  @Slf4j
74  public class PageEditorPresenter implements PageEditorListener, Destructible {
75  
76      private final ActionExecutor actionExecutor;
77      private final PageEditorView view;
78      private final SubAppContext subAppContext;
79      private final SimpleTranslator i18n;
80      private final PageEditorStatus pageEditorStatus;
81      private final ValueContext<Node> valueContext;
82      private final ContentDetailSubApp.LocationContext locationContext;
83      private final NavigationContext<String> navigationContext;
84      private final Registration observation;
85  
86      @Inject
87      public PageEditorPresenter(final ActionExecutor actionExecutor,
88                                 I18NAuthoringSupport<Node> i18NAuthoringSupport,
89                                 PageEditorView view,
90                                 SubAppContext subAppContext,
91                                 SimpleTranslator i18n,
92                                 PageEditorStatus pageEditorStatus,
93                                 LocaleContext localeContext,
94                                 ContentDetailSubApp.LocationContext locationContext,
95                                 NavigationContext<String> navigationContext,
96                                 MoveComponentContext moveComponentContext,
97                                 ValueContext<Node> valueContext,
98                                 JcrDatasource jcrDatasource,
99                                 DatasourceObservation.Manual datasourceObservation) {
100         this.actionExecutor = actionExecutor;
101         this.subAppContext = subAppContext;
102         this.i18n = i18n;
103         this.pageEditorStatus = pageEditorStatus;
104         this.view = view;
105         this.valueContext = valueContext;
106         this.navigationContext = navigationContext;
107         this.locationContext = locationContext;
108 
109         view.setListener(this);
110         view.asVaadinComponent().addAttachListener(event -> loadPageEditor());
111 
112         locationContext.location().observe(detailLocation ->
113                 detailLocation.ifPresent(location -> {
114                     Exceptions.wrap().run(() -> {
115                         Node relatedNode = jcrDatasource.getJCRSession().getNode(location.getNodePath());
116                         pageEditorStatus.updateStatusFromLocation(location);
117                         localeContext.populateFromI18NAuthoringSupport(relatedNode, i18NAuthoringSupport);
118                         loadPageEditor(); //needed by info.magnolia.pages.app.detail.action.ReopenPageAction
119                     });
120                 }));
121 
122         moveComponentContext.moving().observe(isMovingOptional -> isMovingOptional.ifPresent(isMoving -> {
123             if (isMoving) {
124                 view.startMoveComponent();
125             } else if (moveComponentContext.serverSide().nullableValue()) {
126                 view.cancelMoveComponent();
127             }
128         }));
129 
130         valueContext.observe(nodes -> nodes.stream().findFirst()
131                 .filter(Exceptions.wrap().predicate(n -> NodeUtil.isNodeType(n, NodeTypes.Page.NAME)))
132                 .ifPresent(node -> localeContext.populateFromI18NAuthoringSupport(node, i18NAuthoringSupport)));
133 
134         observation = datasourceObservation.register(() -> {
135             updateParameters();
136             view.refresh();
137         });
138     }
139 
140     @Override
141     public void onElementSelect(AbstractElement selectedElement) {
142         pageEditorStatus.setSelectedElement(selectedElement);
143         updateParameters();
144     }
145 
146     @Override
147     public void onExternalPageSelect() {
148         valueContext.clear();
149     }
150 
151     /**
152      * Used for executing actions from 'client-side'. These might have a {@link info.magnolia.ui.vaadin.gwt.client.shared.AbstractElement} as part of the arguments, but don't have to.
153      * See {@link info.magnolia.ui.vaadin.gwt.client.rpc.PageEditorServerRpc} which receives the calls from 'client-side'.
154      */
155     @Override
156     public void onError(ErrorType errorType, String... parameters) {
157         String key = String.format("pages-app.templateErrorAlert.%s.message", errorType.name());
158         Notification.show(i18n.translate("pages-app.templateErrorAlert.title"), i18n.translate(key, (Object[]) parameters), Notification.Type.WARNING_MESSAGE);
159     }
160 
161     @Override
162     public void onNavigation(String url) {
163         navigationContext.url().set(url);
164         locationContext.location().update(detailLocation -> {
165                     valueContext.getSingle()
166                             .filter(Exceptions.wrap().predicate(node -> NodeUtil.isNodeType(node, NodeTypes.Page.NAME)))
167                             .ifPresent(Exceptions.wrap().consumer(node -> detailLocation.updateNodePath(node.getPath())));
168                 }
169         );
170     }
171 
172     /**
173      * Used for executing actions from 'client-side'. These might have a {@link info.magnolia.ui.vaadin.gwt.client.shared.AbstractElement} as part of the arguments, but don't have to.
174      * See {@link info.magnolia.ui.vaadin.gwt.client.rpc.PageEditorServerRpc} which receives the calls from 'client-side'.
175      */
176     @Override
177     public void onAction(String actionName, Object... args) {
178         Stream.of(args)
179                 .filter(AbstractElement.class::isInstance)
180                 .map(AbstractElement.class::cast)
181                 .findFirst()
182                 .ifPresent(pageEditorStatus::setSelectedElement);
183         try {
184             actionExecutor.execute(actionName, args);
185         } catch (ActionExecutionException e) {
186             Message error = new Message(MessageType.ERROR, i18n.translate("pages.pageEditorPresenter.actionExecutionError.message"), e.getMessage());
187             subAppContext.getAppContext().sendLocalMessage(error);
188             log.error("An error occurred while executing action [{}]", actionName, e);
189         }
190     }
191 
192     private void loadPageEditor() {
193         view.load(pageEditorStatus.getParameters());
194     }
195 
196     private void updateParameters() {
197         view.update(pageEditorStatus.getParameters());
198     }
199 
200     @Override
201     public void destroy() {
202         observation.remove();
203     }
204 }