org.codehaus.groovy.grails.validation
Class ConstrainedProperty

java.lang.Object
  extended by org.codehaus.groovy.grails.validation.ConstrainedProperty

public class ConstrainedProperty
extends Object

Provides the ability to set contraints against a properties of a class. Constraints can either be set via the property setters or via the

applyConstraint(String constraintName, Object constrainingValue)
in combination with a constraint constant. Example: ... ConstrainedProperty cp = new ConstrainedProperty(owningClass, propertyName, propertyType); if(cp.supportsConstraint( ConstrainedProperty.EMAIL_CONSTRAINT ) ) { cp.applyConstraint( ConstrainedProperty.EMAIL_CONSTRAINT, new Boolean(true) ); } Alternatively constraints can be applied directly using the java bean getters/setters if a static (as oposed to dynamic) approach to constraint creation is possible: cp.setEmail(true)

Since:
07-Nov-2005
Author:
Graeme Rocher

Field Summary
protected  Map appliedConstraints
           
static String BLANK_CONSTRAINT
           
protected static ResourceBundle bundle
           
protected static Map constraints
           
static String CREDIT_CARD_CONSTRAINT
           
(package private) static String DEFAULT_BLANK_MESSAGE_CODE
           
(package private) static String DEFAULT_DOESNT_MATCH_MESSAGE_CODE
           
(package private) static String DEFAULT_INVALID_CREDIT_CARD_MESSAGE_CODE
           
(package private) static String DEFAULT_INVALID_EMAIL_MESSAGE_CODE
           
(package private) static String DEFAULT_INVALID_MAX_MESSAGE_CODE
           
(package private) static String DEFAULT_INVALID_MAX_SIZE_MESSAGE_CODE
           
(package private) static String DEFAULT_INVALID_MIN_MESSAGE_CODE
           
(package private) static String DEFAULT_INVALID_MIN_SIZE_MESSAGE_CODE
           
(package private) static String DEFAULT_INVALID_RANGE_MESSAGE_CODE
           
(package private) static String DEFAULT_INVALID_SIZE_MESSAGE_CODE
           
(package private) static String DEFAULT_INVALID_URL_MESSAGE_CODE
           
(package private) static String DEFAULT_INVALID_VALIDATOR_MESSAGE_CODE
           
protected static Map DEFAULT_MESSAGES
           
(package private) static String DEFAULT_NOT_EQUAL_MESSAGE_CODE
           
(package private) static String DEFAULT_NOT_INLIST_MESSAGE_CODE
           
(package private) static String DEFAULT_NULL_MESSAGE_CODE
           
static String EMAIL_CONSTRAINT
           
protected static String EXCEEDED_SUFFIX
           
static String IN_LIST_CONSTRAINT
           
protected static String INVALID_SUFFIX
           
protected static org.apache.commons.logging.Log LOG
           
static String MATCHES_CONSTRAINT
           
static String MAX_CONSTRAINT
           
static String MAX_SIZE_CONSTRAINT
           
protected  MessageSource messageSource
           
static String MIN_CONSTRAINT
           
static String MIN_SIZE_CONSTRAINT
           
static String NOT_EQUAL_CONSTRAINT
           
protected static String NOT_PREFIX
           
protected static String NOTMET_SUFFIX
           
static String NULLABLE_CONSTRAINT
           
protected  Class owningClass
           
protected  String propertyName
           
protected  Class propertyType
           
static String RANGE_CONSTRAINT
           
static String SCALE_CONSTRAINT
           
static String SIZE_CONSTRAINT
           
protected static String TOOBIG_SUFFIX
           
protected static String TOOLONG_SUFFIX
           
protected static String TOOSHORT_SUFFIX
           
protected static String TOOSMALL_SUFFIX
           
static String URL_CONSTRAINT
           
static String VALIDATOR_CONSTRAINT
           
 
Constructor Summary
ConstrainedProperty(Class clazz, String propertyName, Class propertyType)
          Constructs a new ConstrainedProperty for the given arguments
 
Method Summary
 void addMetaConstraint(String name, Object value)
          Adds a meta constraints which is a non-validating informational constraint
 void applyConstraint(String constraintName, Object constrainingValue)
          Applies a constraint for the specified name and consraint value
 Constraint getAppliedConstraint(String name)
          Obtains an applied constraint by name
 Collection getAppliedConstraints()
           
 Map getAttributes()
           
 String getFormat()
           
 List getInList()
           
 String getMatches()
           
 Comparable getMax()
           
 Integer getMaxSize()
           
 Object getMetaConstraintValue(String name)
          Obtains the value of the named meta constraint
 Comparable getMin()
           
 Integer getMinSize()
           
 Object getNotEqual()
           
 int getOrder()
           
 String getPropertyName()
           
 Class getPropertyType()
           
 groovy.lang.Range getRange()
           
 Integer getScale()
           
 groovy.lang.Range getSize()
           
 String getWidget()
           
 boolean hasAppliedConstraint(String constraintName)
           
