Class MetaData

java.lang.Object
rife.validation.MetaData
All Implemented Interfaces:
Cloneable, Constrained, MetaDataBeanAware, MetaDataMerged, Validated

public abstract class MetaData extends Object implements Validated, Constrained, MetaDataMerged, MetaDataBeanAware, Cloneable
This abstract base class can be conveniently used to added Constrained and Validated metadata to a POJO.

Besides implementing all the required interfaces for you, it also sets up the underlying data structures in a lazy fashion. This allows you to benefit from a rich API without the memory overhead when the metadata isn't used.

Since:
1.0
  • Constructor Details

    • MetaData

      public MetaData()
  • Method Details

    • activateMetaData

      public void activateMetaData()
      This method is called at least once and maximum once when any meta-data introspection logic is executed.

      You need to implement this method since it will be called after the underlying validation context has been initialized. Incidentally, by doing all your metadata setup here, you don't enforce a performance penalty at each object construction like when you do this in the default constructor.

      Since:
      1.0
    • setMetaDataBean

      public final void setMetaDataBean(Object bean)
      Description copied from interface: MetaDataBeanAware

      This method will be called by RIFE2 when a new instance of the metadata class has been created.

      Specified by:
      setMetaDataBean in interface MetaDataBeanAware
      Parameters:
      bean - the bean instance that this particular metadata instance has been associated with
    • retrieveMetaDataBean

      public Object retrieveMetaDataBean()
      Description copied from interface: MetaDataBeanAware
      Has to return the bean instance that has been associated with this metadata class instance.
      Specified by:
      retrieveMetaDataBean in interface MetaDataBeanAware
      Returns:
      this metadata's bean instance
    • provideValidatedBean

      public void provideValidatedBean(Validated bean)
      Description copied from interface: Validated
      Provide the bean instance that will be validated.

      By default 'this' will be used.

      Specified by:
      provideValidatedBean in interface Validated
      Parameters:
      bean - the bean instance that will be validated
    • retrieveValidatedBean

      public final Validated retrieveValidatedBean()
      Description copied from interface: Validated
      Retrieves the bean instance that will be validated.
      Specified by:
      retrieveValidatedBean in interface Validated
    • addConstraint

      public void addConstraint(ConstrainedBean constrainedBean)
      Description copied from interface: Constrained
      Add a new constrained bean.

      When several constrained beans are added, they are merged at constraint-level. This means for instance that all previous unique constraints will be replaced by those of the new constrained bean, they will not be combined.

      Specified by:
      addConstraint in interface Constrained
      Parameters:
      constrainedBean - the ConstrainedBean instance that has to be added
      See Also:
    • addConstraint

      public void addConstraint(ConstrainedProperty constrainedProperty)
      Description copied from interface: Constrained
      Add a new constrained property.

      When several of the same constrained properties are added, they are merged at constraint-level. This means for instance that a previous inList constraint will be replaced by the one of the new constrained bean, they will not be combined.

      Specified by:
      addConstraint in interface Constrained
      Parameters:
      constrainedProperty - the ConstrainedProperty instance that has to be added
      See Also:
    • getConstrainedBean

      public ConstrainedBean getConstrainedBean()
      Description copied from interface: Constrained
      Retrieves the constrained bean that has been set for this Constrained instance.
      Specified by:
      getConstrainedBean in interface Constrained
      Returns:
      the requested ConstrainedBean; or

      null if no ConstrainedBean is available.

      See Also:
    • getConstrainedProperties

      public Collection<ConstrainedProperty> getConstrainedProperties()
      Description copied from interface: Constrained
      Returns a collection with all the constrained properties that have been registered.
      Specified by:
      getConstrainedProperties in interface Constrained
      Returns:
      A Collection with all the ConstrainedProperty objects that are registered. If no constrained properties are available, an empty collection will be returned, not null.
      See Also:
    • hasPropertyConstraint

      public boolean hasPropertyConstraint(String name)
      Description copied from interface: Constrained
      Indicates whether this constrained bean contains a particular constraint on at least one of its properties.
      Specified by:
      hasPropertyConstraint in interface Constrained
      Returns:
      true if this constraint is present on at least one of the properties; or

      false otherwise

      See Also:
    • getConstrainedProperty

      public ConstrainedProperty getConstrainedProperty(String propertyName)
      Description copied from interface: Constrained
      Retrieve a registered ConstrainedProperty according to its name.
      Specified by:
      getConstrainedProperty in interface Constrained
      Parameters:
      propertyName - the name of the ConstrainedProperty that has to be retrieved
      Returns:
      the requested ConstrainedProperty; or

      null if no such ConstrainedProperty is available.

      See Also:
    • validate

      public boolean validate()
      Description copied from interface: Validated
      Validates the internal subjects.

      This method is not supposed to reset the validation errors or to start the validation from scratch, but it's intended to add additional errors to an existing collection.

      Specified by:
      validate in interface Validated
      Returns:
      true if no validation errors are present after the validation; or

      false if validation errors are available.

      See Also:
    • validate

      public boolean validate(ValidationContext context)
      Description copied from interface: Validated
      Validates the internal subjects and also validates the bean within the provided ValidationContext

      This method is not supposed to reset the validation errors or to start the validation from scratch, but it's intended to add additional errors to an existing collection.

      Specified by:
      validate in interface Validated
      Parameters:
      context - the ValidationContext in which this bean instance will be additionally validated
      Returns:
      true if no validation errors are present after the validation; or

      false if validation errors are available.

      See Also:
    • resetValidation

      public void resetValidation()
      Description copied from interface: Validated

      Resets the validation by removing all validation errors that are currently present.

      This method is typically used to start a new validation from scratch or to re-validate until all errors have been solved.

      Specified by:
      resetValidation in interface Validated
      See Also:
    • addValidationError

      public void addValidationError(ValidationError error)
      Description copied from interface: Validated
      Add a new validation error explicitly to the collection of already existing errors.

      Note that this method should respect subjects with a limited error amount and only store the first error for these subjects.

      Specified by:
      addValidationError in interface Validated
      Parameters:
      error - the ValidationError to add
      See Also:
    • getValidationErrors

      public Set<ValidationError> getValidationErrors()
      Description copied from interface: Validated
      Returns a set with all the stored ValidationErrors.
      Specified by:
      getValidationErrors in interface Validated
      Returns:
      A Set instance with all the stored ValidationErrors. Note that when no errors are available an empty set is returned, not null.
    • countValidationErrors

      public int countValidationErrors()
      Description copied from interface: Validated
      Counts the number of stored ValidationErrors.
      Specified by:
      countValidationErrors in interface Validated
      Returns:
      The number of stored ValidationErrors.
    • replaceValidationErrors

      public void replaceValidationErrors(Set<ValidationError> errors)
      Description copied from interface: Validated
      Replaces the stored ValidationErrors with a new set of errors.
      Specified by:
      replaceValidationErrors in interface Validated
      Parameters:
      errors - the Set instance that contains all the ValidationErrors that have to be stored.
    • limitSubjectErrors

      public void limitSubjectErrors(String subject)
      Description copied from interface: Validated
      Limits the number of errors for a particular subject so that maximum one ValidationError can be stored for it.
      Specified by:
      limitSubjectErrors in interface Validated
      Parameters:
      subject - the name of the subject that has to be limited.
    • unlimitSubjectErrors

      public void unlimitSubjectErrors(String subject)
      Description copied from interface: Validated
      Unlimits the number of errors for a particular subject so that any number of ValidationErrors can be stored for it.
      Specified by:
      unlimitSubjectErrors in interface Validated
      Parameters:
      subject - the name of the subject that has to be unlimited.
    • getValidatedSubjects

      public List<String> getValidatedSubjects()
      Description copied from interface: Validated
      Returns the list of subjects that this object is able to validate internally through the validate() method.
      Specified by:
      getValidatedSubjects in interface Validated
      Returns:
      a List instance with the names of the internally validated subjects
    • isSubjectValid

      public boolean isSubjectValid(String subject)
      Description copied from interface: Validated
      Checks if a subject is valid.

      This is determined by verifying if there are ValidationErrors present for it. This method will thus not execute a validation action.

      Specified by:
      isSubjectValid in interface Validated
      Parameters:
      subject - the name of the subject that has to be checked.
      Returns:
      true when no errors could be found for the subject; or

      false when errors are present for the subject.

      See Also:
    • makeErrorValid

      public void makeErrorValid(String identifier, String subject)
      Description copied from interface: Validated
      Makes errors for a particular subject and identifier valid.

      This is done by removing all ValidationErrors that are stored with this identifier and subject.

      Specified by:
      makeErrorValid in interface Validated
      Parameters:
      identifier - the name of the error identifier that has to be made
      subject - the name of the subject that has to be made valid. valid.
    • makeSubjectValid

      public void makeSubjectValid(String subject)
      Description copied from interface: Validated
      Makes a subject valid.

      This is done by removing all ValidationErrors that are stored for it.

      Specified by:
      makeSubjectValid in interface Validated
      Parameters:
      subject - the name of the subject that has to be made valid.
    • addGroup

      public ValidationGroup addGroup(String name)
      Description copied from interface: Validated
      Adds a new validation group.
      Specified by:
      addGroup in interface Validated
      Parameters:
      name - the name of the validation group that needs to be created and added
      Returns:
      the newly created ValidationGroup
    • focusGroup

      public void focusGroup(String name)
      Description copied from interface: Validated
      Focuses on one particular validation group, showing only the ValidationErrors that were generated by its ValidationRules.
      Specified by:
      focusGroup in interface Validated
      Parameters:
      name - the name of the validation group that will be focused
    • resetGroup

      public void resetGroup(String name)
      Description copied from interface: Validated
      Removed all the ValidationErrors of a particular validation group.
      Specified by:
      resetGroup in interface Validated
      Parameters:
      name - the name of the validation group that will be focused
    • addRule

      public void addRule(ValidationRule rule)
      Description copied from interface: Validated

      Adds a new validation rule.

      The collection of rules is what is supposed to perform the validation, though any other additional method could be used. At least those rules that have been registered will be evaluated.

      Specified by:
      addRule in interface Validated
      Parameters:
      rule - the rule that will be added
      See Also:
    • addConstrainedPropertyRules

      public List<PropertyValidationRule> addConstrainedPropertyRules(ConstrainedProperty constrainedProperty)
      Description copied from interface: Validated
      Adds the validation rules that are related to a particular ConstrainedProperty.

      If the rules of this property name have already been added before through another ConstrainedProperty instance, its existing ValidationRules will be erased and the previous constraints will be merged into the new ConstrainedProperty before adding its validation rules.

      Specified by:
      addConstrainedPropertyRules in interface Validated
      Parameters:
      constrainedProperty - the ConstrainedProperty that will be inspected
      Returns:
      the list of generated ValidationRules
    • generateConstrainedPropertyRules

      public List<PropertyValidationRule> generateConstrainedPropertyRules(ConstrainedProperty constrainedProperty)
      Description copied from interface: Validated
      Generates the validation rules that are related to a particular ConstrainedProperty.
      Specified by:
      generateConstrainedPropertyRules in interface Validated
      Parameters:
      constrainedProperty - the ConstrainedProperty that will be inspected
      Returns:
      the list of generated ValidationRules
    • getRules

      public List<ValidationRule> getRules()
      Description copied from interface: Validated
      Retrieves that validation rules that have been registered.
      Specified by:
      getRules in interface Validated
      See Also:
    • getGroups

      public Collection<ValidationGroup> getGroups()
      Description copied from interface: Validated
      Retrieves all validation groups.
      Specified by:
      getGroups in interface Validated
      Returns:
      the collection of all registered validation groups
    • getGroup

      public ValidationGroup getGroup(String name)
      Description copied from interface: Validated
      Retrieve a particular validation group.
      Specified by:
      getGroup in interface Validated
      Parameters:
      name - the name of the validation group that will be retrieved
      Returns:
      the requested ValidationGroup; or

      null if no such validation group exists

    • validateGroup

      public boolean validateGroup(String name)
      Description copied from interface: Validated
      Validate the ValidationRules of a particular validation group.
      Specified by:
      validateGroup in interface Validated
      Parameters:
      name - the name of the validation group that will be retrieved
      Returns:
      true if no validation errors were generated; or

      false otherwise

    • validateGroup

      public boolean validateGroup(String name, ValidationContext context)
      Description copied from interface: Validated
      Validate the ValidationRules of a particular validation group and also validates the entire bean within the provided ValidationContext
      Specified by:
      validateGroup in interface Validated
      Parameters:
      name - the name of the validation group
      context - the ValidationContext in which this bean instance will be additionally validated
      Returns:
      true if no validation errors were generated; or

      false otherwise

    • getLoadingErrors

      public Collection<String> getLoadingErrors(String propertyName)
      Description copied from interface: Validated
      Returns the collection of error messages that occurred during the loading of the content of a certain property.
      Specified by:
      getLoadingErrors in interface Validated
      Parameters:
      propertyName - the name of the property whose loading errors should be obtained
      Returns:
      null if no errors occurred during the loading of the content of the provided property or if the property doesn't exist; or

      the requested collection of error messages

    • clone

      public Object clone() throws CloneNotSupportedException
      Overrides:
      clone in class Object
      Throws:
      CloneNotSupportedException