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.field;
35  
36  import info.magnolia.ui.editor.ByIndexedChildNodes;
37  import info.magnolia.ui.editor.CurrentItemProviderDefinition;
38  import info.magnolia.ui.editor.DefaultJcrNodeOrderHandler;
39  import info.magnolia.ui.editor.ItemProviderDefinition;
40  import info.magnolia.ui.editor.JcrChildNodeProviderDefinition;
41  import info.magnolia.ui.editor.MultiFieldEntryResolution;
42  import info.magnolia.ui.editor.MultiFormView;
43  
44  import javax.jcr.Node;
45  
46  import org.slf4j.Logger;
47  import org.slf4j.LoggerFactory;
48  
49  /**
50   * Default pre-configured definition of JCR multi-fields.
51   */
52  @FieldType("jcrMultiField")
53  public class JcrMultiFieldDefinition extends MultiFieldDefinition<Node> {
54  
55      private static final Logger log = LoggerFactory.getLogger(JcrMultiFieldDefinition.class);
56  
57      public JcrMultiFieldDefinition() {
58          setEntryResolution(new MultiFieldEntryResolution.Definition());
59          setOrderHandler(new DefaultJcrNodeOrderHandler.Definition());
60          setItemProvider(new JcrChildNodeProviderDefinition());
61          setCanRemoveItems(true);
62      }
63  
64      /**
65       * <pre>
66       * This is to ensure backwards compatibility for two cases:
67       * - when a user already configured multi field with {@link info.magnolia.ui.editor.CurrentItemProvider}, but uses
68       * {@link ByIndexedChildNodes.Definition} the resolving of nodes will be set {@link
69       * ByIndexedChildNodes.Definition#setStrict(boolean)} to false, because otherwise other non-multi related nodes
70       * would be resolved as multis, resulting in errors and possibly overwritten non-multi related data.
71       * - when {@link JcrChildNodeProviderDefinition#isSupportI18N()} is false, because that configuration flattens
72       * localized multi parent nodes into a single parent and then you have to resolve child nodes respecting their
73       * locale suffix, which wouldn't happen when strict is false (it would resolve everything beneath the parent for any
74       * locale. This is to support compatibility of non-ported transformer classes like
75       * MultiValueSubChildrenNodePropertiesTransformer and DelegatingMultiValueSubnodeTransformer.
76       * </pre>
77       */
78      @Deprecated
79      private void safeguardAgainstIncompatibleCombination(ItemProviderDefinition itemProvider, MultiFormView.EntryResolution.Definition<Node> entryResolution) {
80          if (entryResolution instanceof ByIndexedChildNodes.Definition) {
81              ByIndexedChildNodes.Definition def = (ByIndexedChildNodes.Definition) entryResolution;
82              if (itemProvider instanceof JcrChildNodeProviderDefinition) {
83                  if (((JcrChildNodeProviderDefinition) itemProvider).isSupportI18N()) {
84                      if (def.isStrict()) {
85                          def.setStrict(false);
86                          throw new IllegalStateException("Falling back to non-strict entry resolution. All nodes in its localized parent are resolved, regardless of their node names. Set strictness resolution to false to remove this problem.");
87                      }
88                  } else {
89                      if (!def.isStrict()) {
90                          def.setStrict(true);
91                          throw new IllegalStateException("Falling back to strict mode entry resolution, to be able to distinguish multi field entries by locale. Reason: supporti18n is set to false which means entries of different locales share parent. Set entry resolution to strict manually to remove this problem.");
92                      }
93                  }
94              } else if (itemProvider instanceof CurrentItemProviderDefinition) {
95                  if (!def.isStrict()) {
96                      def.setStrict(true);
97                      throw new IllegalStateException("Falling back to strict mode entry resolution, to be able to " +
98                              "distinguish multi field entries. Reason: CurrentItemProvider is used in multi which means " +
99                              "entries are stored without a parent adjacent to nodes that are not related to this multi " +
100                             "field. Non-strict resolution could result in overwriting of non-multi field related nodes." +
101                             "Set entry resolution definition to strict manually, to remove this problem.");
102                 }
103             }
104         }
105     }
106 
107     public void init() {
108         safeguardAgainstIncompatibleCombination(getItemProvider(), getEntryResolution());
109     }
110 }