# ZMW01904SFLogic

## Purpose

`ZMW01904SFLogic` is the view-level business logic controller for the **ENUM Switch Inquiry** screen (screen ID `ZMW01904`) in the telecom portability management system. It handles the full user journey: initial display and search of ENUM (number portability) records, registration confirmation, execution of the ENUM switch, correction, and completion. In short, it is the orchestrator that bridges the UI to backend services for all ENUM portability operations.

## Design

The class follows the **Model-View-Controller** (MVC) view-logic pattern used across the JCC web framework. It extends `JCCWebBusinessLogic` (the framework's base web business logic class) and acts as a **facade** that:

1. Manages screen state and navigation flow across three related screens (`ZMW01904` → `ZMW01905` → `ZMW01906`).
2. Coordinates data retrieval from service `ZMSV0133` (ENUM switch history lookup) and service `ZMSV0134` (ENUM switch registration/execution).
3. Populates `X31SDataBeanAccess` form beans with display data, computed fields, and button-enablement state.
4. Translates backend error codes into user-facing messages.

There are no interfaces implemented. The class is instantiated per request by the framework wiring layer (referenced in XML configuration files).

## Key Methods

### `boolean init()` [lines 214–270]

**Purpose:** Entry point for the screen. Initializes display data, sets mode flags, and prepares the page for the user.

**Flow:**
- Retrieves the common info bean and service form bean.
- Reads the return-screen ID (`screenId`) from screen info. If `null`, defaults to `ZMW01906`.
- Sets mode:
  - `UPD_INQ_MODE_FLG_UPD` ("1") when navigating from the phone number update screen (`ZMW02001`).
  - `UPD_INQ_MODE_FLG_INQ` ("0") when navigating from the phone number detail screen (`ZMW02201`) or on initial entry.
- Clears page link info (pagination state).
- Restores screen info (inherited state across requests).
- Calls `getInfoInitData()` to fetch initial display data (see below).
- Sets the search command to `EVENT_ID_PAGING`.
- Configures next-screen redirect to `ZMW01904`.

**Returns:** Always `true`.

---

### `boolean back()` [lines 278–291]

**Purpose:** Handles the Back button. Redirects to the original screen.

**Flow:**
- Retrieves the common info bean and the return screen ID.
- Sets the next screen ID and name on the common info bean.

**Returns:** Always `true`.

---

### `boolean paging()` [lines 300–309]

**Purpose:** Handles pagination. Re-executes the initial data-fetch to display the requested page of results.

**Flow:**
- Retrieves the service form bean.
- Calls `getInfoInitData()` to reload data for the current page context.

**Returns:** Always `true`.

---

### `boolean addCfm()` [lines 317–394]

**Purpose:** The **Register Confirm** button handler. This is the most critical method — it initiates the ENUM switch registration process by calling the backend service and presenting a confirmation screen.

**Flow:**
1. Retrieves form beans and builds a parameter map with `SV134` as the use-case ID.
2. Creates a `ZMSV0134_ZMSV0134OPDBMapper` and calls `setENUMSWITCHCC()` with `FUNC_CD_2` (confirmation mode, not yet executable).
3. Invokes the service via `invokeService()`.
4. Checks for an error flag (`ERR_FLG`) in the output. If present:
   - Calls `setMessage(errFlg)` to set the appropriate error message.
   - Forces a redirect back to screen `ZMW01904`.
   - Returns `true` (stops further processing).
5. On success, determines the application content type (`SINSEI_NAIYO_FLG`) and sets a success message:
   - `"5"` → "Port Out" (port-out cancellation)
   - `"6"` → "Inter-company Transfer" (transfer cancellation)
   - `"7"` → "Number Cancellation" (number cancellation cancellation)
6. Retrieves the selected transfer destination carrier code name from the pull-down and stores it.
7. Displays a confirmation message ("ENUM Switch Registration") with a highlighted message area.
8. Sets the next screen to `ZMW01905` (confirmation screen).

**Returns:** Always `true`.

**Error handling:** The method delegates to `setMessage()` to map error codes to user-readable Japanese messages. Common errors include:
- `0001` – Missing application content value
- `0002` – Phone number not in judgment scope
- `1001`–`7001` – Data under contract (per content type)
- `1002`–`7002` – Token already registered (per content type)
- `1003` – Already in port-out process
- `1004`, `2002`–`7003` – Agreed processing in an invalid state

---

### `boolean fix()` [lines 402–467]

**Purpose:** The **Execute / Confirm** button handler. Finalizes the ENUM switch registration by calling the service in execute mode.

**Flow:**
1. Identical setup to `addCfm()` but uses `FUNC_CD_1` (execution mode, not confirmation mode).
2. Invokes service `ZMSV0134`.
3. On error, sets the message via `setMessage()` and forces redirect to `ZMW01905`.
4. On success, if the application content is type `"7"` (number cancellation cancellation) or type `"1"` (port-out), re-fetches ENUM switch data via `mapper.getENUMSWITCHCC()` because the aging number may have been updated (the service re-registers the aging data, changing its number).
5. Displays success message ("ENUM Switch Registration").
6. Sets the next screen to `ZMW01906` (completion screen).

**Returns:** Always `true`.

---

### `boolean shusei()` [lines 475–485]

**Purpose:** The **Correct** button handler. Returns the user to the inquiry screen (`ZMW01904`) for corrections.

**Flow:** Sets next screen to `ZMW01904`.

**Returns:** Always `true`.

---

### `boolean fin()` [lines 493–538]

**Purpose:** The **Finish** button handler. Cleans up the bean and resets the screen to its initial state.

**Flow:**
- If the application content is type `"7"` (number cancellation cancellation) or type `"1"` (port-out), copies the `BEF_DISP_KEY_AGING_NO` back to `AGING_NO` for continuity.
- If the previous screen was `ZMW02001` (phone number info update), re-sets the aging number in that screen's screen info.
- Calls `creanBean()` to clear all bean fields.
- Calls `getInfoInitData()` to re-initialize the display.
- Sets next screen to `ZMW01904`.

**Returns:** Always `true`.

---

### `void getInfoInitData(X31SDataBeanAccess bean, JCCWebBusinessLogic viewLogic, String gamenId)` [lines 546–613]

**Purpose:** Fetches all initial display data for the inquiry screen. This is the workhorse data-loading method, called from `init()`, `paging()`, `fin()`, and internally by other methods.

**Flow:**
1. Builds a parameter map with use-case ID `SV133` (ENUM switch history retrieval service).
2. Creates a `ZMSV0133_ZMSV0133OPDBMapper`.
3. Calls mapper setters for 8 data sets:
   - `ZMSV013301SC` – main data (always)
   - `ZMSV013302SC` – additional info
   - `ZMSV013303SC` – port reception history
   - `ZMSV013304SC` – ENUM switch history
   - `ZMSV013305SC` – aging key SIF (only if an aging number is provided)
   - `ZMSV013306SC` – service code list
   - `ZMSV013307SC` – service code name list
   - `ZMSV013308SC` – additional list
4. Handles pagination mapping (upmapper and downmapper).
5. Calls `initEdit()` and `initBtnEdit()` to populate display fields and button states.

---

### `void initEdit(X31SDataBeanAccess[] bean)` [lines 620–1325]

**Purpose:** The largest method in the class (~700 lines). Populates all display fields on the form based on the retrieved data. It is responsible for:

1. **Phone number display:** Formats and displays the phone number, individual/legal code, and number acquisition carrier code.
2. **Port reception information analysis:** Determines if there is valid port reception data and extracts fields (port-out number, registration timestamp, port-out status, submission availability, etc.).
3. **Validity determination:** A record is "valid" if:
   - Port-out status is `"030"` (CSV output complete) or `"050"` (work complete).
   - Submission availability (`TK_KH_04`) is `"1"` (yes).
   - Out-of-band judgment is null or `"1"`.
   - Cancel identification is `"0"` (not cancelled).
   - Cancellation identification is `"0"` (not cancelled).
4. **Application content determination:** A massive decision tree (~25+ branches) determines the `SINSEI_NAIYO_FLG` based on:
   - `CHUMN_KIND_04` (order type: 1=port transfer, 2=port return, 3=inter-company transfer, 4=number cancellation).
   - `tln_stku_tsjgs_cd` vs `jisha_jgs_cd` (acquisition carrier vs own carrier).
   - `kojin_hojin_tln_tai_cd` (individual = "1", legal = "2").
   - `kirikaeshori_flg` (switch processing: not executed = "0", executed = "1").

   The application content values include:
   - `"1"` – Port Out (N/P phone number)
   - `"2"` – Inter-company Transfer (N/P phone number)
   - `"3"` – Number Cancellation (other company receipt)
   - `"4"` – Inter-company Transfer (other company phone number)
   - `"5"` – Port Out (N/P phone number) · Cancellation
   - `"6"` – Inter-company Transfer (N/P phone number) · Cancellation
   - `"7"` – Number Cancellation (other company receipt) · Cancellation
   - `"9"` – None (not applicable)

5. **Before/after carrier display:** Populates "before switch" and "after switch" carrier code names and domains based on the determined application content and switch processing flag.

**Side effects:** Modifies all display bean fields including text fields, pull-down lists, and domain display fields.

---

### `void initBtnEdit(X31SDataBeanAccess[] bean)` [lines 1332–1358]

**Purpose:** Controls the visibility/enabled state of the Register Confirm button (`DSBL_CTL_HTML_BTN_ADD_CFM`).

**Logic:**
- In **update mode** (`UPD_INQ_MODE_FLG_UPD = "1"`): enables the button only if the application content flag is set to something other than `"9"` (None) or empty.
- In **inquiry mode** or any other case: disables the button.

---

### `void creanBean(X31SDataBeanAccess bean, JCCWebBusinessLogic viewLogic)` [lines 1366–1426]

**Purpose:** Clears all bean fields to a clean state. Called by `fin()` to reset the screen after completion.

**Flow:**
- Clears pagination info.
- Nulls out all string fields (display fields, carrier codes, domain, etc.).
- Clears pull-down selections.
- Removes all rows from list data beans (`EZM0541B010CBSMSG1LIST`, `EZM0121A010CBSMSG1LIST`, `ETU0151B040CBSMSG1LIST`, `EZM0111A010CBSMSG1LIST`).

---

### `void setMessage(String errFlg)` [lines 1437–1506]

**Purpose:** Maps error codes from the ENUM switch registration service to user-facing messages. Uses the `EKB1040_JW` message template with message value arrays defined as `private static final String[]` constants.

**Error code categories:**
| Code | Meaning |
|------|---------|
| `0001` | Missing application content value |
| `0002` | Phone number outside judgment scope |
| `1001`, `2001`, `3001`, `4001`, `7001` | Data under contract |
| `1002`, `4002`, `7002` | Token already registered |
| `1003` | Already in port-out process |
| `1004`, `2003`, `3002`, `5003`, `6003`, `7003` | Agreed processing in invalid state |
| `2002`, `5002`, `6002` | Not in port-out state |
| `5001`, `6001` | Number acquisition business provider return application in progress |

---

### `String getStringSearch(String templateId, HashMap searchResultMap, String name)` [lines 1517–1522]

**Purpose:** Utility helper. Extracts a String value from the service output map by template ID and field name.

## Relationships

### Class Diagram

```mermaid
classDiagram
    class ZMW01904SFLogic {
        +boolean init()
        +boolean back()
        +boolean paging()
        +boolean addCfm()
        +boolean fix()
        +boolean shusei()
        +boolean fin()
        -void getInfoInitData()
        -void initEdit()
        -void initBtnEdit()
        -void creanBean()
        -void setMessage()
        -private static final String[]
    }
    class JCCWebBusinessLogic {
        +getCommonInfoBean()
        +getServiceFormBean()
        +invokeService()
        +getScreenId()
    }
    class X31SDataBeanAccess {
        +sendMessageString()
        +getDataBeanArray()
    }
    class ZMSV0133_ZMSV0133OPDBMapper {
        +setZMSV013301SC()
        +getZMSV013301SC()
    }
    class ZMSV0134_ZMSV0134OPDBMapper {
        +setENUMSWITCHCC()
        +getENUMSWITCHCC()
    }
    ZMW01904SFLogic --> JCCWebBusinessLogic : extends
    ZMW01904SFLogic --> X31SDataBeanAccess : uses
    ZMW01904SFLogic --> ZMSV0133_ZMSV0133OPDBMapper : creates
    ZMW01904SFLogic --> ZMSV0134_ZMSV0134OPDBMapper : creates
```

### Usage Diagram

```mermaid
flowchart TD
    A["ZMW01904SFLogic<br>ENUM Switch Inquiry View Logic"] --> B["x31business_logic_ZMW01904SF.xml<br>Screen Configuration"]
    A --> C["WEBGAMEN_ZMW019040PJP.xml<br>Screen 01904 (Inquiry)"]
    A --> D["WEBGAMEN_ZMW019050PJP.xml<br>Screen 01905 (Confirmation)"]
    A --> E["WEBGAMEN_ZMW019060PJP.xml<br>Screen 01906 (Result)"]
    A --> F["JCCWebBusinessLogic<br>Base View Logic Class"]
    A --> G["ZMSV0133 Service<br>Data Retrieval"]
    A --> H["ZMSV0134 Service<br>ENUM Switch Registration"]
```

### Who depends on this class

| Dependent | Description |
|-----------|-------------|
| `x31business_logic_ZMW01904SF.xml` | Screen business logic wiring. Configures how `ZMW01904SFLogic` is instantiated and which methods are called for each action. |
| `WEBGAMEN_ZMW019040PJP.xml` | The inquiry screen (ZMW01904) PJP configuration. Calls `ZMW01904SFLogic` for init, paging, addCfm, fix, shusei, fin, and back events. |
| `WEBGAMEN_ZMW019050PJP.xml` | The confirmation screen (ZMW01905) PJP configuration. May reference this class for returning to or continuing from the confirmation state. |
| `WEBGAMEN_ZMW019060PJP.xml` | The completion screen (ZMW01906) PJP configuration. References this class for the fin button to return to inquiry. |

### What this class depends on

| Dependency | Role |
|------------|------|
| `JCCWebBusinessLogic` | Base class providing `invokeService()`, `getCommonInfoBean()`, `getServiceFormBean()`, screen info management, and common utilities. |
| `X31SDataBeanAccess` | Form bean for passing data between the view and logic layers. |
| `ZMSV0133_ZMSV0133OPDBMapper` | Maps data for the ENUM switch history retrieval service (SV133). Handles 8 data sets including port reception history and switch history. |
| `ZMSV0134_ZMSV0134OPDBMapper` | Maps data for the ENUM switch registration service (SV134). Uses `FUNC_CD_1` for execution and `FUNC_CD_2` for confirmation. |
| `ZMW01904SFConst` | Constants file defining field names, flags, and mode values. |
| `JZMWebCommon`, `JCCWebCommon` | Framework utility classes for screen navigation, pagination, and message setting. |

## Usage Example

The typical flow through this class follows this sequence:

```
1. User navigates to ZMW01904
   → init() is called
   → getInfoInitData() fetches ENUM switch history via ZMSV0133
   → initEdit() populates the form with phone number, port reception info, and carrier data
   → initBtnEdit() enables the Register Confirm button (in update mode)

2. User clicks "Register Confirm"
   → addCfm() is called
   → ZMSV0134 is invoked with FUNC_CD_2 (confirmation mode)
   → If validation passes, user is redirected to ZMW01905 (confirmation screen)
   → If error, message is set via setMessage() and user stays on ZMW01904

3. User clicks "Execute / Confirm" on ZMW01905
   → fix() is called
   → ZMSV0134 is invoked with FUNC_CD_1 (execution mode)
   → If port-out or number-cancellation-cancellation, aging data is re-fetched
   → User is redirected to ZMW01906 (completion screen)

4. User clicks "Correct"
   → shusei() is called, returns to ZMW01904

5. User clicks "Finish"
   → fin() is called, bean is cleared via creanBean(), screen resets
```

## Notes for Developers

**Error code patterns:** Error codes follow a consistent numbering scheme based on the `SINSEI_NAIYO_FLG`:
- Codes in the `1xxx` range relate to application content type 1 (Port Out).
- Codes in the `2xxx` range relate to type 2 (Inter-company Transfer - N/P phone number).
- Codes in the `3xxx` range relate to type 3 (Number Cancellation - other company receipt).
- Codes in the `4xxx` range relate to type 4 (Inter-company Transfer - other company phone number).
- Codes in the `5xxx` range relate to type 5 (Port Out · Cancellation).
- Codes in the `6xxx` range relate to type 6 (Inter-company Transfer · Cancellation).
- Codes in the `7xxx` range relate to type 7 (Number Cancellation · Cancellation).

The suffix digits (001, 002, 003, 004) represent error categories:
- `001` = Data under contract
- `002` = Token already registered
- `003` = Invalid agreed processing state (or not in port-out state for 2xxx/5xxx/6xxx)
- `004` = Agreed processing in non-recoverable state

**Thread safety:** The class is not thread-safe. It uses instance methods that modify the request-scoped `X31SDataBeanAccess` beans. Each HTTP request creates a new instance (per framework convention).

**`initEdit()` complexity:** This ~700-line method contains a massive nested decision tree for determining the application content flag and the before/after carrier display fields. Any changes to the ENUM switch business rules will almost certainly require modifications here. The logic branches on four key dimensions: order type (chumn_kind), carrier match, individual/legal code, and switch processing flag. Consider refactoring into smaller, testable methods.

**Bean clearing:** The `creanBean()` method explicitly nulls and removes every field and list. Ensure any new fields added to the form bean are also cleaned here, or they may retain stale data across screen transitions.

**Aging number handling:** When application content type is `"1"` (port-out) or `"7"` (number cancellation cancellation), the aging number may change during ENUM switch registration. The `fix()` and `fin()` methods contain special logic to re-read the aging number from output data or propagate it to the previous screen's context. Do not remove these branches without understanding the edge cases.

**Hardcoded screen IDs:** Screen IDs (`ZMW01904`, `ZMW01905`, `ZMW01906`, `ZMW02001`, `ZMW02201`) are referenced directly in the logic rather than through a single constants class. If these IDs change, updates would be needed in multiple places.

**No validation layer:** Field-level validation (e.g., phone number format) does not appear to be performed here — it is likely handled by the service layer (`ZMSV0134`) or by view-level JavaScript. This class relies on service-layer error codes for validation feedback.
