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.basic;
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.Transformer;
40  import info.magnolia.ui.form.field.transformer.UndefinedPropertyType;
41  import info.magnolia.ui.vaadin.integration.jcr.DefaultPropertyUtil;
42  
43  import java.util.Locale;
44  import java.util.Objects;
45  
46  import javax.inject.Inject;
47  
48  import org.apache.commons.lang3.StringUtils;
49  import org.slf4j.Logger;
50  import org.slf4j.LoggerFactory;
51  
52  import com.vaadin.v7.data.Item;
53  import com.vaadin.v7.data.Property;
54  import com.vaadin.v7.data.util.ObjectProperty;
55  
56  /**
57   * Basic implementation of a {@link Transformer}.<br>
58   * This transformer is used for most of the basic fields (textBox, Date, ...).<br>
59   * His responsibility is to: <br>
60   * - retrieve or create a basic property from the related item <br>
61   * - update the item property value in case of changes performed on the related field.
62   *
63   * @param <T>
64   */
65  public class BasicTransformer<T> implements Transformer<T> {
66      private static final Logger log = LoggerFactory.getLogger(BasicTransformer.class);
67  
68      protected final ConfiguredFieldDefinition definition;
69  
70      protected Item relatedFormItem;
71  
72      protected String basePropertyName;
73  
74      /**
75       * @deprecated since 5.4.2 - should not be used any longer, alter {@link #basePropertyName} in {@link #setLocale(Locale)} method if needed.
76       */
77      @Deprecated
78      protected String i18NPropertyName;
79      private Locale locale;
80      protected Class<T> type;
81  
82      private boolean isReadOnly = false;
83  
84      private I18NAuthoringSupport i18NAuthoringSupport;
85  
86      @Inject
87      public BasicTransformer(Item relatedFormItem, ConfiguredFieldDefinition definition, Class<T> type, I18NAuthoringSupport i18NAuthoringSupport) {
88          this.definition = definition;
89          this.relatedFormItem = relatedFormItem;
90          this.i18NAuthoringSupport = i18NAuthoringSupport;
91          this.basePropertyName = definition.getName();
92  
93          setType(type);
94      }
95  
96      /**
97       * @deprecated since 5.4.2 - use {@link #BasicTransformer(Item, ConfiguredFieldDefinition, Class, I18NAuthoringSupport)} instead.
98       */
99      @Deprecated
100     public BasicTransformer(Item relatedFormItem, ConfiguredFieldDefinition definition, Class<T> type) {
101         this(relatedFormItem, definition, type, Components.getComponent(I18NAuthoringSupport.class));
102     }
103 
104     public Item getRelatedFormItem() {
105         return relatedFormItem;
106     }
107 
108     @Override
109     public void writeToItem(T newValue) {
110         Property<T> p = getOrCreateProperty(type);
111         p.setValue(newValue);
112     }
113 
114     @Override
115     public T readFromItem() {
116         return getOrCreateProperty(type).getValue();
117     }
118 
119     /**
120      * If the value type is not initialize by the field factory ({@link UndefinedPropertyType}), check if the property already exist in the Item.<br>
121      * If the Item has already this property, return the property value type.<br>
122      * Else return the default type 'String'
123      */
124     protected void setType(Class<T> typeFromDefinition) {
125         if (typeFromDefinition.isAssignableFrom(UndefinedPropertyType.class)) {
126             String propertyName = definePropertyName();
127             Property<T> property = relatedFormItem.getItemProperty(propertyName);
128             if (property != null) {
129                 this.type = (Class<T>) property.getType();
130             } else {
131                 this.type = (Class<T>) String.class;
132             }
133         } else {
134             this.type = typeFromDefinition;
135         }
136     }
137 
138     /**
139      * If the desired property (propertyName) already exist in the JcrNodeAdapter, return this property<br>
140      * else create a new {@link Property}.<br>
141      * If the returned property is not of the desired type, cast this property to the proper type.
142      *
143      * @param <T>
144      */
145     protected <T> Property<T> getOrCreateProperty(Class<T> type) {
146         return getOrCreateProperty(type, true);
147     }
148 
149     /**
150      * If the desired property (propertyName) already exist in the JcrNodeAdapter, return this property<br>
151      * else create a new {@link Property}.<br>
152      * If 'checkTypes' is set to true and if the returned property is not of the desired type, cast this property to the proper type.
153      */
154     protected <T> Property<T> getOrCreateProperty(Class<T> type, boolean checkTypes) {
155         String propertyName = definePropertyName();
156         Property<T> property = relatedFormItem.getItemProperty(propertyName);
157 
158         if (property == null) {
159             property = new ObjectProperty<>(null, type);
160             relatedFormItem.addItemProperty(propertyName, property);
161 
162         } else if (checkTypes && !type.isAssignableFrom(property.getType())) {
163             // solve MGNLUI-2494
164             // 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.
165             // get the value as String
166             String stringValue = ((property.getValue() != null && StringUtils.isNotBlank(property.getValue().toString()))
167                     ? property.getValue().toString()
168                     : null);
169             T value = null;
170             try {
171                 // Convert the String value to the desired type.
172                 value = (T) DefaultPropertyUtil.createTypedValue(type, stringValue);
173             } catch (Exception e) {
174                 // Ignore. In case of exception, set a null value.
175             }
176             if (!Objects.equals(value, property.getValue())) {
177                 property = new ObjectProperty<>(value, type, property.isReadOnly()); // honor original read-only state
178                 // This will replace the previous property (with the wrong type) with the new one (correctly typed).
179                 relatedFormItem.addItemProperty(propertyName, property);
180             }
181         }
182 
183         // mirror the read-only state of the underlying property (#isReadOnly() could now be refactored as lazy I suppose)
184         this.isReadOnly = property.isReadOnly();
185         return property;
186     }
187 
188     /**
189      * Based on the i18n information, define the property name to use.
190      */
191     protected String definePropertyName() {
192         final String propertyName = deriveLocaleAwareName(this.basePropertyName);
193         this.i18NPropertyName = propertyName;
194         return propertyName;
195     }
196 
197     protected String deriveLocaleAwareName(String baseName) {
198         if (hasI18NSupport() && locale != null && !i18NAuthoringSupport.isDefaultLocale(locale, relatedFormItem)) {
199             return i18NAuthoringSupport.deriveLocalisedPropertyName(baseName, locale);
200         }
201         return baseName;
202     }
203 
204     protected I18NAuthoringSupport getI18NAuthoringSupport() {
205         return i18NAuthoringSupport;
206     }
207 
208     // //////
209 
210     @Override
211     public void setLocale(Locale locale) {
212         this.locale = locale;
213     }
214 
215     @Override
216     public void setI18NPropertyName(String i18nPropertyName) {
217         log.warn("BasicTransformer.setI18NPropertyName() is deprecated since 5.4.2 without replacement, override BasicTransformer.definePropertyName() and construct the locale-specific property name there if needed.");
218     }
219 
220     @Override
221     public Locale getLocale() {
222         return this.locale;
223     }
224 
225     @Override
226     public String getBasePropertyName() {
227         return basePropertyName;
228     }
229 
230     @Override
231     public boolean hasI18NSupport() {
232         return definition.isI18n();
233     }
234 
235     @Override
236     public Class<T> getType() {
237         return type;
238     }
239 
240     @Override
241     public boolean isReadOnly() {
242         return this.isReadOnly;
243     }
244 
245     @Override
246     @Deprecated
247     public void setReadOnly(boolean isReadOnly) {
248         log.warn("Transformer #readOnly cannot be set, it just mirrors read-only state of the underlying Property.");
249     }
250 }