Forked from hemanth415/PageAssetReferencesScript.jsp
Created
October 30, 2024 13:46
-
-
Save ncautotest/d8c19abfc89a7744a5cf0856873d1b34 to your computer and use it in GitHub Desktop.
AEM Fiddle Script to list out all page asset references
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@include file="/libs/foundation/global.jsp"%><% | |
%><%@page session="false" contentType="text/html; charset=utf-8" | |
pageEncoding="UTF-8" | |
import="com.day.cq.dam.api.Asset, | |
com.day.cq.search.PredicateGroup, | |
com.day.cq.search.Query, | |
com.day.cq.search.QueryBuilder, | |
com.day.cq.search.result.Hit, | |
com.day.cq.search.result.SearchResult"%> | |
<%@ page import="org.apache.sling.api.resource.Resource" %> | |
<%@ page import="org.apache.sling.api.resource.ValueMap" %> | |
<%@ page import="javax.jcr.Session" %> | |
<%@ page import="java.util.HashMap" %> | |
<%@ page import="java.util.List" %> | |
<%@ page import="java.util.Map" %> | |
<%@ page import="javax.jcr.Node" %> | |
<%@ page import="com.day.cq.dam.commons.util.AssetReferenceSearch" %> | |
<%@ page import="com.day.cq.commons.jcr.JcrConstants" %> | |
<%@ page import="java.util.ArrayList" %> | |
<% | |
response.setContentType("text/plain"); | |
// Code here | |
// ReferenceSearch API can also be used here. | |
// As we have some dependency issues with this in this ui project, AssetReferenceSearch API being used. | |
QueryBuilder builder = slingRequest.getResourceResolver().adaptTo(QueryBuilder.class); | |
Session session = slingRequest.getResourceResolver().adaptTo(Session.class); | |
Map<String, String> map = new HashMap<String, String>(); | |
//Put your map parameters here | |
map.put("path", "/content/projectSite"); | |
map.put("type","cq:Page"); | |
map.put("p.limit", "-1"); | |
Query query = builder.createQuery(PredicateGroup.create(map), session); | |
SearchResult results = query.getResult(); | |
out.println("Result set Count:" + results.getHits().size()); | |
out.println("</br>"); | |
out.println("Asset Path" + "$" + "Page Path"); | |
out.println("</br>"); | |
int j = 0; | |
HashMap<String, List<String>> assetPageMapper = new HashMap<String, List<String>>(); | |
List<String> pages = null; | |
for (Hit hit : results.getHits()) { | |
Resource currentResource = resourceResolver.resolve(hit.getPath() + "/"); | |
if (currentResource != null) { | |
Resource jcrContentResource = currentResource.getChild(JcrConstants.JCR_CONTENT); | |
if (jcrContentResource != null) { | |
Node node = jcrContentResource.adaptTo(Node.class); | |
AssetReferenceSearch assetReference = new AssetReferenceSearch(node, | |
"/content/dam/floder", resourceResolver); | |
for (Map.Entry<String, Asset> entry : assetReference.search().entrySet()) { | |
String val = entry.getKey(); | |
if (assetPageMapper.containsKey(val)) { | |
pages = assetPageMapper.get(val); | |
pages.add(hit.getPath()); | |
assetPageMapper.put(val, pages); | |
} else { | |
pages = new ArrayList<String>(); | |
pages.add(hit.getPath()); | |
assetPageMapper.put(val, pages); | |
} | |
} | |
} | |
} | |
} | |
for (Map.Entry<String, List<String>> entry : assetPageMapper.entrySet()) { | |
//if (entry.getValue().size() > 1) { | |
out.println("<xmp>"); | |
for (String string: entry.getValue()) { | |
out.println(entry.getKey() + "$" + string); | |
} | |
out.println("</xmp>"); | |
j++; | |
//} | |
} | |
out.println("</br>"); | |
out.println("Number of Asset with issue : " + j); | |
%> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment