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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. * @since 8.10
  63. */
  64. public BeanValidationBinder(Class<BEAN> beanType,
  65. boolean scanNestedDefinitions) {
  66. super(beanType, scanNestedDefinitions);
  67. if (!BeanUtil.checkBeanValidationAvailable()) {
  68. throw new IllegalStateException(BeanValidationBinder.class
  69. .getSimpleName()
  70. + " cannot be used because a JSR-303 Bean Validation "
  71. + "implementation not found on the classpath or could not be initialized. Use "
  72. + Binder.class.getSimpleName() + " instead");
  73. }
  74. this.beanType = beanType;
  75. }
  76. /**
  77. * Sets a logic which allows to configure require indicator via
  78. * {@link HasValue#setRequiredIndicatorVisible(boolean)} based on property
  79. * descriptor.
  80. * <p>
  81. * Required indicator configuration will not be used at all if
  82. * {@code configurator} is null.
  83. * <p>
  84. * By default the {@link RequiredFieldConfigurator#DEFAULT} configurator is
  85. * used.
  86. *
  87. * @param configurator
  88. * required indicator configurator, may be {@code null}
  89. */
  90. public void setRequiredConfigurator(
  91. RequiredFieldConfigurator configurator) {
  92. requiredConfigurator = configurator;
  93. }
  94. /**
  95. * Gets field required indicator configuration logic.
  96. *
  97. * @see #setRequiredConfigurator(RequiredFieldConfigurator)
  98. *
  99. * @return required indicator configurator, may be {@code null}
  100. */
  101. public RequiredFieldConfigurator getRequiredConfigurator() {
  102. return requiredConfigurator;
  103. }
  104. @Override
  105. protected BindingBuilder<BEAN, ?> configureBinding(
  106. BindingBuilder<BEAN, ?> binding,
  107. PropertyDefinition<BEAN, ?> definition) {
  108. Class<?> actualBeanType = findBeanType(beanType, definition);
  109. BeanValidator validator = new BeanValidator(actualBeanType,
  110. definition.getTopLevelName());
  111. if (requiredConfigurator != null) {
  112. configureRequired(binding, definition, validator);
  113. }
  114. return binding.withValidator(validator);
  115. }
  116. /**
  117. * Finds the bean type containing the property the given definition refers
  118. * to.
  119. *
  120. * @param beanType
  121. * the root beanType
  122. * @param definition
  123. * the definition for the property
  124. * @return the bean type containing the given property
  125. */
  126. @SuppressWarnings({ "rawtypes" })
  127. private Class<?> findBeanType(Class<BEAN> beanType,
  128. PropertyDefinition<BEAN, ?> definition) {
  129. if (definition instanceof NestedBeanPropertyDefinition) {
  130. return ((NestedBeanPropertyDefinition) definition).getParent()
  131. .getType();
  132. } else {
  133. // Non nested properties must be defined in the main type
  134. return beanType;
  135. }
  136. }
  137. private void configureRequired(BindingBuilder<BEAN, ?> binding,
  138. PropertyDefinition<BEAN, ?> definition, BeanValidator validator) {
  139. assert requiredConfigurator != null;
  140. Class<?> propertyHolderType = definition.getPropertyHolderType();
  141. BeanDescriptor descriptor = validator.getJavaxBeanValidator()
  142. .getConstraintsForClass(propertyHolderType);
  143. PropertyDescriptor propertyDescriptor = descriptor
  144. .getConstraintsForProperty(definition.getTopLevelName());
  145. if (propertyDescriptor == null) {
  146. return;
  147. }
  148. if (propertyDescriptor.getConstraintDescriptors().stream()
  149. .map(ConstraintDescriptor::getAnnotation)
  150. .anyMatch(requiredConfigurator)) {
  151. binding.getField().setRequiredIndicatorVisible(true);
  152. }
  153. }
  154. }