View Javadoc
1   /**
2    * This file Copyright (c) 2021 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.ui.editor.JcrIndexedMultiPropertyValueProvider.TMP_REMOVED;
37  
38  import info.magnolia.jcr.util.NodeTypes;
39  import info.magnolia.jcr.util.NodeUtil;
40  import info.magnolia.jcr.wrapper.DelegatePropertyWrapper;
41  import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
42  import info.magnolia.ui.field.EditorPropertyDefinition;
43  
44  import java.util.ArrayList;
45  import java.util.List;
46  import java.util.Locale;
47  import java.util.Optional;
48  
49  import javax.jcr.Node;
50  import javax.jcr.Property;
51  import javax.jcr.RepositoryException;
52  import javax.jcr.Session;
53  import javax.jcr.Value;
54  
55  import com.machinezoo.noexception.Exceptions;
56  
57  /**
58   * Resolves multi-field entries based on single value properties.
59   *
60   * @deprecated since 6.2.7. This class is for compatibility use only to compensate compatibility UI
61   * {@link info.magnolia.ui.form.field.transformer.multi.MultiValueChildNodeTransformer} behaviour.
62   */
63  @Deprecated
64  public class MultiValueAsMultipleProperties implements ItemProviderStrategy<Property, Node> {
65  
66      private final EditorPropertyDefinition relatedFieldDefinition;
67      private final LocaleContext localeContext;
68      private final I18NAuthoringSupport<Node> i18NAuthoringSupport;
69  
70      public MultiValueAsMultipleProperties(LocaleContext localeContext, I18NAuthoringSupport<Node> i18NAuthoringSupport, EditorPropertyDefinition relatedFieldDefinition) {
71          this.relatedFieldDefinition = relatedFieldDefinition;
72          this.localeContext = localeContext;
73          this.i18NAuthoringSupport = i18NAuthoringSupport;
74      }
75  
76      @Override
77      public Optional<Property> read(Node reference) {
78          return read(reference, i18NAuthoringSupport.getDefaultLocale(reference));
79      }
80  
81      @Override
82      public Optional<Property> read(Node node, Locale locale) {
83          return Optional.of(node)
84                  .map(Exceptions.wrap().function(parent -> getMultiFieldNode(parent, locale)))
85                  .map(Wrapper::new);
86      }
87  
88      private Node getMultiFieldNode(Node node, Locale locale) throws RepositoryException {
89          return NodeUtil.createPath(node, getLocalisedParentNodeName(locale), NodeTypes.ContentNode.NAME);
90      }
91  
92      private String getLocalisedParentNodeName(Locale locale) {
93          String parentNodeName = relatedFieldDefinition.getName();
94          if (!localeContext.getDefault().equals(locale)) {
95              parentNodeName = i18NAuthoringSupport.deriveLocalisedPropertyName(parentNodeName, locale);
96          }
97          return parentNodeName;
98      }
99  
100     /**
101      * Definition of {@link MultiValueAsMultipleProperties}.
102      *
103      * deprecated since 6.2.7, use {@link JcrPropertyProvider.Definition} instead.
104      */
105     @Deprecated
106     public static class Definition extends JcrPropertyProvider.Definition {
107 
108         public Definition() {
109             setImplementationClass((Class) MultiValueAsMultipleProperties.class);
110         }
111     }
112 
113     private static class Wrapper extends DelegatePropertyWrapper {
114 
115         private final Node node;
116 
117         private Wrapper(Node node) {
118             super(null);
119             this.node = node;
120         }
121 
122         @Override
123         public Value[] getValues() throws RepositoryException {
124             int i = 0;
125             final List<Value> values = new ArrayList<>();
126             while (node.hasProperty(String.valueOf(i))) {
127                 values.add(node.getProperty(String.valueOf(i)).getValue());
128                 i++;
129             }
130             return values.toArray(new Value[] {});
131         }
132 
133         @Override
134         public void setValue(Value[] values) throws RepositoryException {
135             for (int i = 0; i < values.length; i++) {
136                 node.setProperty(String.valueOf(i), TMP_REMOVED.equals(values[i].getString()) ? null : values[i]);
137             }
138         }
139 
140         @Override
141         public Session getSession() throws RepositoryException {
142             return node.getSession();
143         }
144 
145         @Override
146         public boolean isNode() {
147             return false;
148         }
149     }
150 }