# Business Logic — JBSbatTUBmpHaishiSodHakTrn.terminal() [20 LOC]

| Field | Value |
|-------|-------|
| Fully Qualified Name | `eo.business.service.JBSbatTUBmpHaishiSodHakTrn` |
| Layer | Service (Batch Service — extends `JBSbatBusinessService`) |
| Module | `service` (Package: `eo.business.service`) |

## 1. Role

### JBSbatTUBmpHaishiSodHakTrn.terminal()

The `terminal()` method performs the **business service termination processing** (業務サービス終了処理) for the Number Porting Suspension SOD Issuance batch module (`JBSbatTUBmpHaishiSodHakTrn`). It is part of the batch lifecycle pattern: every batch service extending `JBSbatBusinessService` is expected to implement this `terminal()` hook to clean up resources and report the final processing status before the batch framework tears down the service instance. Specifically, it closes all active database access connections (three `JBSbatSQLAccess` handles — `db_KK_T_SVC_KEI_UCWK`, `db_KK_T_SVKEIUW_EOH_TEL`, and `db_ZM_M_TELNO`), then checks the in-memory `errCnt` counter that was incremented during prior processing blocks. If errors occurred (`errCnt > 0`), it sets the global batch error flag via `JBSbatBatchInvoker.setErrFlg(true)` and emits a standardized warning log message `ETUB0640CI` containing the total error count. This ensures the batch scheduler and monitoring infrastructure can detect that processing completed with warnings, even though the main business logic (data retrieval, validation, registration) ran in earlier lifecycle methods.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["START: terminal() - Business Service Termination"])

    STEP1["Close DB Access: db_KK_T_SVC_KEI_UCWK"]
    STEP2["Close DB Access: db_KK_T_SVKEIUW_EOH_TEL"]
    STEP3["Close DB Access: db_ZM_M_TELNO"]

    COND_ERR{errCnt > 0?}

    NO_ERR["No errors - Normal termination"]
    YES_ERR["Set error flag: JBSbatBatchInvoker.setErrFlg(true)"]
    LOG_ERR["Log warning: ETUB0640CI with error count"]

    END_NODE(["END: Return"])

    START --> STEP1
    STEP1 --> STEP2
    STEP2 --> STEP3
    STEP3 --> COND_ERR
    COND_ERR -- "No" --> NO_ERR
    NO_ERR --> END_NODE
    COND_ERR -- "Yes" --> YES_ERR
    YES_ERR --> LOG_ERR
    LOG_ERR --> END_NODE
