# Business Logic — JBSbatKKKDDIAnkenIktTrkm.createKDDITrkmRslt() [30 LOC]

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

## 1. Role

### JBSbatKKKDDIAnkenIktTrkm.createKDDITrkmRslt()

This method is responsible for creating and initializing the KDDI order import result file object used during batch processing of KDDI agency orders (KDDI取次案件 — KDDI shidousaki anken, KDDI agency cases). It acts as a lazy-initialization guard that produces the `kddiTrkmRsltFileObj` instance only when it has not yet been created, ensuring the result file is ready for subsequent write operations in the batch pipeline.

The method performs a two-phase preparation: first, it retrieves the result file name template from the batch work parameter storage using a registered parameter ID (`KK_FILE_NM_KDDI_TRKM`) and resolves any embedded date placeholder (`yyyyMMdd`) with the current system date. Second, it constructs a `JBSbatBusinessFileUtil` file object configured for Windows-31J encoding, CRLF line separators, and comma delimiters, sourced from the free-item path directory.

If the file name cannot be retrieved from the work parameter or if the free-item path is absent, the method short-circuits and returns `false` to signal a system-level error to the caller — this typically indicates a configuration issue in the batch parameter setup.

When the file object is already initialized (i.e., the method was called previously), it skips all preparation logic and immediately returns `true`, indicating readiness with no error. This idempotent behavior supports the batch pipeline's call-chain, where `execute()` may invoke this method as a prerequisite guard before populating and writing result data.

The method implements the **guard-clause** and **lazy-initialization** patterns, centralizing file-object lifecycle management within the service and delegating infrastructure concerns (work-param lookup, date formatting, file object construction) to shared batch-common utilities.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["createKDDITrkmRslt()"])
    CHECK_FILE_OBJ["kddiTrkmRsltFileObj == null"]
    GET_FILE_NAME["getWorkParamSetteValue: file name from work param"]
    VALIDATE_PARAMS["isNotNull(kddiTrkmRslt) && isNotNull(commonItem.getFreeItem())"]
    SYSTEM_ERROR["Return false: system error - missing file name or free item"]
    FILE_PLACEHOLDER["KDDI_DATE_YMD = yyyyMMdd (date placeholder)"]
    REPLACE_DATE["kddiTrkmRsltFileName = file name.replace(yyyyMMdd, getSysDate())"]
    CREATE_FILE["createBusinessFileUtil: generate file object"]
    RETURN_SUCCESS["Return true: no error"]
    END_RETURN(["Return / Next"])

    START --> CHECK_FILE_OBJ
    CHECK_FILE_OBJ --> |true| GET_FILE_NAME
    CHECK_FILE_OBJ --> |false| RETURN_SUCCESS
    GET_FILE_NAME --> VALIDATE_PARAMS
    VALIDATE_PARAMS --> |true| FILE_PLACEHOLDER
    FILE_PLACEHOLDER --> REPLACE_DATE
    REPLACE_DATE --> CREATE_FILE
    VALIDATE_PARAMS --> |false| SYSTEM_ERROR
    SYSTEM_ERROR --> END_RETURN
    CREATE_FILE --> RETURN_SUCCESS
    RETURN_SUCCESS --> END_RETURN
```

**CRITICAL — Constant Resolution:**

| Constant | Actual Value | Business Meaning |
|----------|-------------|-----------------|
| `JKKStrConst.KDDI_WORK_PARAM_ID_TRKM_RSLT` | `"KK_FILE_NM_KDDI_TRKM"` | Work parameter ID identifying the KDDI order import result file name template |
| `JKKStrConst.KDDI_DATE_YMD` | `"yyyyMMdd"` | Date placeholder string embedded in file name template — replaced with current system date at runtime |
| `JKKStrConst.CHAR_SET_WIN31J` | `"Windows-31J"` | Windows Shift-JIS character encoding for Japanese text output |
| `JKKStrConst.LINE_SEPARATOR` | `"\r
"` | Line separator used in the result file (CRLF, Windows-style) |
| `JKKStrConst.KDDI_CONMA` | `","` | Field delimiter (comma) used in the output CSV file |

**Requirements:**
- Every if/else, loop, and method call is represented as a node.
- Diamond nodes for conditions (represented as labeled edges from diamond `CHECK_FILE_OBJ` and `VALIDATE_PARAMS` nodes).
- ALL branches are shown — including the early-return on system error and the skip-path when the file object is already initialized.

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| - | (none) | - | - |

This method takes no explicit parameters. It operates entirely on instance fields and shared batch context:

| # | Field / External State | Type | Business Description |
|---|----------------------|------|---------------------|
| 1 | `kddiTrkmRsltFileObj` | `JBSbatBusinessFileUtil` | The KDDI import result file object. When `null`, initialization is required; when non-null, the method skips initialization and returns `true`. |
| 2 | `kddiTrkmRsltFileName` | `String` | The resolved KDDI import result file name, derived from the work parameter template with date substitution. |
| 3 | `commonItem` | `JBSbatKKKDDIAnkenIktTrkmCommonItem` (inferred) | Shared batch context object providing `getFreeItem()` which yields the file path prefix (directory) for the result file. |
| 4 | `db_ZM_M_WORK_PARAM_KNRI` | `Map<String, Object>` (inferred) | Work parameter data source — a lookup map passed to `getWorkParamSetteValue` for retrieving registered batch parameters. |

## 4. CRUD Operations / Called Services

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

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `JKKBatCommon.getWorkParamSetteValue` | (batch-common utility) | Work Parameter Table (ZM_M_WORK_PARAM_KNRI) | Reads the KDDI import result file name template from work parameter storage |
| R | `JKKBatCommon.isNotNull` | (batch-common utility) | - | Validates that the file name and free item path are non-null/non-empty |
| R | `JKKBatCommon.getSysDate` | (batch-common utility) | - | Retrieves the current system date for file name date-substitution |
| C | `JKKBatCommon.createBusinessFileUtil` | (batch-common utility) | File System | Creates and initializes the business file object with encoding, delimiter, and path configuration |

**How to classify:**
- **R** (Read): `getWorkParamSetteValue` retrieves a string value from work parameter storage; `isNotNull` performs a validation check; `getSysDate` returns a timestamp.
- **C** (Create): `createBusinessFileUtil` instantiates a new file utility object configured for writing the KDDI import result file.
- No **U** (Update) or **D** (Delete) operations — this method only reads from storage, validates, and creates a file object. No data is modified or persisted beyond the file object instantiation.

## 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: `isNotNull` [-], `getWorkParamSetteValue` [R], `getSysDate` [R], `createBusinessFileUtil` [C]

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Batch: `JBSbatKKKDDIAnkenIktTrkm.execute()` | `execute()` -> `createKDDITrkmRslt()` | `getWorkParamSetteValue [R] ZM_M_WORK_PARAM_KNRI`, `getSysDate [R] system`, `createBusinessFileUtil [C] file system` |

**Instructions:**
- `JBSbatKKKDDIAnkenIktTrkm.execute()` is the batch execution entry point for the KDDI agency order import batch job.
- The call chain is direct: `execute()` invokes `createKDDITrkmRslt()` as a prerequisite step before writing result data to the file.
- This method is not called from any screen — it is exclusive to the batch processing layer.

## 6. Per-Branch Detail Blocks

**Block 1** — IF `(kddiTrkmRsltFileObj == null)` (L474–L493)

> Guard clause: checks whether the KDDI import result file object has already been initialized. If it is `null`, the method proceeds to build the file object from the work parameter. If non-null, the method skips this entire block and returns `true` (no initialization needed).

| # | Type | Code |
|---|------|------|
| 1 | CHECK | `kddiTrkmRsltFileObj == null` | IF true, proceed to initialize the result file object |

**Block 1.1** — nested IF `(inside Block 1: file not yet initialized)` (L476–L501)

> The file object needs creation. Resolve the file name template, validate required inputs, and build the file utility object.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `JKKBatCommon.getWorkParamSetteValue(commonItem, JKKStrConst.KDDI_WORK_PARAM_ID_TRKM_RSLT, db_ZM_M_WORK_PARAM_KNRI)` // Retrieves KDDI import result file name template from work parameter storage [-> `KDDI_WORK_PARAM_ID_TRKM_RSLT = "KK_FILE_NM_KDDI_TRKM"`] |
| 2 | SET | `kddiTrkmRslt` = result of `getWorkParamSetteValue(...)` |
| 3 | CALL | `JKKBatCommon.isNotNull(kddiTrkmRslt)` // Validates file name is not null/empty |
| 4 | EXEC | `commonItem.getFreeItem()` // Gets the free-item path prefix for result file directory |
| 5 | CALL | `JKKBatCommon.isNotNull(commonItem.getFreeItem())` // Validates free item path is not null/empty |

**Block 1.1.1** — IF `(NOT (isNotNull(kddiTrkmRslt) AND isNotNull(commonItem.getFreeItem())))` (L479–L481)

> **Early-return guard.** If either the file name template could not be retrieved from the work parameter, or the free-item path is absent, this is a system configuration error. Return `false` to signal the batch pipeline to abort or handle the error at a higher level.

