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.composite;
35  
36  import info.magnolia.objectfactory.Components;
37  import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
38  import info.magnolia.ui.form.field.definition.ConfiguredFieldDefinition;
39  import info.magnolia.ui.form.field.transformer.basic.BasicTransformer;
40  
41  import java.util.Iterator;
42  import java.util.List;
43  
44  import com.vaadin.v7.data.Item;
45  import com.vaadin.v7.data.Property;
46  import com.vaadin.v7.data.util.PropertysetItem;
47  
48  /**
49   * Default composite field {@link info.magnolia.ui.form.field.transformer.Transformer} implementation storing and retrieving properties defined under an Item as {@link PropertysetItem} element.<br>
50   * Storage strategy: <br>
51   * - getValue(): <br>
52   * -- iterate the fieldsName property and retrieve all stored property.<br>
53   * -- Fulfill the {@link PropertysetItem}.<br>
54   * - setValue(): <br>
55   * -- iterate the incoming {@link PropertysetItem}.<br>
56   * -- if the related parent item do not contain this property, add it.<br>
57   */
58  public class CompositeTransformer extends BasicTransformer<PropertysetItem> {
59  
60      protected List<String> fieldsName;
61      protected String propertyPrefix;
62  
63      /**
64       * @deprecated since 5.4.2 - use {@link #CompositeTransformer(Item, ConfiguredFieldDefinition, Class, List, I18NAuthoringSupport)} instead.
65       */
66      @Deprecated
67      public CompositeTransformer(Item relatedFormItem, ConfiguredFieldDefinition definition, Class<PropertysetItem> type, List<String> fieldsName) {
68          this(relatedFormItem, definition, type, fieldsName, Components.getComponent(I18NAuthoringSupport.class));
69      }
70  
71      public CompositeTransformer(Item relatedFormItem, ConfiguredFieldDefinition definition, Class<PropertysetItem> type, List<String> fieldsName, I18NAuthoringSupport i18NAuthoringSupport) {
72          super(relatedFormItem, definition, type, i18NAuthoringSupport);
73          this.fieldsName = fieldsName;
74          this.propertyPrefix = createPropertyPrefix(definition);
75      }
76  
77      /**
78       * @return propertyPrefix used to prefix the property name.
79       */
80      protected String createPropertyPrefix(ConfiguredFieldDefinition definition) {
81          return definition.getName();
82      }
83  
84      @Override
85      public void writeToItem(PropertysetItem newValues) {
86          // Get iterator.
87          Iterator<?> propertyNames = newValues.getItemPropertyIds().iterator();
88  
89          while (propertyNames.hasNext()) {
90              String propertyName = (String) propertyNames.next();
91              String compositePropertyName = getCompositePropertyName(propertyName);
92              Property<?> property = relatedFormItem.getItemProperty(compositePropertyName);
93              if (property == null && newValues.getItemProperty(propertyName) != null) {
94                  relatedFormItem.addItemProperty(compositePropertyName, newValues.getItemProperty(propertyName));
95              }
96          }
97      }
98  
99      @Override
100     public PropertysetItem readFromItem() {
101         PropertysetItem newValues = new PropertysetItem();
102         for (String propertyName : fieldsName) {
103             String compositePropertyName = getCompositePropertyName(propertyName);
104             if (relatedFormItem.getItemProperty(compositePropertyName) != null) {
105                 newValues.addItemProperty(propertyName, relatedFormItem.getItemProperty(compositePropertyName));
106             }
107         }
108         return newValues;
109     }
110 
111     protected String getCompositePropertyName(String propertyName) {
112         propertyName = propertyPrefix + deriveLocaleAwareName(propertyName);
113         return propertyName;
114     }
115 }