Getting to an other node from groovy script

I have a groovy script that uses Xpath to query nodes under a particular path, then I Iterate over all the resultant nodes.
Now this each and every node contains a “UUID”. Is there a way I can get to the node of the “UUID” from the groovy script?
ex. How can i use this method getNodeByIdentifier() inside the groovy script?
or Is there any other way i could do that?

You can get a Node by UUID from a Session:

session.getNodeByIdentifier(“some-uuid”)

From every node you can get the session of the current user:

node.getSession()

So if you know a UUID and have a node, you can get that other node via:

node.getSession().getNodeByIdentifier(“uuid-of-other-node”)

Does that answer your question?

For more methods etc. you can have a look at the JCR API: https://docs.adobe.com/docs/en/spec/javax.jcr/javadocs/jcr-2.0/index.html

hth,

Mathijs

@mathijs Thank you for the answer. It solved my problem. I was in an impression that the session cannot be used inside a groovy script.

It is actually explicitly allowed to get a dedicated JCR session in your groovy script. You can override #initialize(session) method in your script like the following:

class ExampleUpdateVisitor extends BaseNodeUpdateVisitor {

  def session

  void initialize(Session session) {
    this.session = session
  }

  boolean doUpdate(Node node) {
    def updated = false
    // SNIP; you can use this.session here too.
    return updated
  }

  boolean undoUpdate(Node node) {
    throw new UnsupportedOperationException('Updater does not implement undoUpdate method')
  }

  void destroy() {
    // you can override #destroy() after execution if necessary too
  }
}
1 Like

@woonsanko Thank you, this is very helpful!