View Javadoc
1   /**
2    * This file Copyright (c) 2020 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.resources.app.action;
35  
36  import info.magnolia.jcr.RuntimeRepositoryException;
37  import info.magnolia.jcr.util.NodeNameHelper;
38  import info.magnolia.jcr.util.NodeTypes;
39  import info.magnolia.jcr.util.NodeUtil;
40  import info.magnolia.resourceloader.Resource;
41  import info.magnolia.resourceloader.ResourceOrigin;
42  import info.magnolia.resourceloader.jcr.JcrResourceOrigin;
43  import info.magnolia.ui.CloseHandler;
44  import info.magnolia.ui.ValueContext;
45  import info.magnolia.ui.contentapp.Datasource;
46  import info.magnolia.ui.editor.FormView;
47  import info.magnolia.ui.observation.DatasourceObservation;
48  
49  import java.util.Calendar;
50  
51  import javax.jcr.Node;
52  import javax.jcr.RepositoryException;
53  
54  import com.machinezoo.noexception.Exceptions;
55  
56  /**
57   * Save the form view present in the context.
58   */
59  public class AddResourceAction extends AbstractAddResourceAction {
60      private final ValueContext<Resource> valueContext;
61      private final FormView<Resource> form;
62      private final NodeNameHelper nodeNameHelper;
63      private final ResourceOrigin resourceOrigin;
64  
65      public AddResourceAction(AddResourceActionDefinition definition, CloseHandler closeHandler,
66                               ValueContext<Resource> valueContext, FormView<Resource> form,
67                               NodeNameHelper nodeNameHelper,
68                               Datasource<Resource> datasource,
69                               DatasourceObservation.Manual datasourceObservation,
70                               ResourceOrigin resourceOrigin) {
71          super(definition, closeHandler, valueContext, form, datasource, datasourceObservation, resourceOrigin);
72          this.form = form;
73          this.valueContext = valueContext;
74          this.nodeNameHelper = nodeNameHelper;
75          this.resourceOrigin = resourceOrigin;
76      }
77  
78  
79      @Override
80      protected void addResource(Resource parent, Node rootNode) {
81          try {
82              String resourceName = form.<String>getPropertyValue("name").orElse("untitled");
83              String parentPath = parent.getPath();
84  
85              Node childNode = provideResourceNode(resourceName, rootNode, parentPath);
86  
87              Resource updatedParentResource = resourceOrigin.getByPath(parentPath);
88              valueContext.set(updatedParentResource.findResource(childNode.getName()).orElse(parent));
89  
90              NodeTypes.LastModified.update(childNode);
91              childNode.getSession().save();
92          } catch (RepositoryException e) {
93              throw new RuntimeException(e);
94          }
95      }
96  
97  
98      private Node provideResourceNode(String name, Node rootNode, String selectedResourcePath) {
99          try {
100             // ensure folder node existence
101             Node selectedFolderNode = NodeUtil.createPath(rootNode, selectedResourcePath, NodeTypes.Folder.NAME);
102 
103             // get unique child resource name
104             String uniqueValidatedName = getUniqueResourceName(selectedFolderNode, name);
105 
106             boolean isFolder = getDefinition().isFolder();
107             String primaryNodeTypeName = isFolder ? NodeTypes.Folder.NAME : NodeTypes.Content.NAME;
108             Node childNode = selectedFolderNode.addNode(uniqueValidatedName, primaryNodeTypeName);
109             childNode.setProperty(NodeTypes.Created.NAME, Calendar.getInstance());
110 
111             if (!isFolder) {
112                 childNode.setProperty(JcrResourceOrigin.TEXT_PROPERTY, "");
113             }
114             selectedFolderNode.getSession().save();
115 
116             return childNode;
117         } catch (RepositoryException e) {
118             throw new RuntimeRepositoryException(e);
119         }
120     }
121 
122     private String getUniqueResourceName(Node parent, String name) {
123         String uniqueName = Exceptions.wrap().get(() -> nodeNameHelper.getUniqueName(parent, name));
124         return nodeNameHelper.getValidatedName(uniqueName);
125     }
126 }