biz.c24.io.api.data
Class DatabaseLookupValidationRule

java.lang.Object
  extended by biz.c24.io.api.data.DefaultValidationRule
      extended by biz.c24.io.api.data.DatabaseLookupValidationRule
All Implemented Interfaces:
ModelComponent, ValidationRule, Validator, Serializable

public class DatabaseLookupValidationRule
extends DefaultValidationRule

A validator implementation for on-the-fly validation against a database without loading all the data into memory. A select statement is issued to validate each value.

The select query should use '%' (percentage sign) as a place holder for the value that needs validation. e.g. SELECT * FROM MyCodeTable WHERE MyCodeColumn = '%'

If the query returns a result, then the validation passes.

When used in production it is imperative to handle the connection management / pooling outside of this class. This can be done either by setting the property 'hibernate.connection.datasource' in the rule definition or via the useSession(org.hibernate.classic.Session) method. NOTE: Please ensure that the Hibernate & required 3rd party libraries added to your classpath (hibernate3.jar etc.). For more information about the Hibernate please visit: http://www.hibernate.org/

See Also:
Serialized Form

Constructor Summary
DatabaseLookupValidationRule(String sqlQuery, Properties properties)
          Creates a new lookup validator which will use the given properties and execute the supplied SQL query.
 
Method Summary
 Properties getProperties()
          Returns the database configuration properties (not used if an external session is specified).
 String getSqlQuery()
          Returns the SQL query being executed.
 void setProperties(Properties properties)
          Sets the database configuration properties.
 void setSqlQuery(String sqlQuery)
          Sets the SQL query to be executed.
 String toString()
           
 void useSession(org.hibernate.classic.Session session)
          Specifies an external Hibernate Session to be used to connect to the database.
 boolean validate(Object object, DataComponent component, ComplexDataObject context, ValidationManager manager)
          Validate the given object.
 
Methods inherited from class biz.c24.io.api.data.DefaultValidationRule
addDependentPredicateCode, getAnnotation, getContextForPredicateChecking, getDependentPredicateCode, getDependentPredicateCodeCount, getErrorCode, getName, getPredicateCode, getSeverity, removeDependentPredicateCode, setAnnotation, setErrorCode, setName, setPredicateCode, setSeverity
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DatabaseLookupValidationRule

public DatabaseLookupValidationRule(String sqlQuery,
                                    Properties properties)
Creates a new lookup validator which will use the given properties and execute the supplied SQL query.

Parameters:
sqlQuery - the query that tests whether the value is valid.
properties - The configuration properties containing database connection details (may be null).
Method Detail

useSession

public void useSession(org.hibernate.classic.Session session)
Specifies an external Hibernate Session to be used to connect to the database.

This method should be used to control connection pooling and thereby prevent the default behaviour of reconnecting to the database for every validation.

Parameters:
session - The session to be used, or null to use a new session for each validation.

validate

public boolean validate(Object object,
                        DataComponent component,
                        ComplexDataObject context,
                        ValidationManager manager)
                 throws ValidationException
Description copied from interface: Validator
Validate the given object.

Specified by:
validate in interface Validator
Overrides:
validate in class DefaultValidationRule
Parameters:
object - The object to be validated - commonly a String, Integer, Double, Date etc.
component - The Element or Attribute inside context which defines object.
context - The parent of object - could be null.
manager - The validation manager which is performing this validation.
Returns:
Whether the object is valid if the validation manager is not operating in exception mode.
Throws:
ValidationException - If the object is not valid and the validation maanger is operating in exception mode.

getSqlQuery

public String getSqlQuery()
Returns the SQL query being executed.

The '%' symbol is replaced with the data to be validated.

Returns:
The SQL query.

setSqlQuery

public void setSqlQuery(String sqlQuery)
Sets the SQL query to be executed. The '%' symbol is replaced with the data to be validated.

Parameters:
sqlQuery - The SQL query.

getProperties

public Properties getProperties()
Returns the database configuration properties (not used if an external session is specified).

Returns:
The properties.

setProperties

public void setProperties(Properties properties)
Sets the database configuration properties.

Parameters:
properties - The properties.

toString

public String toString()
Overrides:
toString in class DefaultValidationRule


C24 Technologies © 2002-2012: All Rights Reserved.