static boolean hasRegisteredConstraint(String constraintName)
           
 boolean isBlank()
           
 boolean isCreditCard()
           
 boolean isDisplay()
           
 boolean isEditable()
           
 boolean isEmail()
           
 boolean isNullable()
           
 boolean isPassword()
           
 boolean isUrl()
           
static void registerNewConstraint(String name, Class constraintClass)
           
static void registerNewConstraint(String name, ConstraintFactory factory)
           
 void setAttributes(Map attributes)
           
 void setBlank(boolean blank)
           
 void setCreditCard(boolean creditCard)
           
 void setDisplay(boolean display)
           
 void setEditable(boolean editable)
           
 void setEmail(boolean email)
           
 void setFormat(String format)
           
 void setInList(List inList)
           
 void setMatches(String regex)
           
 void setMax(Comparable max)
           
 void setMaxSize(Integer maxSize)
           
 void setMessageSource(MessageSource source)
          The message source used to evaluate error messages
 void setMin(Comparable min)
           
 void setMinSize(Integer minSize)
           
 void setNotEqual(Object notEqual)
           
 void setNullable(boolean nullable)
           
 void setOrder(int order)
           
 void setPassword(boolean password)
           
 void setPropertyName(String propertyName)
           
 void setRange(groovy.lang.Range range)
           
 void setSize(groovy.lang.Range size)
           
 void setUrl(boolean url)
           
 void setWidget(String widget)
           
 boolean supportsContraint(String constraintName)
          Checks with this ConstraintedProperty instance supports applying the specified constraint
 String toString()
           
 void validate(Object target, Object propertyValue, Errors errors)
          Validate this constrainted property against specified property value
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_NULL_MESSAGE_CODE

static final String DEFAULT_NULL_MESSAGE_CODE
See Also:
Constant Field Values

DEFAULT_INVALID_MIN_SIZE_MESSAGE_CODE

static final String DEFAULT_INVALID_MIN_SIZE_MESSAGE_CODE
See Also:
Constant Field Values

DEFAULT_INVALID_MAX_SIZE_MESSAGE_CODE

static final String DEFAULT_INVALID_MAX_SIZE_MESSAGE_CODE
See Also:
Constant Field Values

DEFAULT_NOT_EQUAL_MESSAGE_CODE

static final String DEFAULT_NOT_EQUAL_MESSAGE_CODE
See Also:
Constant Field Values

DEFAULT_INVALID_MIN_MESSAGE_CODE

static final String DEFAULT_INVALID_MIN_MESSAGE_CODE
See Also:
Constant Field Values

DEFAULT_INVALID_MAX_MESSAGE_CODE

static final String DEFAULT_INVALID_MAX_MESSAGE_CODE
See Also:
Constant Field Values

DEFAULT_INVALID_SIZE_MESSAGE_CODE

static final String DEFAULT_INVALID_SIZE_MESSAGE_CODE
See Also:
Constant Field Values

DEFAULT_NOT_INLIST_MESSAGE_CODE

static final String DEFAULT_NOT_INLIST_MESSAGE_CODE
See Also:
Constant Field Values

DEFAULT_INVALID_RANGE_MESSAGE_CODE

static final String DEFAULT_INVALID_RANGE_MESSAGE_CODE
See Also:
Constant Field Values

DEFAULT_INVALID_EMAIL_MESSAGE_CODE

static final String DEFAULT_INVALID_EMAIL_MESSAGE_CODE
See Also:
Constant Field Values

DEFAULT_INVALID_CREDIT_CARD_MESSAGE_CODE

static final String DEFAULT_INVALID_CREDIT_CARD_MESSAGE_CODE
See Also:
Constant Field Values

DEFAULT_INVALID_URL_MESSAGE_CODE

static final String DEFAULT_INVALID_URL_MESSAGE_CODE
See Also:
Constant Field Values

DEFAULT_INVALID_VALIDATOR_MESSAGE_CODE

static final String DEFAULT_INVALID_VALIDATOR_MESSAGE_CODE
See Also:
Constant Field Values

DEFAULT_DOESNT_MATCH_MESSAGE_CODE

static final String DEFAULT_DOESNT_MATCH_MESSAGE_CODE
See Also:
Constant Field Values

DEFAULT_BLANK_MESSAGE_CODE

