# (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 is the read-only entry point used to display a single owner’s detail page in the PetClinic owner module. It takes the owner identifier from the URL path, resolves the corresponding owner record from the repository, and prepares a view model for the `owners/ownerDetails` screen. In business terms, it supports the “view owner profile” use case, where a user can inspect the owner’s personal information and related pet data in one consolidated screen.

The method implements a simple routing-and-delegation pattern typical of Spring MVC controllers: the web request is mapped to a controller action, the controller delegates the data retrieval to the repository, and the resulting domain object is exposed to the view layer. There is one functional branch: if the owner exists, the method returns the populated detail view; if the owner does not exist, it throws an `IllegalArgumentException` with a clear not-found message. This makes the method a boundary handler for the owner detail screen rather than a reusable business service.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["showOwner(ownerId)"])
    STEP1(["Create ModelAndView for owners/ownerDetails"])
    STEP2(["Call owners.findById(ownerId)"])
    DECISION{"Owner present?"}
    STEP3(["Resolve Owner from Optional"])
    STEP4(["Add owner to model"])
    STEP5(["Return ModelAndView"])
    STEP6(["Throw IllegalArgumentException - Owner not found with id"])
    END_NODE(["Return / Next"])

    START --> STEP1
    STEP1 --> STEP2
    STEP2 --> DECISION
    DECISION -->|Yes| STEP3
    STEP3 --> STEP4
    STEP4 --> STEP5
    STEP5 --> END_NODE
    DECISION -->|No| STEP6
    STEP6 --> END_NODE
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `ownerId` | `@PathVariable("ownerId") int` | The unique owner identifier extracted from the URL, used to locate one specific owner record for the detail screen. It can be any integer supplied by the request path; the method uses it as the lookup key in `OwnerRepository.findById`. If the ID does not match an existing owner, the method fails fast with an exception instead of rendering the screen. |

**Instance fields / external state read by the method:**
- `this.owners` — repository dependency used to read the owner record.
- View name constant embedded in code: `owners/ownerDetails`.
- Error message template embedded in code for the not-found branch.

## 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.

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `OwnerRepository.findById` | OwnerRepository | Owner | Reads a single owner aggregate by primary key from the owner repository. |
| R | `Optional.orElseThrow` | Java Optional | N/A | Converts the repository result into either a resolved owner or an exception when the owner is absent. |
| C | `ModelAndView.addObject` | ModelAndView | N/A | Adds the owner domain object to the response model for rendering in the detail view. |

## 5. Dependency Trace

Trace who calls this method and what this method ultimately calls.

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Test: `OwnerControllerTests` | `MockMvc.perform(get("/owners/{ownerId}", TEST_OWNER_ID))` -> `OwnerController.showOwner` | `OwnerRepository.findById [R] Owner` |

## 6. Per-Branch Detail Blocks

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

> Entry point for the owner detail screen request.

| # | Type | Code |
|---|------|------|
| 1 | SET | `ModelAndView mav = new ModelAndView("owners/ownerDetails");` |
| 2 | CALL | `this.owners.findById(ownerId);` |

**Block 2** — [IF] `(optionalOwner is present)` (L169-L171)

> Successful read path that prepares the view model with the located owner.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `optionalOwner.orElseThrow(...);` |
| 2 | SET | `Owner owner = ...;` |
| 3 | CALL | `mav.addObject(owner);` |
| 4 | RETURN | `return mav;` |

**Block 3** — [ELSE] `(optionalOwner is empty)` (L169-L171)

> Failure path used when the requested owner ID does not exist.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `optionalOwner.orElseThrow(() -> new IllegalArgumentException("Owner not found with id: " + ownerId + ". Please ensure the ID is correct "));` |
| 2 | RETURN | Exception thrown, no normal return path. |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `ownerId` | Field | Unique identifier of the owner record used for detail-page lookup. |
| `Owner` | Domain object | Pet owner entity containing contact information and related pets. |
| `owners/ownerDetails` | View name | Owner detail screen that renders the selected owner and related data. |
| `ModelAndView` | Technical term | Spring MVC response wrapper containing the view name and model attributes. |
| `Optional` | Technical term | Container used to represent a possibly missing owner record. |
| Repository | Technical term | Data access abstraction used to read owner records from persistence. |
| `findById` | CRUD operation | Primary-key lookup used to fetch one owner by identifier. |
| `IllegalArgumentException` | Technical term | Runtime exception used here to reject requests for nonexistent owner IDs. |
| PetClinic | Business term | Sample veterinary clinic domain used by the application. |
