package com.vaadin.data.validator;\r
\r
import java.io.Serializable;\r
-import java.lang.annotation.Annotation;\r
-import java.lang.reflect.Method;\r
import java.util.ArrayList;\r
-import java.util.Iterator;\r
import java.util.List;\r
import java.util.Locale;\r
import java.util.Set;\r
import javax.validation.MessageInterpolator.Context;\r
import javax.validation.Validation;\r
import javax.validation.ValidatorFactory;\r
-import javax.validation.constraints.NotNull;\r
-import javax.validation.metadata.BeanDescriptor;\r
import javax.validation.metadata.ConstraintDescriptor;\r
-import javax.validation.metadata.PropertyDescriptor;\r
\r
import com.vaadin.data.Validator;\r
-import com.vaadin.ui.Field;\r
\r
/**\r
* Vaadin {@link Validator} using the JSR-303 (javax.validation)\r
locale = Locale.getDefault();\r
}\r
\r
- /**\r
- * Apply a bean validation validator to a field based on a bean class and\r
- * the identifier of the property the field displays. The field is also\r
- * marked as required if the bean field has the {@link NotNull} annotation.\r
- * <p>\r
- * No actual Vaadin validator is added in case no or only {@link NotNull}\r
- * validation is used (required is practically same as NotNull validation).\r
- * \r
- * @param field\r
- * the {@link Field} component to which to add a validator\r
- * @param objectPropertyId\r
- * the property ID of the field of the bean that this field\r
- * displays\r
- * @param beanClass\r
- * the class of the bean with the bean validation annotations\r
- * @return the created validator\r
- */\r
- public static BeanValidationValidator addValidator(Field<?> field,\r
- Object objectPropertyId, Class<?> beanClass) {\r
- if (objectPropertyId == null || !(objectPropertyId instanceof String)) {\r
- throw new IllegalArgumentException(\r
- "Property id must be a non-null String");\r
- }\r
-\r
- String propertyId = (String) objectPropertyId;\r
- BeanValidationValidator validator = new BeanValidationValidator(\r
- beanClass, propertyId);\r
- PropertyDescriptor constraintsForProperty = validator\r
- .getJavaxBeanValidator().getConstraintsForClass(beanClass)\r
- .getConstraintsForProperty(propertyId);\r
- if (constraintsForProperty != null) {\r
- int nonNotNullValidators = constraintsForProperty\r
- .getConstraintDescriptors().size();\r
- if (validator.isRequired()) {\r
- field.setRequired(true);\r
- field.setRequiredError(validator.getRequiredMessage());\r
- nonNotNullValidators--;\r
- }\r
- if (nonNotNullValidators > 0) {\r
- field.addValidator(validator);\r
- }\r
- }\r
- return validator;\r
- }\r
-\r
- /**\r
- * Check the validity of a value. Normally, {@link #validate(Object)} should\r
- * be used instead of this method to also get the validation error message.\r
+ /*\r
+ * (non-Javadoc)\r
* \r
- * @param value\r
- * @return true if the value is valid\r
+ * @see com.vaadin.data.Validator#validate(java.lang.Object)\r
*/\r
- public boolean isValid(Object value) {\r
- try {\r
- validate(value);\r
- return true;\r
- } catch (Exception e) {\r
- return false;\r
- }\r
- }\r
-\r
- /**\r
- * Checks if the property has been marked as required (has the\r
- * {@link NotNull} annotation.\r
- * \r
- * @return true if the field is marked as not null\r
- */\r
- public boolean isRequired() {\r
- PropertyDescriptor desc = getJavaxBeanValidator()\r
- .getConstraintsForClass(beanClass).getConstraintsForProperty(\r
- propertyName);\r
- if (desc != null) {\r
- Iterator<ConstraintDescriptor<?>> it = desc\r
- .getConstraintDescriptors().iterator();\r
- while (it.hasNext()) {\r
- final ConstraintDescriptor<?> d = it.next();\r
- Annotation a = d.getAnnotation();\r
- if (a instanceof NotNull) {\r
- return true;\r
- }\r
- }\r
- }\r
- return false;\r
- }\r
-\r
- /**\r
- * Returns the message to show if a value is required but missing. The\r
- * message that of the {@link NotNull} annotation.\r
- * \r
- * @return error message to show for missing required value\r
- */\r
- @SuppressWarnings("unchecked")\r
- public String getRequiredMessage() {\r
- return getErrorMessage(null, NotNull.class);\r
- }\r
-\r
public void validate(final Object value) throws InvalidValueException {\r
Set<?> violations = getJavaxBeanValidator().validateValue(beanClass,\r
propertyName, value);\r
}\r
}\r
\r
- private String getErrorMessage(final Object value,\r
- Class<? extends Annotation>... an) {\r
- BeanDescriptor beanDesc = getJavaxBeanValidator()\r
- .getConstraintsForClass(beanClass);\r
- PropertyDescriptor desc = beanDesc\r
- .getConstraintsForProperty(propertyName);\r
- if (desc == null) {\r
- // validate() reports a conversion error in this case\r
- return null;\r
- }\r
- Iterator<ConstraintDescriptor<?>> it = desc.getConstraintDescriptors()\r
- .iterator();\r
- List<String> exceptions = new ArrayList<String>();\r
- while (it.hasNext()) {\r
- final ConstraintDescriptor<?> d = it.next();\r
- Annotation a = d.getAnnotation();\r
- boolean skip = false;\r
- if (an != null && an.length > 0) {\r
- skip = true;\r
- for (Class<? extends Annotation> t : an) {\r
- if (t == a.annotationType()) {\r
- skip = false;\r
- break;\r
- }\r
- }\r
- }\r
- if (!skip) {\r
- String messageTemplate = null;\r
- try {\r
- Method m = a.getClass().getMethod("message");\r
- messageTemplate = (String) m.invoke(a);\r
- } catch (Exception ex) {\r
- throw new InvalidValueException(\r
- "Annotation must have message attribute");\r
- }\r
- String msg = getJavaxBeanValidatorFactory()\r
- .getMessageInterpolator().interpolate(messageTemplate,\r
- new SimpleContext(value, d), locale);\r
- exceptions.add(msg);\r
- }\r
- }\r
- if (exceptions.size() > 0) {\r
- StringBuilder b = new StringBuilder();\r
- for (int i = 0; i < exceptions.size(); i++) {\r
- if (i != 0) {\r
- b.append("<br/>");\r
- }\r
- b.append(exceptions.get(i));\r
- }\r
- return b.toString();\r
- }\r
- return null;\r
- }\r
-\r
/**\r
* Sets the locale used for validation error messages.\r
* \r
}\r
return implementationAvailable;\r
}\r
+\r
}
\ No newline at end of file