View Javadoc
1   /**
2    * This file Copyright (c) 2019-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.editor;
35  
36  import info.magnolia.jcr.RuntimeRepositoryException;
37  import info.magnolia.jcr.util.NodeNameHelper;
38  import info.magnolia.jcr.util.NodeUtil;
39  import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
40  
41  import java.util.List;
42  import java.util.Locale;
43  import java.util.regex.Matcher;
44  import java.util.regex.Pattern;
45  
46  import javax.inject.Inject;
47  import javax.jcr.Item;
48  import javax.jcr.Node;
49  import javax.jcr.RepositoryException;
50  
51  import org.apache.commons.lang3.StringUtils;
52  import org.slf4j.Logger;
53  import org.slf4j.LoggerFactory;
54  
55  import com.google.common.collect.Lists;
56  
57  /**
58   * Arranges provided JCR nodes according to their order, renames them so that
59   * the node names reflect the indices correctly.
60   */
61  public class DefaultJcrNodeOrderHandler implements MultiFormDefinition.OrderHandler<Node> {
62  
63      private static final Logger log = LoggerFactory.getLogger(DefaultJcrNodeOrderHandler.class);
64  
65      private static final Pattern INDEX_PATTERN = Pattern.compile("(?<name>.*?)(?<index>\\d+)(?<i18n>_[a-z]{2})?");
66      private static final String TMP_NAME_PREFIX = "__TMP_NAME_PREFIX";
67  
68      private final I18NAuthoringSupport<Item> i18NAuthoringSupport;
69      private final NodeNameHelper nodeNameHelper;
70      private final Locale locale;
71  
72      @Inject
73      public DefaultJcrNodeOrderHandler(I18NAuthoringSupport<Item> i18NAuthoringSupport,
74                                        Locale locale,
75                                        NodeNameHelper nodeNameHelper) {
76          this.i18NAuthoringSupport = i18NAuthoringSupport;
77          this.locale = locale;
78          this.nodeNameHelper = nodeNameHelper;
79      }
80  
81      @Override
82      public void applyOrder(List<Node> nodeOrder) {
83          if (nodeOrder.isEmpty()) {
84              return;
85          }
86  
87          try {
88              Node parent = nodeOrder.get(0).getParent();
89  
90              for (int idx = 0; idx < nodeOrder.size(); ++idx) {
91                  Node node = nodeOrder.get(idx);
92                  Matcher indexedNameMatcher = INDEX_PATTERN.matcher(node.getName());
93                  if (indexedNameMatcher.matches()) {
94                      String name = createIndexedName(indexedNameMatcher.group("name"), idx);
95                      if (i18NAuthoringSupport.getDefaultLocale(node) != null && !i18NAuthoringSupport.isDefaultLocale(locale, node)) {
96                          name = i18NAuthoringSupport.deriveLocalisedPropertyName(name, locale);
97                      }
98  
99                      node.getSession().move(node.getPath(), getParentPathWithSlash(node) + TMP_NAME_PREFIX + name);
100                 } else {
101                     log.warn("Node name [{}] mentions no index, node is ignored by [{}]", node.getName(), DefaultJcrNodeOrderHandler.class.getName());
102                 }
103             }
104 
105             for (Node node : Lists.newArrayList(NodeUtil.getNodes(parent))) {
106                 String nodeName = node.getName();
107                 if (nodeName.startsWith(TMP_NAME_PREFIX)) {
108                     String parentPath = getParentPathWithSlash(node);
109                     node.getSession().move(
110                             parentPath + nodeName,
111                             parentPath + nodeName.substring(TMP_NAME_PREFIX.length()));
112                 }
113             }
114 
115         } catch (RepositoryException e) {
116             throw new RuntimeRepositoryException(e);
117         }
118     }
119 
120     protected String createIndexedName(String name, int idx) {
121         return name + idx;
122     }
123 
124     private String getParentPathWithSlash(Node node) throws RepositoryException {
125         return StringUtils.appendIfMissing(node.getParent().getPath(), "/");
126     }
127 
128     /**
129      * Definition of {@link DefaultJcrNodeOrderHandler}.
130      */
131     public static class Definition extends MultiFormDefinition.OrderHandlerDefinition<Node> {
132 
133         public Definition() {
134             setImplementationClass(DefaultJcrNodeOrderHandler.class);
135         }
136     }
137 }