---
# (DD24) Business Logic — OwnerController.showOwner() [9 LOC]

| Field | Value |
|-------|-------|
| Fully Qualified Name | `org.springframework.samples.petclinic.owner.OwnerController` |
| Layer | Controller |
| Module | `owner` (Package: `org.springframework.samples.petclinic.owner`) |

## 1. Role

### OwnerController.showOwner()

This method serves as the read-only entry point for the owner detail screen. Given an owner identifier from the request path, it retrieves the corresponding `Owner` aggregate and prepares a `ModelAndView` for the `owners/ownerDetails` view. The method implements a simple routing-and-delegation pattern: it routes the request to the correct UI template, delegates persistence access to `OwnerRepository.findById`, and then assembles the model payload for rendering. Business-wise, it supports the customer service use case of opening a specific pet owner record and viewing the owner profile together with the associated pets and visits. If the requested owner does not exist, it fails fast with an `IllegalArgumentException` to prevent rendering an invalid detail page. There are only two outcomes: successful presentation of the owner detail screen or an exception path for an unknown owner ID.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
START(["showOwner(ownerId)"])
M1["Create ModelAndView with view owners/ownerDetails"]
M2["Call owners.findById(ownerId)"]
D1{"Owner present?"}
M3["Resolve owner from Optional"]
M4["Throw IllegalArgumentException with ownerId"]
M5["Add owner to ModelAndView"]
END_NODE(["Return ModelAndView"])
START --> M1
M1 --> M2
M2 --> D1
D1 -->|Yes| M3
D1 -->|No| M4
M3 --> M5
M5 --> END_NODE
M4 --> END_NODE
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `ownerId` | `@PathVariable("ownerId") int` | Unique owner identifier supplied by the URL `/owners/{ownerId}`. It determines which owner record is loaded for display and directly controls the repository lookup and the exception path when no matching record exists. |

Instance state and external state read by the method:
- `this.owners` repository instance from the controller field injection
- Request path variable `ownerId` from the HTTP request

## 4. CRUD Operations / Called Services

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

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `OwnerRepository.findById` | OwnerRepository | Owner | Calls `findById` in `OwnerRepository` |

Analyze all method calls within this method and classify each as a CRUD operation.
Use the pre-computed evidence above. If SC Code or Entity/DB is missing, try to infer from:
- The **SC Code** (Service Component code, e.g., `EKK0361A010SC`, `EKK1081D010CBS`) — look at the class name of the called method or its containing class.
- The **Entity/DB tables** — search for table name constants (often `KK_T_*` pattern), SQL references, or entity names in the called method's source code.

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `OwnerRepository.findById` | OwnerRepository | Owner | Reads one owner aggregate by primary key for the detail screen |
| R | `Optional.orElseThrow` | N/A | N/A | Converts the repository result into a mandatory business object and raises an error if the owner is missing |
| C | `new ModelAndView("owners/ownerDetails")` | N/A | View: `owners/ownerDetails` | Creates the MVC response container for the owner detail page |
| C | `mav.addObject(owner)` | N/A | Model attribute `owner` | Places the retrieved owner into the model for view rendering |
| U | `return mav` | N/A | N/A | Completes the request handling by returning the populated model and view |

## 5. Dependency Trace

Trace who calls this method and what this method ultimately calls.
Use the pre-computed evidence and caller search results from Step 2 above.

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Screen:OwnerControllerTests | `OwnerControllerTests.showOwner` -> `OwnerController.showOwner` | `OwnerRepository.findById [R] Owner` |

## 6. Per-Branch Detail Blocks

**Block 1** — [SEQUENTIAL] `(showOwner(ownerId))` (L167)

> Entry point for owner detail retrieval and page preparation.

| # | Type | Code |
|---|------|------|
| 1 | SET | `ModelAndView mav = new ModelAndView("owners/ownerDetails");` // create the detail view container |
| 2 | CALL | `this.owners.findById(ownerId);` // load the owner record from persistence |

**Block 2** — [IF] `(optionalOwner.isPresent())` (L169)

> Successful lookup path when the owner exists in the repository.

| # | Type | Code |
|---|------|------|
| 1 | SET | `Owner owner = optionalOwner.orElseThrow(...);` // resolve the owner from the Optional |
| 2 | EXEC | `optionalOwner.orElseThrow(() -> new IllegalArgumentException("Owner not found with id: " + ownerId + ". Please ensure the ID is correct "));` // throw when no owner is present |
| 3 | SET | `mav.addObject(owner);` // add the owner aggregate to the model |
| 4 | RETURN | `return mav;` // return the populated ModelAndView |

**Block 3** — [ELSE] `(optionalOwner.isEmpty())` (L169)

> Error path used when the requested owner ID does not match any persisted owner.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `optionalOwner.orElseThrow(() -> new IllegalArgumentException("Owner not found with id: " + ownerId + ". Please ensure the ID is correct "));` // raises an IllegalArgumentException |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|-------------------|
| `ownerId` | Field | Primary key of the owner being requested for display |
| `Owner` | Domain entity | Pet clinic owner record containing identity and contact details |
| `owners/ownerDetails` | View | MVC template used to display an owner profile and related pets |
| `ModelAndView` | Technical term | Spring MVC response object that carries both the view name and model data |
| `Optional` | Technical term | Container used to represent the possible absence of an owner record |
| `IllegalArgumentException` | Technical term | Runtime exception used to signal an invalid or missing owner ID |
| Repository | Technical term | Persistence access component used to retrieve domain objects from storage |
| CRUD | Acronym | Create, Read, Update, Delete — basic data access operation categories |
| MVC | Acronym | Model-View-Controller — web application design pattern |
| Pet clinic | Business term | Domain of the sample application, centered on managing owners, pets, and visits |
| Aggregate | Business term | A grouped domain object loaded and displayed as a single business unit |
