View Javadoc
1   /**
2    * This file Copyright (c) 2019 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.browser;
35  
36  import info.magnolia.jcr.util.NodeNameHelper;
37  import info.magnolia.jcr.util.NodeTypes;
38  import info.magnolia.jcr.util.NodeUtil;
39  import info.magnolia.objectfactory.Components;
40  import info.magnolia.pages.app.data.NewRenderableNodeWrapper;
41  import info.magnolia.pages.app.data.RenderableNodeMetadata;
42  import info.magnolia.rendering.template.TemplateDefinition;
43  import info.magnolia.rendering.template.registry.TemplateDefinitionRegistry;
44  import info.magnolia.ui.CloseHandler;
45  import info.magnolia.ui.ValueContext;
46  import info.magnolia.ui.api.action.ActionExecutionException;
47  import info.magnolia.ui.contentapp.Datasource;
48  import info.magnolia.ui.contentapp.action.CommitAction;
49  import info.magnolia.ui.datasource.jcr.JcrDatasource;
50  import info.magnolia.ui.editor.FormView;
51  import info.magnolia.ui.observation.DatasourceObservation;
52  
53  import javax.inject.Inject;
54  import javax.jcr.Node;
55  
56  import com.machinezoo.noexception.Exceptions;
57  
58  /**
59   * Validates the form (page name and template) filled by the user in the UI, and creates a new JCR node from
60   * form data. If the page has no dialog defined the newly created page is saved to the repository and pushed into the
61   * {@link ValueContext} to update the parent view, otherwise the page is wrapped inside
62   * {@link NewRenderableNodeWrapper}, to bypass the session per request limitation of a JCR node, before pushed into the
63   * {@link ValueContext} for {@link EditPagePropertiesAction} to present the dialog corresponding to the page template.
64   */
65  public class CreatePageAction extends CommitAction<Node> {
66  
67      private final JcrDatasource jcrDatasource;
68      private final CloseHandler closeHandler;
69      private final NodeNameHelper nodeNameHelper;
70      private final TemplateDefinitionRegistry templateDefinitionRegistry;
71  
72      @Inject
73      CreatePageAction(CreatePageActionDefinition definition,
74                              FormView<Node> form,
75                              ValueContext<Node> valueContext,
76                              JcrDatasource jcrDatasource,
77                              CloseHandler closeHandler,
78                              NodeNameHelper nodeNameHelper,
79                              TemplateDefinitionRegistry templateDefinitionRegistry,
80                              DatasourceObservation.Manual datasourceObservation
81      ) {
82          super(definition, closeHandler, valueContext, form, (Datasource) jcrDatasource, datasourceObservation);
83          this.jcrDatasource = jcrDatasource;
84          this.closeHandler = closeHandler;
85          this.nodeNameHelper = nodeNameHelper;
86          this.templateDefinitionRegistry = templateDefinitionRegistry;
87      }
88  
89      /**
90       * @deprecated since 6.2.5. Use {@link #CreatePageAction(CreatePageActionDefinition, info.magnolia.ui.editor.FormView, info.magnolia.ui.ValueContext, info.magnolia.ui.datasource.jcr.JcrDatasource, info.magnolia.ui.CloseHandler, info.magnolia.jcr.util.NodeNameHelper, info.magnolia.rendering.template.registry.TemplateDefinitionRegistry, info.magnolia.ui.observation.DatasourceObservation.Manual)} instead.
91       */
92      @Deprecated
93      public CreatePageAction(CreatePageActionDefinition definition, FormView<Node> form, ValueContext<Node> valueContext, JcrDatasource jcrDatasource, CloseHandler closeHandler, NodeNameHelper nodeNameHelper, TemplateDefinitionRegistry templateDefinitionRegistry) {
94          this(definition, form, valueContext, jcrDatasource, closeHandler, nodeNameHelper, templateDefinitionRegistry, Components.getComponent(DatasourceObservation.Manual.class));
95      }
96  
97      @Override
98      public void write() {
99          Exceptions.wrap().run(() -> {
100             Node parent = getValueContext().getSingle().orElseGet(jcrDatasource::getRoot);
101 
102             // New page name and template cannot be empty because of prior form validation
103             String pageName = getForm().<String>getPropertyValue("jcrName")
104                     .orElseThrow(ActionExecutionException::new);
105 
106             // Appends counter if the name of the page is not unique
107             pageName = nodeNameHelper.getUniqueName(parent, pageName);
108 
109             // Sanitizes page name replacing invalid characters in page name with valid ones
110             pageName = nodeNameHelper.getValidatedName(pageName);
111 
112             String pageTemplate = getForm().<String>getPropertyValue(NodeTypes.Renderable.TEMPLATE)
113                     .orElseThrow(ActionExecutionException::new);
114 
115             TemplateDefinition templateDefinition = templateDefinitionRegistry.getProvider(pageTemplate).get();
116             String pageDialog = templateDefinition.getDialog();
117 
118             // Create Child page under the currently selected parent in workspace
119             Node page = NodeUtil.createPath(parent, pageName, NodeTypes.Page.NAME);
120 
121             RenderableNodeMetadata pageMetadata = RenderableNodeMetadata.builder()
122                     .nodeName(pageName)
123                     .templateId(templateDefinition.getId())
124                     .build();
125 
126             // Wrap the page inside NewRenderableNodeWrapper for cross
127             // request availability - for InitPageProperties action to present page dialog. If the page
128             // has no dialog, we just save the newly created page, before it is pushed into ValueContext.
129             page = new NewRenderableNodeWrapper(page, pageMetadata);
130 
131             if (pageDialog == null) {
132                 page.getSession().save();
133             }
134 
135             getValueContext().set(page);
136 
137             // Close the create page dialog
138             closeHandler.close();
139         });
140     }
141 
142     @Override
143     protected FormView<Node> getForm() {
144         return (FormView<Node>) super.getForm();
145     }
146 }