View Javadoc
1   /**
2    * This file Copyright (c) 2013-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.pages.app.action;
35  
36  import info.magnolia.event.EventBus;
37  import info.magnolia.objectfactory.Components;
38  import info.magnolia.pages.app.editor.PagesContentConnector;
39  import info.magnolia.ui.api.action.AbstractAction;
40  import info.magnolia.ui.api.action.ActionExecutionException;
41  import info.magnolia.ui.api.app.SubAppContext;
42  import info.magnolia.ui.api.app.SubAppDescriptor;
43  import info.magnolia.ui.api.app.SubAppEventBus;
44  import info.magnolia.ui.api.event.ContentChangedEvent;
45  import info.magnolia.ui.contentapp.definition.ContentSubAppDescriptor;
46  import info.magnolia.ui.dialog.formdialog.FormDialogPresenter;
47  import info.magnolia.ui.dialog.formdialog.FormDialogPresenterFactory;
48  import info.magnolia.ui.form.EditorCallback;
49  import info.magnolia.ui.vaadin.gwt.client.shared.AbstractElement;
50  import info.magnolia.ui.vaadin.integration.contentconnector.ContentConnectorDefinition;
51  import info.magnolia.ui.vaadin.integration.jcr.JcrItemAdapter;
52  
53  import javax.inject.Inject;
54  import javax.inject.Named;
55  
56  import org.slf4j.Logger;
57  import org.slf4j.LoggerFactory;
58  
59  /**
60   * Opens a dialog for editing a {@link AbstractElement}. Reads the dialog-name from {@link AbstractElement#getDialog()}
61   * which is extracted from the 'cms-comment' on client-side.
62   */
63  public class EditElementAction extends AbstractAction<EditElementActionDefinition> {
64  
65      private static final Logger log = LoggerFactory.getLogger(EditElementAction.class);
66  
67      private final FormDialogPresenterFactory dialogPresenterFactory;
68      private final JcrItemAdapter item;
69      private final AbstractElement element;
70      private final SubAppContext subAppContext;
71      private final EventBus eventBus;
72  
73      @Inject
74      public EditElementAction(final JcrItemAdapter item, final EditElementActionDefinition definition, final AbstractElement element,
75                               final SubAppContext subAppContext, final @Named(SubAppEventBus.NAME) EventBus eventBus,
76                               final FormDialogPresenterFactory dialogPresenterFactory) {
77          super(definition);
78          this.item = item;
79          this.element = element;
80          this.subAppContext = subAppContext;
81          this.eventBus = eventBus;
82          this.dialogPresenterFactory = dialogPresenterFactory;
83      }
84  
85      /**
86       * @deprecated since 5.5, use {@link EditElementAction(JcrItemAdapter, EditElementActionDefinition, AbstractElement, SubAppContext, EventBus, FormDialogPresenterFactory)}
87       */
88      @Deprecated
89      public EditElementAction(EditElementActionDefinition definition, AbstractElement element,
90                               SubAppContext subAppContext, @Named(SubAppEventBus.NAME) EventBus eventBus, FormDialogPresenterFactory dialogPresenterFactory) {
91          this(resolveItemForDeprecations(element, subAppContext, eventBus), definition, element, subAppContext, eventBus, dialogPresenterFactory);
92      }
93  
94      /*
95       * This is a work-around to resolve the content-connector from the subAppDescriptor.
96       * Using {@link info.magnolia.objectfactory.Components.getComponent} does not work as it resides in the subapp-scope,
97       * whereas Components is only bound to main-scope.
98       */
99      private static JcrItemAdapter resolveItemForDeprecations(AbstractElement element, SubAppContext subAppContext, EventBus eventBus) {
100         SubAppDescriptor subAppDescriptor = subAppContext.getSubAppDescriptor();
101         if (subAppDescriptor instanceof ContentSubAppDescriptor) {
102             ContentConnectorDefinition contentConnectorDefinition = ((ContentSubAppDescriptor) subAppDescriptor).getContentConnector();
103             if (contentConnectorDefinition != null) {
104                 PagesContentConnector contentConnector = (PagesContentConnector) Components.newInstance(contentConnectorDefinition.getImplementationClass(), subAppContext, eventBus, contentConnectorDefinition);
105                 return contentConnector.getItemByElement(element);
106             } else {
107                 log.warn("Sub-app descriptor {} ({}) expected a ContentConnectorDefinition, but no contentConnector is currently configured.", subAppDescriptor.getName(), subAppDescriptor.getClass().getSimpleName());
108             }
109         }
110         log.warn("Not able to resolve the content-connector using the fallback mechanism in '{}'. Please omit the deprecated code. Return 'null'.",
111                 EditElementAction.class.getName());
112         return null;
113     }
114 
115     @Override
116     public void execute() throws ActionExecutionException {
117         String dialogId = element.getDialog();
118 
119         final FormDialogPresenter formDialogPresenter = dialogPresenterFactory.createFormDialogPresenter(dialogId);
120         formDialogPresenter.start(item, dialogId, subAppContext, new EditorCallback() {
121 
122             @Override
123             public void onSuccess(String actionName) {
124                 eventBus.fireEvent(new ContentChangedEvent(item.getItemId()));
125                 formDialogPresenter.closeDialog();
126             }
127 
128             @Override
129             public void onCancel() {
130                 formDialogPresenter.closeDialog();
131             }
132         });
133     }
134 }