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.rendering.template.TemplateDefinition;
40  import info.magnolia.rendering.template.registry.TemplateDefinitionRegistry;
41  import info.magnolia.ui.CloseHandler;
42  import info.magnolia.ui.ValueContext;
43  import info.magnolia.ui.api.action.AbstractAction;
44  import info.magnolia.ui.api.action.ActionExecutionException;
45  import info.magnolia.ui.datasource.jcr.JcrDatasource;
46  import info.magnolia.ui.editor.FormView;
47  
48  import javax.inject.Inject;
49  import javax.jcr.Node;
50  
51  import com.machinezoo.noexception.Exceptions;
52  import com.vaadin.data.BinderValidationStatus;
53  
54  /**
55   * Creates a new JCR node for the new page. If the page has no dialog defined the
56   * newly created page is saved to the repository, otherwise the {@link EditPagePropertiesAction}
57   * dialog is opened as a chained action to edit page properties using the defined dialog.
58   */
59  public class CreatePageAction extends AbstractAction<CreatePageActionDefinition> {
60  
61      private final FormView<Node> form;
62      private ValueContext<Node> valueContext;
63      private final JcrDatasource jcrDatasource;
64      private final CloseHandler closeHandler;
65      private final NodeNameHelper nodeNameHelper;
66      private final TemplateDefinitionRegistry templateDefinitionRegistry;
67  
68      @Inject
69      public CreatePageAction(CreatePageActionDefinition definition, FormView<Node> form,
70                              ValueContext<Node> valueContext,
71                              JcrDatasource jcrDatasource,
72                              CloseHandler closeHandler,
73                              NodeNameHelper nodeNameHelper,
74                              TemplateDefinitionRegistry templateDefinitionRegistry) {
75          super(definition);
76          this.form = form;
77          this.valueContext = valueContext;
78          this.jcrDatasource = jcrDatasource;
79          this.closeHandler = closeHandler;
80          this.nodeNameHelper = nodeNameHelper;
81          this.templateDefinitionRegistry = templateDefinitionRegistry;
82      }
83  
84  
85      @Override
86      public void execute() {
87          Exceptions.wrap().run(() -> {
88              Node parent = valueContext.getSingle().orElseGet(jcrDatasource::getRoot);
89  
90              // Create unique path for untitled page used later to validate the uniqueness of page name
91              String uniquePath = nodeNameHelper.getUniqueName(parent.getSession(), parent.getPath(), "untitled");
92  
93              // Create a node at the parent with the unique path
94              Node page = NodeUtil.createPath(parent, uniquePath, NodeTypes.Page.NAME);
95  
96              valueContext.set(page);
97  
98              // Validation status of the newly created page. The form is invalid if one of the conditions fail
99              // - page name and its uniqueness, and page template
100             boolean isInvalidForm = form.validate().stream().anyMatch(BinderValidationStatus::hasErrors);
101 
102             // If the form is invalid, reset the value context to the parent and return
103             if (isInvalidForm) {
104                 valueContext.set(parent);
105                 return;
106             }
107 
108             // New page name and template cannot be empty because of prior form validation
109             String pageName = form.<String>getPropertyValue("jcrName")
110                     .orElseThrow(ActionExecutionException::new);
111 
112             // Appends counter if the name of the page is not unique
113             pageName = nodeNameHelper.getUniqueName(parent, pageName);
114 
115             // Sanitizes page name replacing invalid characters in page name with valid ones
116             pageName = nodeNameHelper.getValidatedName(pageName);
117 
118             String pageTemplate = form.<String>getPropertyValue(NodeTypes.Renderable.TEMPLATE)
119                     .orElseThrow(ActionExecutionException::new);
120 
121             TemplateDefinition templateDefinition = templateDefinitionRegistry.getProvider(pageTemplate).get();
122 
123             // Rename the page node previously created to the one provided in the form
124             NodeUtil.renameNode(page, pageName);
125 
126             // Set the page template from the form
127             NodeTypes.Renderable.set(page, templateDefinition.getId());
128 
129             // Save the page
130             page.getSession().save();
131 
132             // Update the value context to the newly created page
133             valueContext.set(page);
134 
135             // Close the create page dialog
136             closeHandler.close();
137         });
138     }
139 }