org.springframework.extensions.jcr
Class JcrTemplate

java.lang.Object
  extended by org.springframework.extensions.jcr.JcrAccessor
      extended by org.springframework.extensions.jcr.JcrTemplate
All Implemented Interfaces:
org.springframework.beans.factory.InitializingBean, JcrModel1Operations, JcrModel2Operations, JcrOperations, JcrOptionalOperations

public class JcrTemplate
extends JcrAccessor
implements JcrOperations

Helper class that simplifies JCR data access code. Typically used to implement data access or business logic services that use JCR within their implementation but are JCR-agnostic in their interface. Requires a JcrSessionFactory to provide access to a JCR repository. A workspace name is optional, as the repository will choose the default workspace if a name is not provided.

Author:
Costin Leau, Sergio Bossa, Salvatore Incandela

Constructor Summary
JcrTemplate()
           
JcrTemplate(SessionFactory sessionFactory)
           
 
Method Summary
 void addLockToken(java.lang.String lock)
           
protected  javax.jcr.Session createSessionProxy(javax.jcr.Session session)
          Create a close-suppressing proxy for the given Jcr Session.
 java.lang.String dump(javax.jcr.Node node)
          Dump the contents of the given node in a String.
protected  java.lang.String dumpNode(javax.jcr.Node node)
          Recursive method for dumping a node.
 java.lang.Object execute(JcrCallback callback)
          Execute the action specified by the given action object within a Session.
 java.lang.Object execute(JcrCallback action, boolean exposeNativeSession)
          Execute the action specified by the given action object within a Session.
 java.lang.Object getAttribute(java.lang.String name)
           
 java.lang.String[] getAttributeNames()
           
 org.xml.sax.ContentHandler getImportContentHandler(java.lang.String parentAbsPath, int uuidBehavior)
           
 javax.jcr.Item getItem(java.lang.String absPath)
           
 java.lang.String[] getLockTokens()
           
 java.lang.String getNamespacePrefix(java.lang.String uri)
           
 java.lang.String[] getNamespacePrefixes()
           
 java.lang.String getNamespaceURI(java.lang.String prefix)
           
 javax.jcr.Node getNodeByUUID(java.lang.String uuid)
           
 javax.jcr.Node getRootNode()
           
protected  javax.jcr.Session getSession()
          Return a Session for use by this template.
 java.lang.String getUserID()
           
 javax.jcr.ValueFactory getValueFactory()
           
 boolean hasPendingChanges()
           
 void importXML(java.lang.String parentAbsPath, java.io.InputStream in, int uuidBehavior)
           
 boolean isAllowCreate()
           
 boolean isExposeNativeSession()
           
 boolean isLive()
           
protected  boolean isVersionable(javax.jcr.Node node)
           
 boolean itemExists(java.lang.String absPath)
           
 void move(java.lang.String srcAbsPath, java.lang.String destAbsPath)
           
 java.util.Map query(java.util.List list)
          Default method for doing multiple queries.
 java.util.Map query(java.util.List list, java.lang.String language, boolean ignoreErrors)
          Utility method for executing a list of queries against the repository.
 javax.jcr.query.QueryResult query(javax.jcr.Node node)
          Execute a persistent query from the given node.
 javax.jcr.query.QueryResult query(java.lang.String statement)
          Execute a query with the given strings with XPATH as default language.
 javax.jcr.query.QueryResult query(java.lang.String statement, java.lang.String language)
          Execute a query with the given strings.
 void refresh(boolean keepChanges)
           
 void removeLockToken(java.lang.String lt)
           
 void rename(javax.jcr.Node node, java.lang.String newName)
          Renames a node (with the given name)
 void save()
           
 void setAllowCreate(boolean allowCreate)
           
 void setExposeNativeSession(boolean exposeNativeSession)
           
 void setNamespacePrefix(java.lang.String prefix, java.lang.String uri)
           
 
Methods inherited from class org.springframework.extensions.jcr.JcrAccessor
afterPropertiesSet, convertJcrAccessException, convertJcrAccessException, convertJcrAccessException, getSessionFactory, setSessionFactory
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JcrTemplate

public JcrTemplate()

JcrTemplate

public JcrTemplate(SessionFactory sessionFactory)
Method Detail

execute

public java.lang.Object execute(JcrCallback action,
                                boolean exposeNativeSession)
                         throws org.springframework.dao.DataAccessException
Description copied from interface: JcrOperations
Execute the action specified by the given action object within a Session.

Specified by:
execute in interface JcrOperations
Parameters:
action - callback object that specifies the Jcr action
exposeNativeSession - whether to expose the native Jcr Session to callback code
Returns:
a result object returned by the action, or null
Throws:
org.springframework.dao.DataAccessException - in case of Jcr errors
See Also:
JcrOperations.execute(org.springframework.extensions.jcr.JcrCallback, boolean)

execute

public java.lang.Object execute(JcrCallback callback)
                         throws org.springframework.dao.DataAccessException
Description copied from interface: JcrOperations
Execute the action specified by the given action object within a Session. Application exceptions thrown by the action object get propagated to the caller (can only be unchecked). JCR exceptions are transformed into appropriate DAO ones. Allows for returning a result object, i.e. a domain object or a collection of domain objects. Note: Callback code does not need to explicitly log out of the Session; this method will handle that itself. The workspace logged into will be that named by the workspaceName property; if that property is null, the repository's default workspace will be used.

Specified by:
execute in interface JcrOperations
Parameters:
callback - the JCRCallback that executes the client operation
Throws:
org.springframework.dao.DataAccessException
See Also:
JcrOperations.execute(org.springframework.extensions.jcr.JcrCallback)

getSession

protected javax.jcr.Session getSession()
Return a Session for use by this template. A pre-bound Session in case of "allowCreate" turned off and a pre-bound or new Session else (new only if no transactional or otherwise pre-bound Session exists).

See Also:
SessionFactoryUtils.getSession(org.springframework.extensions.jcr.SessionFactory, boolean), SessionFactoryUtils#getNewSession, setAllowCreate(boolean)

addLockToken

public void addLockToken(java.lang.String lock)
Specified by:
addLockToken in interface JcrOptionalOperations
See Also:
JcrOptionalOperations.addLockToken(java.lang.String)

getAttribute

public java.lang.Object getAttribute(java.lang.String name)
Specified by:
getAttribute in interface JcrModel1Operations
See Also:
JcrModel1Operations.getAttribute(java.lang.String)

getAttributeNames

public java.lang.String[] getAttributeNames()
Specified by:
getAttributeNames in interface JcrModel1Operations
See Also:
JcrModel1Operations.getAttributeNames()

getImportContentHandler

public org.xml.sax.ContentHandler getImportContentHandler(java.lang.String parentAbsPath,
                                                          int uuidBehavior)
Specified by:
getImportContentHandler in interface JcrModel1Operations
See Also:
JcrModel1Operations.getImportContentHandler(java.lang.String, int)

getItem

public javax.jcr.Item getItem(java.lang.String absPath)
Specified by:
getItem in interface JcrModel1Operations
See Also:
JcrModel1Operations.getItem(java.lang.String)

getLockTokens

public java.lang.String[] getLockTokens()
Specified by:
getLockTokens in interface JcrOptionalOperations
See Also:
JcrOptionalOperations.getLockTokens()

getNamespacePrefix

public java.lang.String getNamespacePrefix(java.lang.String uri)
Specified by:
getNamespacePrefix in interface JcrModel1Operations
See Also:
JcrModel1Operations.getNamespacePrefix(java.lang.String)

getNamespacePrefixes

public java.lang.String[] getNamespacePrefixes()
Specified by:
getNamespacePrefixes in interface JcrModel1Operations
See Also:
JcrModel1Operations.getNamespacePrefixes()

getNamespaceURI

public java.lang.String getNamespaceURI(java.lang.String prefix)
Specified by:
getNamespaceURI in interface JcrModel1Operations
See Also:
JcrModel1Operations.getNamespaceURI(java.lang.String)

getNodeByUUID

public javax.jcr.Node getNodeByUUID(java.lang.String uuid)
Specified by:
getNodeByUUID in interface JcrModel1Operations
See Also:
JcrModel1Operations.getNodeByUUID(java.lang.String)

getRootNode

public javax.jcr.Node getRootNode()
Specified by:
getRootNode in interface JcrModel1Operations
See Also:
JcrModel1Operations.getRootNode()

getUserID

public java.lang.String getUserID()
Specified by:
getUserID in interface JcrModel1Operations
See Also:
JcrModel1Operations.getUserID()

getValueFactory

public javax.jcr.ValueFactory getValueFactory()
Specified by:
getValueFactory in interface JcrModel1Operations
See Also:
JcrModel1Operations.getValueFactory()

hasPendingChanges

public boolean hasPendingChanges()
Specified by:
hasPendingChanges in interface JcrModel2Operations
See Also:
JcrModel2Operations.hasPendingChanges()

importXML

public void importXML(java.lang.String parentAbsPath,
                      java.io.InputStream in,
                      int uuidBehavior)
Specified by:
importXML in interface JcrModel2Operations
See Also:
JcrModel2Operations.importXML(java.lang.String, java.io.InputStream, int)

refresh

public void refresh(boolean keepChanges)
Specified by:
refresh in interface JcrModel2Operations
See Also:
JcrModel2Operations.refresh(boolean)

removeLockToken

public void removeLockToken(java.lang.String lt)
Specified by:
removeLockToken in interface JcrOptionalOperations
See Also:
JcrOptionalOperations.removeLockToken(java.lang.String)

rename

public void rename(javax.jcr.Node node,
                   java.lang.String newName)
Description copied from interface: JcrOperations
Renames a node (with the given name)

Specified by:
rename in interface JcrOperations
Parameters:
node - node to rename
newName - new name for the node
See Also:
JcrOperations.rename(javax.jcr.Node, java.lang.String)

setNamespacePrefix

public void setNamespacePrefix(java.lang.String prefix,
                               java.lang.String uri)
Specified by:
setNamespacePrefix in interface JcrModel2Operations
See Also:
JcrModel2Operations.setNamespacePrefix(java.lang.String, java.lang.String)

isLive

public boolean isLive()
Specified by:
isLive in interface JcrModel1Operations
See Also:
JcrModel1Operations.isLive()

itemExists

public boolean itemExists(java.lang.String absPath)
Specified by:
itemExists in interface JcrModel1Operations
See Also:
JcrModel1Operations.itemExists(java.lang.String)

move

public void move(java.lang.String srcAbsPath,
                 java.lang.String destAbsPath)
Specified by:
move in interface JcrModel2Operations
See Also:
JcrModel2Operations.move(java.lang.String, java.lang.String)

save

public void save()
Specified by:
save in interface JcrModel2Operations
See Also:
JcrModel2Operations.save()

dump

public java.lang.String dump(javax.jcr.Node node)
Description copied from interface: JcrOperations
Dump the contents of the given node in a String. This method parses the whole tree under the node and can generate a huge String.

Specified by:
dump in interface JcrOperations
Parameters:
node - node to be dumped (null is equivalent to the root node)
Returns:
node tree in a string representation.
See Also:
JcrOperations.dump(javax.jcr.Node)

dumpNode

protected java.lang.String dumpNode(javax.jcr.Node node)
                             throws javax.jcr.RepositoryException
Recursive method for dumping a node. This method is separate to avoid the overhead of searching and opening/closing JCR sessions.

Parameters:
node -
Returns:
Throws:
javax.jcr.RepositoryException

query

public javax.jcr.query.QueryResult query(javax.jcr.Node node)
Description copied from interface: JcrModel1Operations
Execute a persistent query from the given node.

Specified by:
query in interface JcrModel1Operations
Parameters:
node - node to be dumped
Returns:
query result
See Also:
JcrModel1Operations.query(javax.jcr.Node)

query

public javax.jcr.query.QueryResult query(java.lang.String statement)
Description copied from interface: JcrModel1Operations
Execute a query with the given strings with XPATH as default language. It's the same as #query(java.lang.String, java.lang.String)

Specified by:
query in interface JcrModel1Operations
Parameters:
statement - query statement
Returns:
query result
See Also:
JcrModel1Operations.query(java.lang.String)

query

public javax.jcr.query.QueryResult query(java.lang.String statement,
                                         java.lang.String language)
Description copied from interface: JcrModel1Operations
Execute a query with the given strings.

Specified by:
query in interface JcrModel1Operations
Parameters:
statement - query statement
language - language statement
Returns:
query result
See Also:
JcrModel1Operations.query(java.lang.String, java.lang.String)

query

public java.util.Map query(java.util.List list)
Description copied from interface: JcrModel1Operations
Default method for doing multiple queries. It assumes the language is XPATH and that errors will not be ignored.

Specified by:
query in interface JcrModel1Operations
Parameters:
list - a list of queries that will be executed against the repository
Returns:
a map containing the queries as keys and results as values
See Also:
JcrModel1Operations.query(java.util.List)

query

public java.util.Map query(java.util.List list,
                           java.lang.String language,
                           boolean ignoreErrors)
Description copied from interface: JcrModel1Operations
Utility method for executing a list of queries against the repository. Reads the queries given and returns the results in a map.

If possible the map will be a LinkedHashSet on JDK 1.4+, otherwise LinkedHashSet from Commons collections 3.1 if the package is found. If the above fails a HashMap will be returned.

Specified by:
query in interface JcrModel1Operations
Parameters:
list - list of queries
language - language of the queries. If null XPATH is assumed.
ignoreErrors - if true it will populate unfound nodes with null
Returns:
a map containing the queries as keys and results as values
See Also:
JcrModel1Operations.query(java.util.List, java.lang.String, boolean)

isAllowCreate

public boolean isAllowCreate()
Returns:
Returns the allowCreate.

setAllowCreate

public void setAllowCreate(boolean allowCreate)
Parameters:
allowCreate - The allowCreate to set.

createSessionProxy

protected javax.jcr.Session createSessionProxy(javax.jcr.Session session)
Create a close-suppressing proxy for the given Jcr Session.

Parameters:
session - the Jcr Session to create a proxy for
Returns:
the Session proxy
See Also:
Session.logout()

isVersionable

protected boolean isVersionable(javax.jcr.Node node)
                         throws javax.jcr.RepositoryException
Throws:
javax.jcr.RepositoryException

isExposeNativeSession

public boolean isExposeNativeSession()
Returns:
Returns the exposeNativeSession.

setExposeNativeSession

public void setExposeNativeSession(boolean exposeNativeSession)
Parameters:
exposeNativeSession - The exposeNativeSession to set.


Copyright © 2009. All Rights Reserved.