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.ui.editor;
35  
36  import static info.magnolia.jcr.util.NodeUtil.getName;
37  import static java.util.stream.Collectors.toList;
38  
39  import info.magnolia.config.NamedDefinition;
40  import info.magnolia.jcr.predicate.AbstractPredicate;
41  import info.magnolia.jcr.util.NodeUtil;
42  import info.magnolia.objectfactory.ComponentProvider;
43  import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
44  import info.magnolia.ui.field.ConfiguredComplexPropertyDefinition;
45  import info.magnolia.ui.field.NoopNameDecorator;
46  import info.magnolia.ui.field.WithPropertyNameDecorator;
47  
48  import java.util.List;
49  import java.util.Locale;
50  import java.util.Optional;
51  import java.util.concurrent.CompletableFuture;
52  import java.util.stream.Stream;
53  import java.util.stream.StreamSupport;
54  
55  import javax.inject.Inject;
56  import javax.jcr.Node;
57  
58  import com.machinezoo.noexception.Exceptions;
59  
60  import lombok.AllArgsConstructor;
61  import lombok.EqualsAndHashCode;
62  import lombok.Getter;
63  import lombok.Setter;
64  
65  /**
66   * Resolves multi-field entries based on indexed sub-nodes.
67   */
68  public abstract class ByIndexedChildNodes implements MultiFormView.EntryResolution<Node> {
69  
70      private static final String INCREMENT_REGEX = "(\\d{1,3})";
71  
72      private final I18NAuthoringSupport<Node> i18NAuthoringSupport;
73  
74      private final Locale locale;
75  
76      protected final info.magnolia.ui.editor.ByIndexedChildNodes.Definition definition;
77  
78      protected final NamedDefinition propertyDefinition;
79  
80      private final WithPropertyNameDecorator.PropertyNameDecorator propertyNameDecorator;
81  
82      @Inject
83      public ByIndexedChildNodes(info.magnolia.ui.editor.ByIndexedChildNodes.Definition definition, NamedDefinition relatedPropertyDefinition, I18NAuthoringSupport<Node> i18NAuthoringSupport, ComponentProvider componentProvider, Locale locale) {
84          this.definition = definition;
85          this.propertyDefinition = relatedPropertyDefinition;
86          this.i18NAuthoringSupport = i18NAuthoringSupport;
87          this.locale = locale;
88          this.propertyNameDecorator = componentProvider.newInstance(definition.propertyNameDecorator);
89      }
90  
91      /**
92       * @deprecated since 6.2.1, use {@link #ByIndexedChildNodes(Definition, NamedDefinition, I18NAuthoringSupport, ComponentProvider, Locale)} instead.
93       */
94      @Deprecated
95      public ByIndexedChildNodes(NamedDefinition relatedPropertyDefinition, I18NAuthoringSupport<Node> i18NAuthoringSupport, ComponentProvider componentProvider, Locale locale) {
96          this(new info.magnolia.ui.editor.ByIndexedChildNodes.Definition(), relatedPropertyDefinition, i18NAuthoringSupport, componentProvider, locale);
97      }
98  
99      @Override
100     public Stream<ComplexPropertyDefinition<Node>> resolveForRoot(Node rootNode) {
101         List<Node> childItems = getChildNodes(rootNode, getEntryPropertyNameBase()).collect(toList());
102         Stream.Builder<ComplexPropertyDefinition<Node>> collector = Stream.builder();
103         for (int idx = 0; idx < childItems.size(); ++idx) {
104             resolvePropertyDefinition(childItems.get(idx), idx).ifPresent(collector::add);
105         }
106         return collector.build();
107     }
108 
109     protected String getEntryPropertyNameBase() {
110         return this.propertyNameDecorator.apply(propertyDefinition.getName());
111     }
112 
113     @Override
114     public CompletableFuture<ComplexPropertyDefinition<Node>> pick() {
115         return CompletableFuture.completedFuture(resolvePropertyDefinition(null, -1).orElse(null));
116     }
117 
118     protected abstract Optional<EditorDefinition<Node>> resolveEntryEditorDefinition(Node item, int index);
119 
120     protected Optional<ConfiguredComplexPropertyDefinition<Node>> resolvePropertyDefinition(Node item, int index) {
121         return resolveEntryEditorDefinition(item, index)
122                 .map(entryEditorDefinition -> {
123                     ConfiguredComplexPropertyDefinition<Node> property = new ConfiguredComplexPropertyDefinition.Wrapper<>(entryEditorDefinition);
124                     ItemProviderDefinition<Node, Node> indexedItemProvider = resolveItemProvider(item, index);
125                     property.setItemProvider(indexedItemProvider);
126                     return property;
127                 });
128     }
129 
130     /**
131      * @deprecated since 6.2.1
132      */
133     @Deprecated
134     protected JcrIndexedChildNodeProvider.Definition resolveItemProvider(Node node, int index) {
135         JcrIndexedChildNodeProvider.Definition indexedItemProvider = new JcrIndexedChildNodeProvider.Definition();
136 
137         indexedItemProvider.setIndex(index);
138         indexedItemProvider.setNodeName(getEntryPropertyNameBase());
139 
140         return indexedItemProvider;
141     }
142 
143     protected Stream<Node> getChildNodes(Node node, String name) {
144         Iterable<Node> nodes = Exceptions.wrap().get(() ->
145                 NodeUtil.getNodes(node, new info.magnolia.ui.editor.ByIndexedChildNodes.IndexedChildNodeNameRegexPredicate(name)));
146 
147         return StreamSupport.stream(nodes.spliterator(), false);
148     }
149 
150     private String deriveLocaleAwareName(String name, Locale locale) {
151         return i18NAuthoringSupport.deriveLocalisedPropertyName(name, locale);
152     }
153 
154     @AllArgsConstructor
155     private class IndexedChildNodeNameRegexPredicate extends AbstractPredicate<Node> {
156 
157         private final String baseName;
158 
159         private String getIndexedChildNodeRegexp(String name) {
160             return name + INCREMENT_REGEX;
161         }
162 
163         @Override
164         public boolean evaluateTyped(Node node) {
165             String childNodeNameRegex = getIndexedChildNodeRegexp(baseName);
166             return getName(node).matches(localiseChildName(childNodeNameRegex, node));
167         }
168 
169         private String localiseChildName(String targetChildNodeName, Node node) {
170             if (locale != null &&
171                     i18NAuthoringSupport.getDefaultLocale(node) != null &&
172                     !i18NAuthoringSupport.isDefaultLocale(locale, node)) {
173                 targetChildNodeName = deriveLocaleAwareName(targetChildNodeName, locale);
174             }
175             return targetChildNodeName;
176         }
177     }
178 
179     /**
180      * Definition of {@link ByIndexedChildNodes}.
181      */
182     @Getter
183     @Setter
184     @EqualsAndHashCode(callSuper = true)
185     public static class Definition extends MultiFormView.EntryResolution.Definition<Node> implements WithPropertyNameDecorator {
186 
187         private Class<? extends PropertyNameDecorator> propertyNameDecorator = NoopNameDecorator.class;
188 
189         public Definition() {
190             setImplementationClass(ByIndexedChildNodes.class);
191         }
192 
193         @Override
194         public Class<? extends PropertyNameDecorator> getPropertyNameDecorator() {
195             return propertyNameDecorator;
196         }
197 
198         public void setPropertyNameDecorator(Class<? extends PropertyNameDecorator> propertyNameDecoratorClass) {
199             this.propertyNameDecorator = propertyNameDecoratorClass;
200         }
201     }
202 }