# Business Logic — JBSbatKKKjFinClDataInTrn.updateSvcKeiUcwk2() [64 LOC]

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

## 1. Role

### JBSbatKKKjFinClDataInTrn.updateSvcKeiUcwk2()

This method performs the **update operation for Service Contract Line Items (Cancellation Section)** — specifically, it marks an existing service contract line record as having been terminated (解約済, kaiyazu). It is the dedicated updater for the "contract cancellation line items" subset of the service contract detail table (`KK_T_SVC_KEI_UCWK`), as distinguished from general or other-purpose line items. The method follows an **optimistic locking with pessimistic select-then-update** pattern: first, it acquires a pessimistic row-level lock via `selectByPrimaryKeysForUpdateWait` to prevent concurrent modifications, then applies a fixed set of status-change fields to reflect the cancellation lifecycle (service contract status set to "terminated", plan end dates cleared, contract cancellation date set to the batch operation date, and the invalid flag deactivated). Its **role in the larger system** is as an internal batch service handler, called from `JBSbatKKKjFinClDataInTrn.execute()` during the batch data-in processing phase. The method implements a **delegation pattern** — it prepares all parameter arrays and delegates the actual SQL update to `executeKK_T_SVC_KEI_UCWK_PKUPDATE2()`. If the target record does not exist (no matching row under the given service contract line number and generation timestamp), it throws a business exception (`EKKB0210CE`), ensuring the batch fails fast rather than silently skipping records.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["updateSvcKeiUcwk2 svcKeiUcwkNo2_151, geneAddDtm2_151"])
    INIT["Initialize where_map with SVC_KEI_UCWK_NO + GENE_ADD_DTM"]
    LOCK["Pessimistic Lock selectByPrimaryKeysForUpdateWait on KK_T_SVC_KEI_UCWK"]
    COND{Record found
map not null}
    YES["Allocate value7 and param2 arrays"]
    SET1["Set value0 = SVC_KEI_STAT_DLS_ZM = 910
Contract Terminated"]
    SET2["Set value1-3 empty
Plan End YMD, Plan Charge End YMD, Rsv Apply YMD"]
    SET3["Set value4 = opeDate
Contract Cancellation YMD"]
    SET4["Set value5 = RSV_APLY_CD_TTDK = 1
Reservation Application In Progress"]
    SET5["Set value6 = MK_FLG_MK = 1
Invalid Flag Deactivated"]
    SET_P0["Set param0 = svcKeiUcwkNo2_151
Service Contract Line No 1"]
    SET_P1["Set param1 = geneAddDtm2_151
Generation Registration Timestamp 1"]
    DEBUG{Debug log
enabled}
    LOG["Print debug log param and value contents"]
    EXEC["Execute executeKK_T_SVC_KEI_UCWK_PKUPDATE2 value, param"]
    NO["Throw JBSbatBusinessException
EKKB0210CE Record not found"]
    END(["Return Next"])

    START --> INIT --> LOCK --> COND
    COND -->|Yes| YES
    YES --> SET1 --> SET2 --> SET3 --> SET4 --> SET5 --> SET_P0 --> SET_P1 --> DEBUG
    DEBUG -->|Yes| LOG --> EXEC --> END
    DEBUG -->|No| EXEC --> END
    COND -->|No| NO
```

**Constant Resolution Notes:**

| Constant | Resolved Value | Business Meaning |
|----------|---------------|------------------|
| `JBSbatKKConst.SVC_KEI_STAT_DLS_ZM` | `"910"` | Service Contract Status — Terminated (解約済) |
| `JBSbatKKConst.RSV_APLY_CD_TTDK` | `"1"` | Reservation Application Code — In Progress (予約手続中) |
| `JBSbatKKConst.MK_FLG_MK` | `"1"` | Invalid Flag — Invalid/Deactivated (無効) |

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `svcKeiUcwkNo2_151` | `String` | Service Contract Line Item Number 2 — the primary key identifier for a specific service contract line item (cancellation section). Used to uniquely identify the record in the `KK_T_SVC_KEI_UCWK` table, combined with `geneAddDtm2_151` for pessimistic locking. |
| 2 | `geneAddDtm2_151` | `String` | Generation Registration Timestamp — the date/time stamp associated with when this line item record was created or last regenerated. Used as the second part of the composite primary key for optimistic conflict detection and pessimistic row locking. |

**Instance Fields Read:**

| Field | Type | Business Description |
|-------|------|---------------------|
| `super.opeDate` | `String` | Batch operation date — the current batch run date used as the "Contract Cancellation YMD" (予約取消年月日) when setting the updated record. |
| `super.logPrint` | `JBSbatLogPrint` (inferred) | Logging handler used for debug-level output of parameter and value contents. |
| `db_KK_T_SVC_KEI_UCWK_2` | `JBSbatSQLAccess` | SQL access object configured for the `KK_T_SVC_KEI_UCWK` table, used for the pessimistic lock select operation. |

## 4. CRUD Operations / Called Services

### Pre-computed evidence from code analysis graph:

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| - | `JBSbatCommonDBInterface()` constructor | JBSbatCommon | - | Instantiates in-memory data mapping objects |
| U | `JBSbatKKKjFinClDataInTrn.executeKK_T_SVC_KEI_UCWK_PKUPDATE2` | (self-delegation) | `KK_T_SVC_KEI_UCWK` | Updates service contract line item record by primary key with 7 status fields |
| - | `JBSbatCommonDBInterface.setValue` | JBSbatCommon | - | Sets key-value pairs on in-memory mapping objects |
| - | `JBSbatSQLAccess.selectByPrimaryKeysForUpdateWait` | JBSbatSQLAccess | `KK_T_SVC_KEI_UCWK` | Performs pessimistic row lock select (SELECT ... FOR UPDATE WAIT) |
| - | `JBSbatLogPrint.chkLogLevel` | JBSbatLogPrint | - | Checks whether debug-level logging is enabled |
| - | `JBSbatLogPrint.printDebugLog` | JBSbatLogPrint | - | Outputs debug log messages with parameter/value details |
| - | `JBSbatBusinessException()` constructor | JBSbatCommon | - | Constructs a business exception for the "record not found" error case |

### Updated CRUD Classification:

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R+U | `db_KK_T_SVC_KEI_UCWK_2.selectByPrimaryKeysForUpdateWait` | (DB Access Layer) | `KK_T_SVC_KEI_UCWK` | Pessimistic lock read — selects the target record by primary key and acquires an exclusive row lock to prevent concurrent updates (排他検索 = exclusive search) |
| U | `executeKK_T_SVC_KEI_UCWK_PKUPDATE2(value, param)` | (Self-delegation) | `KK_T_SVC_KEI_UCWK` | Primary key update — updates 7 fields (SVC_KEI_UCWK_STAT, PLAN_ENDYMD, PLAN_CHRG_ENDYMD, RSV_APLY_YMD, RSV_CL_YMD, RSV_APLY_CD, MK_FLG) for the row identified by SVC_KEI_UCWK_NO and GENE_ADD_DTM |

### Called Method Detail — `executeKK_T_SVC_KEI_UCWK_PKUPDATE2`:

This private method (L1109, 26 LOC) is a **delegated primary-key update handler**. It maps the incoming `value[]` array to individual field names (SVC_KEI_UCWK_STAT, PLAN_ENDYMD, PLAN_CHRG_ENDYMD, RSV_APLY_YMD, RSV_CL_YMD, RSV_APLY_CD, MK_FLG) and the `param[]` array to the WHERE clause keys (SVC_KEI_UCWK_NO, GENE_ADD_DTM), then calls `db_KK_T_SVC_KEI_UCWK_2.updateByPrimaryKeys()` to execute the SQL UPDATE.

## 5. Dependency Trace

### Pre-computed evidence from code analysis graph:

No screen/batch entry points found within 8 hops. Direct callers found: 1 methods.
Terminal operations from this method: `executeKK_T_SVC_KEI_UCWK_PKUPDATE2` [U], `printDebugLog` [-], `printDebugLog` [-], `printDebugLog` [-], `printDebugLog` [-], `printDebugLog` [-], `printDebugLog` [-], `printDebugLog` [-], `printDebugLog` [-], `printDebugLog` [-], `printDebugLog` [-], `printDebugLog` [-], `printDebugLog` [-], `printDebugLog` [-], `printDebugLog` [-], `printDebugLog` [-], `printDebugLog` [-], `printDebugLog` [-], `printDebugLog` [-], `printDebugLog` [-]

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Batch: `JBSbatKKKjFinClDataInTrn.execute()` | `execute()` → `updateSvcKeiUcwk2(svcKeiUcwkNo2_151, geneAddDtm2_151)` | `executeKK_T_SVC_KEI_UCWK_PKUPDATE2 [U] KK_T_SVC_KEI_UCWK` |

**Call Chain Details:**
- `JBSbatKKKjFinClDataInTrn` is a batch service class that processes financial/contract data ingestion (`FinClDataInTrn` = Finance/Contract Data Insert/Transfer).
- The `execute()` method is the main batch entry point, orchestrating multiple data processing sub-steps.
- `updateSvcKeiUcwk2` is invoked as one of the batch's update sub-operations, specifically handling cancellation section line items.

## 6. Per-Branch Detail Blocks

**Block 1** — VARIABLE DECLARATION (L1963)

> Initializes local data structures for the update operation.

| # | Type | Code | Business Description |
|---|------|------|---------------------|
| 1 | SET | `JBSbatCommonDBInterface map = new JBSbatCommonDBInterface()` | Creates in-memory map for lock-select result data |
| 2 | SET | `JBSbatCommonDBInterface where_map = new JBSbatCommonDBInterface()` | Creates in-memory map for primary-key WHERE clause values |
| 3 | SET | `String[] value = null;` | Declares update value array (7 fields) |
| 4 | SET | `String[] param = null;` | Declares primary-key parameter array (2 fields) |

**Block 2** — WHERE CLAUSE CONSTRUCTION (L1968-L1969)

> Builds the composite primary key lookup map to identify the target record.

| # | Type | Code | Business Description |
|---|------|------|---------------------|
| 1 | CALL | `where_map.setValue(JBSbatKK_T_SVC_KEI_UCWK.SVC_KEI_UCWK_NO, svcKeiUcwkNo2_151)` | Sets service contract line number in WHERE map |
| 2 | CALL | `where_map.setValue(JBSbatKK_T_SVC_KEI_UCWK.GENE_ADD_DTM, geneAddDtm2_151)` | Sets generation timestamp in WHERE map |

**Block 3** — EXCLUSIVE LOCK SELECT (L1972)

> Performs a pessimistic lock read — this is the "排他制御" (exclusive control) step described in the Javadoc.

| # | Type | Code | Business Description |
|---|------|------|---------------------|
| 1 | CALL | `map = db_KK_T_SVC_KEI_UCWK_2.selectByPrimaryKeysForUpdateWait(where_map)` | SELECT ... FOR UPDATE WAIT on KK_T_SVC_KEI_UCWK. Returns null if no matching row exists. Blocks if the row is locked by another transaction. |

**Block 4** — RECORD FOUND (IF) `(map != null)` (L1974)

> The record exists under the given primary key. Proceeds to build update values and execute the update. This corresponds to "排他検索結果がある場合" (when exclusive search result exists) in the source.

| # | Type | Code | Business Description |
|---|------|------|---------------------|
| 1 | SET | `value = new String[7]` | Allocates 7-element value array for update fields |
| 2 | SET | `param = new String[2]` | Allocates 2-element parameter array for WHERE clause |
| 3 | SET | `value[0] = JBSbatKKConst.SVC_KEI_STAT_DLS_ZM` [-> `"910"`] | Service Contract Status — Terminated (解約済). The cancelled line item status. |
| 4 | SET | `value[1] = ""` | Plan End Date (プラン終了年月日) — cleared to empty for terminated contracts |
| 5 | SET | `value[2] = ""` | Plan Charge End Date (プラン課金終了年月日) — cleared to empty for terminated contracts |
| 6 | SET | `value[3] = ""` | Reservation Apply Date (予約適用年月日) — cleared to empty for terminated contracts |
| 7 | SET | `value[4] = super.opeDate` | Contract Cancellation Date (予約取消年月日) — set to the batch operation date |
| 8 | SET | `value[5] = JBSbatKKConst.RSV_APLY_CD_TTDK` [-> `"1"`] | Reservation Application Code — "In Progress" (予約手続中). Indicates the cancellation is being processed. |
| 9 | SET | `value[6] = JBSbatKKConst.MK_FLG_MK` [-> `"1"`] | Invalid Flag (無効フラグ) — "Invalid/Deactivated" (無効). Marks the line item as deactivated. |
| 10 | SET | `param[0] = svcKeiUcwkNo2_151` | Primary key param[0] — Service Contract Line Number 1 (サービス契約番号1) |
| 11 | SET | `param[1] = geneAddDtm2_151` | Primary key param[1] — Generation Registration Timestamp 1 (世代登録年月日时分秒1) |

**Block 4.1** — DEBUG LOGGING (IF) `(super.logPrint.chkLogLevel(JBSbatLogUtil.MODE_DEBUG))` (L1997)

> Conditional debug output — only active when the batch is running in debug mode. Logs all 7 value fields and 2 parameter fields for audit purposes.

| # | Type | Code | Business Description |
|---|------|------|---------------------|
| 1 | EXEC | `super.logPrint.printDebugLog("param(サービス契約内訳番号)：" + param[0])` | Logs the service contract line item number |
| 2 | EXEC | `super.logPrint.printDebugLog("param(世代登録年月日时分秒)：" + param[1])` | Logs the generation registration timestamp |
| 3 | EXEC | `super.logPrint.printDebugLog("value(サービス契約内訳ステータス（解約済）)：" + value[0])` | Logs the contract status value ("910" = Terminated) |
| 4 | EXEC | `super.logPrint.printDebugLog("value(プラン終了年月日（空文字設定）)：" + value[1])` | Logs plan end date (empty string) |
| 5 | EXEC | `super.logPrint.printDebugLog("value(プラン課金終了年月日（空文字設定）)：" + value[2])` | Logs plan charge end date (empty string) |
| 6 | EXEC | `super.logPrint.printDebugLog("value(予約適用年月日（空文字設定）)：" + value[3])` | Logs reservation apply date (empty string) |
| 7 | EXEC | `super.logPrint.printDebugLog("value(予約取消年月日（運用日付）)：" + value[4])` | Logs contract cancellation date (operation date) |
| 8 | EXEC | `super.logPrint.printDebugLog("value(予約適用コード（予約手続中）)：" + value[5])` | Logs reservation application code ("1" = In Progress) |
| 9 | EXEC | `super.logPrint.printDebugLog("value(無効フラグ（無効）)：" + value[6])` | Logs invalid flag ("1" = Deactivated) |

**Block 4.2** — DELEGATED UPDATE EXECUTION (L2014)

> Delegates to the primary-key update handler. This is the actual SQL UPDATE operation.

| # | Type | Code | Business Description |
|---|------|------|---------------------|
| 1 | CALL | `executeKK_T_SVC_KEI_UCWK_PKUPDATE2(value, param)` | Executes the SQL UPDATE on KK_T_SVC_KEI_UCWK table, setting 7 fields for the row matching the given SVC_KEI_UCWK_NO and GENE_ADD_DTM |

**Block 5** — RECORD NOT FOUND (ELSE) (L2017)

> The pessimistic lock select returned null — no record exists with the given primary key. The batch treats this as an error condition and aborts with a business exception.

| # | Type | Code | Business Description |
|---|------|------|---------------------|
| 1 | EXEC | `throw new JBSbatBusinessException(JPCBatchMessageConstant.EKKB0210CE, new String[]{"サービス契約内訳", "サービス契約内訳番号2" + svcKeiUcwkNo2_151})` | Throws business exception EKKB0210CE indicating the contract line item record was not found. The error message identifies the entity ("サービス契約内訳" = Service Contract Line Item) and the specific number that could not be located. |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `svcKeiUcwkNo` | Field | Service Contract Line Item Number — the unique identifier for a line item within a service contract. This method handles the "cancellation section" (解約分) subset. |
| `geneAddDtm` | Field | Generation Registration Timestamp — the date/time stamp indicating when a particular version of a line item record was created or last regenerated. Used for optimistic concurrency control. |
| `SVC_KEI_UCWK_STAT` | Field | Service Contract Line Item Status — the current status code of the line item. Value `"910"` means "Terminated" (解約済), indicating the contract line has been cancelled. |
| `PLAN_ENDYMD` | Field | Plan End Date (プラン終了年月日) — the date when the service plan is scheduled to end. Cleared to empty upon cancellation. |
| `PLAN_CHRG_ENDYMD` | Field | Plan Charge End Date (プラン課金終了年月日) — the date when billing for the plan ends. Cleared to empty upon cancellation. |
| `RSV_APLY_YMD` | Field | Reservation Apply Date (予約適用年月日) — the date when a reservation takes effect. Cleared to empty upon cancellation. |
| `RSV_CL_YMD` | Field | Contract Cancellation Date (予約取消年月日) — the date the contract is cancelled. Set to the batch operation date. |
| `RSV_APLY_CD` | Field | Reservation Application Code (予約適用コード) — indicates the status of the reservation process. `"1"` means "In Progress" (予約手続中). |
| `MK_FLG` | Field | Invalid Flag (無効フラグ) — marks whether a record is deactivated. `"1"` means the record is invalid/deactivated (無効). |
| `opeDate` | Field | Batch Operation Date (運用日付) — the current batch run date, used as the effective date for contract cancellation records. |
| `SVC_KEI_STAT_DLS_ZM` | Constant | Service Contract Status — Terminated (解約済). Value `"910"`. `DLS` = DSL (Digital Subscriber Line), `ZM` = 済 (completed/done). |
| `RSV_APLY_CD_TTDK` | Constant | Reservation Application Code — In Progress (予約手続中). Value `"1"`. `TTDK` = 手続中 (in progress/processing). |
| `MK_FLG_MK` | Constant | Invalid Flag — Invalid/Deactivated (無効). Value `"1"`. `MK` = 無効 (invalid). |
| `KK_T_SVC_KEI_UCWK` | DB Table | Service Contract Line Item Table — the database table storing service contract line item records. `KK` = K-OPT (telecom service brand), `T` = Table, `SVC` = Service, `KEI` = 内訳 (detail/line item), `UCWK` = 更新訳 (update detail — cancellation section). |
| `selectByPrimaryKeysForUpdateWait` | Method | Pessimistic lock read — performs SELECT ... FOR UPDATE WAIT on the target table, acquiring an exclusive row lock and blocking if the row is held by another transaction. |
| `executeKK_T_SVC_KEI_UCWK_PKUPDATE2` | Method | Primary Key Update — private delegation method that maps array values to field names and executes the SQL UPDATE by primary key on KK_T_SVC_KEI_UCWK. |
| `EKKB0210CE` | Error Code | Business exception for "Record Not Found" — thrown when a required record does not exist in the database. |
| `JBSbatCommonDBInterface` | Class | In-memory key-value data structure used as a mapping container for database operation parameters and results. |
| `JBSbatSQLAccess` | Class | Database access abstraction layer providing select, update, insert, and delete operations against tables. |
| 解約分 | Japanese | Cancellation section — refers to the subset of service contract line items related to contract terminations. |
| 排他制御 | Japanese | Exclusive control / concurrency control — the mechanism to prevent concurrent modifications to the same record. |
| 排他検索 | Japanese | Exclusive search — a SELECT operation that acquires a row lock to prevent other transactions from modifying the record. |
