Skip to content

Instantly share code, notes, and snippets.

@hferentschik
Created February 9, 2011 12:14
Show Gist options
  • Save hferentschik/818374 to your computer and use it in GitHub Desktop.
Save hferentschik/818374 to your computer and use it in GitHub Desktop.
private <T, U, V, E extends ConstraintViolation<T>> void validateConstraintsForCurrentGroup(ValidationContext<T, E> validationContext, ValueContext<U, V> valueContext) {
BeanMetaData<U> beanMetaData = getBeanMetaData( valueContext.getCurrentBeanType() );
boolean validatingDefault = valueContext.validatingDefault();
boolean validatedBeanRedefinesDefault = beanMetaData.defaultGroupSequenceIsRedefined();
// if we are not validating the default group there is nothing special to consider
if ( !validatingDefault ) {
validateConstraintsForNonDefaultGroup( validationContext, valueContext );
return;
}
// if we are validating the default group we have to distinguish between the case where the main entity type redefines the default group and where not
if ( validatedBeanRedefinesDefault ) {
validateConstraintsForRedefinedDefaultGroup( validationContext, valueContext, beanMetaData );
}
else {
validateConstraintsForDefaultGroup( validationContext, valueContext, beanMetaData );
}
}
// Called when the default group is validated, but the current bean does not redefine the default group sequence
// in this case we have to check whether any of the super-types (and the constraints hosted on it) re-defines
// the default group sequence. In this case this sequence must be applied
private <T, U, V, E extends ConstraintViolation<T>> void validateConstraintsForDefaultGroup(ValidationContext<T, E> validationContext, ValueContext<U, V> valueContext, BeanMetaData<U> beanMetaData) {
for ( Map.Entry<Class<?>, List<BeanMetaConstraint<U, ? extends Annotation>>> entry : beanMetaData.getMetaConstraintsAsMap()
.entrySet() ) {
Class<U> hostingBeanClass = (Class<U>) entry.getKey();
List<BeanMetaConstraint<U, ? extends Annotation>> constraints = entry.getValue();
List<Class<?>> defaultGroupSequence;
if ( beanMetaData.isDefaultGroupSequenceProvider() ) {
defaultGroupSequence = getBeanMetaData( hostingBeanClass ).getDefaultGroupSequence( valueContext.getCurrentBean() );
}
else {
defaultGroupSequence = getBeanMetaData( hostingBeanClass ).getDefaultGroupSequence();
}
PathImpl currentPath = valueContext.getPropertyPath();
for ( Class<?> defaultSequenceMember : defaultGroupSequence ) {
valueContext.setCurrentGroup( defaultSequenceMember );
boolean validationSuccessful = true;
for ( BeanMetaConstraint<U, ? extends Annotation> metaConstraint : constraints ) {
boolean tmp = validateConstraint(
validationContext, valueContext, metaConstraint
);
validationSuccessful = validationSuccessful && tmp;
// reset the path
valueContext.setPropertyPath( currentPath );
}
if ( !validationSuccessful ) {
break;
}
}
validationContext.markProcessed(
valueContext.getCurrentBean(),
valueContext.getCurrentGroup(),
valueContext.getPropertyPath()
);
}
}
// Called when the current bean class re-defines the default group sequence. This sequence needs to be used for
// constraints defined on super classes as well
private <T, U, V, E extends ConstraintViolation<T>> void validateConstraintsForRedefinedDefaultGroup(ValidationContext<T, E> validationContext, ValueContext<U, V> valueContext, BeanMetaData<U> beanMetaData) {
List<Class<?>> defaultGroupSequence;
if ( beanMetaData.isDefaultGroupSequenceProvider() ) {
defaultGroupSequence = beanMetaData.getDefaultGroupSequence( valueContext.getCurrentBean() );
}
else {
defaultGroupSequence = beanMetaData.getDefaultGroupSequence();
}
PathImpl currentPath = valueContext.getPropertyPath();
for ( Class<?> defaultSequenceMember : defaultGroupSequence ) {
valueContext.setCurrentGroup( defaultSequenceMember );
boolean validationSuccessful = true;
for ( BeanMetaConstraint<U, ? extends Annotation> metaConstraint : beanMetaData.getMetaConstraintsAsList() ) {
boolean tmp = validateConstraint( validationContext, valueContext, metaConstraint );
validationSuccessful = validationSuccessful && tmp;
// reset the path
valueContext.setPropertyPath( currentPath );
}
if ( !validationSuccessful ) {
break;
}
}
validationContext.markProcessed(
valueContext.getCurrentBean(),
valueContext.getCurrentGroup(),
valueContext.getPropertyPath()
);
}
@hferentschik
Copy link
Author

I renamed the methods, because I thought the names were confusing. The important thing is for the case where the default sequence is validated, but not redefined in the top level bean, that we check where the constraints are hosted in the class hierarchy and whether any of the sub types redefines the default.

Relevant for this are the tests in GroupSequenceIsolationTest, especially testCorrectDefaultSequenceInheritance3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment