View Javadoc
1   /**
2    * This file Copyright (c) 2013-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.form.field.transformer.multi;
35  
36  import info.magnolia.annotation.deprecation.MgnlDeprecated;
37  
38  import info.magnolia.jcr.util.NodeTypes;
39  import info.magnolia.jcr.util.NodeUtil;
40  import info.magnolia.jcr.wrapper.JCRMgnlPropertiesFilteringNodeWrapper;
41  import info.magnolia.objectfactory.Components;
42  import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
43  import info.magnolia.ui.editor.MultiValueAsMultipleProperties;
44  import info.magnolia.ui.form.field.definition.ConfiguredFieldDefinition;
45  import info.magnolia.ui.form.field.transformer.basic.BasicTransformer;
46  import info.magnolia.ui.vaadin.integration.jcr.JcrNewNodeAdapter;
47  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
48  
49  import java.util.ArrayList;
50  import java.util.Collections;
51  import java.util.Comparator;
52  import java.util.Iterator;
53  import java.util.List;
54  
55  import javax.inject.Inject;
56  import javax.jcr.Node;
57  import javax.jcr.PropertyIterator;
58  import javax.jcr.RepositoryException;
59  
60  import org.slf4j.Logger;
61  import org.slf4j.LoggerFactory;
62  
63  import com.google.common.collect.ImmutableList;
64  import com.vaadin.v7.data.Item;
65  import com.vaadin.v7.data.util.PropertysetItem;
66  
67  /**
68   * Sub Nodes implementation of {@link info.magnolia.ui.form.field.transformer.Transformer} storing and retrieving properties (as {@link PropertysetItem}) displayed in MultiField.<br>
69   * Storage strategy: <br>
70   * - root node (relatedFormItem)<br>
71   * -- child node (node name is the name of the related property)<br>
72   * --- property1 (store the first value of the MultiField)<br>
73   * --- property2 (store the second value of the MultiField)<br>
74   * --- ...<br>
75   * @deprecated since 6.2.3. Use new framework and {@link MultiValueAsMultipleProperties} instead.
76   */
77  @Deprecated
78  @MgnlDeprecated(since = "6.2.3.", description = "Use new framework and MultiValueAsMultipleProperties instead.")
79  public class MultiValueChildNodeTransformer extends BasicTransformer<PropertysetItem> {
80  
81      private static final Logger log = LoggerFactory.getLogger(MultiValueChildNodeTransformer.class);
82  
83      private String childNodeType = NodeTypes.ContentNode.NAME;
84  
85      /**
86       * @deprecated since 5.4.2 - use {@link #MultiValueChildNodeTransformer(Item, ConfiguredFieldDefinition, Class)} instead.
87       */
88      @Deprecated
89      public MultiValueChildNodeTransformer(Item relatedFormItem, ConfiguredFieldDefinition definition, Class<PropertysetItem> type) {
90          this(relatedFormItem, definition, type, Components.getComponent(I18NAuthoringSupport.class));
91      }
92  
93      @Inject
94      public MultiValueChildNodeTransformer(Item relatedFormItem, ConfiguredFieldDefinition definition, Class<PropertysetItem> type, I18NAuthoringSupport i18NAuthoringSupport) {
95          super(relatedFormItem, definition, type, i18NAuthoringSupport);
96      }
97  
98      @Override
99      public PropertysetItem readFromItem() {
100         // i18n support
101         String childNodeName = definePropertyName();
102         PropertysetItem newValues = new PropertysetItem();
103         // Get the child node containing the list of properties.
104         try {
105             JcrNodeAdaptergnolia/ui/vaadin/integration/jcr/JcrNodeAdapter.html#JcrNodeAdapter">JcrNodeAdapter child = getOrCreateChildItem((JcrNodeAdapter) relatedFormItem, childNodeName);
106             // Populate
107             if (!(child instanceof JcrNewNodeAdapter)) {
108                 int pos = 0;
109                 // Sort id in a natural order.
110                 List<Object> ids = new ArrayList<Object>(child.getItemPropertyIds());
111                 Collections.sort(ids, new Comparator<Object>() {
112                     @Override
113                     public int compare(Object o1, Object o2) {
114                         int int1 = Integer.valueOf(((String) o1));
115                         int int2 = Integer.valueOf(((String) o2));
116                         return int1 - int2;
117                     }
118                 });
119 
120                 for (Object id : ids) {
121                     newValues.addItemProperty(pos, child.getItemProperty(id));
122                     pos += 1;
123                 }
124             }
125         } catch (RepositoryException re) {
126             log.warn("Not able to access the child node of '{}'", ((JcrNodeAdapter) relatedFormItem).getNodeName());
127         }
128         return newValues;
129     }
130 
131     @Override
132     public void writeToItem(PropertysetItem newValue) {
133         // i18n support
134         String childNodeName = definePropertyName();
135         try {
136             // get the child item
137             JcrNodeAdaptergnolia/ui/vaadin/integration/jcr/JcrNodeAdapter.html#JcrNodeAdapter">JcrNodeAdapter child = getOrCreateChildItem((JcrNodeAdapter) relatedFormItem, childNodeName);
138             // Remove all old properties
139 
140             ImmutableList<Object> propertyIds = ImmutableList.copyOf(child.getItemPropertyIds());
141             for (Object id : propertyIds) {
142                 if (newValue.getItemProperty(Integer.valueOf((String) id)) == null) {
143                     child.removeItemProperty(id);
144                 }
145             }
146             // add all the new properties
147             if (newValue != null) {
148                 Iterator<?> it = newValue.getItemPropertyIds().iterator();
149                 while (it.hasNext()) {
150                     Object id = it.next();
151                     child.addItemProperty(id.toString(), newValue.getItemProperty(id));
152                 }
153             }
154         } catch (RepositoryException re) {
155             log.warn("Not able to access the child node of '{}'", NodeUtil.getName(((JcrNodeAdapter) relatedFormItem).getJcrItem()));
156         }
157     }
158 
159     /**
160      * Return the child item containing the properties (displayed in the multiField).
161      */
162     private JcrNodeAdapter./info/magnolia/ui/vaadin/integration/jcr/JcrNodeAdapter.html#JcrNodeAdapter">JcrNodeAdapter getOrCreateChildItem(JcrNodeAdapter parent, String childNodeName) throws RepositoryException {
163 
164         JcrNodeAdapter child = null;
165         Node rootNode = parent.getJcrItem();
166         if (rootNode.hasNode(childNodeName)) {
167             child = new JcrNodeAdapter(rootNode.getNode(childNodeName));
168             Node childNode = new JCRMgnlPropertiesFilteringNodeWrapper(rootNode.getNode(childNodeName));
169             PropertyIterator iterator = childNode.getProperties();
170             while (iterator.hasNext()) {
171                 // Make sure we populate the adapter with existing JCR properties.
172                 child.getItemProperty(iterator.nextProperty().getName());
173             }
174         } else {
175             child = new JcrNewNodeAdapter(rootNode, childNodeType, childNodeName);
176         }
177         parent.addChild(child);
178         return child;
179     }
180 
181 
182 
183 }