com.uwyn.rife.site
Class ConstrainedProperty<T extends ConstrainedProperty>

java.lang.Object
  extended by com.uwyn.rife.site.ConstrainedProperty<T>
All Implemented Interfaces:
Cloneable
Direct Known Subclasses:
CmfProperty

public class ConstrainedProperty<T extends ConstrainedProperty>
extends Object
implements Cloneable

A ConstrainedProperty object makes it possible to easily define all constraints for a named property of a bean.

The property name refers to the actual name of the bean property. However, this sometimes doesn't correspond to its conceptual usage. It can be handy to receive constraint violation reports with another conceptual name: the subject name. Notice that this corresponds to the subject that is used in a ValidationError. If no subject name is specified, the property name will be used instead.

It's possible to add constraints to a ConstrainedProperty instance through regular setters, but chainable setters are also available to make it possible to easily define a series of constraints, for example:

ConstrainedProperty constrained = new ConstrainedProperty("password")
    .maxLength(8)
    .notNull(true);

Constrained properties are typically added to a Constrained bean in its constructor. These are the static constraints that will be set for each and every instance of the bean. You'll however most of the time use the MetaData class that provides the activateMetaData method which initializes the constraints on a need-to-have basis. This dramatically reduces memory usage since otherwise all constraints will be initialized for every bean instance, even though you don't use them, for example:

public class Credentials extends MetaData
{
    private String mLogin = null;
    private String mPassword = null;
    private String mLanguage = null;

    public Credentials()
    {
    }

    public activateMetaData()
    {
        addConstraint(new ConstrainedProperty("login").maxLength(6).notNull(true));
        addConstraint(new ConstrainedProperty("password").maxLength(8).notNull(true));
        addConstraint(new ConstrainedProperty("language").notNull(true));
    }

    public void setLogin(String login) { mLogin = login; }
    public String getLogin() { return mLogin; }
    public void setPassword(String password) { mPassword = password; }
    public String getPassword() { return mPassword; }
    public void setLanguage(String language) { mLanguage = language; }
    public String getLanguage() { return mLanguage; }
}

It's however also possible to add constraints to a single bean instance whenever they can't be determined beforehand. These are then dynamic constraints than can be populated at runtime, for example:

Credentials credentials = new Credentials();
credentials.addConstraint(new ConstrainedProperty("language").inList(new String[] {"nl", "fr", "en"}));

Since:
1.0
Version:
$Revision: 3732 $
Author:
Geert Bevin (gbevin[remove] at uwyn dot com)
See Also:
Constrained, ConstrainedBean

Nested Class Summary
 class ConstrainedProperty.ManyToMany
           
 class ConstrainedProperty.ManyToManyAssociation
           
 class ConstrainedProperty.ManyToOne
           
 class ConstrainedProperty.ManyToOneAssociation
           
 
Field Summary
static String AUTO_RETRIEVED
           
static String CACHED_LOADED_DATA
           
static String CONTENT_ATTRIBUTES
           
static String DEFAULT_VALUE
           
static String DISPLAYED_RAW
           
static String EDITABLE
           
static String EMAIL
           
static String FILE
           
static String FORMAT
           
static String FRAGMENT
           
static String IDENTIFIER
           
static String IN_LIST
           
static String LISTED
           
static String MANY_TO_MANY
           
static String MANY_TO_MANY_ASSOCIATION
           
static String MANY_TO_ONE
           
static String MANY_TO_ONE_ASSOCIATION
           
static String MAX_DATE
           
static String MAX_LENGTH
           
protected  Map<String,Object> mConstraints
           
static String MIMETYPE
           
static String MIN_DATE
           
static String MIN_LENGTH
           
protected  List<ConstrainedPropertyListener> mListeners
           
static String NAME
           
static String NOT_EMPTY
           
static String NOT_EQUAL
           
static String NOT_NULL
           
static String ORDINAL
           
static String ORDINAL_RESTRICTION
           
static String PERSISTENT
           
static String POSITION
           
static String RANGE_BEGIN
           
static String RANGE_END
           
static String REGEXP
           
static String REPOSITORY
           
static String SAME_AS
           
static String SAVED
           
static String SCALE
           
static String SPARSE
           
static String TRANSFORMER
           
static String UNIQUE
           
static String URL
           
 
Constructor Summary
ConstrainedProperty(String propertyName)
          Creates a new ConstrainedProperty for the specified property name.
 
