View Javadoc

1   /**
2    * This file Copyright (c) 2013 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  
41  import java.util.Locale;
42  
43  import javax.inject.Inject;
44  
45  import com.vaadin.data.Item;
46  import com.vaadin.data.Property;
47  
48  /**
49   * Basic implementation of a {@link Transformer}.<br>
50   * This handler is used for most of the basic fields (textBox, Date, ...).<br>
51   * His responsibility is to: <br>
52   * - retrieve or create a basic property from the related item <br>
53   * - update the item property value in case of changes performed on the related field.
54   *
55   * @param <T>
56   */
57  public class BasicTransformer<T> implements Transformer<T> {
58  
59      protected Item relatedFormItem;
60      protected final ConfiguredFieldDefinition definition;
61  
62      protected String basePropertyName;
63      protected String i18NPropertyName;
64      private Locale locale;
65      protected Class<T> type;
66  
67      @Inject
68      public BasicTransformer(Item relatedFormItem, ConfiguredFieldDefinition definition, Class<T> type) {
69          this.definition = definition;
70          this.relatedFormItem = relatedFormItem;
71          this.basePropertyName = definition.getName();
72          if (hasI18NSupport()) {
73              this.i18NPropertyName = this.basePropertyName;
74          }
75          setType(type);
76      }
77  
78      @Override
79      public void writeToItem(T newValue) {
80          Property<T> p = getOrCreateProperty(type);
81          p.setValue(newValue);
82      }
83  
84      @Override
85      public T readFromItem() {
86          String defaultValue = definition.getDefaultValue();
87          Property<T> p = getOrCreateProperty(type);
88          if (definition.isReadOnly()) {
89              p.setReadOnly(true);
90          }
91          return p.getValue();
92      }
93  
94      /**
95       * If the value type is not initialize by the field factory ({@link UndefinedPropertyType}), check if the property already exist in the Item.<br>
96       * If the Item has already this property, return the property value type.<br>
97       * Else return the default type 'String'
98       */
99      protected void setType(Class<T> typeFromDefinition) {
100         if (typeFromDefinition.isAssignableFrom(UndefinedPropertyType.class)) {
101             String propertyName = definePropertyName();
102             Property<T> property = relatedFormItem.getItemProperty(propertyName);
103             if (property != null) {
104                 this.type = (Class<T>) property.getType();
105             } else {
106                 this.type = (Class<T>) String.class;
107             }
108         } else {
109             this.type = typeFromDefinition;
110         }
111     }
112 
113     /**
114      * If the desired property (propertyName) already exist in the JcrNodeAdapter, return this property<br>
115      * else create a new {@link Property}.<br>
116      * If the defaultValueString is empty or null, return a typed null value property.
117      *
118      * @param <T>
119      */
120     @SuppressWarnings("unchecked")
121     protected <T> Property<T> getOrCreateProperty(Class<T> type) {
122         String propertyName = definePropertyName();
123         Property<T> property = relatedFormItem.getItemProperty(propertyName);
124 
125         if (property == null) {
126             property = new DefaultProperty<T>(type, null);
127             relatedFormItem.addItemProperty(propertyName, property);
128         }
129         return property;
130     }
131 
132     /**
133      * Based on the i18n information, define the property name to use.
134      */
135     protected String definePropertyName() {
136         String propertyName = this.basePropertyName;
137 
138         if (hasI18NSupport()) {
139             propertyName = this.i18NPropertyName;
140         }
141         return propertyName;
142     }
143 
144     // //////
145     // I18N support
146     // /////
147 
148     @Override
149     public void setLocale(Locale locale) {
150         this.locale = locale;
151     }
152 
153     @Override
154     public void setI18NPropertyName(String i18nPropertyName) {
155         this.i18NPropertyName = i18nPropertyName;
156     }
157 
158     @Override
159     public Locale getLocale() {
160         return this.locale;
161     }
162 
163     @Override
164     public String getBasePropertyName() {
165         return basePropertyName;
166     }
167 
168     @Override
169     public boolean hasI18NSupport() {
170         return definition.isI18n();
171     }
172 
173     @Override
174     public Class<T> getType() {
175         return type;
176     }
177 
178 }