> ## Documentation Index
> Fetch the complete documentation index at: https://docs.abbyy.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 単一ドキュメントを処理・検証するための関数セットの作成

> 単一ドキュメントの抽出用に、Pega ライブラリ関数 SendForProcessing、WaitFirstVerificationOrProcessed、FetchingCapturedData を構築します。

単一ドキュメント処理のシナリオを実装するには、次の関数を作成します。

[SendForProcessing](#sendforprocessing)

[WaitFirstVerificationOrProcessed](#waitfirstverificationorprocessed)

[FetchingCapturedData](#fetchingcaptureddata)

[SingleDocumentBatchIsProcessed](#singledocumentbatchisprocessed)

## <a id="sendforprocessing" />SendForProcessing

*SendForProcessing* function は、認識のためにファイルを FC に送信し、バッチ識別子 を返します。

**入力パラメーター**:

| **Name**      | **Java 型** | **説明**           |
| ------------- | ---------- | ---------------- |
| fileName      | String     | ファイル名            |
| base64Content | String     | Base64 形式のファイル内容 |

**Output parameter 型**: *int* (バッチ識別子) 。

**スローされる例外**: Exception。

**Java ソース**:

```
// FlexiCapture Web Services APIクライアントを作成します。 
// FlexiCapture Application Serverのアドレス、テナント名、ユーザー名、パスワードを指定してください。 
try (TenantClient tenantClient = new TenantClient(new URI(baseUri), tenantName, userName, password)) {
  // FlexiCaptureプロジェクトAPIを取得します。プロジェクト名を指定してください。
  try(ProjectApi projApi = tenantClient.getProjects().getProjectApi(singleFileProjectName)) {
    // FlexiCaptureのバッチとドキュメントを作成します。
    Batch batch = new Batch();
    Document doc = new Document();
    // FlexiCaptureプロジェクトにバッチを追加します。
    int batchId = projApi.getBatches().add(batch);
    // FlexiCapture Batch APIを取得します。バッチ識別子を指定してください。
    BatchApi batchApi = projApi.getBatches().getBatchApi(batchId);
    // ファイルのインスタンスを作成します。 
    File file = new File(fileName, Base64.getDecoder().decode(base64Content.replace("\n", "").replace("\r", "")));
    // FlexiCaptureバッチにドキュメントを追加します。
    // ファイルはドキュメントの内容です。
    int docId = batchApi.getDocuments().add(doc, file);
    // FlexiCaptureバッチを実行して処理を開始します。
    batchApi.start();
    //バッチ識別子を返します。
    return batchId;
  }
} catch(Exception ex){
  // エラーメッセージを連結します。 
Throwable cause = ex.getCause();
String message = ex.getMessage();
while (cause != null) {
message += " " + cause.getMessage();
cause = cause.getCause();
}
// 連結したメッセージをスローします。
throw new Exception(message);
}
```

## <a id="waitfirstverificationorprocessed" />WaitFirstVerificationOrProcessed

*WaitFirstVerificationOrProcessed* 関数は、検証 URL を返します。ドキュメントが検証段階に達していない場合は、空の文字列が返されます。

**入力パラメーター**:

| **Name** | **Java 型** | **説明** |
| -------- | ---------- | ------ |
| batchId  | int        | バッチ識別子 |

**出力パラメーターの型**: *String* (検証 URL) 。

**スローされる例外**: Exception。

**Java ソース**:

```
// FlexiCapture Web Services APIクライアントを作成します。 
// FlexiCapture Application Serverのアドレス、テナント名、ユーザー名、パスワードを指定してください。 
try (TenantClient tenantClient = new TenantClient(new URI(baseUri), tenantName, userName, password)) {
  // FlexiCaptureプロジェクトAPIを取得します。プロジェクト名を指定してください。
  try(ProjectApi projApi = tenantClient.getProjects().getProjectApi(singleFileProjectName)) {
    // FlexiCapture バッチ APIを取得します。バッチ識別子を指定してください。
    BatchApi batchApi = projApi.getBatches().getBatchApi(batchId);
    // バッチが検証ステージで停止するまで待機し、FlexiCaptureドキュメントを取得します。
    Document doc = batchApi.getDocuments().waitFirstVerificationOrProcessed();
    // バッチのステージがVerificationかどうかを確認します。
    if (doc.getStageType() == ProcessingStageType.Verification) {
      // FlexiCapture バッチの検証URL文字列（最初の要素）を返します。
      return batchApi.getVerificationUrls(WebPageMode.Mini).get(0).toString();
    } 
    //バッチが検証ステージなしで処理された場合は空の文字列を返します。
    return "";
  }
}catch(Exception ex){
  // エラーメッセージを連結します。 
Throwable cause = ex.getCause();
String message = ex.getMessage();
while (cause != null) {
message += " " + cause.getMessage();
cause = cause.getCause();
}
// 連結したメッセージをスローします。
throw new Exception(message);
}
```

## <a id="fetchingcaptureddata" />FetchingCapturedData

*FetchingCapturedData* 関数は、JSON 形式の出力フィールドのリストを返します。

**入力パラメーター**:

| **Name** | **Java type** | **Description** |
| -------- | ------------- | --------------- |
| batchId  | int           | バッチ識別子          |

**出力パラメーターの型**: *String* (JSON 形式のドキュメントフィールド) 。

**スローされる例外**: Exception。

**Java ソース**:

```
// FlexiCapture Web Services APIクライアントを作成します。 
// FlexiCapture Application Serverのアドレス、テナント名、ユーザー名、パスワードを指定してください。 
try (TenantClient tenantClient = new TenantClient(new URI(baseUri), tenantName, userName, password)) {
  // FlexiCaptureプロジェクトAPIを取得します。プロジェクト名を指定してください。
  try(ProjectApi projApi = tenantClient.getProjects().getProjectApi(singleFileProjectName)) {
    // FlexiCapture バッチ APIを取得します。バッチ識別子を指定してください。
    BatchApi batchApi = projApi.getBatches().getBatchApi(batchId);
    // バッチの処理完了を待機し、FlexiCaptureドキュメントを取得します。
    Document doc = batchApi.getDocuments().waitFirstVerificationOrProcessed();
    // FlexiCapture Document APIを取得します。ドキュメント識別子を指定してください。
    DocumentApi docApi = batchApi.getDocuments().getDocumentApi(doc.getId());
    // 抽出されたfieldsをJSON文字列として返します。
    return docApi.getExportedFields();
  }
}catch(Exception ex){
  // エラーメッセージを連結します。 
Throwable cause = ex.getCause();
String message = ex.getMessage();
while (cause != null) {
message += " " + cause.getMessage();
cause = cause.getCause();
}
// 連結したメッセージをスローします。
throw new Exception(message);
}
```

## <a id="singledocumentbatchisprocessed" />SingleDocumentBatchIsProcessed

*SingleDocumentBatchIsProcessed* 関数は、バッチが処理されたかどうかを示す値を返します。

**入力パラメーター**:

| **名前**  | **Java 型** | **説明** |
| ------- | ---------- | ------ |
| batchId | int        | バッチ識別子 |

**出力パラメーターの型**: *boolean* (ドキュメントが *Processed* ステージにある場合は *true*、それ以外の場合は *false*) 。

**スローされる例外**: Exception.

**Java ソース**:

```
// FlexiCapture Web Services APIクライアントを作成します。 
// FlexiCapture Application Serverのアドレスとテナント名、ユーザー名、パスワードを指定してください。 
try (TenantClient tenantClient = new TenantClient(new URI(baseUri), tenantName, userName, password)) {
  // FlexiCaptureプロジェクトAPIを取得します。プロジェクト名を指定してください。
  try(ProjectApi projApi = tenantClient.getProjects().getProjectApi(singleFileProjectName)) {
    // FlexiCapture Batch APIを取得します。バッチIDを指定してください。
    BatchApi batchApi = projApi.getBatches().getBatchApi(batchId);
    // バッチが検証ステージまたは処理完了ステージで停止するまで待機し、FlexiCaptureドキュメントを取得します。
    Document doc = batchApi.getDocuments().waitFirstVerificationOrProcessed();
    // バッチのステージがProcessedかどうかを確認します。
    return doc.getStageType() == ProcessingStageType.Processed;    
  }
}catch(Exception ex){
  // エラーメッセージを連結します。 
Throwable cause = ex.getCause();
String message = ex.getMessage();
while (cause != null) {
message += " " + cause.getMessage();
cause = cause.getCause();
}
// 連結したメッセージをスローします。
throw new Exception(message);
}
```