static final String DEFAULT_BLANK_MESSAGE_CODE
See Also:
Constant Field Values

bundle

protected static final ResourceBundle bundle

CREDIT_CARD_CONSTRAINT

public static final String CREDIT_CARD_CONSTRAINT
See Also:
Constant Field Values

EMAIL_CONSTRAINT

public static final String EMAIL_CONSTRAINT
See Also:
Constant Field Values

BLANK_CONSTRAINT

public static final String BLANK_CONSTRAINT
See Also:
Constant Field Values

RANGE_CONSTRAINT

public static final String RANGE_CONSTRAINT
See Also:
Constant Field Values

IN_LIST_CONSTRAINT

public static final String IN_LIST_CONSTRAINT
See Also:
Constant Field Values

URL_CONSTRAINT

public static final String URL_CONSTRAINT
See Also:
Constant Field Values

MATCHES_CONSTRAINT

public static final String MATCHES_CONSTRAINT
See Also:
Constant Field Values

SIZE_CONSTRAINT

public static final String SIZE_CONSTRAINT
See Also:
Constant Field Values

MIN_CONSTRAINT

public static final String MIN_CONSTRAINT
See Also:
Constant Field Values

MAX_CONSTRAINT

public static final String MAX_CONSTRAINT
See Also:
Constant Field Values

MAX_SIZE_CONSTRAINT

public static final String MAX_SIZE_CONSTRAINT
See Also:
Constant Field Values

MIN_SIZE_CONSTRAINT

public static final String MIN_SIZE_CONSTRAINT
See Also:
Constant Field Values

SCALE_CONSTRAINT

public static final String SCALE_CONSTRAINT
See Also:
Constant Field Values

NOT_EQUAL_CONSTRAINT

public static final String NOT_EQUAL_CONSTRAINT
See Also:
Constant Field Values

NULLABLE_CONSTRAINT

public static final String NULLABLE_CONSTRAINT
See Also:
Constant Field Values

VALIDATOR_CONSTRAINT

public static final String VALIDATOR_CONSTRAINT
See Also:
Constant Field Values

INVALID_SUFFIX

protected static final String INVALID_SUFFIX
See Also:
Constant Field Values

EXCEEDED_SUFFIX

protected static final String EXCEEDED_SUFFIX
See Also:
Constant Field Values

NOTMET_SUFFIX

protected static final String NOTMET_SUFFIX
See Also:
Constant Field Values

NOT_PREFIX

protected static final String NOT_PREFIX
See Also:
Constant Field Values

TOOBIG_SUFFIX

protected static final String TOOBIG_SUFFIX
See Also:
Constant Field Values

TOOLONG_SUFFIX

protected static final String TOOLONG_SUFFIX
See Also:
Constant Field Values

TOOSMALL_SUFFIX

protected static final String TOOSMALL_SUFFIX
See Also:
Constant Field Values

TOOSHORT_SUFFIX

protected static final String TOOSHORT_SUFFIX
See Also:
Constant Field Values

constraints

protected static Map constraints

DEFAULT_MESSAGES

protected static final Map DEFAULT_MESSAGES

LOG

protected static final org.apache.commons.logging.Log LOG

propertyName

protected String propertyName

propertyType

protected Class propertyType

appliedConstraints

protected Map appliedConstraints

owningClass

protected Class owningClass

messageSource

protected MessageSource messageSource
Constructor Detail

ConstrainedProperty

public ConstrainedProperty(Class clazz,
                           String propertyName,
                           Class propertyType)
Constructs a new ConstrainedProperty for the given arguments

Parameters:
clazz - The owning class
propertyName - The name of the property
propertyType - The property type
Method Detail

registerNewConstraint

public static void registerNewConstraint(String name,
                                         Class constraintClass)

registerNewConstraint

public static void registerNewConstraint(String name,
                                         ConstraintFactory factory)

hasRegisteredConstraint

public static boolean hasRegisteredConstraint(String constraintName)

getAppliedConstraints

public Collection getAppliedConstraints()
Returns:
Returns the appliedConstraints.

getAppliedConstraint

public Constraint getAppliedConstraint(String name)
Obtains an applied constraint by name

Parameters:
name - The name of the constraint
Returns:
The applied constraint

hasAppliedConstraint

public boolean hasAppliedConstraint(String constraintName)
Parameters:
constraintName - The name of the constraint to check
Returns:
Returns true if the specified constraint name is being applied to this property

getPropertyType

public Class getPropertyType()
Returns:
Returns the propertyType.

getMax

public Comparable getMax()
Returns:
Returns the max.

setMax

public void setMax(Comparable max)
Parameters:
max - The max to set.

getMin

