I have existing script created as a base to get a list of all components that currently are being used under /content/<myProject>/en.
To execute the script, I build and installed the groovy script console in my local aem instance.
I need a solution for to get a list of all components under /apps/<myProject>/components. And then to get seperate list for unused Components.
Please give me a better solution to get result.
Solved! Go to Solution.
Topics help categorize Community content and increase your ability to discover relevant content.
Views
Replies
Total Likes
Hello @gvk19694
You already have the list of all used components.
List of all components can easily be generated
Now to find the list of all components not in use:
List<Integer> unusedComponents= new ArrayList<>(allComponents);
unusedComponents.removeAll(usedComponents);
Refernce: https://www.baeldung.com/java-lists-difference
Assure that eaither all component paths should have full path from '/apps', else just remove it from all Strings
Hello @gvk19694
You already have the list of all used components.
List of all components can easily be generated
Now to find the list of all components not in use:
List<Integer> unusedComponents= new ArrayList<>(allComponents);
unusedComponents.removeAll(usedComponents);
Refernce: https://www.baeldung.com/java-lists-difference
Assure that eaither all component paths should have full path from '/apps', else just remove it from all Strings
Hi @gvk19694,
You can use script like below. It's creating 2 lists on is including all the components form given path, and send that only contains unused components in it. At the end both list are printed.
Of course you have to set proper paths in the script replacing /content/<myProject>/en and /apps/<myProject>/components with real paths.
import javax.jcr.query.Query
import com.day.cq.wcm.api.components.Component
def allComponentsList = []
def unsedComponentsList = []
// getting list of all components
resourceResolver.findResources("SELECT * FROM [cq:Component] AS s WHERE ISDESCENDANTNODE([/apps/<myProject>/components])", Query.JCR_SQL2).each { resource ->
def component = resource.adaptTo(Component.class)
allComponentsList.add(component)
}
// getting list of all unused components
allComponentsList.each { component ->
def resourceType = component.getResourceType()
def numberOfComponentUsage = resourceResolver.findResources("""
SELECT * FROM [nt:base] AS s WHERE ISDESCENDANTNODE([/content/<myProject>/en])
AND s.[sling:resourceType] = '$resourceType'""", Query.JCR_SQL2).size()
if (numberOfComponentUsage == 0) {
unsedComponentsList.add(component)
}
}
println "All components"
allComponentsList.each {cmp -> println "Component title: $cmp.title, resource type: $cmp.resourceType"}
println "Unused components"
println unsedComponentsList.each {cmp -> println "Component title: $cmp.title, resource type: $cmp.resourceType"}
@gvk19694 Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies