# Business Logic — JBSbatKKCourseChgFixTgCst.initial() [29 LOC]

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

## 1. Role

### JBSbatKKCourseChgFixTgCst.initial()

This method performs the **initialization phase** of a batch process that identifies and extracts **target records for course change confirmation (コ—ス変更確认定対象抽出)** — specifically service contracts undergoing course changes that need to be written to a CSV output file for downstream processing. The class is named `JBSbatKKCourseChgFixTgCst`, where "CourseChgFix" stands for "Course Change Confirmation" and "TgCst" stands for "Target Constant" (i.e., extracting the target data set).

As the entry point invoked by the batch framework before the main `execute()` method, this method acts as a **preparation and validation stage**. It configures shared batch parameters via the parent class, establishes database access handles for three core tables (`KK_T_IDO_RSV`, `KK_T_SVKEI_EXC_CTRL`, `KK_T_SVC_KEI_UCWK`) that the batch will query during its main processing loop, and validates that the output file path parameter is provided. It initializes an internal list (`svcKeiNoList`) used to track service contract numbers already processed, preventing duplicate handling across the batch run. The business purpose is to ensure the batch environment is correctly set up before attempting to extract and write course-change target service contracts to a CSV file.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["initial(commonItem)"])
    
    START --> S1["SET super.setCommonInfo(commonItem)"]
    
    S1 --> DB1["CREATE db_KK_T_IDO_RSV → KK_T_IDO_RSV"]
    DB1 --> DB2["CREATE db_KK_T_SVKEI_EXC_CTRL → KK_T_SVKEI_EXC_CTRL"]
    DB2 --> DB3["CREATE db_KK_T_SVC_KEI_UCWK → KK_T_SVC_KEI_UCWK"]
    DB3 --> DB4["CREATE db_KK_T_IDO_RSV_063 → KK_T_IDO_RSV"]
    
    DB4 --> GET1["GET file_path = commonItem.getFreeItem()"]
    
    GET1 --> COND{"file_path.length == 0?"}
    
    COND -->|true| THROW["THROW JBSbatBusinessException
エラー：送信ファイル格納パス名"]
    COND -->|false| SET1["SET mid_dir_kk_path = file_path"]
    
    SET1 --> SET2["SET svcKeiNoList = new ArrayList<String>()"]
    
    SET2 --> END_NODE(["Return / Next"])
    
    THROW --> END_NODE
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `commonItem` | `JBSbatCommonItem` | Batch common parameter message (バッチ共通パラメータ電文) — carries shared batch execution context including the free-item field that holds the output file directory path. This object is the single source of batch-level configuration passed from the batch scheduler framework into the service component. |
| — | `super.setCommonInfo()` | — | Sets `commonItem` into the parent `JBSbatBusinessService`, making it available as shared state for all downstream methods in this batch component. |
| — | `file_path` (instance field) | `String` | Output file storage path (出力ファイル格納パス) — set from `commonItem.getFreeItem()`, used by the batch to determine where to write the CSV target extraction file. |
| — | `mid_dir_kk_path` (instance field) | `String` | Intermediate directory path — mirrors `file_path`, used as the working directory for output file generation during main processing. |
| — | `svcKeiNoList` (instance field) | `List<String>` | Service contract number list (サービス契約番号保持用リスト) — initialized empty, populated during main processing to track which service contracts have already been processed, preventing duplicate CSV output. |
| — | `db_KK_T_IDO_RSV` (instance field) | `JBSbatSQLAccess` | Database access handle for `KK_T_IDO_RSV` (异动预约表) — used to query service contracts with pending course change movements. |
| — | `db_KK_T_SVKEI_EXC_CTRL` (instance field) | `JBSbatSQLAccess` | Database access handle for `KK_T_SVKEI_EXC_CTRL` (サービス契約排他制御) — used for service contract exclusive control queries. |
| — | `db_KK_T_SVC_KEI_UCWK` (instance field) | `JBSbatSQLAccess` | Database access handle for `KK_T_SVC_KEI_UCWK` (サービス契約内容) — used to query service contract detail records. |
| — | `db_KK_T_IDO_RSV_063` (instance field) | `JBSbatSQLAccess` | Secondary database access handle for `KK_T_IDO_RSV` — uses SQL key `KK_SELECT_063` for a different query variant on the same table. |

## 4. CRUD Operations / Called Services

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

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| U | `JBSbatBusinessService.setCommonInfo` | — | — | Sets common batch info into parent service (state mutation, not a DB operation) |
| C | `new JBSbatSQLAccess(commonItem, D_TBL_NAME_KK_T_IDO_RSV)` | — | `KK_T_IDO_RSV` (异动预约表 / Movement Reservation Table) | Creates SQL access layer for querying movement reservation records |
| C | `new JBSbatSQLAccess(commonItem, D_TBL_NAME_KK_T_SVKEI_EXC_CTRL)` | — | `KK_T_SVKEI_EXC_CTRL` (サービス契約排他制御 / Service Contract Exclusive Control) | Creates SQL access layer for querying service contract exclusive control records |
| C | `new JBSbatSQLAccess(commonItem, D_TBL_NAME_KK_T_SVC_KEI_UCWK)` | — | `KK_T_SVC_KEI_UCWK` (サービス契約内容 / Service Contract Details) | Creates SQL access layer for querying service contract detail records |
| C | `new JBSbatSQLAccess(commonItem, D_TBL_NAME_KK_T_IDO_RSV)` | — | `KK_T_IDO_RSV` (异动预约表 / Movement Reservation Table) | Creates a second SQL access handle to the same table with SQL key `KK_SELECT_063` |
| R | `JBSbatCommonItem.getFreeItem()` | — | — | Retrieves the free-item field from common parameters — returns the output file directory path |

**Note:** This method performs no actual database CRUD operations (SELECT/INSERT/UPDATE/DELETE). It only establishes SQL access handles (instantiating `JBSbatSQLAccess` objects), which are used later by the main `execute()` method to perform actual queries against the three tables.

## 5. Dependency Trace

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Batch: JBSbatKKCourseChgFixTgCst | `batch framework → JBSbatKKCourseChgFixTgCst.initial` | `setCommonInfo → [state set]`<br>`SQLAccess → KK_T_IDO_RSV`<br>`SQLAccess → KK_T_SVKEI_EXC_CTRL`<br>`SQLAccess → KK_T_SVC_KEI_UCWK` |

**Notes:**
- This is a batch service component (`eo.business.service`) — it is typically invoked by a batch scheduler (not a user-facing screen).
- The class extends `JBSbatBusinessService`, which is the standard base class for batch service components in this codebase.
- No external callers (other classes or screens) were found directly invoking `initial()` in the searched codebase — this method serves as the internal entry point for the batch execution lifecycle, called by the batch framework or the `execute()` method's internal flow.

## 6. Per-Branch Detail Blocks

**Block 1** — [CALL] (super method delegation) (L165)

> Sets shared batch parameters into the parent class, propagating the common item context to the parent service layer.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `super.setCommonInfo(commonItem)` // Set common batch parameters (共通パラメータを設定します) |

**Block 2** — [CALL × 4] [DB access handle creation] (L168–L171)

> Creates four database access layer instances, each wrapping a specific table. The batch will use these handles to execute SQL queries during main processing.

| # | Type | Code |
|---|------|------|
| 1 | CREATE | `db_KK_T_IDO_RSV = new JBSbatSQLAccess(commonItem, D_TBL_NAME_KK_T_IDO_RSV)` // Generate DB access class (DBアクセスクラスを生成します) — `D_TBL_NAME_KK_T_IDO_RSV = "KK_T_IDO_RSV"` (异动预约表 / Movement Reservation Table) |
| 2 | CREATE | `db_KK_T_SVKEI_EXC_CTRL = new JBSbatSQLAccess(commonItem, D_TBL_NAME_KK_T_SVKEI_EXC_CTRL)` // Generate DB access class — `D_TBL_NAME_KK_T_SVKEI_EXC_CTRL = "KK_T_SVKEI_EXC_CTRL"` (サービス契約排他制御 / Service Contract Exclusive Control) |
| 3 | CREATE | `db_KK_T_SVC_KEI_UCWK = new JBSbatSQLAccess(commonItem, D_TBL_NAME_KK_T_SVC_KEI_UCWK)` // Generate DB access class — `D_TBL_NAME_KK_T_SVC_KEI_UCWK = "KK_T_SVC_KEI_UCWK"` (サービス契約内容 / Service Contract Details) |
| 4 | CREATE | `db_KK_T_IDO_RSV_063 = new JBSbatSQLAccess(commonItem, D_TBL_NAME_KK_T_IDO_RSV)` // Generate DB access class — same table as Block 2.1 but with different SQL key `KK_SELECT_063` |

**Block 3** — [SET] [Retrieve free item] (L173)

> Extracts the free-item field from the common parameters to obtain the output file storage path (フリー項目から格納先を取得します).

| # | Type | Code |
|---|------|------|
| 1 | SET | `file_path = commonItem.getFreeItem()` // Retrieve file storage path from free item (フリー項目から格納先を取得します) |

**Block 4** — [IF-ELSE] [Path validation] (L176)

> Validates that the retrieved file path is non-empty. If empty, throws a batch business exception with message "ファイル出力" (File output).

| # | Type | Code |
|---|------|------|
| 1 | COND | `0 == file_path.length()` // If path length is zero (取得出来なかった場合、エラーとします / If retrieval failed, treat as error) |
| 2 | THROW | `throw new JBSbatBusinessException(JPCBatchMessageConstant.EKKB0130CE, new String[]{"ファイル出力"})` // Throw batch business exception with error code EKKB0130CE (エラーとします) |

**Block 4.1** — [ELSE] (implicit, when condition is false)

| # | Type | Code |
|---|------|------|
| 1 | (continue to Block 5) | // Path is valid, proceed with initialization |

**Block 5** — [SET] (L178)

> Assigns the validated file path to the instance field that the main processing will use for output directory resolution.

| # | Type | Code |
|---|------|------|
| 1 | SET | `mid_dir_kk_path = file_path` // Assign to intermediate directory path (出力ファイル格納パス) |

**Block 6** — [SET] (L180)

> Initializes the service contract number tracking list. This list is populated during main processing to track which contract numbers have been written to the CSV, ensuring no duplicates.

| # | Type | Code |
|---|------|------|
| 1 | SET | `svcKeiNoList = new ArrayList<String>()` // Initialize empty list (サービス契約番号保持用リストの初期化) |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `KK_T_IDO_RSV` | Table | Movement Reservation Table (异动预约表) — stores records of service contracts scheduled for course/movement changes |
| `KK_T_SVKEI_EXC_CTRL` | Table | Service Contract Exclusive Control (サービス契約排他制御) — manages exclusive control locks on service contract records to prevent concurrent modification |
| `KK_T_SVC_KEI_UCWK` | Table | Service Contract Details (サービス契約内容) — stores detailed line-item information for service contracts |
| `KK_SELECT_007` | SQL Key | SQL definition key for a specific query against `KK_T_IDO_RSV` |
| `KK_SELECT_044` | SQL Key | SQL definition key for a specific query against `KK_T_SVC_KEI_UCWK` |
| `KK_SELECT_030` | SQL Key | SQL definition key for a specific query against `KK_T_IDO_RSV` |
| `KK_SELECT_063` | SQL Key | SQL definition key for a specific query against `KK_T_IDO_RSV` (used by `db_KK_T_IDO_RSV_063`) |
| `CRS_CHG_FIX_IF_ID` | Constant | `"KKIFM156001"` — File interface ID for the course change confirmation target information output file |
| ENCODE | Constant | `"Shift-JIS"` — Character encoding used for the output CSV file |
| FILE_KEISHIKI | Constant | `".csv"` — File extension for the output file |
| CONMA | Constant | `","` — Delimiter character for the CSV output file |
| KAIGYOU_CODE | Constant | `"
"` — Line feed code for the CSV output file |
| SVC_KEI_NO | Field | Service contract number (サービス契約番号) — unique identifier for a service contract in the telecom billing system |
| Course Change (コ—ス変更) | Business term | Modification of a telecom service contract's plan/course (e.g., changing bandwidth tiers, adding/removing service features) |
| Course Change Confirmation (コ—ス変更確认定) | Business term | The batch process that extracts and generates a CSV report of service contracts undergoing course changes for confirmation and downstream processing |
| 异动预约 (IDO_RSV) | Business term | Movement/Change reservation — records indicating a service contract is scheduled to change course or configuration |
| 排他制御 (EXC_CTRL) | Business term | Exclusive control — mechanism to prevent multiple processes from simultaneously modifying the same contract record |
| バッチ共通パラメータ電文 | Business term | Batch common parameter message — the standard parameter structure passed between batch framework and service components |
| ファイル出力 | Business term | File output — the act of writing the extracted target data to a CSV file |
| JBSbatBusinessService | Parent class | Base batch service class providing common batch processing utilities including `setCommonInfo`, logging, and framework integration |
| JBSbatSQLAccess | Utility class | Database access abstraction layer used to execute SQL queries against tables by table name and SQL definition key |
| 自由項目 (freeItem) | Field | Free item field — a flexible metadata field in `JBSbatCommonItem` used to pass arbitrary string parameters (here, the output directory path) |
