Where I work we were thinking of writing a library to support the hibernate
validator <http://www.hibernate.org/412.html> library...
The idea I had so far, was to make a behaviour, that can be mixed with a
PropertyModel..
Something like the attached code...
What do you guys think?
/*
* Copyright 2008 Kindleit Technologies. All rights reserved. This file, all
* proprietary knowledge and algorithms it details are the sole property of
* Kindleit Technologies unless otherwise specified. The software this file
* belong with is the confidential and proprietary information of Kindleit
* Technologies. ("Confidential Information"). You shall not disclose such
* Confidential Information and shall use it only in accordance with the terms
* of the license agreement you entered into with Kindleit.
*/
package com.kindleit.wicketweb.behaviors;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.List;
import org.apache.wicket.Component;
import org.apache.wicket.behavior.AbstractBehavior;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.validation.IValidator;
import org.hibernate.validator.NotEmpty;
import org.hibernate.validator.NotNull;
import com.kindleit.wicketweb.exceptions.WicketWebRuntimeException;
import com.kindleit.wicketweb.validators.Validators;
/**
* This behavior adds validators to the components it binds to, based on the
* Hibernate Validator annotations written in a property, which is specified by
* a given [EMAIL PROTECTED] PropertyModel}.
* @author [EMAIL PROTECTED]
*/
public class HibernateValidatorBehavior extends AbstractBehavior {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* [EMAIL PROTECTED] PropertyModel} that provide access to the property getter method
* that has the validations annotations.
*/
private PropertyModel propertyModel;
/**
* Construct a [EMAIL PROTECTED] HibernateValidatorBehavior}.
* @param propertyModel [EMAIL PROTECTED] PropertyModel} object that provide access to
* the property getter method that has the validations annotations.
*/
public HibernateValidatorBehavior(final PropertyModel propertyModel) {
super();
this.propertyModel = propertyModel;
}
/**
* Add a [EMAIL PROTECTED] IValidator} for each Hibernate Validator annotation written in
* the property specified by [EMAIL PROTECTED] #getPropertyModel()}.
* @see AbstractBehavior#bind(Component);
*/
@Override
public void bind(final Component component) {
super.bind(component);
if (component instanceof FormComponent) {
final FormComponent formComponent = (FormComponent) component;
final Method getter = getPropertyModel().getPropertyGetter();
final Annotation[] annotations = getter.getAnnotations();
addValidators(formComponent, annotations);
if (isRequiredProperty(annotations)) {
formComponent.setRequired(true);
}
} else {
throw WicketWebRuntimeException
.invalidComponentType(FormComponent.class);
}
}
/**
* Search for [EMAIL PROTECTED] NotNull} and [EMAIL PROTECTED] NotEmpty} annotations in a given
* array.
* @param annotations An array of [EMAIL PROTECTED] Annotation} objects.
* @return true if [EMAIL PROTECTED] NotNull} of [EMAIL PROTECTED] NotEmpty} annotations are
* present.
*/
public boolean isRequiredProperty(final Annotation[] annotations) {
for (final Annotation anno : annotations) {
if (anno instanceof NotNull || anno instanceof NotEmpty) {
return true;
}
}
return false;
}
/**
* Creates [EMAIL PROTECTED] IValidator} objects according to the annotations, then add
* the validators to a given [EMAIL PROTECTED] FormComponent}.
* @param component a [EMAIL PROTECTED] FormComponent} object to which validators will be
* added.
* @param annotations An array of [EMAIL PROTECTED] Annotation} objects.
*/
public void addValidators(final FormComponent component,
final Annotation[] annotations) {
final List<IValidator> validators = Validators.create(annotations);
for (final IValidator validator : validators) {
component.add(validator);
}
}
/**
* Getter for the <tt>propertyModel</tt>.
* @return the propertyModel
*/
public PropertyModel getPropertyModel() {
return propertyModel;
}
/**
* Setter for the <tt>propertyModel</tt>.
* @param propertyModel the propertyModel to set
*/
public void setPropertyModel(final PropertyModel propertyModel) {
this.propertyModel = propertyModel;
}
}