為查詢產生器實作自訂述詞求值器 implementing-a-custom-predicate-evaluator-for-the-query-builder

本檔案說明如何實作自訂述詞求值器來擴充查詢產生器

概觀 overview

查詢產生器提供查詢內容存放庫的簡單方法。 AEM隨附一組述詞評估器,可協助您查詢資料。

不過,您可能想要實作自訂述詞評估器來簡化查詢,該評估器會隱藏一些複雜度並確保語意更佳。

自訂述詞也可以執行其他不直接與XPath一起執行的動作,例如:

  • 正在查詢其他服務的資料
  • 根據計算的自訂篩選
NOTE
實作自訂述詞時必須考慮效能問題。
TIP
您可以在查詢產生器檔案中找到查詢範例。
TIP
您可以在GitHub上找到此頁面的程式碼
NOTE
GitHub上的連結程式碼以及本檔案中的程式碼片段僅供示範之用。

述詞求值器詳細資料 predicate-evaluator-in-detail

述詞評估器處理特定述詞的評估,這些述詞是查詢的定義限制。

它將較高層級的搜尋條件約束(例如width>200)對應到符合實際內容模型(例如metadata/@width > 200)的特定JCR查詢。 或者,它可以手動篩選節點並檢查其限制。

TIP
如需PredicateEvaluatorcom.day.cq.search封裝的詳細資訊,請參閱Java檔案

實施復寫中繼資料的自訂述詞求值器 implementing-a-custom-predicate-evaluator-for-replication-metadata

本節以範例說明如何建立自訂述詞評估器,協助根據復寫中繼資料的資料:

  • 儲存上次復寫動作日期的cq:lastReplicated
  • cq:lastReplicatedBy儲存觸發上次復寫動作之使用者的識別碼
  • 儲存上次復寫動作(例如啟用、停用)的cq:lastReplicationAction

使用預設述詞評估器查詢復寫中繼資料 querying-replication-metadata-with-default-predicate-evaluators

下列查詢會擷取/content分支中自年初以來由admin啟動的節點清單。

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=>=

此查詢有效,但難以讀取,且不會強調三個復寫屬性之間的關係。 實作自訂述詞求值器將降低複雜性並改善此查詢的語意。

目標 objectives

ReplicationPredicateEvaluator的目標是使用下列語法支援上述查詢。

path=/content

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

使用自訂述詞求值器將復寫中繼資料述詞分組,有助於建立有意義的查詢。

更新Maven相依性 updating-maven-dependencies

TIP
WKND教學課程會詳細說明新AEM專案的設定,包括使用Maven。

首先,您需要更新專案的Maven相依性。 PredicateEvaluatorcq-search成品的一部分,因此必須將其新增到您的Maven pom檔案。

NOTE
cq-search相依性的範圍已設定為provided,因為cq-search是由OSGi容器所提供。

下列程式碼片段顯示pom.xml檔案中的差異,格式為統一的diff

@@ -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>

正在寫入ReplicationPredicateEvaluator writing-the-replicationpredicateevaluator

cq-search專案包含AbstractPredicateEvaluator抽象類別。 這可以透過幾個步驟來延伸以實作您自己的自訂述詞求值器(PredicateEvaluator)。

NOTE
下列程式說明如何建置Xpath運算式以篩選資料。 另一個選項是實作以列為基礎選取資料的includes方法。 如需詳細資訊,請參閱Java檔案
  1. 建立擴充com.day.cq.search.eval.AbstractPredicateEvaluator的新Java類別

  2. 統一的diff格式使用@Component之類的程式碼片段來標註您的類別

    code language-text
    @@ -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 note
    NOTE
    factory必須是以com.day.cq.search.eval.PredicateEvaluator/開頭並以自訂PredicateEvaluator的名稱結尾的唯一字串。
    note note
    NOTE
    PredicateEvaluator的名稱是建立查詢時使用的述詞名稱。
  3. 覆寫:

    code language-java
    public String getXPathExpression(Predicate predicate, EvaluationContext context)
    

    在覆寫方法中,您會根據引數中指定的Predicate建置Xpath運算式。

復寫中繼資料的自訂述詞求值器範例 example-of-a-custom-predicate-evaluator-for-replication-metadata

PredicateEvaluator的完整實作可能類似於以下類別。

/*
 * #%L
 * aem-docs-custom-predicate-evaluator
 * %%
 * Copyright (C) 2013 Adobe Research
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

package com.adobe.aem.docs.search;

import org.apache.felix.scr.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.day.cq.search.Predicate;
import com.day.cq.search.eval.AbstractPredicateEvaluator;
import com.day.cq.search.eval.EvaluationContext;

@Component(metatype = false, factory = "com.day.cq.search.eval.PredicateEvaluator/repli")

public class ReplicationPredicateEvaluator extends AbstractPredicateEvaluator {

    static final String PE_NAME = "replic";


    static final String PN_LAST_REPLICATED_BY = "cq:lastReplicatedBy";
    static final String PN_LAST_REPLICATED = "cq:lastReplicated";
    static final String PN_LAST_REPLICATED_ACTION = "cq:lastReplicationAction";

    static final String PREDICATE_BY = "by";
    static final String PREDICATE_SINCE = "since";
    static final String PREDICATE_SINCE_OP = " >= ";
    static final String PREDICATE_ACTION = "action";

    Logger log = LoggerFactory.getLogger(getClass());

    /**
     * Returns a XPath expression filtering by replication metadata.
     *
     * @see com.day.cq.search.eval.AbstractPredicateEvaluator#getXPathExpression(com.day.cq.search.Predicate,
     *      com.day.cq.search.eval.EvaluationContext)
     */

    @Override

    public String getXPathExpression(Predicate predicate,
            EvaluationContext context) {

        log.debug("predicate {}", predicate);

        String date = predicate.get(PREDICATE_SINCE);
        String user = predicate.get(PREDICATE_BY);
        String action = predicate.get(PREDICATE_ACTION);

        StringBuilder sb = new StringBuilder();

        if (date != null) {

            sb.append(PN_LAST_REPLICATED).append(PREDICATE_SINCE_OP);
            sb.append("xs:dateTime('").append(date).append("')");

        }

        if (user != null) {

            addAndOperator(sb);
            sb.append(PN_LAST_REPLICATED_BY);
            sb.append("='").append(user).append("'");

        }

        if (action != null) {

            addAndOperator(sb);
            sb.append(PN_LAST_REPLICATED_ACTION);
            sb.append("='").append(action).append("'");

        }

        String xpath = sb.toString();

        log.debug("xpath **{}**", xpath);

        return xpath;

    }

    /**
     * Add an and operator if the builder is not empty.
     *
     * @param sb a {@link StringBuilder} containing the query under construction
     */

    private void addAndOperator(StringBuilder sb) {

        if (sb.length() != 0) {

            sb.append(" and ");

        }

    }

}
recommendation-more-help
fbcff2a9-b6fe-4574-b04a-21e75df764ab