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