View Javadoc

1   /**
2    * This file Copyright (c) 2013-2014 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.lang.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      @Override
82      public void writeToItem(T newValue) {
83          Property<T> p = getOrCreateProperty(type);
84          p.setValue(newValue);
85      }
86  
87      @Override
88      public T readFromItem() {
89          Property<T> p = getOrCreateProperty(type);
90          if (definition.isReadOnly()) {
91              p.setReadOnly(true);
92          }
93          return p.getValue();
94      }
95  
96      /**
97       * If the value type is not initialize by the field factory ({@link UndefinedPropertyType}), check if the property already exist in the Item.<br>
98       * If the Item has already this property, return the property value type.<br>
99       * Else return the default type 'String'
100      */
101     protected void setType(Class<T> typeFromDefinition) {
102         if (typeFromDefinition.isAssignableFrom(UndefinedPropertyType.class)) {
103             String propertyName = definePropertyName();
104             Property<T> property = relatedFormItem.getItemProperty(propertyName);
105             if (property != null) {
106                 this.type = (Class<T>) property.getType();
107             } else {
108                 this.type = (Class<T>) String.class;
109             }
110         } else {
111             this.type = typeFromDefinition;
112         }
113     }
114 
115     /**
116      * If the desired property (propertyName) already exist in the JcrNodeAdapter, return this property<br>
117      * else create a new {@link Property}.<br>
118      * If the returned property is not of the desired type, cast this property to the proper type.
119      * 
120      * @param <T>
121      */
122     protected <T> Property<T> getOrCreateProperty(Class<T> type) {
123         return getOrCreateProperty(type, true);
124     }
125 
126     /**
127      * If the desired property (propertyName) already exist in the JcrNodeAdapter, return this property<br>
128      * else create a new {@link Property}.<br>
129      * If 'checkTypes' is set to true and if the returned property is not of the desired type, cast this property to the proper type.
130      */
131     protected <T> Property<T> getOrCreateProperty(Class<T> type, boolean checkTypes) {
132         String propertyName = definePropertyName();
133         Property<T> property = relatedFormItem.getItemProperty(propertyName);
134 
135         if (property == null) {
136             property = new DefaultProperty<T>(type, null);
137             relatedFormItem.addItemProperty(propertyName, property);
138         } else if (checkTypes && !type.isAssignableFrom(property.getType())) {
139             // solve MGNLUI-2494
140             // 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.
141             // get the value as String
142             String stringValue = ((property.getValue() != null && StringUtils.isNotBlank(property.getValue().toString()))
143                     ? property.getValue().toString()
144                     : null);
145             T value = null;
146             try {
147                 // Convert the String value to the desired type.
148                 value = (T) DefaultPropertyUtil.createTypedValue(type, stringValue);
149             } catch (Exception e) {
150                 // Ignore. In case of exception, set a null value.
151             }
152             property = new DefaultProperty<T>(type, value);
153             // This will replace the previous property (with the wrong type) with the new one (correctly typed).
154             relatedFormItem.addItemProperty(propertyName, property);
155         }
156 
157         return property;
158     }
159 
160 
161     /**
162      * Based on the i18n information, define the property name to use.
163      */
164     protected String definePropertyName() {
165         String propertyName = this.basePropertyName;
166 
167         if (hasI18NSupport()) {
168             propertyName = this.i18NPropertyName;
169         }
170         return propertyName;
171     }
172 
173     // //////
174     // I18N support
175     // /////
176 
177     @Override
178     public void setLocale(Locale locale) {
179         this.locale = locale;
180     }
181 
182     @Override
183     public void setI18NPropertyName(String i18nPropertyName) {
184         this.i18NPropertyName = i18nPropertyName;
185     }
186 
187     @Override
188     public Locale getLocale() {
189         return this.locale;
190     }
191 
192     @Override
193     public String getBasePropertyName() {
194         return basePropertyName;
195     }
196 
197     @Override
198     public boolean hasI18NSupport() {
199         return definition.isI18n();
200     }
201 
202     @Override
203     public Class<T> getType() {
204         return type;
205     }
206 
207 }