org.jcrom.dao
Class AbstractJcrDAO<T>

java.lang.Object
  extended by org.jcrom.dao.AbstractJcrDAO<T>
All Implemented Interfaces:
JcrDAO<T>

public abstract class AbstractJcrDAO<T>
extends java.lang.Object
implements JcrDAO<T>

An abstract implementation of the JcrDAO interface. This should be extended for specific entity implementations. This class implements all the methods defined in the JcrDAO interface, and provides a few protected methods that are useful for implementing custom finder methods.

The constructor takes a JCR session, so an instance should be created per session. The constructor also takes a Jcrom instance that can be shared across multiple DAOs.

This implementation encapsulates exceptions in JcrMappingException, which is a RuntimeException.

Author:
Olafur Gauti Gudmundsson

Field Summary
protected  java.lang.Class<T> entityClass
           
protected  boolean isVersionable
           
protected  Jcrom jcrom
           
protected  java.lang.String[] mixinTypes
           
protected  javax.jcr.Session session
           
 
Constructor Summary
AbstractJcrDAO(java.lang.Class<T> entityClass, Jcrom jcrom)
          Use this constructor when you intend to override the getSession() method to provide your own session management (for example via Guice providers).
AbstractJcrDAO(java.lang.Class<T> entityClass, javax.jcr.Session session, Jcrom jcrom)
          Constructor.
AbstractJcrDAO(java.lang.Class<T> entityClass, javax.jcr.Session session, Jcrom jcrom, java.lang.String[] mixinTypes)
          Constructor.
 
Method Summary
 T create(java.lang.String parentNodePath, T entity)
          Creates a new JCR Node from the object supplied, under the parent node with the path specified.
 T create(T entity)
          Creates a new JCR Node from the object supplied.
 boolean exists(java.lang.String path)
          Check whether an entity with the path supplied exists in JCR.
 java.util.List<T> findAll(java.lang.String rootPath)
          Find all entities represented by this DAO.
 java.util.List<T> findAll(java.lang.String rootPath, long startIndex, long resultSize)
          Find all entities represented by this DAO.
 java.util.List<T> findAll(java.lang.String rootPath, java.lang.String childNameFilter, int maxDepth)
          Find all entities represented by this DAO.
 java.util.List<T> findAll(java.lang.String rootPath, java.lang.String childNameFilter, int maxDepth, long startIndex, long resultSize)
          Find all entities represented by this DAO.
protected  java.util.List<T> findByXPath(java.lang.String xpath, java.lang.String childNameFilter, int maxDepth)
          Find JCR nodes that match the xpath supplied, and map to objects.
protected  java.util.List<T> findByXPath(java.lang.String xpath, java.lang.String childNameFilter, int maxDepth, long startIndex, long resultSize)
          Find JCR nodes that match the xpath supplied, and map to objects.
 T get(java.lang.String path)
          Get an entity from JCR by path (from a @JcrPath field).
 T get(java.lang.String path, java.lang.String childNodeFilter, int maxDepth)
          Get an entity from JCR by path (from a @JcrPath field).
protected  javax.jcr.Session getSession()
           
 long getSize(java.lang.String rootPath)
          Get the number of entities.
protected  T getVersion(javax.jcr.Node node, java.lang.String versionName, java.lang.String childNodeFilter, int maxDepth)
           
 T getVersion(java.lang.String path, java.lang.String versionName)
           
 T getVersion(java.lang.String path, java.lang.String versionName, java.lang.String childNodeFilter, int maxDepth)
           
 T getVersionByUUID(java.lang.String uuid, java.lang.String versionName)
           
 T getVersionByUUID(java.lang.String uuid, java.lang.String versionName, java.lang.String childNodeFilter, int maxDepth)
           
protected  java.util.List<T> getVersionList(javax.jcr.Node node, java.lang.String childNameFilter, int maxDepth)
           
