Skip to content

Instantly share code, notes, and snippets.

@Rampai94
Last active February 28, 2025 20:56
Show Gist options
  • Save Rampai94/148436beecef24c31764aaffdedb6676 to your computer and use it in GitHub Desktop.
Save Rampai94/148436beecef24c31764aaffdedb6676 to your computer and use it in GitHub Desktop.
Sample script that queries the JCR and updates property
/*This method is used to Query the JCR and find results as per the Query.*/
def buildQuery(parent) {
def queryManager = session.workspace.queryManager;
def statement = 'select * from [nt:file] as t where ISDESCENDANTNODE(['+parent+']) AND NAME() LIKE \'%.jpg\'';
queryManager.createQuery(statement, 'JCR-SQL2');
}
final def query = buildQuery(<replace_parent_path_here>);
final def result = query.execute()
count = 0;
//Get initial number of nodes
println 'No Of nodes found :' + result.nodes.size();
result.nodes.each {
node ->
String nodePath = node.path;
//check if has content
if(node.hasNode('jcr:content')){
def contentNode = node.getNode('jcr:content');
//check if has mimeType
if(contentNode.hasProperty('jcr:mimeType')){
contentNode.setProperty('jcr:mimeType', 'image/jpeg')
count ++;
}
//Log the path updated
println 'updated --' + contentNode ;
//Save the session
session.save();
}
}
//Verify if all nodes updated
println 'Number of nodes updated: ' + count;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment