Groovy Script


Hi Friends,

Throughout my project experiences , I have been surrounded with quite challenging tasks. One of that was exploring Groovy Script. By this blog post , I would try to make you familiar with Groovy and I am sure by the end of it, you will be loving this language. Check out all the examples I worked out during my projects and I have tried to explain what magic you can do with Groovy by comments. So go ahead explore and let me know if you have any challenging problems on Groovy.


Installation of Groovy Console :

The first step to learn Groovy would be to install its IDE. Citytechinc provides a Downloadable zip package which you can install in your instances and you are good to go.

Groovy for AEM 5.6.1 & AEM 6

Groovy for AEM 6.1 with Intelligence (Preferable)

AEM Groovy Console provides an interface for running Groovy scripts in the AEM (Adobe CQ) container. What I will try to do is make you familiar with the language through various examples and then you can solve any use case as per your clients business need.


AEM examples and Sample Use Cases solved by Groovy

  • Hello World

This is the most basic program for any language and its a convention to let you know this. We will jump to more client specific requirements in the subsequent examples.

println 'Hello Groovy World '

  • Find Number of Pages, Page Title , Page Names in a Site hierarchy 

By this code piece I will try to show you guys how a page is traversed in groovy and how its properties can be retrieved and displayed.

/**@author Hashim Khan */

import javax.jcr.Node

/*Flag to count the number of pages*/
noOfPages = 0
/*Pathfield which needs to be iterated for an operation*/
path='/content/geometrixx/en/'
findAllPages()

/*This method is used to Iterate all the pages under a hierarchy
 *and get their page title ,path and the overall number of
 *pages.*/

def findAllPages(){
  getPage(path).recurse
      { page ->
        println 'Title:'+page.title
        println 'Path:'+page.path
        noOfPages ++
      }
}

  • Find all the pages wherein a particular component is being used.

Sometimes there will be a scenario where you have to find (and modify/delete) a particular component from the complete site structure in multiple environments. There is no better solution for that kind of problem , other than Groovy Script.

/** @author Hashim Khan */

/** @author Hashim Khan */

/*This method is used to Query the JCR and find results as per the Query.*/
  def buildQuery(page, term) {
        def queryManager = session.workspace.queryManager;
       def statement = 'select * from nt:base where jcr:path like \''+page.path+'/%\' and sling:resourceType = \'' + term + '\'';
        queryManager.createQuery(statement, 'sql');
    }

    /*Defined Content Hierarchy */
   final def page = getPage('/content/geometrixx/en/')
   /*Component ResourceType which is searched in the content hierarchy */
   final def query = buildQuery(page, 'foundation/components/text');
   final def result = query.execute()

   count = 0;
   result.nodes.each { node ->
   String nodePath = node.path;
   println nodePath
 }
   println 'No Of Pages found :' + result.nodes.size();
/** @author Hashim Khan */


  • Find all the pages of a particular Template .

I have depicted to display all the pages using a specific template. In real world you might be asked to modify some of the properties of a template or add, subtract something. All this you can easily do in an instant using groovy script.

/**@author Hashim Khan */

import javax.jcr.Node

/*Flag to count the number of pages*/
noOfPages = 0
/*Pathfield which needs to be iterated for an operation*/
path='/content/geometrixx/en/'
findAllPagesWidTemplate()

/*This method is used to Iterate all the pages under a hierarchy
 *and find pages with a specific template
 */

def findAllPagesWidTemplate(){
 getPage(path).recurse
 { page ->
      def content = page.node
      def property= content.get('sling:resourceType')
      if(property=="geometrixx/components/contentpage"){
         noOfPages ++
         println 'Page Path:'+content.path
      }
 }
}
println 'No Of Pages::'+noOfPages

Alternatively we could have used Query Builder API to find the pages with a particular template. The below method is more robust and user friendly :

SQL QUERY

/** @author Hashim Khan */

