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 Details

  • Constructor Details

    • JcrUtils

      public JcrUtils()
  • Method Details

    • getNodeIfExists

      public static Node getNodeIfExists(Node baseNode, String relPath) throws RepositoryException
      Get the node at relPath from baseNode or null if no such node exists.
      Parameters:
      baseNode - existing node that should be the base for the relative path
      relPath - relative path to the node to get
      Returns:
      the node at relPath from baseNode or null 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 at absPath or null if no such node exists.
      Parameters:
      absPath - the absolute path to the node to return
      session - to use
      Returns:
      the node at absPath or null 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 at absPath or null if no such node exists. In case there are more nodes at absPath, the last node is returned.
      Parameters:
      absPath - the absolute path to the node to return
      session - to use
      Returns:
      the node at absPath or null 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 at relPath from baseNode or defaultValue if no such property exists.
      Parameters:
      baseNode - existing node that should be the base for the relative path
      relPath - relative path to the property to get
      defaultValue - default value to return when the property does not exist
      Returns:
      the string property value at relPath from baseNode or defaultValue 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 at relPath from baseNode or defaultValue if no such property exists.
      Parameters:
      baseNode - existing node that should be the base for the relative path
      relPath - relative path to the property to get
      defaultValue - default value to return when the property does not exist
      Returns:
      the enum value of a string property at relPath from baseNode or defaultValue 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 at relPath from baseNode or defaultValue if no such property exists.
      Parameters:
      baseNode - existing node that should be the base for the relative path
      relPath - relative path to the property to get
      defaultValue - default value to return when the property does not exist
      Returns:
      the multiple string property value at relPath from baseNode or defaultValue 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 at relPath from baseNode or defaultValue if no such property exists.
      Parameters:
      baseNode - existing node that should be the base for the relative path
      relPath - relative path to the property to get
      defaultValue - default value to return when the property does not exist
      Returns:
      the multiple string property value at relPath from baseNode or defaultValue 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 at relPath from baseNode or defaultValue if no such property exists.
      Parameters:
      baseNode - existing node that should be the base for the relative path
      relPath - relative path to the property to get
      defaultValue - default value to return when the property does not exist
      Returns:
      the unique and unordered multiple string property value at relPath from baseNode or defaultValue 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 at relPath from baseNode or defaultValue if no such property exists.
      Parameters:
      baseNode - existing node that should be the base for the relative path
      relPath - relative path to the property to get
      defaultValue - default value to return when the property does not exist
      Returns:
      the long property value at relPath from baseNode or defaultValue 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 at relPath from baseNode or defaultValue if no such property exists.
      Parameters:
      baseNode - existing node that should be the base for the relative path
      relPath - relative path to the property to get
      defaultValue - default value to return when the property does not exist
      Returns:
      the double property value at relPath from baseNode or defaultValue 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 at relPath from baseNode or defaultValue if no such property exists.
      Parameters:
      baseNode - existing node that should be the base for the relative path
      relPath - relative path to the property to get
      defaultValue - default value to return when the property does not exist
      Returns:
      the boolean property value at relPath from baseNode or defaultValue 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 at relPath from baseNode or defaultValue if no such property exists.
      Parameters:
      baseNode - existing node that should be the base for the relative path
      relPath - relative path to the property to get
      defaultValue - default value to return when the property does not exist
      Returns:
      the date property value at relPath from baseNode or defaultValue 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 at relPath from baseNode or defaultValue if no such property exists.
      Parameters:
      baseNode - existing node that should be the base for the relative path
      relPath - relative path to the property to get
      defaultValue - default value to return when the property does not exist
      Returns:
      the decimal property value at relPath from baseNode or defaultValue 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 at relPath from baseNode or defaultValue if no such property exists.
      Parameters:
      baseNode - existing node that should be the base for the relative path
      relPath - relative path to the property to get
      defaultValue - default value to return when the property does not exist
      Returns:
      the binary property value at relPath from baseNode or defaultValue 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 at relPath from baseNode or defaultValue if no such property exists.
      Parameters:
      baseNode - existing node that should be the base for the relative path
      relPath - relative path to the property to get
      defaultValue - default value to return when the property does not exist
      Returns:
      the node property value at relPath from baseNode or defaultValue 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 at absPath or defaultValue if no such property exists.
      Parameters:
      session - to use
      absPath - absolute path to the property to get
      defaultValue - default value to return when the property does not exist
      Returns:
      the string property value at absPath or defaultValue 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 at absPath or defaultValue if no such property exists.
      Parameters:
      session - to use
      absPath - absolute path to the property to get
      defaultValue - default value to return when the property does not exist
      Returns:
      the long property value at absPath or defaultValue 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 at absPath or defaultValue if no such property exists.
      Parameters:
      session - to use
      absPath - absolute path to the property to get
      defaultValue - default value to return when the property does not exist
      Returns:
      the double property value at absPath or defaultValue 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 at absPath or defaultValue if no such property exists.
      Parameters:
      session - to use
      absPath - absolute path to the property to get
      defaultValue - default value to return when the property does not exist
      Returns:
      the boolean property value at absPath or defaultValue 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 at absPath or defaultValue if no such property exists.
      Parameters:
      session - to use
      absPath - absolute path to the property to get
      defaultValue - default value to return when the property does not exist
      Returns:
      the date property value at absPath or defaultValue 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 at absPath or defaultValue if no such property exists.
      Parameters:
      session - to use
      absPath - absolute path to the property to get
      defaultValue - default value to return when the property does not exist
      Returns:
      the decimal property value at absPath or defaultValue 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 at absPath or defaultValue if no such property exists.
      Parameters:
      session - to use
      absPath - absolute path to the property to get
      defaultValue - default value to return when the property does not exist
      Returns:
      the binary property value at absPath or defaultValue 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 at absPath or defaultValue if no such property exists.
      Parameters:
      session - to use
      absPath - absolute path to the property to get
      defaultValue - default value to return when the property does not exist
      Returns:
      the node property value at absPath or defaultValue 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 at relPath from baseNode or null if no such property exists.
      Parameters:
      baseNode - existing node that should be the base for the relative path
      relPath - relative path to the property to get
      Returns:
      the property at relPath from baseNode or null 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 at absPath or null if no such property exists.
      Parameters:
      absPath - the absolute path to the property to return
      session - to use
      Returns:
      the property at absPath or null 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 at srcAbsPath to destAbsPath as session operation.
      Parameters:
      session - to use
      srcAbsPath - the absolute path of the source node
      destAbsPath - 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
      Copies Node srcNode to destParentNode with name destNodeName.
      Parameters:
      srcNode - the node to copy
      destNodeName - the name of the to be newly created node
      destParentNode - 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
      Copies Node srcNode to destNode.
      Parameters:
      srcNode - the node to copy
      destNode - 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
      Copies Node srcNode to destNode with a handler to rewrite content if necessary.
      Parameters:
      srcNode - the node to copy
      chain - 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 given object into a binary JCR value.
      Parameters:
      session - to use
      object - 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 a Node, or null 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.

      Parameters:
      node - - The Node to get the path of
      Returns:
      The path of the Node, or null if node is null or an exception happens.
    • getNodeNameQuietly

      public static String getNodeNameQuietly(Node node)
      Get the name of a Node, or null 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.

      Parameters:
      node - - The Node to get the name of
      Returns:
      The name of the Node, or null if node is null or an exception happens.
    • getDisplayNameQuietly

      public static String getDisplayNameQuietly(Node node)
      Get the display name of a Node, or null 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.

      Parameters:
      node - - The Node to get the name of
      Returns:
      The display name of the Node, or null if node is null or an exception happens.
    • getProperties

      public static Iterable<Property> getProperties(Node node) throws RepositoryException
      Calls Node.getProperties() on the given node and returns the it as an Iterable instance for use in a Java 5 for-each loop.
      Parameters:
      node - node
      Returns:
      properties of the node as an iterable
      Throws:
      RepositoryException - if the Node.getProperties() call fails
    • isPropertyEvent

      public static boolean isPropertyEvent(Event event)
    • getNextSiblingIfExists

      public static Node getNextSiblingIfExists(Node current) throws RepositoryException

      Returns the next sibling Node of current. If there is no next sibling it returns null. If the parent Node of current is not an orderable Node, an UnsupportedRepositoryOperationException will be thrown. Checking whether the parent is orderable can be done easily as follows

               Node 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 - the Node 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 of current is not orderable
      RepositoryException - 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 type nodeType, where source is never included in the result. When prune is true, 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