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