You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BeanValidationBinder.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.data;
  17. import javax.validation.metadata.BeanDescriptor;
  18. import javax.validation.metadata.ConstraintDescriptor;
  19. import javax.validation.metadata.PropertyDescriptor;
  20. import com.vaadin.data.BeanPropertySet.NestedBeanPropertyDefinition;
  21. import com.vaadin.data.util.BeanUtil;
  22. import com.vaadin.data.validator.BeanValidator;
  23. /**
  24. * @author Vaadin Ltd
  25. * @see Binder
  26. * @see HasValue
  27. *
  28. * @since 8.0
  29. */
  30. public class BeanValidationBinder<BEAN> extends Binder<BEAN> {
  31. private final Class<BEAN> beanType;
  32. private RequiredFieldConfigurator requiredConfigurator = RequiredFieldConfigurator.DEFAULT;
  33. /**
  34. * Creates a new binder that uses reflection based on the provided bean type
  35. * to resolve bean properties. It assumes that JSR-303 bean validation
  36. * implementation is present on the classpath. If there is no such
  37. * implementation available then {@link Binder} class should be used instead
  38. * (this constructor will throw an exception). Otherwise
  39. * {@link BeanValidator} is added to each binding that is defined using a
  40. * property name.
  41. *
  42. * @param beanType
  43. * the bean type to use, not <code>null</code>
  44. */
  45. public BeanValidationBinder(Class<BEAN> beanType) {
  46. this(beanType,false);
  47. }
  48. /**
  49. * Creates a new binder that uses reflection based on the provided bean type
  50. * to resolve bean properties. It assumes that JSR-303 bean validation
  51. * implementation is present on the classpath. If there is no such
  52. * implementation available then {@link Binder} class should be used instead
  53. * (this constructor will throw an exception). Otherwise
  54. * {@link BeanValidator} is added to each binding that is defined using a
  55. * property name.
  56. *
  57. * @param beanType
  58. * the bean type to use, not {@code null}
  59. * @param scanNestedDefinitions
  60. * if {@code true}, scan for nested property definitions as well
  61. */
  62. public BeanValidationBinder(Class<BEAN> beanType, boolean scanNestedDefinitions) {
  63. super(beanType, scanNestedDefinitions);
  64. if (!BeanUtil.checkBeanValidationAvailable()) {
  65. throw new IllegalStateException(BeanValidationBinder.class
  66. .getSimpleName()
  67. + " cannot be used because a JSR-303 Bean Validation "
  68. + "implementation not found on the classpath or could not be initialized. Use "
  69. + Binder.class.getSimpleName() + " instead");
  70. }
  71. this.beanType = beanType;
  72. }
  73. /**
  74. * Sets a logic which allows to configure require indicator via
  75. * {@link HasValue#setRequiredIndicatorVisible(boolean)} based on property
  76. * descriptor.
  77. * <p>
  78. * Required indicator configuration will not be used at all if
  79. * {@code configurator} is null.
  80. * <p>
  81. * By default the {@link RequiredFieldConfigurator#DEFAULT} configurator is
  82. * used.
  83. *
  84. * @param configurator
  85. * required indicator configurator, may be {@code null}
  86. */
  87. public void setRequiredConfigurator(
  88. RequiredFieldConfigurator configurator) {
  89. requiredConfigurator = configurator;
  90. }
  91. /**
  92. * Gets field required indicator configuration logic.
  93. *
  94. * @see #setRequiredConfigurator(RequiredFieldConfigurator)
  95. *
  96. * @return required indicator configurator, may be {@code null}
  97. */
  98. public RequiredFieldConfigurator getRequiredConfigurator() {
  99. return requiredConfigurator;
  100. }
  101. @Override
  102. protected BindingBuilder<BEAN, ?> configureBinding(
  103. BindingBuilder<BEAN, ?> binding,
  104. PropertyDefinition<BEAN, ?> definition) {
  105. Class<?> actualBeanType = findBeanType(beanType, definition);
  106. BeanValidator validator = new BeanValidator(actualBeanType,
  107. definition.getTopLevelName());
  108. if (requiredConfigurator != null) {
  109. configureRequired(binding, definition, validator);
  110. }
  111. return binding.withValidator(validator);
  112. }
  113. /**
  114. * Finds the bean type containing the property the given definition refers
  115. * to.
  116. *
  117. * @param beanType
  118. * the root beanType
  119. * @param definition
  120. * the definition for the property
  121. * @return the bean type containing the given property
  122. */
  123. @SuppressWarnings({ "rawtypes" })
  124. private Class<?> findBeanType(Class<BEAN> beanType,
  125. PropertyDefinition<BEAN, ?> definition) {
  126. if (definition instanceof NestedBeanPropertyDefinition) {
  127. return ((NestedBeanPropertyDefinition) definition).getParent()
  128. .getType();
  129. } else {
  130. // Non nested properties must be defined in the main type
  131. return beanType;
  132. }
  133. }
  134. private void configureRequired(BindingBuilder<BEAN, ?> binding,
  135. PropertyDefinition<BEAN, ?> definition, BeanValidator validator) {
  136. assert requiredConfigurator != null;
  137. Class<?> propertyHolderType = definition.getPropertyHolderType();
  138. BeanDescriptor descriptor = validator.getJavaxBeanValidator()
  139. .getConstraintsForClass(propertyHolderType);
  140. PropertyDescriptor propertyDescriptor = descriptor
  141. .getConstraintsForProperty(definition.getTopLevelName());
  142. if (propertyDescriptor == null) {
  143. return;
  144. }
  145. if (propertyDescriptor.getConstraintDescriptors().stream()
  146. .map(ConstraintDescriptor::getAnnotation)
  147. .anyMatch(requiredConfigurator)) {
  148. binding.getField().setRequiredIndicatorVisible(true);
  149. }
  150. }
  151. }