View Javadoc
1   /**
2    * This file Copyright (c) 2018 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.databinding;
35  
36  import info.magnolia.context.Context;
37  import info.magnolia.jcr.predicate.AbstractPredicate;
38  import info.magnolia.jcr.util.NodeUtil;
39  import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
40  import info.magnolia.ui.framework.databinding.ItemProviderStrategy;
41  import info.magnolia.ui.framework.databinding.MultiFieldOrderHandler;
42  import info.magnolia.ui.field.EditorPropertyDefinition;
43  
44  import java.util.Collection;
45  import java.util.List;
46  import java.util.Locale;
47  import java.util.Optional;
48  import java.util.stream.Stream;
49  import java.util.stream.StreamSupport;
50  
51  import javax.inject.Inject;
52  import javax.inject.Provider;
53  import javax.jcr.Node;
54  import javax.jcr.RepositoryException;
55  
56  import org.apache.commons.lang3.RandomStringUtils;
57  import org.slf4j.Logger;
58  import org.slf4j.LoggerFactory;
59  
60  import lombok.AllArgsConstructor;
61  import lombok.SneakyThrows;
62  
63  /**
64   * JCR implementation of the {@link MultiFieldOrderHandler}.
65   */
66  public class JcrChildNodeOrderHandler implements MultiFieldOrderHandler<Node> {
67  
68      private static final Logger log = LoggerFactory.getLogger(JcrChildNodeOrderHandler.class);
69  
70      private static final String INCREMENT_REGEX = "(\\d{1,3})";
71  
72      private final JcrChildNodeOrderHandlerDefinition definition;
73  
74      private final EditorPropertyDefinition propertyDefinition;
75  
76      private final I18NAuthoringSupport i18NAuthoringSupport;
77  
78      private final Provider<Context> contextProvider;
79  
80      @Inject
81      public JcrChildNodeOrderHandler(JcrChildNodeOrderHandlerDefinition definition, EditorPropertyDefinition propertyDefinition, I18NAuthoringSupport i18NAuthoringSupport, Provider<Context> contextProvider) {
82          this.definition = definition;
83          this.propertyDefinition = propertyDefinition;
84          this.i18NAuthoringSupport = i18NAuthoringSupport;
85          this.contextProvider = contextProvider;
86      }
87  
88      @Override
89      public Stream<ItemProviderStrategy<Node>> readItems(Node parent, Locale locale) {
90          return getChildNodes(parent, propertyDefinition.getName(), locale).map(node -> new AlwaysFetchJcrNodeFromNewSession(contextProvider, node));
91      }
92  
93      @Override
94      @SneakyThrows(RepositoryException.class)
95      public Node getOrCreate(Node parent, String name, Locale locale) {
96          return NodeUtil.createPath(parent, deriveLocaleAwareName(name, locale), definition.getNodeType());
97      }
98  
99      @Override
100     public void removeItems(Collection<Node> items) {
101         items.forEach(node -> {
102             try {
103                 node.remove();
104             } catch (RepositoryException e) {
105                 log.warn("Failed to remove node: {}", e.getMessage());
106             }
107         });
108     }
109 
110     @Override
111     @SneakyThrows(RepositoryException.class)
112     public void applyOrder(Node parent, List<Node> itemOrder, Locale locale) {
113         for (int idx = 0; idx < itemOrder.size() - 1; idx++) {
114             NodeUtil.orderAfter(itemOrder.get(idx + 1), itemOrder.get(idx).getName());
115         }
116 
117         String baseName = propertyDefinition.getName();
118 
119         for (Node node : itemOrder) {
120             String nodeName = deriveLocaleAwareName(baseName + itemOrder.indexOf(node), locale);
121             if (parent.hasNode(nodeName)) {
122                 NodeUtil.renameNode(parent.getNode(nodeName), RandomStringUtils.randomAlphanumeric(10));
123             }
124             NodeUtil.renameNode(node, nodeName);
125         }
126     }
127 
128     /**
129      * @return The regex used to filter child items
130      */
131     private String childItemRegexRepresentation(String name) {
132         return name + INCREMENT_REGEX;
133     }
134 
135     private String deriveLocaleAwareName(String name, Locale locale) {
136         if (i18NAuthoringSupport != null && locale != null && !i18NAuthoringSupport.isDefaultLocale(locale)) {
137             return i18NAuthoringSupport.deriveLocalisedPropertyName(name, locale);
138         }
139         return name;
140     }
141 
142     @SneakyThrows(RepositoryException.class)
143     private Stream<Node> getChildNodes(Node node, String name, Locale locale) {
144         return StreamSupport.stream(NodeUtil.getNodes(node, new NodeNameRegexPredicate(name, locale)).spliterator(), false);
145     }
146 
147     @AllArgsConstructor
148     private class NodeNameRegexPredicate extends AbstractPredicate<Node> {
149 
150         private final String baseName;
151 
152         private final Locale locale;
153 
154         @Override
155         @SneakyThrows(RepositoryException.class)
156         public boolean evaluateTyped(Node node) {
157             return node.getName().matches(deriveLocaleAwareName(childItemRegexRepresentation(baseName), locale));
158         }
159     }
160 
161     private class AlwaysFetchJcrNodeFromNewSession implements ItemProviderStrategy<Node> {
162 
163         private final Provider<Context> contextProvider;
164         private final String workspace;
165         private final String identifier;
166 
167         @SneakyThrows(RepositoryException.class)
168         AlwaysFetchJcrNodeFromNewSession(Provider<Context> contextProvider, Node node) {
169             this.contextProvider = contextProvider;
170             workspace = node.getSession().getWorkspace().getName();
171             identifier = node.getIdentifier();
172         }
173 
174         @Override
175         @SneakyThrows(RepositoryException.class)
176         public Optional<Node> read() {
177             return Optional.of(contextProvider.get().getJCRSession(workspace).getNodeByIdentifier(identifier));
178         }
179     }
180 }