| # | Type | Code |
|---|------|------|
| 1 | CHECK | `!isNotNull(kddiTrkmRslt)` OR `!isNotNull(commonItem.getFreeItem())` | IF either condition is true, this is a system error |
| 2 | RETURN | `return false;` // エラーがありません。(No error — this is a system error signal; the caller interprets false as an error condition occurred) |

**Block 1.2** — ELSE `(both validations passed)` (L485–L495)

> Both the file name and free-item path are valid. Resolve the file name by replacing the date placeholder with the current system date, then create the file utility object.

| # | Type | Code |
|---|------|------|
| 1 | SET | `kddiTrkmRsltFileName` = `kddiTrkmRslt.replace(JKKStrConst.KDDI_DATE_YMD, JKKBatCommon.getSysDate())` // KDDI import result file name resolution — replaces date placeholder [-> `KDDI_DATE_YMD = "yyyyMMdd"`] with current system date |
| 2 | CALL | `JKKBatCommon.getSysDate()` // Retrieves current system date |
| 3 | EXEC | `commonItem.getFreeItem() + "/" + kddiTrkmRsltFileName` // Constructs full file path by concatenating free-item directory with resolved file name |
| 4 | SET | `kddiTrkmRsltFileObj` = `JKKBatCommon.createBusinessFileUtil(path, JKKStrConst.CHAR_SET_WIN31J, JKKStrConst.LINE_SEPARATOR, JKKStrConst.KDDI_CONMA)` // Creates business file utility object with Windows-31J encoding, CRLF line separator, and comma delimiter |
| 5 | EXEC | `createBusinessFileUtil(fileFullPath, "Windows-31J", "\r
", ",")` // Configures the file object for CSV output in Japanese character set |

**Block 2** — RETURN (L501)

| # | Type | Code |
|---|------|------|
| 1 | RETURN | `return true;` // trueである場合、エラーがありません。(true means no error occurred) |

> **Return true path.** This is reached in two scenarios: (a) the file object was already initialized (Block 1 condition was false), so initialization was skipped; (b) the file object was just created (Block 1.2 completed successfully). In both cases, the caller receives `true` indicating the file is ready for subsequent write operations.

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `kddiTrkmRsltFileObj` | Field | KDDI import result file object — the `JBSbatBusinessFileUtil` instance used for writing the KDDI agency order import result CSV |
| `kddiTrkmRsltFileName` | Field | KDDI import result file name — the fully resolved file name (with date) used for the result output |
| `commonItem` | Field | Common batch item — shared context object carrying batch execution parameters including the free-item path |
| `getFreeItem()` | Method | Retrieves the free-item path (directory prefix) stored in the batch common item, used as the file's parent directory |
| `db_ZM_M_WORK_PARAM_KNRI` | Field | Work parameter knowledge database — a map of batch configuration parameters keyed by parameter ID, sourced from the ZM_M_WORK_PARAM_KNRI master table |
| KDDI | Acronym | KDDI Kabushiki Gaisha — major Japanese telecommunications operator. In this context, refers to KDDI agency orders (KDDI取次案件) — orders routed through KDDI's distribution network |
| 取次案件 (shidousaki anken) | Field/Domain | Agency case / intermediary order — a business order handled through a distribution channel or agent rather than directly |
| 取込結果 (torikomi kekka) | Field/Domain | Import result — the output/result data produced after importing and processing records |
| KDDI_WORK_PARAM_ID_TRKM_RSLT | Constant | `"KK_FILE_NM_KDDI_TRKM"` — work parameter ID key for retrieving the KDDI import result file name template |
| KDDI_DATE_YMD | Constant | `"yyyyMMdd"` — date placeholder embedded in file name templates; replaced at runtime with the actual system date |
| CHAR_SET_WIN31J | Constant | `"Windows-31J"` — Windows Japanese character encoding (Windows Shift-JIS); used for the output file to ensure proper Japanese text rendering |
| LINE_SEPARATOR | Constant | `"\r
"` — CRLF line separator for Windows-style line endings in the output file |
| KDDI_CONMA | Constant | `","` — comma delimiter; the field separator used in the KDDI import result CSV output |
| JBSbatBusinessFileUtil | Class | Business file utility class — provides methods for creating, writing to, and closing business output files with configurable encoding and delimiters |
| JKKBatCommon | Class | KDD batch common utility — shared static utility class providing batch-wide helpers for work parameter access, date retrieval, string validation, and file object creation |
| JKKStrConst | Class | KDD string constants — centralized constant definitions for all KDDI batch string literals including parameter IDs, encoding names, separators, and delimiters |