Method Summary
 void addListener(ConstrainedPropertyListener listener)
          Adds a new listener.
 T autoRetrieved(boolean autoRetrieved)
          Sets whether the content data of this property should be retrieved automatically from the back-end.
 ConstrainedProperty clone()
           
 T constraint(String name, Object constraintData)
          Sets the data of a particular constraint in a generic fashion.
 T contentAttribute(String name, boolean value)
          Sets a named content attribute for this property that will be converted internally to a String value.
 T contentAttribute(String name, byte value)
          Sets a named content attribute for this property that will be converted internally to a String value.
 T contentAttribute(String name, char value)
          Sets a named content attribute for this property that will be converted internally to a String value.
 T contentAttribute(String name, double value)
          Sets a named content attribute for this property that will be converted internally to a String value.
 T contentAttribute(String name, float value)
          Sets a named content attribute for this property that will be converted internally to a String value.
 T contentAttribute(String name, int value)
          Sets a named content attribute for this property that will be converted internally to a String value.
 T contentAttribute(String name, long value)
          Sets a named content attribute for this property that will be converted internally to a String value.
 T contentAttribute(String name, short value)
          Sets a named content attribute for this property that will be converted internally to a String value.
 T contentAttribute(String name, String value)
          Sets a named content attribute for this property.
 T defaultValue(boolean value)
           
 T defaultValue(byte value)
           
 T defaultValue(char value)
           
 T defaultValue(double value)
           
 T defaultValue(float value)
           
 T defaultValue(int value)
           
 T defaultValue(long value)
           
 T defaultValue(Object value)
           
 T defaultValue(short value)
           
 T displayedRaw(boolean displayedRaw)
           
 T editable(boolean editable)
           
 T email(boolean email)
           
 T file(boolean file)
           
 T format(Format format)
           
 T fragment(boolean fragment)
          Sets whether the content data of this property is a fragment.
 Object getCachedLoadedData()
          Retrieves the cached loaded content data.
 Object getConstraint(String name)
          Retrieves the value of a particular constraint in a generic fashion
 Map<String,Object> getConstraints()
          Retrieves the map of all the constraints.
 Map<String,String> getContentAttributes()
          Retrieves the map of named content attributes for this property.
 Object getDefaultValue()
           
 Format getFormat()
           
 String[] getInList()
           
 ConstrainedProperty.ManyToMany getManyToMany()
           
 ConstrainedProperty.ManyToManyAssociation getManyToManyAssociation()
           
 ConstrainedProperty.ManyToOne getManyToOne()
           
 ConstrainedProperty.ManyToOneAssociation getManyToOneAssociation()
           
 Date getMaxDate()
           
 int getMaxLength()
           
 MimeType getMimeType()
          Retrieves the mime type of the property.
 Date getMinDate()
           
 int getMinLength()
           
 String getName()
          Retrieves the name of this property.
 Object getNotEqual()
          Retrieves the reference object to which the property value can't be equal.
 String getOrdinalRestriction()
          Retrieves the ordinal restriction of this property.
 int getPosition()
          Retrieves the position in which the property should be displayed.
 int getPrecision()
           
 String getPropertyName()
          Retrieves the property name.
 Comparable getRangeBegin()
           
 Comparable getRangeEnd()
           
 String getRegexp()
           
 String getRepository()
          Retrieves the repository where the content data of this property will be stored.
 String getSameAs()
           
 int getScale()
           
 String getSubjectName()
          Retrieves the subject name.
 ContentTransformer getTransformer()
          Retrieves the content transformer of this property.
 boolean hasDefaultValue()
           
 boolean hasLimitedLength()
           
 boolean hasManyToMany()
           
 boolean hasManyToManyAssociation()
           
 boolean hasManyToOne()
           
 boolean hasManyToOneAssociation()
           
 boolean hasMaxLength()
           
 boolean hasMimeType()
          Indicates whether the property has a mime type.
 boolean hasMixLength()
           
 boolean hasName()
          Indicates whether this property has a name.
 boolean hasOrdinalRestriction()
          Indicates whether this property has an ordinal restricting column.
 boolean hasPosition()
          Indicates whether the position of the property is set.
 boolean hasPrecision()
           
 boolean hasRepository()
          Indicates whether this property will be stored in another repository than the default repository.
 boolean hasScale()
           
 boolean hasTransformer()
          Indicates whether this property has a content transformer.
 T identifier(boolean identifier)
          Set whether the property value is an identifier.
 T inList(byte... inList)
           
 T inList(char... inList)
           
 T inList(Collection inList)
           
 T inList(double... inList)
           
 T inList(float... inList)
           
 T inList(int... inList)
           
 T inList(long... inList)
           
 T inList(short... inList)
           
 T inList(String... inList)
           
 boolean isAutoRetrieved()
          Indicates whether the content data of this property is automatically retrieved from the back-end.
 boolean isDisplayedRaw()
           
 boolean isEditable()
           
 boolean isEmail()
           
 boolean isFile()
           
 boolean isFormatted()
           
 boolean isFragment()
          Indicates whether the content data of this property is a fragment.
 boolean isIdentifier()
          Retrieves whether the property is an identifier.
 boolean isInList()
           
 boolean isLimitedDate()
           
 boolean isListed()
          Retrieves whether the property should be included in data lists.
 boolean isNotEmpty()
          Retrieves whether the property value can be empty.
 boolean isNotEqual()
          Retrieves whether the property can't be equal to a specific reference value.
 boolean isNotNull()
          Retrieves whether the property value can be null.
 boolean isOrdinal()
          Indicates whether this property has to be used as an ordinal.
 boolean isPersistent()
           
 boolean isRange()
           
 boolean isSameAs()
           
 boolean isSaved()
           
 boolean isSparse()
           
 boolean isUnique()
          Retrieves whether the property value has to be unique.
 boolean isUrl()
           
 T listed(boolean listed)
          Sets whether the property should be included in data lists.
 T manyToMany()
           
 T manyToMany(Class klass)
           
 T manyToMany(Class klass, CreateTable.ViolationAction onUpdate, CreateTable.ViolationAction onDelete)
           
 T manyToMany(CreateTable.ViolationAction onUpdate, CreateTable.ViolationAction onDelete)
           
 T manyToManyAssociation()
           
 T manyToManyAssociation(Class klass, String property)
           
 T manyToManyAssociation(String property)
           
 T manyToOne()
           
 T manyToOne(Class klass)
           
 T manyToOne(Class klass, String columnReference)
           
 T manyToOne(Class klass, String columnReference, CreateTable.ViolationAction onUpdate, CreateTable.ViolationAction onDelete)
           
 T manyToOne(String table, String columnReference)
           
 T manyToOne(String table, String columnReference, CreateTable.ViolationAction onUpdate, CreateTable.ViolationAction onDelete)
           
 T manyToOneAssociation()
           
 T manyToOneAssociation(Class klass, String property)
           
 T manyToOneAssociation(String property)
           
 boolean matchesRegexp()
           
 T maxDate(Date maxDate)
           
 T maxLength(int maxLength)
           
 T mimeType(MimeType mimeType)
          Sets the mime type of the property.
 T minDate(Date minDate)
           
 T minLength(int minLength)
           
 T name(String name)
          Sets the name of the content data of this property.
 T notEmpty(boolean notEmpty)
          Set whether the property value can be empty.
 T notEqual(boolean reference)
          Set that the property value can't be equal to a specified boolean reference value.
 T notEqual(byte reference)
          Set that the property value can't be equal to a specified byte reference value.
 T notEqual(char reference)
          Set that the property value can't be equal to a specified char reference value.
 T notEqual(double reference)
          Set that the property value can't be equal to a specified double reference value.
 T notEqual(float reference)
          Set that the property value can't be equal to a specified float reference value.
 T notEqual(int reference)
          Set that the property value can't be equal to a specified int reference value.
 T notEqual(long reference)
          Set that the property value can't be equal to a specified long reference value.
 T notEqual(Object reference)
          Set that the property value can't be equal to a specified Object reference value.
 T notEqual(short reference)
          Set that the property value can't be equal to a specified short reference value.
 T notNull(boolean notNull)
          Set whether the property value can be null.
 T ordinal(boolean ordinal)
          Sets whether this property has to be used as an ordinal.
 T ordinal(boolean ordinal, String restriction)
          Sets whether this property has to be used as an ordinal with a restricting column.
 T persistent(boolean persistent)
           
 T position(int position)
          Sets the position in which the property should be displayed.
 T precision(int precision)
           
 T rangeBegin(byte value)
           
 T rangeBegin(char value)
           
 T rangeBegin(Comparable value)
           
 T rangeBegin(double value)
           
 T rangeBegin(float value)
           
 T rangeBegin(int value)
           
 T rangeBegin(long value)
           
 T rangeBegin(short value)
           
 T rangeEnd(byte value)
           
 T rangeEnd(char value)
           
 T rangeEnd(Comparable value)
           
 T rangeEnd(double value)
           
 T rangeEnd(float value)
           
 T rangeEnd(int value)
           
 T rangeEnd(long value)
           
 T rangeEnd(short value)
           
 T regexp(String regexp)
           
 boolean removeListener(ConstrainedPropertyListener listener)
          Removes a listener.
 T repository(String repository)
          Sets the repository where the content data of this property will be stored.
 T sameAs(String reference)
           
 T saved(boolean saved)
           
 T scale(int scale)
           
 void setAutoRetrieved(boolean autoRetrieved)
          Sets whether the content data of this property should be retrieved automatically from the back-end.
 void setCachedLoadedData(Object data)
          Sets the cached loaded data.
 void setConstraint(String name, Object constraintData)
          Sets the data of a particular constraint in a generic fashion.
 void setDefaultValue(Object value)
           
 void setDisplayedRaw(boolean displayedRaw)
           
 void setEditable(boolean editable)
           
 void setEmail(boolean email)
           
 void setFile(boolean file)
           
 void setFormat(Format format)
           
 void setFragment(boolean fragment)
          Sets whether the content data of this property is a fragment.
 void setIdentifier(boolean identifier)
          Set whether the property value is an identifier.
 void setInList(byte... inList)
           
 void setInList(char... inList)
           
 void setInList(Collection inList)
           
 void setInList(double... inList)
           
 void setInList(float... inList)
           
 void setInList(int... inList)
           
 void setInList(long... inList)
           
 void setInList(short... inList)
           
 void setInList(String... inList)
           
 void setListed(boolean listed)
          Sets whether the property should be included in data lists.
 void setManyToMany()