# Business Logic — JBSbatKKBndWdtOvrSendMl.getMailInfoMap() [40 LOC]

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

## 1. Role

### JBSbatKKBndWdtOvrSendMl.getMailInfoMap()

This method implements a **cache-and-freshen pattern** for retrieving email configuration information in the domain-limit-over-notification batch system. Given a mail code (メールコード) that identifies a specific email template or recipient type, it first checks an in-memory cache (`mailInfList`) to see whether the email information has already been loaded during the current batch execution. If found in the cache, it returns the cached instance immediately. If not — or if the cache is empty — it delegates to the data access layer method `executeCC_M_MAIL_KK_SELECT_004` to perform a database query using SQL key `KK_SELECT_004` against the `CC_M_MAIL` table, then stores the freshly retrieved result back into the cache for future lookups within the same execution. This caching strategy prevents redundant database round-trips when the same mail code is requested multiple times during a single batch run. The method serves as a **shared utility** for the batch's email-sending logic, specifically supporting the domain-limit-over-communication-quantity-notification feature (帯域制御通信量超過通知機能). It implements a **routing/dispatch pattern** where cache hits are served from memory and cache misses are routed to the persistence layer.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["getMailInfoMap(mailCd)"])
    INIT["Initialize wFindFlg, newMailInfMap, mailInfo"]

    START --> INIT

    INIT --> CHECK_NULL{"mailInfList != null?"}

    CHECK_NULL -->|Yes| LOOP["For each mailInfMap in mailInfList"]
    CHECK_NULL -->|No| FINAL_CHECK{"mailInfList.size() == 0?"}

    LOOP --> GET["tgMailInfo = mailInfMap.get(mailCd)"]
    GET --> CHECK_TG{"tgMailInfo != null?"}

    CHECK_TG -->|Yes| FOUND["mailInfo = tgMailInfo; wFindFlg = '1'; break"]
    CHECK_TG -->|No| LOOP_END{More iterations?}
    LOOP_END -->|Yes| LOOP
    LOOP_END -->|No| FINAL_CHECK

    FOUND --> RETURN_MAIL["Return mailInfo"]

    FINAL_CHECK -->|True| DB_QUERY["Build param array; executeCC_M_MAIL_KK_SELECT_004(param)"]
    FINAL_CHECK -->|False| RETURN_MAIL

    DB_QUERY --> CACHE_PUT["newMailInfMap.put(mailCd, mailInfo); mailInfList.add(newMailInfMap)"]
    CACHE_PUT --> RETURN_MAIL