```

**Processing description:**

1. **Close DB access handles (lines 306–308):** Three database access connections are closed in sequence. These were opened during earlier processing steps (the batch's main data reading and registration phase) to access the service contract detail table, the FTTH service contract info table, and the telephone number master table.
2. **Error count check (line 312):** The method checks the instance field `errCnt` (error count), which is incremented during prior processing when validation or data operations fail.
3. **Error termination branch (lines 314–318):** If `errCnt > 0`, the batch error flag is set globally so the batch scheduler knows the run completed with warnings. A warning log message `ETUB0640CI` is printed, embedding the error count as `%1%` placeholder value.
4. **Normal termination branch:** If `errCnt == 0`, the method returns normally without setting an error flag, indicating clean processing.

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| - | (none) | - | This method takes no parameters. It operates entirely on instance fields set during the batch's prior lifecycle stages. |

**Instance fields accessed:**

| Field | Type | Business Description |
|-------|------|---------------------|
| `errCnt` | `int` | Error count — accumulated number of processing errors (レコード読込件数/エラー回数) detected during the batch's main processing phase. When non-zero, triggers warning termination logic. |

## 4. CRUD Operations / Called Services

### Pre-computed evidence from code analysis:

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| - | `db_KK_T_SVC_KEI_UCWK.close` | TU_SELECT_005 | KK_T_SVC_KEI_UCWK | Closes the SQL access handle for the service contract detail table |
| - | `db_KK_T_SVKEIUW_EOH_TEL.close` | TU_SELECT_002 | KK_T_SVKEIUW_EOH_TEL | Closes the SQL access handle for the FTTH telephone service contract info table |
| - | `db_ZM_M_TELNO.close` | TU_SELECT_002 | ZM_M_TELNO | Closes the SQL access handle for the telephone number master table |
| - | `JBSbatBatchInvoker.setErrFlg` | JBSbatBatchInvoker | - | Sets the global batch error flag (true) to signal processing completed with errors |
| - | `logPrint.printBusinessErrorLog` | ETUB0640CI | - | Logs a warning error message containing the error record count |

**Classification notes:**
- All three `close()` calls are **resource cleanup operations** (not CRUD on business data). They release JDBC/connection resources held by `JBSbatSQLAccess` instances.
- `setErrFlg(true)` is a **framework state mutation** — it sets a static/global flag used by the batch orchestration layer to determine overall success/failure status.
- `printBusinessErrorLog` is a **logging operation** — it writes a warning message (not a data modification).

## 5. Dependency Trace

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Batch: TUSV0109 | `JBSbatBusinessService.run()` (batch framework) -> `JBSbatTUBmpHaishiSodHakTrn.terminal()` | `close [R] KK_T_SVC_KEI_UCWK`, `close [R] KK_T_SVKEIUW_EOH_TEL`, `close [R] ZM_M_TELNO` |

**Explanation:**
- This `terminal()` method is a **framework-lifecycle hook** invoked by the batch execution framework (`JBSbatBusinessService` base class) as part of the standard batch processing lifecycle. The batch job module `TUSV0109` (Number Porting Suspension SOD Issuance Processing) instantiates this class and the framework calls `terminal()` after the main processing logic completes.
- The method `TITLE_1 = "TUSV010901SC"` identifies this service as SC code `TUSV010901SC` within the batch process.
- No direct application-level callers (screens or other services) invoke `terminal()` directly — it is always invoked by the framework's batch lifecycle manager.

## 6. Per-Branch Detail Blocks

### Block 1 — [CLOSE SEQUENCE] (L306)

> Closes all three database access connections established during the batch's main processing phase.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `db_KK_T_SVC_KEI_UCWK.close();` // Close DB access for Service Contract Detail table [KK_T_SVC_KEI_UCWK] (SQL Key: TU_SELECT_005) |
| 2 | EXEC | `db_KK_T_SVKEIUW_EOH_TEL.close();` // Close DB access for FTTH Telephone Service Contract Info table [KK_T_SVKEIUW_EOH_TEL] (SQL Key: TU_SELECT_002) |
| 3 | EXEC | `db_ZM_M_TELNO.close();` // Close DB access for Telephone Number Master table [ZM_M_TELNO] (SQL Key: TU_SELECT_002) |

### Block 2 — [IF CONDITIONAL] `(errCnt > 0)` (L312)

> Checks if any errors occurred during prior batch processing. If the error count is positive, performs warning termination: sets the global error flag and logs a warning message.

| # | Type | Code |
|---|------|------|
| 1 | COND | `if(errCnt > 0)` // Error count check - if any errors detected during processing [errCnt: レコード読込件数/エラー回数] |
| 2 | SET | `JBSbatBatchInvoker.setErrFlg(true);` // Set global batch error flag [JBSbatBatchInvoker.ERR_FLG = true] |
| 3 | CALL | `super.logPrint.printBusinessErrorLog(JPCBatchMessageConstant.ETUB0640CI, new String[] { Integer.toString(errCnt) });` // Log warning termination with error count [ETUB0640CI = "ETUB0640CI"] |

### Block 2.1 — [ELSE IMPLICIT: errCnt == 0] (L312)

> When no errors occurred, the method falls through to the end naturally — no additional actions required.

| # | Type | Code |
|---|------|------|
| 1 | RETURN | `return;` // Normal termination - no errors detected |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `terminal()` | Method | Business service termination hook — cleanup method called at the end of batch service lifecycle |
| `errCnt` | Field | Error count (エラー回数) — accumulated number of processing errors detected during the batch's main processing phase |
| `recordCnt` | Field | Record read count (レコード読込件数) — tracks the number of records processed (used by prior lifecycle methods, not directly accessed here) |
| `KK_T_SVC_KEI_UCWK` | Table | Service Contract Detail table — stores service contract line item details (サービス契約内訳) |
| `KK_T_SVKEIUW_EOH_TEL` | Table | FTTH Telephone Service Contract Info table — stores contract information for optical telephone services (サービス契約内訳：eo光電話) |
| `ZM_M_TELNO` | Table | Telephone Number Master table — master reference for telephone numbers (電話番号) |
| `JBSbatBatchInvoker` | Class | Batch framework invoker — provides static methods for batch lifecycle state management (error flag, return code, DB access maps) |
| `ETUB0640CI` | Constant | Batch warning message code — "Warning termination: Error object record count = (%1%)" (警告終了：【情報】エラー対象レコード＝(%1%)) |
| `JBSbatBusinessService` | Class | Abstract base class for all batch business services — defines the batch lifecycle (init, main process, terminal hooks) |
| `JBSbatSQLAccess` | Class | Framework SQL access wrapper — manages database connection and statement resources for batch operations |
| SOD | Acronym | Service Order Data — telecom order fulfillment entity for service change/registration requests |
| Number Porting Suspension SOD | Business term | Service order type for suspending number porting requests (番号廃止SOD発行処理) — the specific business domain of this batch module |
| TUSV010901SC | SC Code | Service Component identifier for this specific batch service within the TUSV0109 batch job |
| TU_SELECT_005 | SQL Key | SQL definition key for selecting from KK_T_SVC_KEI_UCWK |
| TU_SELECT_002 | SQL Key | SQL definition key for selecting from KK_T_SVKEIUW_EOH_TEL and ZM_M_TELNO |
| `ido_div` | Field | Disruption division (異動区分) — classification code used in batch processing |
| `sod_kihon_info` | Field | SOD basic information (SOD基本情報) — core service order data structure |
| `trgt_data_list` | Field | Target data list (対象データリスト) — collection of data records to be processed |
