View Javadoc
1   /**
2    * This file Copyright (c) 2018-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.ui.contentapp.action;
35  
36  import info.magnolia.jcr.util.NodeNameHelper;
37  import info.magnolia.jcr.util.NodeUtil;
38  import info.magnolia.objectfactory.Components;
39  import info.magnolia.ui.ValueContext;
40  import info.magnolia.ui.datasource.DatasourceDefinition;
41  import info.magnolia.ui.datasource.jcr.JcrDatasourceDefinition;
42  
43  import java.util.Optional;
44  import java.util.regex.Matcher;
45  import java.util.regex.Pattern;
46  
47  import javax.inject.Inject;
48  import javax.jcr.Node;
49  import javax.jcr.RepositoryException;
50  import javax.jcr.Session;
51  
52  import org.apache.commons.lang3.StringUtils;
53  
54  /**
55   * Action that creates a new node by duplicating an existing one.
56   *
57   * @see DuplicateNodeActionDefinition
58   */
59  public class DuplicateNodeAction extends AbstractJcrAction<Node, DuplicateNodeActionDefinition> {
60  
61      private final JcrDatasourceDefinition jcrDatasourceDefinition;
62  
63      @Inject
64      public DuplicateNodeAction(DuplicateNodeActionDefinition definition, ValueContext<Node> valueContext, NodeNameHelper nodeNameHelper, JcrDatasourceDefinition jcrDatasourceDefinition) {
65          super(definition, valueContext, nodeNameHelper);
66          this.jcrDatasourceDefinition = jcrDatasourceDefinition;
67      }
68  
69      /**
70       * @deprecated since 6.2.4, use {@linkplain #DuplicateNodeAction(DuplicateNodeActionDefinition, ValueContext, NodeNameHelper, JcrDatasourceDefinition)} instead.
71       */
72      @Deprecated
73      public DuplicateNodeAction(DuplicateNodeActionDefinition definition, ValueContext<Node> valueContext, NodeNameHelper nodeNameHelper) {
74          this(definition, valueContext, nodeNameHelper, (JcrDatasourceDefinition) Components.getComponent(DatasourceDefinition.class));
75      }
76  
77      @Override
78      public Node process(Node node) throws RepositoryException {
79          Node parentNode = node.getParent();
80  
81          // Generate name and path of the new node
82          String newPath = NodeUtil.getAbsolutePath(parentNode.getPath(), getUniqueName(node));
83  
84          // Duplicate node
85          Session session = node.getSession();
86          session.getWorkspace().copy(node.getPath(), newPath);
87          Node duplicatedNode = session.getNode(newPath);
88  
89          updateAllNameProperties(duplicatedNode);
90  
91          NodeUtil.orderAfter(duplicatedNode, node.getName());
92          session.save();
93  
94          return duplicatedNode;
95      }
96  
97      protected String getUniqueName(Node node) throws RepositoryException {
98          return getNodeNameHelper().getUniqueName(node.getSession(), node.getParent().getPath(), node.getName());
99      }
100 
101     protected void updateAllNameProperties(Node node) throws RepositoryException {
102         updateProperty(node, Optional.ofNullable(jcrDatasourceDefinition.getNodeNameProperty()).orElse("jcrName"), node.getName());
103     }
104 
105     protected Matcher getRegexMatcher(String property) throws RepositoryException {
106         Pattern digitSuffixPattern = Pattern.compile("(?<name>.*[^0-9])?(?<suffix>[0-9]+)$");
107         return digitSuffixPattern.matcher(property);
108     }
109 
110     protected void updateProperty(Node node, String property, String nodeName) throws RepositoryException {
111         if (node.hasProperty(property)) {
112             String propertyValue = node.getProperty(property).getString();
113 
114             Matcher nodeNameSuffixMatch = getRegexMatcher(nodeName);
115             Matcher jcrNameSuffixMatch = getRegexMatcher(propertyValue);
116 
117             if (nodeNameSuffixMatch.find()) {
118                 if (jcrNameSuffixMatch.find()) {
119                     propertyValue = StringUtils.defaultString(jcrNameSuffixMatch.group("name"));
120                 }
121                 propertyValue += StringUtils.defaultString(nodeNameSuffixMatch.group("suffix"));
122             }
123 
124             node.setProperty(property, propertyValue);
125         }
126     }
127 }