```

**Processing steps explained:**

1. **Initialization** — Creates local variables: `wFindFlg` set to `"0"` (not found), `newMailInfMap` as an empty `HashMap` for caching, and `mailInfo` as an empty `JBSbatCommonDBInterface` result holder.

2. **Cache availability check** — Verifies that `mailInfList` (the in-memory cache) is not `null`. If the cache is null, it skips the search and goes directly to the final condition check.

3. **Linear cache scan** — Iterates over each `HashMap` entry in `mailInfList`, calling `mailInfMap.get(mailCd)` to check if the requested mail code exists. If `tgMailInfo` is non-null, a cache hit is confirmed.

4. **Cache hit path** — Sets `mailInfo` to the cached result, sets `wFindFlg` to `"1"` (found), and breaks out of the loop.

5. **Cache miss condition** — If `mailInfList.size() == 0` (cache is empty) **OR** `wFindFlg.equals("0")` (not found after scan), proceeds to fetch from the database.

6. **Database fetch path** — Builds a parameter array with `opeDate` (from `super.opeDate` — the operation date inherited from the batch superclass) and the `mailCd`, calls `executeCC_M_MAIL_KK_SELECT_004(param)` to query the `CC_M_MAIL` table, then caches the result in `newMailInfMap` and adds it to `mailInfList`.

7. **Return** — Returns the `mailInfo` object in all paths (either from cache or freshly fetched).

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `mailCd` | `String` | Mail code — a unique identifier used to look up email template/recipient configuration records in the `CC_M_MAIL` master table. This code maps to specific email types used in domain-limit-over-communication-quantity notification messages (e.g., MAIL_CD_YKK for return receipts, MAIL_CD_JSHI for notifications). The value determines which row is fetched from the email master table and which email sender/subject/recipient settings are applied. |

**Instance fields read by this method:**

| Field | Type | Business Description |
|-------|------|---------------------|
| `mailInfList` | `ArrayList<HashMap<String, JBSbatCommonDBInterface>>` | In-memory cache of email information maps. Each element is a HashMap keyed by mail code, holding a `JBSbatCommonDBInterface` containing the fetched email record. Used across multiple invocations within a single batch execution to avoid redundant DB queries. |
| `super.opeDate` | `String` | Operation date inherited from the batch superclass (`JBSbatBatch`), used as a parameter for the database query (representing the batch processing date). |

## 4. CRUD Operations / Called Services

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

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| - | `JBSbatKKBndWdtOvrSendMl.executeCC_M_MAIL_KK_SELECT_004` | - | `CC_M_MAIL` (via SQL key `KK_SELECT_004`) | Calls `executeCC_M_MAIL_KK_SELECT_004` in `JBSbatKKBndWdtOvrSendMl`, which performs a database SELECT via `db_CC_M_MAIL.selectBySqlDefine` using SQL key `KK_SELECT_004`, then fetches the next row via `selectNext()`. Throws `JBSbatBusinessException` with message key `EKKB0150JE` if no email record is found. |

**CRUD classification of called operations:**

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `executeCC_M_MAIL_KK_SELECT_004` | KK_SELECT_004 | `CC_M_MAIL` | Read email master record by mail code. Uses `db_CC_M_MAIL.selectBySqlDefine` with SQL key `KK_SELECT_004` and `db_CC_M_MAIL.selectNext()` to retrieve the email configuration row. Parameters include operation date (x5) and mail code. Throws business exception if record is not found (メールマスタ不正). |

**Notes on DB access:**
- The `db_CC_M_MAIL` field is a `JBSbatSQLAccess` object initialized in the `initial()` method with the table name `CC_M_MAIL` (defined by constant `D_TBL_NAME_CC_M_MAIL`).
- The SQL key `KK_SELECT_004` (constant `CC_M_MAIL_KK_SELECT_004`) references a predefined SQL statement in the SQL definition file that performs a SELECT on the `CC_M_MAIL` table.
- The method parameters passed to the SQL include `opeDate` (super.opeDate) repeated five times and the `mailCd` — these correspond to the expected parameters of the underlying SQL query.

## 5. Dependency Trace

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Batch:JBSbatKKBndWdtOvrSendMl.insertTMailSend | `insertTMailSend` → `getMailInfoMap` | `executeCC_M_MAIL_KK_SELECT_004 [R] CC_M_MAIL` |

**Details:**
- The method is called from `insertTMailSend` (line 838) within the same class `JBSbatKKBndWdtOvrSendMl`, which is a batch service class that sends domain-limit-over-communication-quantity notification emails.
- In the caller, `mailCd` is derived from `sendKbn` (send category): if `SEND_KBN_YOKOKU` (return receipt), it uses `MAIL_CD_YKK`; otherwise it uses `MAIL_CD_JSHI`.
- No screen (KKSV) entry points or batch entry points were found within 8 hops of the call chain. The method is an internal utility of the batch class.
- Terminal operation: `executeCC_M_MAIL_KK_SELECT_004` performs a SELECT (R) on the `CC_M_MAIL` table.

## 6. Per-Branch Detail Blocks

### Block 1 — [LOCAL VARIABLE INITIALIZATION] (L1218)

> Initializes the cache lookup tracking variables and result holders.

| # | Type | Code |
|---|------|------|
| 1 | SET | `String wFindFlg = "0"` // Cache miss indicator — "0" means not found, "1" means found [-> "0"] |
| 2 | SET | `HashMap<String, JBSbatCommonDBInterface> newMailInfMap = new HashMap<>()` // Cache container for new email info to be added after DB fetch |
| 3 | SET | `JBSbatCommonDBInterface mailInfo = new JBSbatCommonDBInterface()` // Result holder for email information record |

### Block 2 — [IF] `mailInfList != null` (L1222)

> Checks if the in-memory cache of email information lists has been initialized. If null, skips the cache search entirely and goes to the DB fetch condition.

| # | Type | Code |
|---|------|------|
| 1 | COND | `mailInfList != null` // Check if cache list is available |

**Block 2.1 — [FOR-EACH] Cache iteration (L1224)**

> Iterates through each cached HashMap entry to find a matching mail code.

| # | Type | Code |
|---|------|------|
| 1 | ITER | `for (HashMap<String, JBSbatCommonDBInterface> mailInfMap : mailInfList)` // Scan all cached email info maps |

**Block 2.1.1 — [IF] `tgMailInfo != null` (L1227)**

> Cache hit: the requested mail code was found in one of the cached entries.

| # | Type | Code |
|---|------|------|
| 1 | SET | `JBSbatCommonDBInterface tgMailInfo = mailInfMap.get(mailCd)` // Retrieve email info by mail code from current cache entry [-> mailCd from parameter] |
| 2 | COND | `tgMailInfo != null` // True if the mail code was found in this cache map |

**Block 2.1.1.1 — [CACHE HIT PATH] (L1231)**

> The corresponding mail code exists in the cache (メール情報マップに当該のメールコードが存在する). Sets the result and breaks out of the loop.

| # | Type | Code |
|---|------|------|
| 1 | SET | `mailInfo = tgMailInfo` // Use cached email info as result [-> cache hit] |
| 2 | SET | `wFindFlg = "1"` // Set found flag to "1" [-> "1"] |
| 3 | EXEC | `break` // Exit cache scan loop early — result already found |

### Block 3 — [IF] `mailInfList.size() == 0 || wFindFlg.equals("0")` (L1239)

> Cache miss or empty cache: either the cache list is empty (no entries at all) or the mail code was not found after scanning all cache entries (メール情報マップに当該のメールコードが存在しない — the mail code does not exist in the email information map).

| # | Type | Code |
|---|------|------|
| 1 | COND | `mailInfList.size() == 0` // Cache is empty |
| 2 | COND | `wFindFlg.equals("0")` // Not found after cache scan [-> "0"] |
| 3 | OPER | OR — Proceed if either condition is true |

**Block 3.1 — [DB FETCH PATH] (L1243–L1251)**

> Fetch email information from the database, then cache the result for future lookups (メール情報を取得し、メール情報マップに格納する).

| # | Type | Code |
|---|------|------|
| 1 | SET | `Object[] param = new String[] { super.opeDate, super.opeDate, mailCd, super.opeDate, super.opeDate, super.opeDate }` // Build 6-element parameter array: operation date (x5) + mail code |
| 2 | SET | `param[0] = super.opeDate` // Operation date — first parameter |
| 3 | SET | `param[1] = super.opeDate` // Operation date — second parameter |
| 4 | SET | `param[2] = mailCd` // Mail code — lookup key |
| 5 | SET | `param[3] = super.opeDate` // Operation date — fourth parameter |
| 6 | SET | `param[4] = super.opeDate` // Operation date — fifth parameter |
| 7 | SET | `param[5] = super.opeDate` // Operation date — sixth parameter |
| 8 | CALL | `mailInfo = this.executeCC_M_MAIL_KK_SELECT_004(param)` // Query database for email info; SQL key KK_SELECT_004; returns JBSbatCommonDBInterface or throws JBSbatBusinessException if not found |
| 9 | SET | `newMailInfMap.put(mailCd, mailInfo)` // Store fetched result in cache map keyed by mail code |
| 10 | SET | `mailInfList.add(newMailInfMap)` // Add new cache entry to the list |

**Block 3.2 — [ELSE: CACHE HIT PATH] (implicit)**

> When `mailInfList.size() > 0 && wFindFlg.equals("1")` — the mail code was found in the cache during Block 2. The method falls through to the return statement. No additional processing is needed.

### Block 4 — [RETURN] (L1255)

> Returns the email information object. In all code paths (cache hit from Block 2.1.1 or DB fetch from Block 3.1), `mailInfo` holds the result.

| # | Type | Code |
|---|------|------|
| 1 | RETURN | `return mailInfo` // Returns cached email info or freshly fetched record from DB |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `mailCd` | Parameter | Mail code — unique identifier for email template/recipient configuration in the `CC_M_MAIL` master table |
| メールコード | Field (Japanese) | Mail code — identifies specific email types used in the notification system |
| メール情報 | Field (Japanese) | Email information — configuration data for email sending including sender, recipient, subject |
| メール情報マップ | Field (Japanese) | Email information map — HashMap structure used to organize email records by mail code |
| メールマスタ不正 | Field (Japanese) | Email master incorrect — error condition when an email master record is not found in the database |
| メール情報を取得し、メール情報マップに格納する | Field (Japanese) | Fetch email information and store in email information map — comment describing the DB fetch + cache population path |
| メール情報マップに当該のメールコードが存在する | Field (Japanese) | The corresponding mail code exists in the email information map — comment describing the cache hit path |
| メール情報マップに当該のメールコードが存在しない | Field (Japanese) | The corresponding mail code does not exist in the email information map — comment describing the cache miss / DB fetch condition |
| `mailInfList` | Field | In-memory cache list — ArrayList of HashMaps, each mapping mail codes to email info records, preventing redundant DB queries |
| `D_TBL_NAME_CC_M_MAIL` | Constant | Table name constant — "CC_M_MAIL" (テーブル(メール)) — the email master table in the database |
| `CC_M_MAIL_KK_SELECT_004` | Constant | SQL definition key constant — "KK_SELECT_004" (SQL定義キー) — the SQL key used to query the CC_M_MAIL table |
| `db_CC_M_MAIL` | Field | Database access object — JBSbatSQLAccess instance for performing SELECT operations on the CC_M_MAIL table |
| `CC_M_MAIL` | Entity | Email master table — database table storing email template and recipient configuration data (送信者名, メール宛先, 件名, etc.) |
| `KK_SELECT_004` | SQL Key | Predefined SQL statement key for SELECT query on CC_M_MAIL table |
| `JBSbatCommonDBInterface` | Type | Common database interface — generic data container used across the batch framework for holding query result sets and parameter maps |
| `JBSbatSQLAccess` | Type | Database SQL access class — framework class for executing SQL queries against database tables via SQL keys |
| `JBSbatBusinessException` | Type | Business exception — thrown when business logic rules are violated (e.g., email master record not found) |
| `EKKB0150JE` | Constant | Error message key — Japanese error message identifier for email master not found condition |
| `opeDate` | Field | Operation date — batch processing date inherited from the superclass, used as a query parameter |
| `insertTMailSend` | Method | Insert and send email — caller method within the same class that sends domain-limit-over-communication-quantity notification emails; invokes getMailInfoMap to retrieve email configuration |
| `SEND_KBN_YOKOKU` | Constant | Send category — return receipt (返信) email type |
| `MAIL_CD_YKK` | Constant | Mail code — return receipt email code (訳却) |
| `MAIL_CD_JSHI` | Constant | Mail code — notification email code (通知) |
| Batch | System type | This method belongs to a batch processing system (koptBatch) that handles domain-limit-over-communication-quantity notification (帯域制御通信量超過通知) |
| 帯域制御通信量超過通知 | Business term | Domain-limit-over-communication-quantity notification — the business function that sends alerts when a subscriber's data usage exceeds their allocated domain limit |
