AEM 6550 - Query Builder Predicate Evaluator for searching both PSB (PhotoShopBig) and PSD (PhotoShopDocument)


Goal

Assets Search filter for searching both PSB (PhotoShop Big) with mime type application/vnd.3gpp.pic-bw-small and PSD (Photoshop Doucment) with mime type image/vnd.adobe.photoshop, by selecting the filter File Type > Images > Bitmaps > Adobe Photoshop

For adding a new filter option to search for PSB files check this post

Github



Solution

Create a predicate evaluator apps.eaem.PhotoshopPredicateEvaluator extending com.day.cq.search.eval.JcrPropertyPredicateEvaluator to create a xpath query something like...

/jcr:root/content/dam/eaem//element(*, dam:Asset)[(jcr:content/metadata/dc:format = 'image/vnd.adobe.photoshop' or jcr:content/metadata/dc:format = 'application/vnd.3gpp.pic-bw-small')]


package com.eaem;

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

import java.io.StringWriter;

@Component(
        factory = "com.day.cq.search.eval.PredicateEvaluator/property"
)
public class PhotoshopPredicateEvaluator extends JcrPropertyPredicateEvaluator {
    private String DC_FORMAT_METADATA_PREFIX = "(jcr:content/metadata/@dc:format";
    private String PSD_MIME_TYPE = "'image/vnd.adobe.photoshop'";
    private String PSB_MIME_TYPE = "'application/vnd.3gpp.pic-bw-small'";

    public String getXPathExpression(Predicate p, EvaluationContext context) {
        String xPathExpr = super.getXPathExpression(p, context);

        if(StringUtils.isEmpty(xPathExpr) || !xPathExpr.startsWith(DC_FORMAT_METADATA_PREFIX)){
            return xPathExpr;
        }

        String value = xPathExpr.substring(xPathExpr.indexOf("=") + 1, xPathExpr.lastIndexOf(")"));

        if(!PSD_MIME_TYPE.equals(value.trim())){
            return xPathExpr;
        }

        StringWriter sw = new StringWriter();

        String firstExpr = xPathExpr.substring(0, xPathExpr.lastIndexOf(")"));

        sw.append(firstExpr).append(" or jcr:content/metadata/@dc:format = " + PSB_MIME_TYPE + ")");

        return sw.toString();
    }
}


No comments:

Post a Comment