/*This method is used to Query the JCR and find results as per the Query.*/
  def buildQuery(page, term) {
        def queryManager = session.workspace.queryManager;
        def statement = 'select * from nt:base where jcr:path like \''+page.path+'/%\' and sling:resourceType = \'' + term + '\'';
/*Here term is the sling:resourceType property value*/
        queryManager.createQuery(statement, 'sql');
    }

    /*Defined Content Hierarchy */
   final def page = getPage('/content/geometrixx/en/')
   /*Template which is searched in the content hierarchy */
   final def query = buildQuery(page, 'geometrixx/components/contentpage');
   final def result = query.execute()

   println 'No Of pages found = ' + result.nodes.size();

   result.nodes.each { node ->
        println 'nodePath::'+node.path
   }

XPATH QUERY


/*This method is used to Query the JCR and find results as per the Query.*/
 def buildQuery(page, term) {
 def queryManager = session.workspace.queryManager;
 def statement = "/jcr:root${page.path}//element(*, cq:Page)[jcr:content/@cq:template = '"+term+"']"
 /*Here term is the cq:template value*/
 def query = queryManager.createQuery(statement, 'xpath')
 }

 /*Defined Content Hierarchy */
 final def page = getPage('/content/geometrixx/en/')
 /*Component ResourceType which is searched in the content hierarchy */
 final def query = buildQuery(page, '/apps/geometrixx/templates/contentpage');
 final def result = query.execute()

 count = 0;
 result.nodes.each { node ->
 String nodePath = node.path;
 println nodePath
 }
 println 'No Of component found :' + result.nodes.size();
 result.nodes.each { node ->
 println 'nodePath::'+node.path
 }


  • Delete all the nodes of a particular type with a specific property.

Deletion of a particular node is quite handy when you have to similar use case and want to modify the content quickly and easily.


/** @author Hashim Khan */

/*This method is used to Query the JCR and find results as per the Query.*/
 def buildQuery(page, term) {
 def queryManager = session.workspace.queryManager;
 def statement = 'select * from nt:base where jcr:path like \''+page.path+'/%\' and sling:resourceType = \'' + term + '\'';
 queryManager.createQuery(statement, 'sql');
 }

 /*Defined Content Hierarchy */
 final def page = getPage('/content/geometrixx/en/')
 /*Component ResourceType which is searched in the content hierarchy */
 final def query = buildQuery(page, 'foundation/components/flash');
 final def result = query.execute()

 count = 0;
 result.nodes.each { node ->
 String nodePath = node.path;

 if(nodePath.contains('flash') && !nodePath.contains('jcr:versionStorage') ){
 count ++;
 println 'deleting--'+nodePath ;
 node.remove();
 /* Save this session if you are sure the correct nodes are being deleted. Once the session is saved the nodes couldn't be retrieved back.
 *session.save();*/
 }
 }
 println 'No Of component found :' + result.nodes.size();
 println 'Number of Component Deleted: ' + count;

  • Modify a property in a complete site hierarchy as per business logic. 

There was a real time problem in one of my project where we have to fill in jcr:title in the Page-title whenever the Page title was a null. Moreover we were having multiple languages sites and have to browse through all of them at once. We used groovy to solve this problem for multiple development environments. Similar to the below example where I am modifying a particular node and its property for the complete hierarchy using Groovy. I have used example for a Geometrixx site (AEM 6.0) so that you can may the results for yourself.


/** @author Hashim Khan */

/*This method is used to Query the JCR and find results as per the Query.*/
 def buildQuery(page, term) {
 def queryManager = session.workspace.queryManager;
 def statement = 'select * from nt:base where jcr:path like \''+page.path+'/%\' and sling:resourceType = \'' + term + '\'';
 queryManager.createQuery(statement, 'sql');
 }

 /*Defined Content Hierarchy */
 final def page = getPage('/content/geometrixx/en/')
 /*Component ResourceType which is searched in the content hierarchy */
 final def query = buildQuery(page, 'collab/calendar/components/event');
 final def result = query.execute()

 count = 0;
 result.nodes.each { node ->
 String nodePath = node.path;

 if(nodePath.contains('event') && !nodePath.contains('jcr:versionStorage') ){
 /*The below iterator is used to fetch the child pages of the parent node */
 node.findAll { it.hasNodes() }.each {
 if(it.name.contains("event")){
 count ++;
 println 'Title--'+it.get('jcr:title') ;
 println 'Node Path--'+it.path ;
 it.set('jcr:title','Hashim');
 println 'Title--'+it.get('jcr:title') ;
 session.save()
 }
 }
 }
 }
 println 'Number Of Component Found :' + result.nodes.size();
 println 'Number of Component Modified:' + count;

  • Count Number of Nodes which have more than 1000+ child nodes.

This a common use case wherein you are asked to check whether a particular hierarchy has nodes which has more than 1000 child nodes . You can change the Search Path as per you convenience and list down the nodes under that hierarchy.


/** @author Hashim Khan */

import javax.jcr.NodeIterator

def path="/etc/tags"
def variable = 1000

println 'Node,COUNT'
getNode(path).recurse { node >
 NodeIterator it = node.getNodes()
 def count =0
 while(it.hasNext()){
 def nodetemp = it.nextNode()
 count++
 }

 if(count>=variable)
 println node.path + ','+count
}

  • Delete all the Unused Tags in an application. 

In this script the unused tags are counted in an application and deleted with a delay . If the tag count is much more it is advisable to run this script in a more specified path.  You have to run this script a few times as it doesn’t delete the tags which has any child nodes.


/** @author Hashim Khan */

 import org.apache.sling.api.resource.Resource
 import com.day.cq.tagging.Tag
 import com.day.cq.tagging.TagManager
 import org.apache.sling.api.resource.ResourceResolver
 import java.lang.Thread.*;
 import javax.jcr.Node;

def tagpath = "/etc/tags";
def delay = 10 ; //in Milliseconds.

def query = getAllTags(tagpath)
def result = query.execute()

def rows = result.rows
def unusedTags = 0

rows.each { row >
 Resource res = resourceResolver.getResource(row.path)
 if(res!=null){
 Tag tag = res.adaptTo(com.day.cq.tagging.Tag)
 Node tempNode = res.adaptTo(javax.jcr.Node);

 if(tag.getCount()==0){
 if(!tempNode.hasNodes()){
 unusedTags++
 println "Deleted Tag : " + tag.getPath()
 tempNode.remove()
 }
 }
 Thread.currentThread().sleep((long)(delay));
 }

}
println "Total Unused Tags :"+unusedTags
//session.save() //Uncomment this to make it working.

def getAllTags(tagpath) {
 def queryManager = session.workspace.queryManager
 def statement = "/jcr:root"+tagpath+"//element(*, cq:Tag)"
 def query = queryManager.createQuery(statement, "xpath")
}

  • Merge Duplicate Tags in an Application

This was a requirement in one of my client who asked us to merge the Duplicate Tags .  This way you can list out all the duplicate tags and merge all of them into the first Master Tag. All the related references in the Pages will automatically be changed as per the API.


/** @author Hashim Khan */

 import org.apache.sling.api.resource.Resource
 import com.day.cq.tagging.Tag
 import org.apache.sling.api.resource.ResourceResolver
 import com.day.cq.tagging.TagManager
 import javax.jcr.Node;
 import java.lang.Thread.*; 

 def tagLocation = "/etc/tags"
 def delay = 10 ; //in Milliseconds.

 def buildQuery(tagLocation) {
 def queryManager = session.workspace.queryManager;
 def statement = "/jcr:root"+tagLocation+ "//element(*, cq:Tag)"
 def query = queryManager.createQuery(statement, 'xpath')
 }

 def findDuplicateTags(tagLocation,tagNodeName) {
 def queryManager = session.workspace.queryManager;
 def statement = "/jcr:root"+tagLocation+ "//element(*, cq:Tag) [fn:name() = '" + tagNodeName + "' ]"
 def query = queryManager.createQuery(statement, 'xpath')
 }

 final def query = buildQuery(tagLocation);
 final def result = query.execute()

 def tagList = []

 result.nodes.each {node->
 String nodeTitle = node.name;
 tagList.add(nodeTitle);
 }
 def duplicates = tagList.findAll {tagList.count(it) > 1}
 def uniqueUsers = duplicates.unique(mutate = false)
 def count = 0;
 TagManager tm = resourceResolver.adaptTo(com.day.cq.tagging.TagManager);
 def mergecount = 0;

 uniqueUsers.each {
 def tagquery = findDuplicateTags(tagLocation,it);
 def pathresult = tagquery.execute()
 Tag tag , masterTag =null;

 count = 0;
 pathresult.nodes.each {node->
 Resource r = resourceResolver.getResource(node.path)
 tag = r.adaptTo(com.day.cq.tagging.Tag)
 Node tempNode = r.adaptTo(javax.jcr.Node);
 if(count == 0 ){
 masterTag = tag ;
 }else if(tm!=null && !(tag.getPath()==masterTag.getPath())){
 if(!tempNode.hasNodes()){
 println 'Merging Tag :: ' + tag.getPath() +' into>> '+ masterTag.getPath()
 mergecount++
 tm.mergeTag(tag,masterTag)
 }
 }
 count++
 Thread.currentThread().sleep((long)(delay));
 }

 }
println 'Merged tags count ::'+ mergecount


  • Create a CSV File for Duplicate Tags List in the Application.

This script can be used to generate a CSV output and store into filesystem . It lists down all the tags which are Duplicate and all the pages where they are being used. It will help to analyse the System Taxonomy.


/** @author Hashim Khan */

import org.apache.sling.api.resource.Resource
import com.day.cq.tagging.Tag
import org.apache.sling.api.resource.ResourceResolver

def filePath = "/opt/adobe/output.csv"
def tagLocation = "/etc/tags/geometrixx-media"

def buildQuery(tagLocation) {
def queryManager = session.workspace.queryManager;
def statement = "/jcr:root"+tagLocation+ "//element(*, cq:Tag)"
def query = queryManager.createQuery(statement, 'xpath')
}

def findDuplicateTags(tagLocation,tagNodeName) {
def queryManager = session.workspace.queryManager;
def statement = "/jcr:root"+tagLocation+ "//element(*, cq:Tag) [fn:name() = '" + tagNodeName + "' ]"
def query = queryManager.createQuery(statement, 'xpath')
}

def findPagesWithTag(tagId, tagPath) {
def queryManager = session.workspace.queryManager;
def statement = "//element(*, cq:Page)[(jcr:content/@cq:tags = '" + tagId + "' or jcr:content/@cq:tags = '" + tagPath + "' )]"
def query = queryManager.createQuery(statement, 'xpath')
}

final def query = buildQuery(tagLocation);
final def result = query.execute()

def tagList = []

f = new File(filePath)

result.nodes.each {node->
String nodeTitle = node.name;
tagList.add(nodeTitle);
}

def duplicates = tagList.findAll {tagList.count(it) > 1}
def uniqueUsers = duplicates.unique(mutate = false)

print 'TAGTITLE ,TAGID , Pages , Count'+'\n'
f.append('TAGTITLE ,TAGID , Pages , Count'+'\n')
uniqueUsers.each {
def tagquery = findDuplicateTags(tagLocation,it);
def pathresult = tagquery.execute()
pathresult.nodes.each {node->
Resource r = resourceResolver.getResource(node.path)
Tag t1 = r.adaptTo(com.day.cq.tagging.Tag)
print t1.getTitle()+','
f.append(t1.getTitle()+',')
def pagequery = findPagesWithTag(t1.getTagID(), node.path);
def pageresult = pagequery.execute()
print t1.getTagID()+','
f.append(t1.getTagID()+',')
count = 0;
def totalResults = pageresult.getTotalSize()
pageresult.nodes.each { pagenode->
if(count>0){
print ','
f.append(',')
}
print pagenode.path+','
f.append(pagenode.path+',')

if(count==0){
print t1.getCount()+','
f.append(t1.getCount())+','
}
count++;

if (totalResults != count ){
print '\n'
f.append('\n')
}
print ','
f.append (',')
}
print '\n'
f.append ('\n')
}
print '\n'
f.append ('\n')
}

So friends as you may have noticed that Groovy could be quite useful in an AEM project where you need to modify some content / property in one go. Do let me know if you face any other challenging issues where I could help you in using Groovy.
There could be million of other things where Groovy is very useful . If you want to explore more on Groovy use the pdf :: Groovy Recipes

Happy Groovying !! :)

Now all the Groovy Scripts are available in the GITHUB Project – https://github.com/hashimkhan786/aem-groovy-scripts

Advertisements

23 thoughts on “Groovy Script

  1. I just get:

    groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.getPage() is applicable for argument types: (java.lang.String) values: [/content/geometrixx/en/] Possible solutions: getAt(java.lang.String), get(java.lang.String), getClass(), eval(java.lang.String)

    Like

  2. Hi Hasim,

    I`m trying to remove tags from tag references on deleting tag from ‘Tagging’ section. I`m suceeded in getting list of tag references that tag is being used in using javascript. But unable to trigger the deletetion of those tags on those references.

    I only need to do it using javascript. no java code. Can u help me… here is the code I worte so far.

    (function () {
    if (window.location.pathname !== “/tagging”) {
    return;
    }

    registerShowRefsAlert();

    //the query to find tag references (pages and assets)
    var CHECK_TAGS_SQL_2_QUERY = “SELECT * from [nt:base] AS t WHERE NAME(t) = ‘jcr:content’ ” +
    “AND CONTAINS(t.*, ‘PLACEHOLDER’)”;

    function registerShowRefsAlert(){
    var tagAdmin = CQ.tagging.TagAdmin,
    deleteTagFn = tagAdmin.deleteTag;

    //override ootb function to inject the logic showing references alert
    tagAdmin.deleteTag = function(){
    var tagPath = tagAdmin.getSelectedTag();

    if (tagPath == null) {
    return;
    }

    tagPath = tagPath.substring( this.tagsBasePath.length + 1);

    var tagInfo = CQ.tagging.parseTag(tagPath, true),
    query = encodeURIComponent(CHECK_TAGS_SQL_2_QUERY.replace(“PLACEHOLDER”, tagInfo.getTagID()));

    //you may want to replace this crxde lite call with a servlet returning query results
    query = “/crx/de/query.jsp?type=JCR-SQL2&showResults=true&stmt=” + query;

    //”this” here is tagadmin object, passed as context
    $.ajax( { url: query, context: this } ).done(showAlert);
    };

    function showAlert(data){
    if(_.isEmpty(data) || _.isEmpty(data.results)){
    deleteTagFn.call(this);
    return;
    }

    var message = “Selected tag is referenced. Click ‘yes’ to proceed deleting, ‘no’ to cancel the operation.”;

    _.each(data.results, function(result){
    message = message + result.path + “”;
    });

    CQ.Ext.Msg.show({
    “title”: “Delete Tag”,
    “msg”: message,
    “buttons”: CQ.Ext.Msg.YESNO,
    “icon”: CQ.Ext.MessageBox.QUESTION,
    “fn”: function (btnId) {
    if (btnId == “yes”) {
    this.postTagCommand(“deleteTag”, tagAdmin.getSelectedTag());
    }
    },
    “scope”: this
    });
    }
    }
    }());

    Like

    • Hi,
      I would not recommend to do this from JS . You should write a servlet or jsp to make an ajax call. This should be a server side call where you can use all the Sling and JAVA APIs . Client side queries from JS to server is quite expensive and is not recommended.

      Like

  3. Hi,installed the zip, tried verifying and got this :
    Error during include of component ‘/apps/groovyconsole/components/console’

    Error Message:
    org.apache.sling.api.scripting.ScriptEvaluationException:org.apache.sling.scripting.jsp.jasper.JasperException: /apps/groovyconsole/components/console/head.jsp(1,1)

    Also, the bundle is in installed state and i am seeing thi =s in imported packages:
    com.day.cq.wcm.tags,version=[6.0,7) — Cannot be resolved

    Could you please let me know what’d be the issue.
    TIA.

    Like

  4. Hi , This is very useful and i am trying to copy and paste a jcr node via groovy script and getting exception for all the common methods that i am using like , move / copy / clone . Can you help in here if you ever had done this ?

    Thanks in Advance.

    Like

    • Hi Saurabh, You can use a query builder within groovy to search for all nodes of jcr:primaryType as rep:User under the path /home/users and then iterate it and add the properties to it based on your excel sheet.

      Like

  5. Hi Hashim,

    after running groovy , we have found it doesn’t update all the node which we are getting from a query. Suppose if i have to update 1000 nodes it will update 850-900 in one go & if i run the script again it will update remaining 150-100 nodes. Why it doesn’t work in one go. Is there any delay needs to implement in script while saving session ?

    Like

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s