protected  java.util.List<T> getVersionList(javax.jcr.Node node, java.lang.String childNameFilter, int maxDepth, long startIndex, long resultSize)
           
 java.util.List<T> getVersionList(java.lang.String path)
           
 java.util.List<T> getVersionList(java.lang.String path, java.lang.String childNameFilter, int maxDepth)
           
 java.util.List<T> getVersionList(java.lang.String path, java.lang.String childNameFilter, int maxDepth, long startIndex, long resultSize)
           
 java.util.List<T> getVersionListByUUID(java.lang.String uuid)
           
 java.util.List<T> getVersionListByUUID(java.lang.String uuid, java.lang.String childNameFilter, int maxDepth)
           
 java.util.List<T> getVersionListByUUID(java.lang.String uuid, java.lang.String childNameFilter, int maxDepth, long startIndex, long resultSize)
           
protected  long getVersionSize(javax.jcr.Node node)
           
 long getVersionSize(java.lang.String path)
           
 long getVersionSizeByUUID(java.lang.String uuid)
           
protected  boolean hasMixinType(javax.jcr.Node node, java.lang.String mixinType)
           
 T loadByUUID(java.lang.String uuid)
          Load an entity from JCR by UUID lookup.
 T loadByUUID(java.lang.String uuid, java.lang.String childNodeFilter, int maxDepth)
          Load an entity from JCR by UUID lookup.
 void move(T entity, java.lang.String newParentPath)
          Move an entity to the parent with the path specified.
protected  java.lang.String relativePath(java.lang.String absolutePath)
           
 void remove(java.lang.String path)
          Permanently remove the entity with the path supplied (from a @JcrPath field).
 void removeByUUID(java.lang.String uuid)
          Permanently remove the entity with the UUID supplied.
protected  void removeVersion(javax.jcr.Node node, java.lang.String versionName)
           
 void removeVersion(java.lang.String path, java.lang.String versionName)
           
 void removeVersionByUUID(java.lang.String uuid, java.lang.String versionName)
           
protected  void restoreVersion(javax.jcr.Node node, java.lang.String versionName, boolean removeExisting)
           
 void restoreVersion(java.lang.String path, java.lang.String versionName)
           
 void restoreVersion(java.lang.String path, java.lang.String versionName, boolean removeExisting)
           
 void restoreVersionByUUID(java.lang.String uuid, java.lang.String versionName)
           
 void restoreVersionByUUID(java.lang.String uuid, java.lang.String versionName, boolean removeExisting)
           
protected  java.util.List<T> toList(javax.jcr.NodeIterator nodeIterator, java.lang.String childNameFilter, int maxDepth)
          Maps JCR nodes to a List of JcrEntity implementations.
protected  java.util.List<T> toList(javax.jcr.NodeIterator nodeIterator, java.lang.String childNameFilter, int maxDepth, long resultSize)
          Maps JCR nodes to a List of JcrEntity implementations.
protected  java.lang.String update(javax.jcr.Node node, T entity, java.lang.String childNodeFilter, int maxDepth)
           
 java.lang.String update(T entity)
          Updates an existing JCR Node with the values extracted from the object supplied.
 java.lang.String update(T entity, java.lang.String childNodeFilter, int maxDepth)
          Updates an existing JCR Node with the values extracted from the object supplied.
 java.lang.String updateByUUID(T entity, java.lang.String uuid)
          Updates an existing JCR Node with the values extracted from the object supplied.
 java.lang.String updateByUUID(T entity, java.lang.String uuid, java.lang.String childNodeFilter, int maxDepth)
          Updates an existing JCR Node with the values extracted from the object supplied.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

jcrom

protected final Jcrom jcrom

session

protected final javax.jcr.Session session

entityClass

protected final java.lang.Class<T> entityClass

mixinTypes

protected final java.lang.String[] mixinTypes

isVersionable

protected final boolean isVersionable
Constructor Detail

AbstractJcrDAO

public AbstractJcrDAO(java.lang.Class<T> entityClass,
                      Jcrom jcrom)
Use this constructor when you intend to override the getSession() method to provide your own session management (for example via Guice providers). You can also use the other constructors and just pass null as the session.

Parameters:
entityClass - the class handled by this DAO implementation
jcrom - the Jcrom instance to use for object mapping

AbstractJcrDAO

public AbstractJcrDAO(java.lang.Class<T> entityClass,
                      javax.jcr.Session session,
                      Jcrom jcrom)
Constructor.

Parameters:
entityClass - the class handled by this DAO implementation
session - the current JCR session
jcrom - the Jcrom instance to use for object mapping

AbstractJcrDAO

public AbstractJcrDAO(java.lang.Class<T> entityClass,
                      javax.jcr.Session session,
                      Jcrom jcrom,
                      java.lang.String[] mixinTypes)
Constructor.

Parameters:
entityClass - the class handled by this DAO implementation
session - the current JCR session
jcrom - the Jcrom instance to use for object mapping
mixinTypes - an array of mixin types to apply to new nodes
Method Detail

getSession

protected javax.jcr.Session getSession()

relativePath

protected java.lang.String relativePath(java.lang.String absolutePath)

create

public T create(T entity)
Description copied from interface: JcrDAO
Creates a new JCR Node from the object supplied. The path to the parent node will be retrieved from the entity path. A path of "/" means that the entity will be created on root level.

Specified by:
create in interface JcrDAO<T>
Parameters:
entity - the object to be mapped to a JCR node
Returns:
the newly created Object

create

public T create(java.lang.String parentNodePath,
                T entity)
Description copied from interface: JcrDAO
Creates a new JCR Node from the object supplied, under the parent node with the path specified.

Specified by:
create in interface JcrDAO<T>
Parameters:
parentNodePath - path to the parent node
entity - the object to be mapped to a JCR node
Returns:
the newly created Object

update

public java.lang.String update(T entity)
Description copied from interface: JcrDAO
Updates an existing JCR Node with the values extracted from the object supplied.

Specified by:
update in interface JcrDAO<T>
Parameters:
entity - the object to be mapped to a JCR node
Returns:
the name of the JCR Node that was updated

update

public java.lang.String update(T entity,
                               java.lang.String childNodeFilter,
                               int maxDepth)
Description copied from interface: JcrDAO
Updates an existing JCR Node with the values extracted from the object supplied.

Specified by:
update in interface JcrDAO<T>
Parameters:
entity - the object to be mapped to a JCR node
childNodeFilter - comma separated list of names of child nodes to load ("*" loads all, "none" loads no children, and "-" at the beginning makes it an exclusion filter)
maxDepth - the maximum depth of loaded child nodes (0 means no child nodes are loaded, while a negative value means that no restrictions are set on the depth).
Returns:
the name of the JCR Node that was updated

updateByUUID

public java.lang.String updateByUUID(T entity,
                                     java.lang.String uuid)
Description copied from interface: JcrDAO
Updates an existing JCR Node with the values extracted from the object supplied. The node to update is loaded using the UUID supplied.

Specified by:
updateByUUID in interface JcrDAO<T>
Parameters:
entity - the object to be mapped to a JCR node
uuid - the JCR UUID of the node to update
Returns:
the name of the node that was updated

updateByUUID

public java.lang.String updateByUUID(T entity,
                                     java.lang.String uuid,
                                     java.lang.String childNodeFilter,
                                     int maxDepth)
Description copied from interface: JcrDAO
Updates an existing JCR Node with the values extracted from the object supplied. The node to update is loaded using the UUID supplied.

Specified by:
updateByUUID in interface JcrDAO<T>
Parameters:
entity - the object to be mapped to a JCR node
uuid - the UUID of the node to update
childNodeFilter - comma separated list of names of child nodes to load ("*" loads all, "none" loads no children, and "-" at the beginning makes it an exclusion filter)
maxDepth - the maximum depth of loaded child nodes (0 means no child nodes are loaded, while a negative value means that no restrictions are set on the depth).
Returns:
the name of the JCR Node that was updated

update

protected java.lang.String update(javax.jcr.Node node,
                                  T entity,
                                  java.lang.String childNodeFilter,
                                  int maxDepth)

