Class JcrUtils
- java.lang.Object
-
- org.hippoecm.repository.util.JcrUtils
-
public class JcrUtils extends Object
Some utility methods for writing code against JCR API. This code can be removed when we upgrade to JR 2.6...
-
-
Field Summary
Fields Modifier and Type Field Description static int
ALL_EVENTS
-
Constructor Summary
Constructors Constructor Description JcrUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Node
copy(Node srcNode, String destNodeName, Node destParentNode)
static Node
copy(Session session, String srcAbsPath, String destAbsPath)
Copies node atsrcAbsPath
todestAbsPath
as session operation.static void
copyTo(Node srcNode, Node destNode)
static Node
copyTo(Node srcNode, CopyHandler chain)
static void
copyToChain(Node srcNode, CopyHandler chain)
static Value
createBinaryValueFromObject(Session session, Object object)
Serialize the givenobject
into a binary JCR value.static NodeIterable
emptyNodeIterable()
static void
ensureIsCheckedOut(Node node)
Make sure the node is in checked out state.static Binary
getBinaryProperty(Node baseNode, String relPath, Binary defaultValue)
Returns the binary property value atrelPath
frombaseNode
ordefaultValue
if no such property exists.static Binary
getBinaryProperty(Session session, String absPath, Binary defaultValue)
Returns the binary property value atabsPath
ordefaultValue
if no such property exists.static Boolean
getBooleanProperty(Node baseNode, String relPath, Boolean defaultValue)
Returns the boolean property value atrelPath
frombaseNode
ordefaultValue
if no such property exists.static Boolean
getBooleanProperty(Session session, String absPath, Boolean defaultValue)
Returns the boolean property value atabsPath
ordefaultValue
if no such property exists.static Calendar
getDateProperty(Node baseNode, String relPath, Calendar defaultValue)
Returns the date property value atrelPath
frombaseNode
ordefaultValue
if no such property exists.static Calendar
getDateProperty(Session session, String absPath, Calendar defaultValue)
Returns the date property value atabsPath
ordefaultValue
if no such property exists.static BigDecimal
getDecimalProperty(Node baseNode, String relPath, BigDecimal defaultValue)
Returns the decimal property value atrelPath
frombaseNode
ordefaultValue
if no such property exists.static BigDecimal
getDecimalProperty(Session session, String absPath, BigDecimal defaultValue)
Returns the decimal property value atabsPath
ordefaultValue
if no such property exists.static List<Node>
getDescendants(Node source, String nodeType, boolean prune)
Returns the list of jcr nodes which are descendants ofsource
and of typenodeType
, wheresource
is never included in the result.static String
getDisplayNameQuietly(Node node)
Get the display name of aNode
, ornull
if the node cannot be retrieved.static Double
getDoubleProperty(Node baseNode, String relPath, Double defaultValue)
Returns the double property value atrelPath
frombaseNode
ordefaultValue
if no such property exists.static Double
getDoubleProperty(Session session, String absPath, Double defaultValue)
Returns the double property value atabsPath
ordefaultValue
if no such property exists.static <E extends Enum<E>>
EgetEnumProperty(Node baseNode, String relPath, E defaultValue)
Returns the enum value of a string property atrelPath
frombaseNode
ordefaultValue
if no such property exists.static Node
getLastNodeIfExists(String absPath, Session session)
Gets the node atabsPath
ornull
if no such node exists.static Long
getLongProperty(Node baseNode, String relPath, Long defaultValue)
Returns the long property value atrelPath
frombaseNode
ordefaultValue
if no such property exists.static Long
getLongProperty(Session session, String absPath, Long defaultValue)
Returns the long property value atabsPath
ordefaultValue
if no such property exists.static NodeType[]
getMixinNodeTypes(Node node)
Retrieve the mixin node types present on a node.static String[]
getMultipleStringProperty(Node baseNode, String relPath, String[] defaultValue)
Returns the multiple string property values atrelPath
frombaseNode
ordefaultValue
if no such property exists.static Node
getNextSiblingIfExists(Node current)
Returns the next siblingNode
ofcurrent
.static Node
getNodeIfExists(String absPath, Session session)
Gets the node atabsPath
ornull
if no such node exists.static Node
getNodeIfExists(Node baseNode, String relPath)
Get the node atrelPath
frombaseNode
ornull
if no such node exists.static String
getNodeNameQuietly(Node node)
Get the name of aNode
, ornull
if the node cannot be retrieved.static String
getNodePathQuietly(Node node)
Get the path of aNode
, ornull
if the path cannot be retrieved.static Node
getNodeProperty(Node baseNode, String relPath, Node defaultValue)
Returns the node property value atrelPath
frombaseNode
ordefaultValue
if no such property exists.static Node
getNodeProperty(Session session, String absPath, Node defaultValue)
Returns the node property value atabsPath
ordefaultValue
if no such property exists.static NodeType
getPrimaryNodeType(Node node)
Retrieve the primary node type.static Iterable<Property>
getProperties(Node node)
CallsNode.getProperties()
on the given node and returns the it as anIterable
instance for use in a Java 5 for-each loop.static Property
getPropertyIfExists(String absPath, Session session)
Gets the property atabsPath
ornull
if no such property exists.static Property
getPropertyIfExists(Node baseNode, String relPath)
Get the property atrelPath
frombaseNode
ornull
if no such property exists.static List<String>
getStringListProperty(Node baseNode, String relPath, List<String> defaultValue)
Returns the multiple string property values atrelPath
frombaseNode
ordefaultValue
if no such property exists.static String
getStringProperty(Node baseNode, String relPath, String defaultValue)
Returns the string property value atrelPath
frombaseNode
ordefaultValue
if no such property exists.static String
getStringProperty(Session session, String absPath, String defaultValue)
Returns the string property value atabsPath
ordefaultValue
if no such property exists.static Set<String>
getStringSetProperty(Node baseNode, String relPath, Set<String> defaultValue)
Returns the unique and unordered multiple string property values atrelPath
frombaseNode
ordefaultValue
if no such property exists.static boolean
isAncestor(Node ancestor, Node descendant)
static boolean
isPropertyEvent(Event event)
static boolean
isVirtual(Node node)
-
-
-
Field Detail
-
ALL_EVENTS
public static final int ALL_EVENTS
- See Also:
- Constant Field Values
-
-
Method Detail
-
getNodeIfExists
public static Node getNodeIfExists(Node baseNode, String relPath) throws RepositoryException
Get the node atrelPath
frombaseNode
ornull
if no such node exists.- Parameters:
baseNode
- existing node that should be the base for the relative pathrelPath
- relative path to the node to get- Returns:
- the node at
relPath
frombaseNode
ornull
if no such node exists. - Throws:
RepositoryException
- in case of exception accessing the Repository
-
getNodeIfExists
public static Node getNodeIfExists(String absPath, Session session) throws RepositoryException
Gets the node atabsPath
ornull
if no such node exists.- Parameters:
absPath
- the absolute path to the node to returnsession
- to use- Returns:
- the node at
absPath
ornull
if no such node exists. - Throws:
RepositoryException
- in case of exception accessing the Repository
-
getLastNodeIfExists
public static Node getLastNodeIfExists(String absPath, Session session) throws RepositoryException
Gets the node atabsPath
ornull
if no such node exists. In case there are more nodes atabsPath
, the last node is returned.- Parameters:
absPath
- the absolute path to the node to returnsession
- to use- Returns:
- the node at
absPath
ornull
if no such node exists. - Throws:
RepositoryException
-
getStringProperty
public static String getStringProperty(Node baseNode, String relPath, String defaultValue) throws RepositoryException
Returns the string property value atrelPath
frombaseNode
ordefaultValue
if no such property exists.- Parameters:
baseNode
- existing node that should be the base for the relative pathrelPath
- relative path to the property to getdefaultValue
- default value to return when the property does not exist- Returns:
- the string property value at
relPath
frombaseNode
ordefaultValue
if no such property exists - Throws:
RepositoryException
- in case of exception accessing the Repository
-
getEnumProperty
public static <E extends Enum<E>> E getEnumProperty(Node baseNode, String relPath, E defaultValue) throws RepositoryException
Returns the enum value of a string property atrelPath
frombaseNode
ordefaultValue
if no such property exists.- Parameters:
baseNode
- existing node that should be the base for the relative pathrelPath
- relative path to the property to getdefaultValue
- default value to return when the property does not exist- Returns:
- the enum value of a string property at
relPath
frombaseNode
ordefaultValue
if no such property exists or property doesn't have any enum value - Throws:
RepositoryException
- in case of exception accessing the Repository
-
getMultipleStringProperty
public static String[] getMultipleStringProperty(Node baseNode, String relPath, String[] defaultValue) throws RepositoryException
Returns the multiple string property values atrelPath
frombaseNode
ordefaultValue
if no such property exists.- Parameters:
baseNode
- existing node that should be the base for the relative pathrelPath
- relative path to the property to getdefaultValue
- default value to return when the property does not exist- Returns:
- the multiple string property value at
relPath
frombaseNode
ordefaultValue
if no such property exists - Throws:
RepositoryException
- in case of exception accessing the Repository
-
getStringListProperty
public static List<String> getStringListProperty(Node baseNode, String relPath, List<String> defaultValue) throws RepositoryException
Returns the multiple string property values atrelPath
frombaseNode
ordefaultValue
if no such property exists.- Parameters:
baseNode
- existing node that should be the base for the relative pathrelPath
- relative path to the property to getdefaultValue
- default value to return when the property does not exist- Returns:
- the multiple string property value at
relPath
frombaseNode
ordefaultValue
if no such property exists - Throws:
RepositoryException
- in case of exception accessing the Repository
-
getStringSetProperty
public static Set<String> getStringSetProperty(Node baseNode, String relPath, Set<String> defaultValue) throws RepositoryException
Returns the unique and unordered multiple string property values atrelPath
frombaseNode
ordefaultValue
if no such property exists.- Parameters:
baseNode
- existing node that should be the base for the relative pathrelPath
- relative path to the property to getdefaultValue
- default value to return when the property does not exist- Returns:
- the unique and unordered multiple string property value at
relPath
frombaseNode
ordefaultValue
if no such property exists - Throws:
RepositoryException
- in case of exception accessing the Repository
-
getLongProperty
public static Long getLongProperty(Node baseNode, String relPath, Long defaultValue) throws RepositoryException
Returns the long property value atrelPath
frombaseNode
ordefaultValue
if no such property exists.- Parameters:
baseNode
- existing node that should be the base for the relative pathrelPath
- relative path to the property to getdefaultValue
- default value to return when the property does not exist- Returns:
- the long property value at
relPath
frombaseNode
ordefaultValue
if no such property exists - Throws:
RepositoryException
- in case of exception accessing the Repository
-
getDoubleProperty
public static Double getDoubleProperty(Node baseNode, String relPath, Double defaultValue) throws RepositoryException
Returns the double property value atrelPath
frombaseNode
ordefaultValue
if no such property exists.- Parameters:
baseNode
- existing node that should be the base for the relative pathrelPath
- relative path to the property to getdefaultValue
- default value to return when the property does not exist- Returns:
- the double property value at
relPath
frombaseNode
ordefaultValue
if no such property exists - Throws:
RepositoryException
- in case of exception accessing the Repository
-
getBooleanProperty
public static Boolean getBooleanProperty(Node baseNode, String relPath, Boolean defaultValue) throws RepositoryException
Returns the boolean property value atrelPath
frombaseNode
ordefaultValue
if no such property exists.- Parameters:
baseNode
- existing node that should be the base for the relative pathrelPath
- relative path to the property to getdefaultValue
- default value to return when the property does not exist- Returns:
- the boolean property value at
relPath
frombaseNode
ordefaultValue
if no such property exists - Throws:
RepositoryException
- in case of exception accessing the Repository
-
getDateProperty
public static Calendar getDateProperty(Node baseNode, String relPath, Calendar defaultValue) throws RepositoryException
Returns the date property value atrelPath
frombaseNode
ordefaultValue
if no such property exists.- Parameters:
baseNode
- existing node that should be the base for the relative pathrelPath
- relative path to the property to getdefaultValue
- default value to return when the property does not exist- Returns:
- the date property value at
relPath
frombaseNode
ordefaultValue
if no such property exists - Throws:
RepositoryException
- in case of exception accessing the Repository
-
getDecimalProperty
public static BigDecimal getDecimalProperty(Node baseNode, String relPath, BigDecimal defaultValue) throws RepositoryException
Returns the decimal property value atrelPath
frombaseNode
ordefaultValue
if no such property exists.- Parameters:
baseNode
- existing node that should be the base for the relative pathrelPath
- relative path to the property to getdefaultValue
- default value to return when the property does not exist- Returns:
- the decimal property value at
relPath
frombaseNode
ordefaultValue
if no such property exists - Throws:
RepositoryException
- in case of exception accessing the Repository
-
getBinaryProperty
public static Binary getBinaryProperty(Node baseNode, String relPath, Binary defaultValue) throws RepositoryException
Returns the binary property value atrelPath
frombaseNode
ordefaultValue
if no such property exists.- Parameters:
baseNode
- existing node that should be the base for the relative pathrelPath
- relative path to the property to getdefaultValue
- default value to return when the property does not exist- Returns:
- the binary property value at
relPath
frombaseNode
ordefaultValue
if no such property exists - Throws:
RepositoryException
- in case of exception accessing the Repository
-
getNodeProperty
public static Node getNodeProperty(Node baseNode, String relPath, Node defaultValue) throws RepositoryException
Returns the node property value atrelPath
frombaseNode
ordefaultValue
if no such property exists.- Parameters:
baseNode
- existing node that should be the base for the relative pathrelPath
- relative path to the property to getdefaultValue
- default value to return when the property does not exist- Returns:
- the node property value at
relPath
frombaseNode
ordefaultValue
if no such property exists - Throws:
RepositoryException
- in case of exception accessing the Repository
-
getStringProperty
public static String getStringProperty(Session session, String absPath, String defaultValue) throws RepositoryException
Returns the string property value atabsPath
ordefaultValue
if no such property exists.- Parameters:
session
- to useabsPath
- absolute path to the property to getdefaultValue
- default value to return when the property does not exist- Returns:
- the string property value at
absPath
ordefaultValue
if no such property exists - Throws:
RepositoryException
- in case of exception accessing the Repository
-
getLongProperty
public static Long getLongProperty(Session session, String absPath, Long defaultValue) throws RepositoryException
Returns the long property value atabsPath
ordefaultValue
if no such property exists.- Parameters:
session
- to useabsPath
- absolute path to the property to getdefaultValue
- default value to return when the property does not exist- Returns:
- the long property value at
absPath
ordefaultValue
if no such property exists - Throws:
RepositoryException
- in case of exception accessing the Repository
-
getDoubleProperty
public static Double getDoubleProperty(Session session, String absPath, Double defaultValue) throws RepositoryException
Returns the double property value atabsPath
ordefaultValue
if no such property exists.- Parameters:
session
- to useabsPath
- absolute path to the property to getdefaultValue
- default value to return when the property does not exist- Returns:
- the double property value at
absPath
ordefaultValue
if no such property exists - Throws:
RepositoryException
- in case of exception accessing the Repository
-
getBooleanProperty
public static Boolean getBooleanProperty(Session session, String absPath, Boolean defaultValue) throws RepositoryException
Returns the boolean property value atabsPath
ordefaultValue
if no such property exists.- Parameters:
session
- to useabsPath
- absolute path to the property to getdefaultValue
- default value to return when the property does not exist- Returns:
- the boolean property value at
absPath
ordefaultValue
if no such property exists - Throws:
RepositoryException
- in case of exception accessing the Repository
-
getDateProperty
public static Calendar getDateProperty(Session session, String absPath, Calendar defaultValue) throws RepositoryException
Returns the date property value atabsPath
ordefaultValue
if no such property exists.- Parameters:
session
- to useabsPath
- absolute path to the property to getdefaultValue
- default value to return when the property does not exist- Returns:
- the date property value at
absPath
ordefaultValue
if no such property exists - Throws:
RepositoryException
- in case of exception accessing the Repository
-
getDecimalProperty
public static BigDecimal getDecimalProperty(Session session, String absPath, BigDecimal defaultValue) throws RepositoryException
Returns the decimal property value atabsPath
ordefaultValue
if no such property exists.- Parameters:
session
- to useabsPath
- absolute path to the property to getdefaultValue
- default value to return when the property does not exist- Returns:
- the decimal property value at
absPath
ordefaultValue
if no such property exists - Throws:
RepositoryException
- in case of exception accessing the Repository
-
getBinaryProperty
public static Binary getBinaryProperty(Session session, String absPath, Binary defaultValue) throws RepositoryException
Returns the binary property value atabsPath
ordefaultValue
if no such property exists.- Parameters:
session
- to useabsPath
- absolute path to the property to getdefaultValue
- default value to return when the property does not exist- Returns:
- the binary property value at
absPath
ordefaultValue
if no such property exists - Throws:
RepositoryException
- in case of exception accessing the Repository
-
getNodeProperty
public static Node getNodeProperty(Session session, String absPath, Node defaultValue) throws RepositoryException
Returns the node property value atabsPath
ordefaultValue
if no such property exists.- Parameters:
session
- to useabsPath
- absolute path to the property to getdefaultValue
- default value to return when the property does not exist- Returns:
- the node property value at
absPath
ordefaultValue
if no such property exists - Throws:
RepositoryException
- in case of exception accessing the Repository
-
getPropertyIfExists
public static Property getPropertyIfExists(Node baseNode, String relPath) throws RepositoryException
Get the property atrelPath
frombaseNode
ornull
if no such property exists.- Parameters:
baseNode
- existing node that should be the base for the relative pathrelPath
- relative path to the property to get- Returns:
- the property at
relPath
frombaseNode
ornull
if no such property exists. - Throws:
RepositoryException
- in case of exception accessing the Repository
-
getPropertyIfExists
public static Property getPropertyIfExists(String absPath, Session session) throws RepositoryException
Gets the property atabsPath
ornull
if no such property exists.- Parameters:
absPath
- the absolute path to the property to returnsession
- to use- Returns:
- the property at
absPath
ornull
if no such property exists. - Throws:
RepositoryException
- in case of exception accessing the Repository
-
copy
public static Node copy(Session session, String srcAbsPath, String destAbsPath) throws RepositoryException
Copies node atsrcAbsPath
todestAbsPath
as session operation.- Parameters:
session
- to usesrcAbsPath
- the absolute path of the source nodedestAbsPath
- the absolute path of the resulting copy- Returns:
- the created node
- Throws:
RepositoryException
IllegalArgumentException
- if srcNode is same as destParentNode or destParentNode is a descendant of srcNode or destAbsPath is the root node path.
-
copy
public static Node copy(Node srcNode, String destNodeName, Node destParentNode) throws RepositoryException
- Parameters:
srcNode
- the node to copydestNodeName
- the name of the to be newly created nodedestParentNode
- the parent of the to be newly created node- Returns:
- the created node
- Throws:
RepositoryException
IllegalArgumentException
- if srcNode is same as destParentNode or destParentNode is a descendant of srcNode
-
copyTo
public static void copyTo(Node srcNode, Node destNode) throws RepositoryException
- Parameters:
srcNode
- the node to copydestNode
- the node that the contents of srcNode will be copied to- Throws:
RepositoryException
IllegalArgumentException
- if scrNode is same as destNode or destNode is a descendant of srcNode
-
copyTo
public static Node copyTo(Node srcNode, CopyHandler chain) throws RepositoryException
- Parameters:
srcNode
- the node to copychain
- the handler that intercepts node and property creation, can be null- Throws:
RepositoryException
-
copyToChain
public static void copyToChain(Node srcNode, CopyHandler chain) throws RepositoryException
- Throws:
RepositoryException
-
getPrimaryNodeType
public static NodeType getPrimaryNodeType(Node node) throws RepositoryException
Retrieve the primary node type. Can handle frozen nodes as well as regular nodes.- Parameters:
node
-- Returns:
- the primary node type
- Throws:
RepositoryException
-
getMixinNodeTypes
public static NodeType[] getMixinNodeTypes(Node node) throws RepositoryException
Retrieve the mixin node types present on a node. Can handle frozen nodes as well as regular nodes.- Parameters:
node
-- Returns:
- the mixin node types present on a node
- Throws:
RepositoryException
-
createBinaryValueFromObject
public static Value createBinaryValueFromObject(Session session, Object object) throws RepositoryException
Serialize the givenobject
into a binary JCR value.- Parameters:
session
- to useobject
- to serialize- Returns:
- a binary value containing the serialized object
- Throws:
RepositoryException
-
emptyNodeIterable
public static NodeIterable emptyNodeIterable()
- Returns:
- an empty
NodeIterable
-
ensureIsCheckedOut
public static void ensureIsCheckedOut(Node node) throws RepositoryException
Make sure the node is in checked out state. If the node is not in checked out state it will get checked out- Parameters:
node
- the node to check- Throws:
RepositoryException
-
isVirtual
public static boolean isVirtual(Node node) throws RepositoryException
- Parameters:
node
- the node to check- Returns:
- whether the node is virtual
- Throws:
RepositoryException
-
getNodePathQuietly
public static String getNodePathQuietly(Node node)
Get the path of aNode
, ornull
if the path cannot be retrieved.This method is mainly provided for convenience of usage, so a developer does not have to worry about exception handling in case it is not of interest.
-
getNodeNameQuietly
public static String getNodeNameQuietly(Node node)
Get the name of aNode
, ornull
if the node cannot be retrieved.This method is mainly provided for convenience of usage, so a developer does not have to worry about exception handling in case it is not of interest.
-
getDisplayNameQuietly
public static String getDisplayNameQuietly(Node node)
Get the display name of aNode
, ornull
if the node cannot be retrieved.This method is mainly provided for convenience of usage, so a developer does not have to worry about exception handling in case it is not of interest.
-
getProperties
public static Iterable<Property> getProperties(Node node) throws RepositoryException
CallsNode.getProperties()
on the given node and returns the it as anIterable
instance for use in a Java 5 for-each loop.- Parameters:
node
- node- Returns:
- properties of the node as an iterable
- Throws:
RepositoryException
- if theNode.getProperties()
call fails
-
isPropertyEvent
public static boolean isPropertyEvent(Event event)
-
getNextSiblingIfExists
public static Node getNextSiblingIfExists(Node current) throws RepositoryException
Returns the next sibling
Node
ofcurrent
. If there is no next sibling it returnsnull
. If the parentNode
ofcurrent
is not an orderableNode
, anUnsupportedRepositoryOperationException
will be thrown. Checking whether the parent is orderable can be done easily as followsNode parent = current.getParent(); parent.getPrimaryNodeType().hasOrderableChildNodes()
If the caller of this method uses this method to reorder nodes, then take into account whether the parent node allows same name siblings or not. If you do not know, use
Node parent = current.getParent(); if (parent.getDefinition().allowsSameNameSiblings()) { Node next = getNextSiblingIfExists(current); parent.orderBefore(current.getName() + "[" + current.getIndex() + "]", next.getName()+ "[" + next.getIndex() + "]"); } else { parent.orderBefore(current.getName(), getNextSiblingIfExists(current).getName()); }
- Parameters:
current
- theNode
for which to find the next sibling- Returns:
- the next sibling of current if there is a next one and
null
in case there is no next sibling - Throws:
UnsupportedRepositoryOperationException
- if the parent ofcurrent
is not orderableRepositoryException
- if some repository exception happens
-
isAncestor
public static boolean isAncestor(Node ancestor, Node descendant) throws RepositoryException
- Throws:
RepositoryException
-
getDescendants
public static List<Node> getDescendants(Node source, String nodeType, boolean prune) throws RepositoryException
Returns the list of jcr nodes which are descendants of
source
and of typenodeType
, wheresource
is never included in the result. Whenprune
istrue
, matching descendants are added and the descendants of a matching descendant are not scanned (and thus also not added)- Parameters:
source
-nodeType
-- Returns:
- Throws:
RepositoryException
-
-