fri.util.database.jpa.tree.uniqueconstraints
Class AbstractUniqueTreeConstraintImpl<N extends TreeNode>

java.lang.Object
  extended by fri.util.database.jpa.tree.uniqueconstraints.AbstractUniqueTreeConstraintImpl<N>
All Implemented Interfaces:
UniqueTreeConstraint<N>
Direct Known Subclasses:
AbstractWholeTreeUniqueConstraintImpl

public abstract class AbstractUniqueTreeConstraintImpl<N extends TreeNode>
extends java.lang.Object
implements UniqueTreeConstraint<N>

Basic unique constraint check implementation. Provides utility methods to check uniqueness (1) in children and (2) in whole tree.

This also provides a check for unique roots. Which of these two checks is performed (roots or tree) is managed internally (depends on the arguments of checkUniqueConstraint(), see isRootsCheck()).

Author:
Fritz Ritzberger, 27.10.2012

Field Summary
protected  boolean shouldCheckRootsForUniqueness
          True when constraint should also check that no root has the same unique property set as another root.
protected  java.lang.String[][] uniquePropertyNames
          One or several property name sets that represent this unique constraint.
 
Constructor Summary
protected AbstractUniqueTreeConstraintImpl(java.lang.String[][] uniquePropertyNames, boolean shouldCheckRootsForUniqueness)
           
 
Method Summary
protected abstract  void appendNodeCheckingCondition(N root, java.lang.StringBuilder queryText, java.util.List<java.lang.Object> parameters)
           
protected abstract  void appendRootCheckingCondition(java.lang.StringBuilder queryText, java.util.List<java.lang.Object> parameters)
           
protected  void appendUniquenessConditions(N node, java.lang.StringBuilder queryText, java.util.List<java.lang.Object> parameters, java.lang.String tableAlias)
          Appends unique property conditions to queryText and parameters, not starting with "AND".
protected  void appendUpdateCondition(N node, java.lang.StringBuilder queryText, java.util.List<java.lang.Object> parameters, java.lang.String tableAlias, TreeActionLocation<N> location)
          When node is persistent and action is neither MOVE nor INSERT, appends a condition saying "node != originalNode", thus avoiding the node to detect itself.
protected  void beforeCheckUniqueness(java.lang.StringBuilder queryText, java.util.List<java.lang.Object> parameters)
          Does nothing.
protected  boolean checkUniqueChildrenConstraint(N node, TreeActionLocation<N> location)
          Checks if passed node would be unique in the children of given location, but can not check roots.
protected  boolean checkUniqueWholeTreeConstraint(java.util.List<N> nodes, TreeActionLocation<N> location)
          Checks if passed node would be unique within its tree.
protected  boolean equal(N n1, N n2)
          Delegates to dao.equal().
protected abstract  java.lang.String fromClause()
           
protected  TreeDao<N> getDao()
           
protected abstract  java.lang.String getNodeTableAlias()
           
protected  java.lang.Object getNodeValueForProperty(java.lang.String uniqueName, java.lang.Object node)
          Retrieves a named value from given Object by reflection.
protected  DbSession getSession()
           
protected  boolean isDifferent(java.lang.Object newValue, java.lang.Object existingValue)
          Equality of node property values.
protected  boolean isRootsCheck(N root, N node)
           
protected  java.lang.String nodeEntityName()
           
protected  java.lang.String pathEntityName()
           
 void setContext(DbSession session, TreeDao<N> dao, java.lang.String nodeEntityName, java.lang.String pathEntityName)
          Called by the DAO to publish context information to this constraint-checker.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface fri.util.database.jpa.tree.uniqueconstraints.UniqueTreeConstraint
checkUniqueConstraint
 

Field Detail

uniquePropertyNames

protected final java.lang.String[][] uniquePropertyNames
One or several property name sets that represent this unique constraint.


shouldCheckRootsForUniqueness

protected final boolean shouldCheckRootsForUniqueness
True when constraint should also check that no root has the same unique property set as another root.

Constructor Detail

AbstractUniqueTreeConstraintImpl

protected AbstractUniqueTreeConstraintImpl(java.lang.String[][] uniquePropertyNames,
                                           boolean shouldCheckRootsForUniqueness)