hasMixinType

protected boolean hasMixinType(javax.jcr.Node node,
                               java.lang.String mixinType)
                        throws javax.jcr.RepositoryException
Throws:
javax.jcr.RepositoryException

move

public void move(T entity,
                 java.lang.String newParentPath)
Description copied from interface: JcrDAO
Move an entity to the parent with the path specified. Handles checkout and checkin for mix:versionable parents.

Specified by:
move in interface JcrDAO<T>

remove

public void remove(java.lang.String path)
Description copied from interface: JcrDAO
Permanently remove the entity with the path supplied (from a @JcrPath field).

Specified by:
remove in interface JcrDAO<T>
Parameters:
path - the full path of the entity

removeByUUID

public void removeByUUID(java.lang.String uuid)
Description copied from interface: JcrDAO
Permanently remove the entity with the UUID supplied.

Specified by:
removeByUUID in interface JcrDAO<T>
Parameters:
uuid - the JCR UUID of the entity

exists

public boolean exists(java.lang.String path)
Description copied from interface: JcrDAO
Check whether an entity with the path supplied exists in JCR.

Specified by:
exists in interface JcrDAO<T>
Parameters:
path - the path of the entity
Returns:
true if the entity exists, else false

get

public T get(java.lang.String path)
Description copied from interface: JcrDAO
Get an entity from JCR by path (from a @JcrPath field).

Specified by:
get in interface JcrDAO<T>
Parameters:
path - the full path of the entity to be loaded
Returns:
an object instance mapped from the JCR node with the path supplied, or null if no such node was found

get

public T get(java.lang.String path,
             java.lang.String childNodeFilter,
             int maxDepth)
Description copied from interface: JcrDAO
Get an entity from JCR by path (from a @JcrPath field).

Specified by:
get in interface JcrDAO<T>
Parameters:
path - the full path of the entity to be loaded
maxDepth - the maximum depth of loaded child nodes (0 means no child nodes are loaded, while a negative value means that no restrictions are set on the depth).
Returns:
an object instance mapped from the JCR node with the path supplied, or null if no such node was found

loadByUUID

public T loadByUUID(java.lang.String uuid)
Description copied from interface: JcrDAO
Load an entity from JCR by UUID lookup.

Specified by:
loadByUUID in interface JcrDAO<T>
Parameters:
uuid - the UUID generated by JCR
Returns:
an object instance mapped from the JCR node with the uuid

loadByUUID

public T loadByUUID(java.lang.String uuid,
                    java.lang.String childNodeFilter,
                    int maxDepth)
Description copied from interface: JcrDAO
Load an entity from JCR by UUID lookup.

Specified by:
loadByUUID in interface JcrDAO<T>
Parameters:
uuid - the UUID generated by JCR
Returns:
an object instance mapped from the JCR node with the uuid

getVersion

public T getVersion(java.lang.String path,
                    java.lang.String versionName)
Specified by:
getVersion in interface JcrDAO<T>

getVersion

public T getVersion(java.lang.String path,
                    java.lang.String versionName,
                    java.lang.String childNodeFilter,
                    int maxDepth)
Specified by:
getVersion in interface JcrDAO<T>

getVersionByUUID

public T getVersionByUUID(java.lang.String uuid,
                          java.lang.String versionName)
Specified by:
getVersionByUUID in interface JcrDAO<T>

getVersionByUUID

public T getVersionByUUID(java.lang.String uuid,
                          java.lang.String versionName,
                          java.lang.String childNodeFilter,
                          int maxDepth)
Specified by:
getVersionByUUID in interface JcrDAO<T>

getVersion

protected T getVersion(javax.jcr.Node node,
                       java.lang.String versionName,
                       java.lang.String childNodeFilter,
                       int maxDepth)

restoreVersion

public void restoreVersion(java.lang.String path,
                           java.lang.String versionName)
Specified by:
restoreVersion in interface JcrDAO<T>

restoreVersionByUUID

public void restoreVersionByUUID(java.lang.String uuid,
                                 java.lang.String versionName)
Specified by:
restoreVersionByUUID in interface JcrDAO<T>

restoreVersion

public void restoreVersion(java.lang.String path,
                           java.lang.String versionName,
                           boolean removeExisting)
Specified by:
restoreVersion in interface JcrDAO<T>

restoreVersionByUUID

public void restoreVersionByUUID(java.lang.String uuid,
                                 java.lang.String versionName,
                                 boolean removeExisting)
Specified by:
restoreVersionByUUID in interface JcrDAO<T>

restoreVersion

protected void restoreVersion(javax.jcr.Node node,
                              java.lang.String versionName,
                              boolean removeExisting)

removeVersion

public void removeVersion(java.lang.String path,
                          java.lang.String versionName)
Specified by:
removeVersion in interface JcrDAO<T>

removeVersionByUUID

public void removeVersionByUUID(java.lang.String uuid,
                                java.lang.String versionName)
Specified by:
removeVersionByUUID in interface JcrDAO<T>

removeVersion

protected void removeVersion(javax.jcr.Node node,
                             java.lang.String versionName)

getVersionSize

public long getVersionSize(java.lang.String path)
Specified by:
getVersionSize in interface JcrDAO<T>

getVersionSizeByUUID

public long getVersionSizeByUUID(java.lang.String uuid)
Specified by:
getVersionSizeByUUID in interface JcrDAO<T>

getVersionSize

protected long getVersionSize(javax.jcr.Node node)

getVersionList

public java.util.List<T> getVersionList(java.lang.String path)
Specified by:
getVersionList in interface JcrDAO<T>

getVersionList

public java.util.List<T> getVersionList(java.lang.String path,
                                        java.lang.String childNameFilter,
                                        int maxDepth)
Specified by:
getVersionList in interface JcrDAO<T>

getVersionList

public java.util.List<T> getVersionList(java.lang.String path,
                                        java.lang.String childNameFilter,
                                        int maxDepth,
                                        long startIndex,
                                        long resultSize)
Specified by:
getVersionList in interface JcrDAO<T>

getVersionListByUUID

public java.util.List<T> getVersionListByUUID(java.lang.String uuid)
Specified by:
getVersionListByUUID in interface JcrDAO<T>

getVersionListByUUID

public java.util.List<T> getVersionListByUUID(java.lang.String uuid,
                                              java.lang.String childNameFilter,
                                              int maxDepth)
Specified by:
getVersionListByUUID in interface JcrDAO<T>

getVersionListByUUID

public java.util.List<T> getVersionListByUUID(java.lang.String uuid,
                                              java.lang.String childNameFilter,
                                              int maxDepth,
                                              long startIndex,
                                              long resultSize)
Specified by:
getVersionListByUUID in interface JcrDAO<T>

getVersionList

protected java.util.List<T> getVersionList(javax.jcr.Node node,
                                           java.lang.String childNameFilter,
                                           int maxDepth)

getVersionList

protected java.util.List<T> getVersionList(javax.jcr.Node node,
                                           java.lang.String childNameFilter,
                                           int maxDepth,
                                           long startIndex,
                                           long resultSize)

getSize

public long getSize(java.lang.String rootPath)
Description copied from interface: JcrDAO
Get the number of entities.

Specified by:
getSize in interface JcrDAO<T>
Returns:
the size of the list returned by findAll()

findAll

public java.util.List<T> findAll(java.lang.String rootPath)
Description copied from interface: JcrDAO
Find all entities represented by this DAO.

Specified by:
findAll in interface JcrDAO<T>
Returns:
all entities represented by this DAO

findAll

public java.util.List<T> findAll(java.lang.String rootPath,
                                 long startIndex,
                                 long resultSize)
Description copied from interface: JcrDAO
Find all entities represented by this DAO. Takes parameters that control the size and offset of the result.

Specified by:
findAll in interface JcrDAO<T>
startIndex - the zero based index of the first item to return
resultSize - the number of items to return
Returns:
all entities represented by this DAO

findAll

