View Javadoc
1   /**
2    * This file Copyright (c) 2013-2015 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 org.apache.commons.lang3.StringUtils;
45  
46  import com.vaadin.data.Item;
47  import com.vaadin.data.Property;
48  import com.vaadin.data.util.PropertysetItem;
49  
50  /**
51   * 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>
52   * Storage strategy: <br>
53   * - getValue(): <br>
54   * -- iterate the fieldsName property and retrieve all stored property.<br>
55   * -- Fulfill the {@link PropertysetItem}.<br>
56   * - setValue(): <br>
57   * -- iterate the incoming {@link PropertysetItem}.<br>
58   * -- if the related parent item do not contain this property, add it.<br>
59   */
60  public class CompositeTransformer extends BasicTransformer<PropertysetItem> {
61  
62      protected List<String> fieldsName;
63      protected String propertyPrefix;
64  
65      /**
66       * @deprecated since 5.4.2 - use {@link #CompositeTransformer(Item, ConfiguredFieldDefinition, Class, List, I18NAuthoringSupport)} instead.
67       */
68      @Deprecated
69      public CompositeTransformer(Item relatedFormItem, ConfiguredFieldDefinition definition, Class<PropertysetItem> type, List<String> fieldsName) {
70          this(relatedFormItem, definition, type, fieldsName, Components.getComponent(I18NAuthoringSupport.class));
71      }
72  
73      public CompositeTransformer(Item relatedFormItem, ConfiguredFieldDefinition definition, Class<PropertysetItem> type, List<String> fieldsName, I18NAuthoringSupport i18NAuthoringSupport) {
74          super(relatedFormItem, definition, type, i18NAuthoringSupport);
75          this.fieldsName = fieldsName;
76          this.propertyPrefix = createPropertyPrefix(definition);
77      }
78  
79      /**
80       * @return propertyPrefix used to prefix the property name.
81       */
82      protected String createPropertyPrefix(ConfiguredFieldDefinition definition) {
83          return definition.getName();
84      }
85  
86      @Override
87      public void writeToItem(PropertysetItem newValues) {
88          // Get iterator.
89          Iterator<?> propertyNames = newValues.getItemPropertyIds().iterator();
90  
91          while (propertyNames.hasNext()) {
92              String propertyName = (String) propertyNames.next();
93              String compositePropertyName = getCompositePropertyName(propertyName);
94              Property<?> property = relatedFormItem.getItemProperty(compositePropertyName);
95              if (property == null && newValues.getItemProperty(propertyName) != null) {
96                  relatedFormItem.addItemProperty(compositePropertyName, newValues.getItemProperty(propertyName));
97              }
98          }
99      }
100 
101     @Override
102     public PropertysetItem readFromItem() {
103         PropertysetItem newValues = new PropertysetItem();
104         for (String propertyName : fieldsName) {
105             String compositePropertyName = getCompositePropertyName(propertyName);
106             if (relatedFormItem.getItemProperty(compositePropertyName) != null) {
107                 newValues.addItemProperty(propertyName, relatedFormItem.getItemProperty(compositePropertyName));
108             }
109         }
110         return newValues;
111     }
112 
113     protected String getCompositePropertyName(String propertyName) {
114         propertyName = propertyPrefix + propertyName;
115         if (hasI18NSupport()) {
116             propertyName = propertyName + StringUtils.difference(basePropertyName, i18NPropertyName);
117         }
118         return propertyName;
119     }
120 }