Parameters:
uniquePropertyNames - JPQL property names that should be checked for uniqueness. For example, to check (1) "name" combined with "code", and (2) "acronym", for uniqueness, pass in new String [][] { { "name", "code" }, { "acronym" } }.
shouldCheckRootsForUniqueness - true when constraint should also check that no root has the same unique property set as another root.
Method Detail

setContext

public void setContext(DbSession session,
                       TreeDao<N> dao,
                       java.lang.String nodeEntityName,
                       java.lang.String pathEntityName)
Description copied from interface: UniqueTreeConstraint
Called by the DAO to publish context information to this constraint-checker.

Specified by:
setContext in interface UniqueTreeConstraint<N extends TreeNode>
Parameters:
session - the database session to use for uniqueness checks.
dao - the calling DAO, provides tree access for reading children, parents, etc.
nodeEntityName - name of the JPQL node database table.
pathEntityName - optionally the name of the JPQL tree-path database table, can be null, this is for ClosureTable only.

nodeEntityName

protected final java.lang.String nodeEntityName()
Returns:
the nodeEntity name passed in as context.

pathEntityName

protected final java.lang.String pathEntityName()
Returns:
the pathEntity name passed in as context, is null for the nested-sets-tree.

getDao

protected final TreeDao<N> getDao()
Returns:
the DAO passed in as context.

getSession

protected final DbSession getSession()
Returns:
the database session passed in as context.

getNodeValueForProperty

protected final java.lang.Object getNodeValueForProperty(java.lang.String uniqueName,
                                                         java.lang.Object node)
Retrieves a named value from given Object by reflection. The uniqueName is case-sensitive.


checkUniqueChildrenConstraint

protected boolean checkUniqueChildrenConstraint(N node,
                                                TreeActionLocation<N> location)
Checks if passed node would be unique in the children of given location, but can not check roots.


equal

protected final boolean equal(N n1,
                              N n2)
Delegates to dao.equal().


isDifferent

protected boolean isDifferent(java.lang.Object newValue,
                              java.lang.Object existingValue)
Equality of node property values. To be overridden.

Returns:
true if the given property values are equal, or one or both are null, else false.

checkUniqueWholeTreeConstraint

protected final boolean checkUniqueWholeTreeConstraint(java.util.List<N> nodes,
                                                       TreeActionLocation<N> location)
Checks if passed node would be unique within its tree.


getNodeTableAlias

protected abstract java.lang.String getNodeTableAlias()
Returns:
the node table alias (not the path table alias!) to be used for appending unique constraint conditions.

fromClause

protected abstract java.lang.String fromClause()
Returns:
the FROM clause contents, not starting with "from", but closing with "where" or "and".

appendRootCheckingCondition

protected abstract void appendRootCheckingCondition(java.lang.StringBuilder queryText,
                                                    java.util.List<java.lang.Object> parameters)

appendNodeCheckingCondition

protected abstract void appendNodeCheckingCondition(N root,
                                                    java.lang.StringBuilder queryText,
                                                    java.util.List<java.lang.Object> parameters)

isRootsCheck

protected boolean isRootsCheck(N root,
                               N node)
Parameters:
root - the root of the node about to be inserted or updated, must be null for root checks.
node - the node about to be updated, null when this is an insert.
Returns:
true when the given entities indicate a uniqueness check among roots.

appendUpdateCondition

protected void appendUpdateCondition(N node,
                                     java.lang.StringBuilder queryText,
                                     java.util.List<java.lang.Object> parameters,
                                     java.lang.String tableAlias,
                                     TreeActionLocation<N> location)
When node is persistent and action is neither MOVE nor INSERT, appends a condition saying "node != originalNode", thus avoiding the node to detect itself. Ends with "AND".


appendUniquenessConditions

protected final void appendUniquenessConditions(N node,
                                                java.lang.StringBuilder queryText,
                                                java.util.List<java.lang.Object> parameters,
                                                java.lang.String tableAlias)
Appends unique property conditions to queryText and parameters, not starting with "AND".


beforeCheckUniqueness

protected void beforeCheckUniqueness(java.lang.StringBuilder queryText,
                                     java.util.List<java.lang.Object> parameters)
Does nothing. Called with ready-made uniqueness query before it is executed. To be overridden for appending temporal conditions. When overriding, use getPathTableAlias() or getNodeTableAlias() to point to the correct table.