AEM 6530 - Assets Admin Search Rail Predicate to search based on Asset ID (jcr:uuid)

Goal


Add an Assets search rail predicate to search using jcr:uuid (index available otb /oak:index/uuid)

Query executed is something like below

/jcr:root/content/dam/texas//element(*, dam:Asset)[(@jcr:uuid = 'e164529c-2ea2-4925-9ad5-0c443ec57ee3')]

Demo | Package Install | Github


Asset ID Filter in Rail




Assets Search form Configuration


      http://localhost:4502/libs/cq/core/content/tools/customsearch.html/libs/settings/dam/search/facets/assets/jcr:content








Solution


1) Login to CRXDe Lite and add the Asset ID predicate configuration eaemjcruuid in /apps/settings/dam/search/facets/formbuilderconfig/predicatetypes/items

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
    jcr:primaryType="cq:Page">
    <adminui/>
    <search jcr:primaryType="nt:unstructured">
        <facets jcr:primaryType="nt:unstructured">
            <formbuilderconfig jcr:primaryType="nt:unstructured">
                <predicatetypes jcr:primaryType="nt:unstructured">
                    <items jcr:primaryType="nt:unstructured">
                        <eaemjcruuid
                            jcr:primaryType="nt:unstructured"
                            fieldLabel="Assed ID (jcr:uuid)"
                            fieldPropResourceType="/apps/eaem-search-form-jcruuid-predicate/jcruuid-field"
                            fieldResourceType="/apps/eaem-search-form-jcruuid-predicate/jcruuid-predicate"
                            fieldTitle="Experience AEM Asset ID Predicate"
                            fieldViewResourceType="granite/ui/components/foundation/form/textfield"
                            renderType="text"/>
                    </items>
                </predicatetypes>
            </formbuilderconfig>
        </facets>
    </search>
</jcr:root>


2) Create the configuration page for predicate (fieldPropResourceType in step 1) /apps/eaem-search-form-jcruuid-predicate/jcruuid-field/jcruuid-field.jsp with the following code

<%@include file="/libs/granite/ui/global.jsp" %>
<%@ page session="false" contentType="text/html" pageEncoding="utf-8"
         import="com.adobe.granite.ui.components.Config" %>

<%
    String key = resource.getName();
    String metaType = "eaemjcruuid";

    String fieldLabel = i18n.get("Asset ID");
%>

<input type="hidden" name="<%= xssAPI.encodeForHTMLAttr("./items/" + key) %>">
<input type="hidden" name="<%= xssAPI.encodeForHTMLAttr("./items/" + key + "/jcr:primaryType") %>" value="nt:unstructured">
<input type="hidden" name="<%= xssAPI.encodeForHTMLAttr("./items/" + key + "/sling:resourceType") %>" value="/apps/eaem-search-form-jcruuid-predicate/jcruuid-predicate">
<input type="hidden" name="<%= xssAPI.encodeForHTMLAttr("./items/" + key + "/fieldLabel") %>" value="<%= fieldLabel %>">
<input type="hidden" name="<%= xssAPI.encodeForHTMLAttr("./items/" + key + "/metaType") %>" value="<%= metaType %>">

<div><h3><%= i18n.get("Asset ID (jcr:uuid) predicate")%></h3></div>

<% request.setAttribute ("com.adobe.cq.datasource.fieldtextplaceholder", i18n.get("Asset ID"));%>

<sling:include resource="<%= resource %>" resourceType="dam/gui/coral/components/admin/customsearch/formbuilder/predicatefieldproperties/fieldlabelpropertyfield"/>

<sling:include resource="<%= resource %>" resourceType="granite/ui/components/foundation/form/formbuilder/formfieldproperties/titlefields"/>

<sling:include resource="<%= resource %>" resourceType="granite/ui/components/foundation/form/formbuilder/formfieldproperties/deletefields"/>


3) Create the predicate widget render html (fieldResourceType in step 1) /apps/eaem-search-form-jcruuid-predicate/jcruuid-predicate/jcruuid-predicate.jsp with following code

<%@include file="/libs/granite/ui/global.jsp" %>
<%@ page session="false" contentType="text/html" pageEncoding="utf-8"
           import="com.adobe.granite.ui.components.Config"%>
<%@ page import="com.adobe.granite.ui.components.AttrBuilder" %>

<%
    Config cfg = new Config(resource);
    String name = cfg.get("text", i18n.get("Property"));
    String metaType = cfg.get("metaType", "eaemjcruuid");

    boolean foldableOpen = cfg.get("open", true);
    String selected = foldableOpen ? "selected":"";

    AttrBuilder inputAttrs = new AttrBuilder(request, xssAPI);
    inputAttrs.add("type", "text");
    inputAttrs.add("name", metaType);
    inputAttrs.addClass("coral-Form-field coral-Textfield coral-DecoratedTextfield-input");
    inputAttrs.add("placeholder", "Asset ID (jcr:uuid)");
%>
<coral-accordion variant="large">
    <coral-accordion-item "<%=selected%>" data-metaType="checkboxgroup" data-type="eaemjcruuid">
        <coral-accordion-item-label><%= xssAPI.encodeForHTML(name) %></coral-accordion-item-label>
        <coral-accordion-item-content class="coral-Form coral-Form--vertical">
            <input <%=inputAttrs.build()%>>
        </coral-accordion-item-content>
    </coral-accordion-item>
</coral-accordion>


4) Add the predicate evaluator apps.experienceaem.assets.JcrUUIDPredicateEvaluator for searching using jcr:uuid

package apps.experienceaem.assets;

import com.day.cq.search.Predicate;
import com.day.cq.search.eval.AbstractPredicateEvaluator;
import com.day.cq.search.eval.EvaluationContext;
import org.apache.commons.lang3.StringUtils;
import org.osgi.service.component.annotations.Component;

@Component(
        factory = "com.day.cq.search.eval.PredicateEvaluator/eaemjcruuid"
)public class JcrUUIDPredicateEvaluator extends AbstractPredicateEvaluator {
    public String getXPathExpression(Predicate predicate, EvaluationContext context) {
        String value = predicate.get(predicate.getName());

        if(StringUtils.isEmpty(value)){
            return null;
        }

        return "(@jcr:uuid = '" + value + "')";
    }
}

No comments:

Post a Comment