Implementing a Custom Predicate Evaluator for Replication Metadata

As an example this section describes how to create a custom predicate evaluator that helps data based on the replication metadata:

  • cq:lastReplicated that stores the date of the last replication action

  • cq:lastReplicatedBy that stores the id of the user who triggered the last replication action

  • cq:lastReplicationAction that stores the last replication action (for example, Activation, Deactivation)

Querying Replication Metadata with Default Predicate Evaluators

The following query fetches the list of nodes in /content branch that have been activated by admin since the beginning of the year.

path=/content

1_property=cq:lastReplicatedBy
1_property.value=admin

2_property=cq:lastReplicationAction
2_property.value=Activate

daterange.property=cq:lastReplicated
daterange.lowerBound=2013-01-01T00:00:00.000+01:00
daterange.lowerOperation=>=

This query is valid but hard to read and does not highlight the relationship between the three replication properties. Implementing a custom predicate evaluator reduces the complexity and improve the semantic of this query.

Objectives

The goal of the ReplicationPredicateEvaluator is to support the above query using the following syntax.

path=/content

replic.by=admin
replic.since=2013-01-01T00:00:00.000+01:00
replic.action=Activate

Grouping replication metadata predicates with a custom predicate evaluator helps to create a meaningful query.

Updating Maven Dependencies

NOTE
The setup of new Adobe Experience Manager (AEM) projects using maven is documented by How to Build AEM Projects using Apache Maven.

First, update the Maven dependencies of your project. The PredicateEvaluator is part of the cq-search artifact so it must be added to your Maven pom.xml file.

NOTE
The scope of the cq-search dependency is set to provided because cq-search is provided by the OSGi container.

pom.xml

The following snippet shows the differences in unified diff format

@@ -120,6 +120,12 @@
             <scope>provided</scope>
         <dependency>
+            <groupid>com.day.cq</groupid>
+            <artifactid>cq-search</artifactid>
+            <version>5.6.4</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
             <groupid>junit</groupid>
             <artifactid>junit</artifactid>
             <version>3.8.1</version></dependency>

aem-search-custom-predicate-evaluator - pom.xml

Writing The ReplicationPredicateEvaluator

The cq-search project contains the AbstractPredicateEvaluator abstract class. This can be extended with a few steps to implement your own custom predicate evaluator (PredicateEvaluator).

NOTE
The following procedure explains how to build an Xpath expression to filter data. Another option would be to implement the includes method that selects data on a row basis. See the Java™ documentation for more information.
  1. Create a Java™ class which extends com.day.cq.search.eval.AbstractPredicateEvaluator

  2. Annotate your class with a @Component like the following

    src/main/java/com/adobe/aem/docs/search/ReplicationPredicateEvaluator.java

    The following snippet shows the differences in unified diff format

@@ -19,8 +19,11 @@
  */
 package com.adobe.aem.docs.search;

+import org.apache.felix.scr.annotations.Component;
+
 import com.day.cq.search.eval.AbstractPredicateEvaluator;

+@Component(metatype = false, factory = "com.day.cq.search.eval.PredicateEvaluator/repli")
 public class ReplicationPredicateEvaluator extends AbstractPredicateEvaluator {

 }
NOTE
The factorymust be a unique string starting with com.day.cq.search.eval.PredicateEvaluator/and ending with the name of your custom PredicateEvaluator.
NOTE
The name of the PredicateEvaluator is the predicate name, which is used when building queries.
  1. Override:

    public String getXPathExpression(Predicate predicate, EvaluationContext context)
    

    In the override method, you build a Xpath expression based on the Predicate given in argument.