View Javadoc
1   /**
2    * This file Copyright (c) 2016 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.jcr.util.NodeTypes;
38  import info.magnolia.rendering.template.registry.TemplateDefinitionRegistry;
39  import info.magnolia.ui.admincentral.dialog.action.SaveDialogAction;
40  import info.magnolia.ui.admincentral.dialog.action.SaveDialogActionDefinition;
41  import info.magnolia.ui.api.action.ActionExecutionException;
42  import info.magnolia.ui.api.app.SubAppContext;
43  import info.magnolia.ui.api.event.AdmincentralEventBus;
44  import info.magnolia.ui.api.event.ContentChangedEvent;
45  import info.magnolia.ui.dialog.DialogPresenter;
46  import info.magnolia.ui.dialog.callback.DefaultEditorCallback;
47  import info.magnolia.ui.dialog.formdialog.FormDialogPresenter;
48  import info.magnolia.ui.dialog.formdialog.FormDialogPresenterFactory;
49  import info.magnolia.ui.form.EditorCallback;
50  import info.magnolia.ui.form.EditorValidator;
51  import info.magnolia.ui.vaadin.integration.contentconnector.ContentConnector;
52  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
53  
54  import javax.inject.Inject;
55  import javax.inject.Named;
56  import javax.jcr.Node;
57  import javax.jcr.RepositoryException;
58  
59  import org.slf4j.Logger;
60  import org.slf4j.LoggerFactory;
61  
62  /**
63   * Create page action.
64   */
65  public class CreatePageAction extends SaveDialogAction {
66  
67      private static final Logger log = LoggerFactory.getLogger(CreatePageAction.class);
68  
69      private final FormDialogPresenterFactory dialogPresenterFactory;
70      private final SubAppContext subAppContext;
71      private final TemplateDefinitionRegistry templateDefinitionRegistry;
72      private final EventBus eventBus;
73      private final ContentConnector contentConnector;
74  
75      @Inject
76      public CreatePageAction(SaveDialogActionDefinition definition, JcrNodeAdapter item, EditorValidator validator, EditorCallback callback, FormDialogPresenterFactory dialogPresenterFactory, SubAppContext subAppContext, TemplateDefinitionRegistry templateDefinitionRegistry, @Named(AdmincentralEventBus.NAME) final EventBus eventBus, ContentConnector contentConnector) {
77          super(definition, item, validator, callback);
78          this.dialogPresenterFactory = dialogPresenterFactory;
79          this.subAppContext = subAppContext;
80          this.templateDefinitionRegistry = templateDefinitionRegistry;
81          this.eventBus = eventBus;
82          this.contentConnector = contentConnector;
83      }
84  
85      @Override
86      public void execute() throws ActionExecutionException {
87          validator.showValidation(true);
88  
89          if (validator.isValid()) {
90              final JcrNodeAdapter itemChanged = (JcrNodeAdapter) item;
91              try {
92                  final String dialogId = templateDefinitionRegistry.getProvider((String) item.getItemProperty(NodeTypes.Renderable.TEMPLATE).getValue()).get().getDialog();
93                  if (dialogId != null) {
94                      callback.onSuccess(getDefinition().getName()); //close the first dialog
95                      final FormDialogPresenter formDialogPresenter = dialogPresenterFactory.createFormDialogPresenter(dialogId);
96                      formDialogPresenter.start(item, dialogId, subAppContext, createDialogCallback(formDialogPresenter));
97                  } else {
98                      Node node = itemChanged.applyChanges();
99                      setNodeName(node, itemChanged);
100                     node.getSession().save();
101                     callback.onSuccess(getDefinition().getName());
102                 }
103             } catch (RepositoryException e) {
104                 throw new ActionExecutionException(e);
105             }
106         } else {
107             log.info("Validation error(s) occurred. No save performed.");
108         }
109     }
110 
111     private EditorCallback createDialogCallback(final DialogPresenter formDialogPresenter) {
112         return new DefaultEditorCallback(formDialogPresenter) {
113             @Override
114             public void onSuccess(String actionName) {
115                 eventBus.fireEvent(new ContentChangedEvent(contentConnector.getItemId(item), true));
116                 super.onSuccess(actionName);
117             }
118         };
119     }
120 
121     /**
122      * @deprecated since 5.4.7. Use {@link #createDialogCallback(DialogPresenter)}. I don't think we'll need to open this
123      * one up. You can still listen to the event.
124      */
125     @Deprecated
126     protected EditorCallback createDialogCallback(final JcrNodeAdapter itemChanged, final DialogPresenter formDialogPresenter) {
127         return createDialogCallback(formDialogPresenter);
128     }
129 
130     /**
131      * Definition for {@link CreatePageAction}.
132      */
133     public static class Definition extends SaveDialogActionDefinition {
134         public Definition() {
135             setImplementationClass(CreatePageAction.class);
136         }
137     }
138 }