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.basic;
35  
36  import info.magnolia.ui.form.field.definition.ConfiguredFieldDefinition;
37  import info.magnolia.ui.form.field.transformer.Transformer;
38  import info.magnolia.ui.form.field.transformer.UndefinedPropertyType;
39  import info.magnolia.ui.vaadin.integration.jcr.DefaultProperty;
40  import info.magnolia.ui.vaadin.integration.jcr.DefaultPropertyUtil;
41  
42  import java.util.Locale;
43  
44  import javax.inject.Inject;
45  
46  import org.apache.commons.lang3.StringUtils;
47  
48  import com.vaadin.data.Item;
49  import com.vaadin.data.Property;
50  
51  /**
52   * Basic implementation of a {@link Transformer}.<br>
53   * This transformer is used for most of the basic fields (textBox, Date, ...).<br>
54   * His responsibility is to: <br>
55   * - retrieve or create a basic property from the related item <br>
56   * - update the item property value in case of changes performed on the related field.
57   *
58   * @param <T>
59   */
60  public class BasicTransformer<T> implements Transformer<T> {
61  
62      protected Item relatedFormItem;
63      protected final ConfiguredFieldDefinition definition;
64  
65      protected String basePropertyName;
66      protected String i18NPropertyName;
67      private Locale locale;
68      protected Class<T> type;
69  
70      @Inject
71      public BasicTransformer(Item relatedFormItem, ConfiguredFieldDefinition definition, Class<T> type) {
72          this.definition = definition;
73          this.relatedFormItem = relatedFormItem;
74          this.basePropertyName = definition.getName();
75          if (hasI18NSupport()) {
76              this.i18NPropertyName = this.basePropertyName;
77          }
78          setType(type);
79      }
80  
81      public Item getRelatedFormItem() {
82          return relatedFormItem;
83      }
84  
85      @Override
86      public void writeToItem(T newValue) {
87          Property<T> p = getOrCreateProperty(type);
88          p.setValue(newValue);
89      }
90  
91      @Override
92      public T readFromItem() {
93          Property<T> p = getOrCreateProperty(type);
94          if (definition.isReadOnly()) {
95              p.setReadOnly(true);
96          }
97          return p.getValue();
98      }
99  
100     /**
101      * If the value type is not initialize by the field factory ({@link UndefinedPropertyType}), check if the property already exist in the Item.<br>
102      * If the Item has already this property, return the property value type.<br>
103      * Else return the default type 'String'
104      */
105     protected void setType(Class<T> typeFromDefinition) {
106         if (typeFromDefinition.isAssignableFrom(UndefinedPropertyType.class)) {
107             String propertyName = definePropertyName();
108             Property<T> property = relatedFormItem.getItemProperty(propertyName);
109             if (property != null) {
110                 this.type = (Class<T>) property.getType();
111             } else {
112                 this.type = (Class<T>) String.class;
113             }
114         } else {
115             this.type = typeFromDefinition;
116         }
117     }
118 
119     /**
120      * If the desired property (propertyName) already exist in the JcrNodeAdapter, return this property<br>
121      * else create a new {@link Property}.<br>
122      * If the returned property is not of the desired type, cast this property to the proper type.
123      *
124      * @param <T>
125      */
126     protected <T> Property<T> getOrCreateProperty(Class<T> type) {
127         return getOrCreateProperty(type, true);
128     }
129 
130     /**
131      * If the desired property (propertyName) already exist in the JcrNodeAdapter, return this property<br>
132      * else create a new {@link Property}.<br>
133      * If 'checkTypes' is set to true and if the returned property is not of the desired type, cast this property to the proper type.
134      */
135     protected <T> Property<T> getOrCreateProperty(Class<T> type, boolean checkTypes) {
136         String propertyName = definePropertyName();
137         Property<T> property = relatedFormItem.getItemProperty(propertyName);
138 
139         if (property == null) {
140             property = new DefaultProperty<T>(type, null);
141             relatedFormItem.addItemProperty(propertyName, property);
142         } else if (checkTypes && !type.isAssignableFrom(property.getType())) {
143             // solve MGNLUI-2494
144             // as we have type inconsistency (type of the jcr value is diff. of the definition one), try to convert the jcr type to the type coming from the definition.
145             // get the value as String
146             String stringValue = ((property.getValue() != null && StringUtils.isNotBlank(property.getValue().toString()))
147                     ? property.getValue().toString()
148                     : null);
149             T value = null;
150             try {
151                 // Convert the String value to the desired type.
152                 value = (T) DefaultPropertyUtil.createTypedValue(type, stringValue);
153             } catch (Exception e) {
154                 // Ignore. In case of exception, set a null value.
155             }
156             property = new DefaultProperty<T>(type, value);
157             // This will replace the previous property (with the wrong type) with the new one (correctly typed).
158             relatedFormItem.addItemProperty(propertyName, property);
159         }
160 
161         return property;
162     }
163 
164 
165     /**
166      * Based on the i18n information, define the property name to use.
167      */
168     protected String definePropertyName() {
169         if (hasI18NSupport()) {
170             return this.i18NPropertyName;
171         }
172         return this.basePropertyName;
173     }
174 
175     // //////
176     // I18N support
177     // /////
178 
179     @Override
180     public void setLocale(Locale locale) {
181         this.locale = locale;
182     }
183 
184     @Override
185     public void setI18NPropertyName(String i18nPropertyName) {
186         this.i18NPropertyName = i18nPropertyName;
187     }
188 
189     @Override
190     public Locale getLocale() {
191         return this.locale;
192     }
193 
194     @Override
195     public String getBasePropertyName() {
196         return basePropertyName;
197     }
198 
199     @Override
200     public boolean hasI18NSupport() {
201         return definition.isI18n();
202     }
203 
204     @Override
205     public Class<T> getType() {
206         return type;
207     }
208 
209 }