Groovy Updater Script Examples

Examples

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

Example 1: Add a Property

This example shows how to add the gettingstarted:copyright property to all documents of type gettingstarted:newsdocument. Use this script when you have updated the document type to include a new field, but existing documents do not yet have the field set.

Scenario:

  1. Create a new project from the archetype.
  2. Create several news documents.
  3. Update the gettingstarted:newsdocument type to add a String field named 'copyright' with the default value "(c) BloomReach".
  4. Commit the updated type definition.
  5. Create additional news documents.

After these steps, only the newly created news documents will have the 'copyright' field set to "(c) BloomReach". Existing documents will have an empty 'copyright' field. To update existing documents, run the following updater script. The script sets the default copyright value for documents missing the property. If you undo the script, it removes the property only from documents that were updated by the script.

After running the updater, all news documents will have the default copyright value. Undoing the script removes the value only from those documents that were changed by the updater.

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

Starting with brXM 7.8.x, the HST no longer uses the hst:content node under hst:site nodes. Previously, this node was of type hippo:facetselect and used the hippo:docbase property to reference the root site content by UUID. The recommended approach is now to use a property hst:content on the hst:site node, which stores the absolute JCR path.

To migrate all hst:content nodes to the new property format, use the following Groovy script.

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 { /* * Replaces hst:content facetselect nodes under hst:site nodes with an * hst:content property containing the absolute JCR path. */ 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 { // Validate 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') } }
Share Feedback
Page: /build/content-updates/groovy-update-script-examples
Section: Build
Category *