public Comparable getMin()
Returns:
Returns the min.

setMin

public void setMin(Comparable min)
Parameters:
min - The min to set.

getInList

public List getInList()
Returns:
Returns the inList.

setInList

public void setInList(List inList)
Parameters:
inList - The inList to set.

getRange

public groovy.lang.Range getRange()
Returns:
Returns the range.

setRange

public void setRange(groovy.lang.Range range)
Parameters:
range - The range to set.

getScale

public Integer getScale()
Returns:
The scale, if defined for this property; null, otherwise

getSize

public groovy.lang.Range getSize()
Returns:
Returns the size.

setSize

public void setSize(groovy.lang.Range size)
Parameters:
size - The size to set.

isBlank

public boolean isBlank()
Returns:
Returns the blank.

setBlank

public void setBlank(boolean blank)
Parameters:
blank - The blank to set.

isEmail

public boolean isEmail()
Returns:
Returns the email.

setEmail

public void setEmail(boolean email)
Parameters:
email - The email to set.

isCreditCard

public boolean isCreditCard()
Returns:
Returns the creditCard.

setCreditCard

public void setCreditCard(boolean creditCard)
Parameters:
creditCard - The creditCard to set.

getMatches

public String getMatches()
Returns:
Returns the matches.

setMatches

public void setMatches(String regex)
Parameters:
regex - The matches to set.

getNotEqual

public Object getNotEqual()
Returns:
Returns the notEqual.

getMaxSize

public Integer getMaxSize()
Returns:
Returns the maxSize.

setMaxSize

public void setMaxSize(Integer maxSize)
Parameters:
maxSize - The maxSize to set.

getMinSize

public Integer getMinSize()
Returns:
Returns the minSize.

setMinSize

public void setMinSize(Integer minSize)
Parameters:
minSize - The minLength to set.

setNotEqual

public void setNotEqual(Object notEqual)
Parameters:
notEqual - The notEqual to set.

isNullable

public boolean isNullable()
Returns:
Returns the nullable.

setNullable

public void setNullable(boolean nullable)
Parameters:
nullable - The nullable to set.

getPropertyName

public String getPropertyName()
Returns:
Returns the propertyName.

setPropertyName

public void setPropertyName(String propertyName)
Parameters:
propertyName - The propertyName to set.

isUrl

public boolean isUrl()
Returns:
Returns the url.

setUrl

public void setUrl(boolean url)
Parameters:
url - The url to set.

isDisplay

public boolean isDisplay()
Returns:
Returns the display.

setDisplay

public void setDisplay(boolean display)
Parameters:
display - The display to set.

isEditable

public boolean isEditable()
Returns:
Returns the editable.

setEditable

public void setEditable(boolean editable)
Parameters:
editable - The editable to set.

getOrder

public int getOrder()
Returns:
Returns the order.

setOrder

public void setOrder(int order)
Parameters:
order - The order to set.

getFormat

public String getFormat()

setFormat

public void setFormat(String format)

isPassword

public boolean isPassword()

setPassword

public void setPassword(boolean password)

getAttributes

public Map getAttributes()

setAttributes

public void setAttributes(Map attributes)

getWidget

public String getWidget()

setWidget

public void setWidget(String widget)

setMessageSource

public void setMessageSource(MessageSource source)
The message source used to evaluate error messages

Parameters:
source - The MessageSource instance to use to resolve messages

validate

public void validate(Object target,
                     Object propertyValue,
                     Errors errors)
Validate this constrainted property against specified property value

Parameters:
target - The target object to validate
propertyValue - The value of the property to validate
errors - The Errors instances to report errors to

supportsContraint

public boolean supportsContraint(String constraintName)
Checks with this ConstraintedProperty instance supports applying the specified constraint

Parameters:
constraintName - The name of the constraint
Returns:
True if the constraint is supported

applyConstraint

public void applyConstraint(String constraintName,
                            Object constrainingValue)
Applies a constraint for the specified name and consraint value

Parameters:
constraintName - The name of the constraint
constrainingValue - The constraining value
Throws:
ConstraintException - Thrown when the specified constraint is not supported by this ConstrainedProperty. Use supportsContraint(String constraintName) to check before calling

toString

public String toString()
Overrides:
toString in class Object

addMetaConstraint

public void addMetaConstraint(String name,
                              Object value)
Adds a meta constraints which is a non-validating informational constraint

Parameters:
name - The name of the constraint
value - The value

getMetaConstraintValue

public Object getMetaConstraintValue(String name)
Obtains the value of the named meta constraint

Parameters:
name - The name of the constraint
Returns:
The value


Copyright (c) 2005-2006 The Grails project