View Javadoc

1   /**
2    * This file Copyright (c) 2012-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.factory;
35  
36  import info.magnolia.cms.i18n.I18nContentSupport;
37  import info.magnolia.objectfactory.ComponentProvider;
38  import info.magnolia.ui.api.view.View;
39  import info.magnolia.ui.form.AbstractFormItem;
40  import info.magnolia.ui.form.field.definition.FieldDefinition;
41  import info.magnolia.ui.form.field.definition.TextFieldDefinition;
42  import info.magnolia.ui.form.field.transformer.TransformedProperty;
43  import info.magnolia.ui.form.field.transformer.Transformer;
44  import info.magnolia.ui.form.field.transformer.UndefinedPropertyType;
45  import info.magnolia.ui.form.field.transformer.basic.BasicTransformer;
46  import info.magnolia.ui.form.validator.definition.FieldValidatorDefinition;
47  import info.magnolia.ui.form.validator.factory.FieldValidatorFactory;
48  import info.magnolia.ui.form.validator.registry.FieldValidatorFactoryFactory;
49  import info.magnolia.ui.vaadin.integration.ItemAdapter;
50  import info.magnolia.ui.vaadin.integration.jcr.DefaultPropertyUtil;
51  
52  import java.util.Locale;
53  
54  import org.apache.commons.lang.StringUtils;
55  import org.slf4j.Logger;
56  import org.slf4j.LoggerFactory;
57  
58  import com.vaadin.data.Item;
59  import com.vaadin.data.Property;
60  import com.vaadin.data.util.converter.AbstractStringToNumberConverter;
61  import com.vaadin.server.Sizeable.Unit;
62  import com.vaadin.ui.AbstractTextField;
63  import com.vaadin.ui.Component;
64  import com.vaadin.ui.CssLayout;
65  import com.vaadin.ui.Field;
66  import com.vaadin.ui.Label;
67  
68  /**
69   * Abstract FieldFactory implementations. This class handle all common attributes defined in {@link FieldDefinition} and binds Vaadin {@link Field} instances created
70   * by subclasses to the {@link Property} they will be reading and writing to.
71   * 
72   * @param <D> definition type
73   * @param <T> field value type
74   */
75  public abstract class AbstractFieldFactory<D extends FieldDefinition, T> extends AbstractFormItem implements FieldFactory {
76      private static final Logger log = LoggerFactory.getLogger(AbstractFieldFactory.class);
77      protected Item item;
78      protected Field<T> field;
79      protected D definition;
80      private FieldValidatorFactoryFactory fieldValidatorFactoryFactory;
81      private I18nContentSupport i18nContentSupport;
82      private ComponentProvider componentProvider;
83  
84      public AbstractFieldFactory(D definition, Item relatedFieldItem) {
85          this.definition = definition;
86          this.item = relatedFieldItem;
87      }
88  
89      @Override
90      public void setFieldValidatorFactoryFactory(FieldValidatorFactoryFactory fieldValidatorFactoryFactory) {
91          this.fieldValidatorFactoryFactory = fieldValidatorFactoryFactory;
92      }
93  
94      @Override
95      public void setI18nContentSupport(I18nContentSupport i18nContentSupport) {
96          this.i18nContentSupport = i18nContentSupport;
97      }
98  
99      @Override
100     public Field<T> createField() {
101         if (field == null) {
102             // Create the Vaadin field
103             this.field = createFieldComponent();
104 
105             Property<?> property = initializeProperty();
106 
107             // MGNLUI-1855 we need to assign converter for properties with type Long because otherwise Vaadin assigns incompatible StringToNumberConverter.
108             if (Long.class.equals(property.getType()) && field instanceof AbstractTextField) {
109                 ((AbstractTextField) field).setConverter(new StringToLongConverter());
110             }
111             // Set the created property with the default value as field Property datasource.
112             setPropertyDataSourceAndDefaultValue(property);
113 
114             if (StringUtils.isNotBlank(definition.getStyleName())) {
115                 this.field.addStyleName(definition.getStyleName());
116             }
117 
118             field.setWidth(100, Unit.PERCENTAGE);
119 
120             // Set label and required marker
121             if (StringUtils.isNotBlank(getFieldDefinition().getLabel())) {
122                 this.field.setCaption(getFieldDefinition().getLabel() + (getFieldDefinition().isRequired() ? "<span class=\"requiredfield\">*</span>" : ""));
123             }
124 
125             setConstraints();
126 
127         }
128         return this.field;
129     }
130 
131     /**
132      * Set the DataSource of the current field.<br>
133      * Set the default value if : <br>
134      * - the item is an instance of {@link ItemAdapter} and this is a new Item (Not yet stored in the related datasource).<br>
135      * - the item is not an instance of {@link ItemAdapter}.<br>
136      * In this case, the Item is a custom implementation of {@link Item} and we have no possibility to define if it is or not a new Item.<br>
137      */
138     public void setPropertyDataSourceAndDefaultValue(Property<?> property) {
139         this.field.setPropertyDataSource(property);
140 
141         if ((item instanceof ItemAdapter && ((ItemAdapter) item).isNew()) || (!(item instanceof ItemAdapter) && property.getValue() == null)) {
142             setPropertyDataSourceDefaultValue(property);
143         }
144     }
145 
146     /**
147      * Set the Field default value is required.
148      */
149     protected void setPropertyDataSourceDefaultValue(Property property) {
150         Object defaultValue = createDefaultValue(property);
151         if (defaultValue != null && !definition.isReadOnly()) {
152             if (defaultValue.getClass().isAssignableFrom(property.getType())) {
153                 property.setValue(defaultValue);
154             } else {
155                 log.warn("Default value {} is not assignable to the field of type {}.", defaultValue, field.getPropertyDataSource().getType().getName());
156             }
157         }
158     }
159 
160     /**
161      * Create a typed default value.
162      */
163     protected Object createDefaultValue(Property<?> property) {
164         String defaultValue = definition.getDefaultValue();
165         if (StringUtils.isNotBlank(defaultValue)) {
166             return DefaultPropertyUtil.createTypedValue(property.getType(), defaultValue);
167         }
168         return null;
169     }
170 
171     @Override
172     public D getFieldDefinition() {
173         return this.definition;
174     }
175 
176     /**
177      * Implemented by subclasses to create and initialize the Vaadin Field instance to use.
178      */
179     protected abstract Field<T> createFieldComponent();
180 
181     @Override
182     public View getView() {
183         Property<?> property = initializeProperty();
184 
185         final CssLayout fieldView = new CssLayout();
186         fieldView.setStyleName("field-view");
187 
188         Label label = new Label();
189         label.setSizeUndefined();
190         label.setCaption(getFieldDefinition().getLabel());
191 
192         if (getFieldDefinition().getClass().isAssignableFrom(TextFieldDefinition.class)) {
193             final TextFieldDefinition textFieldDefinition = (TextFieldDefinition) getFieldDefinition();
194             if (textFieldDefinition.getRows() > 0) {
195                 label.addStyleName("textarea");
196             }
197         }
198         label.setPropertyDataSource(property);
199 
200         fieldView.addComponent(label);
201 
202         return new View() {
203             @Override
204             public Component asVaadinComponent() {
205                 return fieldView;
206             }
207         };
208     }
209 
210     /**
211      * Initialize the property used as field's Datasource.<br>
212      * If no {@link Transformer} is configure to the field definition, use the default {@link BasicTransformer} <br>
213      */
214     @SuppressWarnings("unchecked")
215     private Property<?> initializeProperty() {
216         Class<? extends Transformer<?>> transformerClass = definition.getTransformerClass();
217 
218         if (transformerClass == null) {
219             // TODO explain why down cast
220             transformerClass = (Class<? extends Transformer<?>>) (Object) BasicTransformer.class;
221         }
222         Transformer<?> transformer = initializeTransformer(transformerClass);
223 
224         return new TransformedProperty(transformer);
225     }
226 
227     /**
228      * Exposed method used by field's factory to initialize the property {@link Transformer}.<br>
229      * This allows to add additional constructor parameter if needed.<br>
230      */
231     protected Transformer<?> initializeTransformer(Class<? extends Transformer<?>> transformerClass) {
232         return this.componentProvider.newInstance(transformerClass, item, definition, getFieldType());
233     }
234 
235     /**
236      * Define the field property value type Class.<br>
237      * Return the value defined by the configuration ('type' property).<br>
238      * If this value is not defined, return the value of the overriding method {@link AbstractFieldFactory#getDefaultFieldType()}.<br>
239      * If this method is not override, return {@link UndefinedPropertyType}.<br>
240      * In this case, the {@link Transformer} will have the responsibility to define the property type.
241      */
242     protected Class<?> getFieldType() {
243         Class<?> type = getDefinitionType();
244         if (type == null) {
245             type = getDefaultFieldType();
246         }
247         return type;
248     }
249 
250     /**
251      * @return Class Type defined into the field definition or null if not defined.
252      */
253     protected Class<?> getDefinitionType() {
254         if (StringUtils.isNotBlank(definition.getType())) {
255             return DefaultPropertyUtil.getFieldTypeClass(definition.getType());
256         }
257         return null;
258     }
259 
260     /**
261      * Exposed method used by field's factory in order to define a default Field Type (decoupled from the definition).
262      */
263     protected Class<?> getDefaultFieldType() {
264         return UndefinedPropertyType.class;
265     }
266 
267     @Override
268     protected String getI18nBasename() {
269         return definition.getI18nBasename();
270     }
271 
272     /**
273      * Set all constraints linked to the field:
274      * Build Validation rules.
275      * Set Required field.
276      * Set Read Only.
277      */
278     private void setConstraints() {
279         // Set Validation
280         for (FieldValidatorDefinition validatorDefinition : definition.getValidators()) {
281             FieldValidatorFactory validatorFactory = this.fieldValidatorFactoryFactory.createFieldValidatorFactory(validatorDefinition, item);
282             if (validatorFactory != null) {
283                 field.addValidator(validatorFactory.createValidator());
284             } else {
285                 log.warn("Not able to create Validation for the following definition {}", definition.toString());
286             }
287         }
288         // Set Required
289         if (definition.isRequired()) {
290             field.setInvalidCommitted(true);
291             field.setRequired(true);
292             field.setRequiredError(definition.getRequiredErrorMessage());
293         }
294 
295         // Set ReadOnly (field property has to be updated)
296         if (field.getPropertyDataSource() != null) {
297             field.getPropertyDataSource().setReadOnly(definition.isReadOnly());
298         }
299     }
300 
301     @Override
302     public void setComponentProvider(ComponentProvider componentProvider) {
303         this.componentProvider = componentProvider;
304     }
305 
306     /**
307      * The StringToLongConverter.<br>
308      * MGNLUI-1855 This should be handled by vaadin, but StringToNumberConverter throws conversion exception when used
309      * with a Long property in Vaadin 7.1. This should be fixed, unfortunately not before 7.2, so we need that converter
310      * for the time being.<br>
311      * As a result, this class will have a short life span, this is why it is kept private and deprecated.
312      */
313     @Deprecated
314     private static class StringToLongConverter extends AbstractStringToNumberConverter<Long> {
315 
316         @Override
317         public Long convertToModel(String value, Class<? extends Long> targetType, Locale locale) throws ConversionException {
318             Number n = convertToNumber(value, targetType, locale);
319             return n == null ? null : n.longValue();
320         }
321 
322         @Override
323         public Class<Long> getModelType() {
324             return Long.class;
325         }
326     }
327 
328 }