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