Groovy Updater Script Examples

Examples

  1. Add a property
  2. Update HST configuration to replace deprecated hst:content node

Example 1 : Add a property

The following updater script example adds a property gettingstarted:copyright to all documents of type gettingstarted:newsdocument. The script would be useful in the following situation:

  1. Create a new project from the archetype.
  2. Create some news documents.
  3. Modify the gettingstarted:newsdocument type and add a String field called 'copyright' with default value "(c) BloomReach".
  4. Commit the type.
  5. Create some more news documents.

All newly created news document will now get the 'copyright' field with the default value "(c) BloomReach". However, existing news documents still have an empty 'copyright' field, and need to be updated. The following updater script will add the default copyright statement if it does not exist. It will also undo the operation.

After the updater script is executed, all existing news documents will have the default copyright value too. When the script is undone, the default copyright value will be removed again from only those news documents that were changed by the updater. The other news documents will not be touched.

XPath query

/jcr:root/content/documents//element(*, gettingstarted:newsdocument) 

Groovy script

package org.hippoecm.frontend.plugins.cms.dev.updater

import org.onehippo.repository.update.BaseNodeUpdateVisitor
import javax.jcr.Node

class CopyrightUpdater extends BaseNodeUpdateVisitor {

  private static final PROPERTY_COPYRIGHT = 'gettingstarted:copyright'
  private static final DEFAULT_COPYRIGHT = '(c) BloomReach'

  boolean doUpdate(Node node) {
    if (!node.hasProperty(PROPERTY_COPYRIGHT)) {
        log.debug "Adding copyright to node ${node.path}"
        node.setProperty(PROPERTY_COPYRIGHT, DEFAULT_COPYRIGHT);
        return true;
    }
    return false;
  }

  boolean undoUpdate(Node node) {
    if (node.hasProperty(PROPERTY_COPYRIGHT)) {
      node.getProperty(PROPERTY_COPYRIGHT).remove();
      return true;
    }
    return false;
  }

}

Example 2: Update HST configuration to replace deprecated hst:content node

In Bloomreach Experience Manager 7.8.x, the HST deprecated the hst:content node below hst:site nodes. Instead of a hst:content node of type hippo:facetselect with a hippo:docbase containing UUID of root site content, the HST now supports instead a property hst:content on the hst:site node containing the absolute jcr path. To replace all hst:content nodes by this hst:content property, the following example Groovy script can be run:

Groovy query

//element(*,hst:site) 

Groovy script 

package org.hippoecm.frontend.plugins.cms.dev.updater
import org.onehippo.repository.update.BaseNodeUpdateVisitor
import javax.jcr.Node

class UpdaterTemplate extends BaseNodeUpdateVisitor {

  /*
   * Code to replace hst:content facetselects below hst:site node. Instead of
   * facetselect below hst:site node, we now support an absolute jcr path as
   * hst:content property on hst:site node.
   */
  boolean doUpdate(Node node) {
    if (!node.isNodeType("hst:site")) {
      log.warn "This script should run with query '//element(*, hst:site)'"
      return false
    }
    if (node.hasNode("hst:content")) {
      Node redundantContentNode = node.getNode("hst:content")
      String docbase =
        redundantContentNode.getProperty("hippo:docbase").getString();
      try {
        // test valid UUID format
        java.util.UUID.fromString(docbase);
        String contentPath =
           node.getSession().getNodeByIdentifier(docbase).getPath();
        node.setProperty("hst:content",contentPath);
        log.debug "Replaced hst:content node '${redundantContentNode.path}'
          by hst:content property '${contentPath}' for hst:site '${node.path}'"
        redundantContentNode.remove()
        return true;
      } catch (javax.jcr.ItemNotFoundException e) {
          log.warn "No node found for docbase '${docbase}'.
                                 Cannot replace hst:content node";
      } catch (java.lang.IllegalArgumentException e) {
          log.warn "invalid docbase '${docbase}' found";
      }
    } else if (node.hasProperty("hst:content")){
      log.info "hst:site node '${node.path}' already uses hst:content as
                                   property and does not need to be converted"
    }
    return false
  }

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

}
Did you find this page helpful?
How could this documentation serve you better?
On this page
    Did you find this page helpful?
    How could this documentation serve you better?