public java.util.List<T> findAll(java.lang.String rootPath,
                                 java.lang.String childNameFilter,
                                 int maxDepth)
Description copied from interface: JcrDAO
Find all entities represented by this DAO.

Specified by:
findAll in interface JcrDAO<T>
childNameFilter - comma separated list of names of child nodes to load ("*" loads all, "none" loads no children, and "-" at the beginning makes it an exclusion filter)
maxDepth - the maximum depth of loaded child nodes (0 means no child nodes are loaded, while a negative value means that no restrictions are set on the depth).
Returns:
all entities represented by this DAO

findAll

public java.util.List<T> findAll(java.lang.String rootPath,
                                 java.lang.String childNameFilter,
                                 int maxDepth,
                                 long startIndex,
                                 long resultSize)
Description copied from interface: JcrDAO
Find all entities represented by this DAO. Takes parameters that control the size and offset of the result, and filter which child nodes to load.

Specified by:
findAll in interface JcrDAO<T>
childNameFilter - comma separated list of names of child nodes to load ("*" loads all, "none" loads no children, and "-" at the beginning makes it an exclusion filter)
maxDepth - the maximum depth of loaded child nodes (0 means no child nodes are loaded, while a negative value means that no restrictions are set on the depth).
startIndex - the zero based index of the first item to return
resultSize - the number of items to return
Returns:
all entities represented by this DAO

findByXPath

protected java.util.List<T> findByXPath(java.lang.String xpath,
                                        java.lang.String childNameFilter,
                                        int maxDepth,
                                        long startIndex,
                                        long resultSize)
Find JCR nodes that match the xpath supplied, and map to objects.

Parameters:
xpath - the XPath for finding the nodes
childNameFilter - comma separated list of names of child nodes to load ("*" loads all, "none" loads no children, and "-" at the beginning makes it an exclusion filter)
maxDepth - the maximum depth of loaded child nodes (0 means no child nodes are loaded, while a negative value means that no restrictions are set on the depth).
startIndex - the zero based index of the first item to return
resultSize - the number of items to return
Returns:
a list of all objects found

findByXPath

protected java.util.List<T> findByXPath(java.lang.String xpath,
                                        java.lang.String childNameFilter,
                                        int maxDepth)
Find JCR nodes that match the xpath supplied, and map to objects.

Parameters:
xpath - the XPath for finding the nodes
childNameFilter - comma separated list of names of child nodes to load ("*" loads all, "none" loads no children, and "-" at the beginning makes it an exclusion filter)
maxDepth - the maximum depth of loaded child nodes (0 means no child nodes are loaded, while a negative value means that no restrictions are set on the depth).
Returns:
a list of all objects found

toList

protected java.util.List<T> toList(javax.jcr.NodeIterator nodeIterator,
                                   java.lang.String childNameFilter,
                                   int maxDepth)
Maps JCR nodes to a List of JcrEntity implementations.

Parameters:
nodeIterator - the iterator pointing to the nodes
childNameFilter - comma separated list of names of child nodes to load ("*" loads all, "none" loads no children, and "-" at the beginning makes it an exclusion filter)
maxDepth - the maximum depth of loaded child nodes (0 means no child nodes are loaded, while a negative value means that no restrictions are set on the depth).
Returns:
a list of objects mapped from the nodes

toList

protected java.util.List<T> toList(javax.jcr.NodeIterator nodeIterator,
                                   java.lang.String childNameFilter,
                                   int maxDepth,
                                   long resultSize)
Maps JCR nodes to a List of JcrEntity implementations.

Parameters:
nodeIterator - the iterator pointing to the nodes
childNameFilter - comma separated list of names of child nodes to load ("*" loads all, "none" loads no children, and "-" at the beginning makes it an exclusion filter)
maxDepth - the maximum depth of loaded child nodes (0 means no child nodes are loaded, while a negative value means that no restrictions are set on the depth).
resultSize - the number of items to retrieve from the iterator
Returns:
a list of objects mapped from the nodes


Copyright © 2009. All Rights Reserved.