记录自定义实施的交易 record-a-transaction-for-custom-implementations

CAUTION
AEM 6.4已结束扩展支持,本文档将不再更新。 有关更多详细信息,请参阅 技术支助期. 查找支持的版本 此处.

使用TransactionRecorder API记录未自动入帐为事务的操作

您可以使用自定义代码提交PDF表单、向最终用户发送代理UI预览URL以预览交互式通信,或使用自定义方法提交表单,而不是使用AEM Forms提供的提交方法。 之前提到的所有AEM Forms API操作和自定义实施均不会计为交易。 AEM Forms提供了一个API, 交易记录器,以记录此类操作(如交易)。

要记录事务,请写入 标准sling servlet 并从客户端调用servlet以记录事务。 您可以使用AJAX或任何其他标准方法调用Servlet。

服务器端代码示例 sample-server-sided-code

您可以使用以下示例代码从JAVA类中使用自定义OSGi包运行TransactionRecorder API。

import com.adobe.aem.transaction.core.ITransactionRecorder;
import com.adobe.aem.transaction.core.model.TransactionRecord;
import com.adobe.aem.transaction.core.exception.TransactionException;
import com.adobe.aem.transaction.core.FormsTransactionConstants;

@Reference
private ITransactionRecorder transactionRecorder;

doPost (SlingHttpServletRequest request, SlingHttpServletResponse response) {
    transactionRecorder.startContext();
    TransactionRecord txRecord = extractTxRecordFromRequest(request); //extract transaction relevant data from request
    try {
        bool success = doBillableWork();
        if (success) {
            transactionRecorder.recordTransaction(txRecord);
        }
    } catch (Exception e) {
        //exception handling
    } finally {
        transactionRecorder.endContext();
    }
}

//Here, it is assumed that txInfo is passed in Stringified json form in the ajax call (in data parameter). You can pass txInfo from client in any way that you find suitable.
private TransactionRecord extractTxRecordFromRequest(SlingHttpServletRequest request) {
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(request.getInputStream()));
    Map<String, Object> txDataMap = new HashMap<String, Object>();
    String txData = bufferedReader.readLine();
    JSONObject txInfo= new JSONObject(txData );
    try {
        String resourceType= txInfo.getString("resourceType");
        String transactionType = txInfo.getString("transactionType");
        Integer transactionCount = (Integer)txInfo.get("transactionCount");
        //Extract all the relevant tx record attributes similarly and pass them in Transaction Record constructor as per the java doc}
        return new TransactionRecord(transactionCount, transactionType, resourceType, ..);
    } catch (JSONException e) {
        //exception handling
    } finally {
        bufferedReader.close();
    }
}

示例客户端代码 sample-client-side-code

您可以使用以下示例代码调用具有 TransactionRecorderAPI。

$.ajax({
   type: 'POST',
   url: url, //servlet url
   contentType: 'application/json; UTF-8',
   data: JSON.stringify({transactionCount : 1,
                        transactionType: "SUBMIT",
                        resourceType: "FORM",
                        resourceSubType: "ADAPTIVE-FORM"}),
   success: successHandler,
   error: faultHandler
})

相关文章 related-articles

recommendation-more-help
a6ebf046-2b8b-4543-bd46-42a0d77792da