JCR API

  • Magnolia relies on the JCR API to manage its data.
  • Security is handled by JCR.

Three important Util classes

  • For making coder's life more convenient, we implemented three Util classes.
  • There are Util classes mainly either catch exceptions, or wrap some JCR Api calls together.
  • NodeUtil
  • PropertyUtil
  • NodeTypes

Access a workspace - javax.jcr.Session

  • Generally accessing a workspace with the JCR session 'javax.jcr.Session'

    Session session = MgnlContext.getJCRSession("workspaceName");
     
    Session session = someNode.getSession();

Get javax.jcr.Node or javax.jcr.Property

  • A specific Node or Property can be accessed by the JCR Session

    Node foundNode = session.getNodeByIdentifier("identifier");
    Node foundNode = session.getNode("absolutePath");
    Property foundProperty = session.getProperty("absolutePath");
     
    Node foundNode = NodeUtil.getNodeByIdentifier("workspaceName", "identifier");

Operations on javax.jcr.Node

  • Get a node's name

    String name = someNode.getName();
     
    String name = NodeUtil.getName(someNode);
  • Get all child nodes as a collection

    NodeIterator childrenIterator = someNode.getNodes();
    Iterable<Node> childrenIterable = NodeUtil.asIterable(childrenIterator);
    List<Node> childrenList = NodeUtil.asList(childrenIterable);
     
    Iterable<Node> childrenIterable = NodeUtil.collectAllChildren(someNode);
    List<Node> childrenList = NodeUtil.asList(childrenIterable);
  • Get a specific child node by name

    Node childNode = someNode.getNode("relativePath");
  • Get a node property by name

    Property nodeProperty = someNode.getProperty("relativePath");
     
    Property foundProperty = PropertyUtil.getProperty(someNode, "relativePath");
  • Get a node's path

    String pathToNode = someNode.getPath();
     
    String pathToNode = NodeUtil.getNodePathIfPossible(someNode);

     

  • Create and set a Property value to a specific node property

    someNode.setProperty("propertyName", "propertyValue");
     
    PropertyUtil.setProperty(someNode, "propertyName", "propertyValue");
  • Create a sub node

    someNode.addNode("relativePath", primaryNodeTypeName);
     
    NodeUtil.createPath(parent, "relativePath", primaryNodeTypeName)

     

  • Save the node with its changes

    //Get Session form Node and save.
    someNode.getSession().save();
     
    //I have the session
    session.save() 

Content node MetaData

  • Some examples on content node MetaData with NodeTypes

    NodeTypes.Activatable.getActivationStatus(node);
    NodeTypes.Activatable.update(node, "username", isActivated);
     
    NodeTypes.Renderable.getTemplate(node);
    NodeTypes.Renderable.set(node, "template id");
     
    NodeTypes.LastModified.getLastModified(node);
    NodeTypes.LastModified.update(node);
    NodeTypes.LastModified.update(node, "username", date); 

Operations on javax.jcr.Property

  • Get a property's name

    String name = nodeProperty.getName(); 
  • Get a property's path

    String pathToProperty = nodeProperty.getPath();
  • Get the property's parent node

    Node parentNode = nodeProperty.getParent();
  • Get value from the property

    String stringValue = nodeProperty.getString()
    Calendar dateValue = nodeProperty.getDate();
    Double doubleValue = nodeProperty.getDouble();
    Long longValue = nodeProperty.getLong();
    Binary forBinaryNodeData = nodeProperty.getBinary();
     
    PropertyUtil.getString(someNode, propertyName);
    PropertyUtil.getDate(someNode, propertyName);
    PropertyUtil.getBoolean(someNode, propertyName, defaultValue) 
  • Set the value of the property

    nodeProperty.setValue(booleanValue);
    nodeProperty.setValue(calendarObject);
    nodeProperty.setValue(longValue);
    nodeProperty.setValue(doubleValue);
    nodeProperty.setValue(intValueWillBeStoredAsLong);
    nodeProperty.setValue(booleanValue);
    nodeProperty.setValue(inputStreamForBinary); 

Providing a Node as ContentMap to scripts

  • Operating in template scripts directly on a JCR Node is not very handy.
  • cmsfn provides to all scripts any Node transformed into a ContentMap
  • Much easier access to the Nodes data by the ContentMap.
  • ContentMap provides special attributes which are not properties:
    • @name
    • @path
    • @id
    • @depth
    • @nodeType


      In Java
      ContentMap nodeAsContentMap = new ContentMap(someNode);
      Node backToNode = nodeAsContentMap.getJCRNode(); 
      In Freemarker
      ${cmsfn.asJCRNode(aContentMap)}
      ${cmsfn.asContentMap(aJCRNode)} 

      See cmsfn for more templating functions operating on the JCR API.

#trackbackRdf ($trackbackUtils.getContentIdentifier($page) $page.title $trackbackUtils.getPingUrl($page))