---
# (DD26) Business Logic — OwnerController.findOwner() [7 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.findOwner()

This method acts as a model pre-loader for owner-centric web requests. Before a controller handler renders a view or continues request processing, it supplies the `owner` model attribute either as a brand-new domain object for create flows or as an existing persisted owner for edit/detail flows. In business terms, it supports two service categories: owner creation initialization and owner retrieval by identifier. The method implements a simple routing/delegation pattern based on whether the path variable is present, making it a shared entry point for screens that require an `Owner` object in the model. When `ownerId` is absent, it prepares a blank owner instance for data entry; when `ownerId` is present, it loads the matching owner from the repository and fails fast if the record does not exist. The method therefore protects downstream screens from having to duplicate owner lookup logic and centralizes the not-found behavior in one place.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["findOwner(ownerId)"])
    DECISION{"ownerId == null?"}
    NEW_OWNER(["Create new Owner"])
    FIND_BY_ID(["owners.findById(ownerId)"])
    OR_ELSE_THROW(["orElseThrow IllegalArgumentException"])
    RETURN_NEW(["Return new Owner"])
    RETURN_EXISTING(["Return Owner from repository"])
    ERROR(["Throw IllegalArgumentException - Owner not found"])

    START --> DECISION
    DECISION -->|Yes| NEW_OWNER
    NEW_OWNER --> RETURN_NEW
    DECISION -->|No| FIND_BY_ID
    FIND_BY_ID --> OR_ELSE_THROW
    OR_ELSE_THROW -->|Found| RETURN_EXISTING
    OR_ELSE_THROW -->|Missing| ERROR
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `ownerId` | `@PathVariable(name = "ownerId", required = false) Integer` | Owner identifier embedded in the request path. When omitted, the method initializes a new owner for registration flows; when provided, it is used to retrieve an existing owner record for edit or display flows. Only `null` or a positive integer-like database key is meaningful for this method, and a missing match results in a business error. |

**Instance fields / external state read by this method:** `this.owners` repository bean is consulted to load persisted owner data. The method also relies on the `ownerId` path variable binding supplied by the Spring MVC runtime.

## 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 an owner record by primary key when `ownerId` is present |
| - | `new Owner()` | N/A | Owner | Creates a transient owner object for the create flow; no persistence operation occurs here |
| - | `orElseThrow(...)` | N/A | N/A | Converts a missing repository result into an exception to stop request processing |

## 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 | Screen/Controller: `PetController` | `PetController.findOwner` -> `OwnerController.findOwner` | `OwnerRepository.findById [R] Owner` |
| 2 | Controller: `OwnerController` | `OwnerController.findOwner` -> `OwnerController.findOwner` | `OwnerRepository.findById [R] Owner` |

## 6. Per-Branch Detail Blocks

**Block 1** — IF `(ownerId == null)` (L65)

> Initializes a blank owner model for create flows when the request does not include an owner identifier.

| # | Type | Code |
|---|------|------|
| 1 | RETURN | `return new Owner();` |

**Block 2** — ELSE `(ownerId != null)` (L66)

> Loads an existing owner from the repository for edit or display flows.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `this.owners.findById(ownerId)` |
| 2 | CALL | `.orElseThrow(() -> new IllegalArgumentException("Owner not found with id: " + ownerId + ". Please ensure the ID is correct and the owner exists in the database."))` |
| 3 | RETURN | `return ...` |

**Block 2.1** — IF repository result is present (implicit Optional success path) (L66)

| # | Type | Code |
|---|------|------|
| 1 | RETURN | `return existingOwner;` |

**Block 2.2** — ELSE repository result is empty (implicit Optional failure path) (L66)

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `throw new IllegalArgumentException("Owner not found with id: " + ownerId + ". Please ensure the ID is correct and the owner exists in the database.");` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `ownerId` | Field | Unique identifier of an owner record used in the request path |
| `Owner` | Domain entity | Pet owner master record in the Petclinic domain |
| `ModelAttribute` | Technical annotation | Spring MVC annotation that places the returned object into the model before the handler runs |
| `PathVariable` | Technical annotation | Spring MVC annotation that binds a URI path segment to a method parameter |
| `required = false` | Technical flag | Allows the path variable to be absent, enabling create flows without an existing owner identifier |
| `owners` | Repository bean | Data access component used to read owner records from persistence |
| `orElseThrow` | Java Optional API | Fails fast when the requested owner is not present in storage |
| `IllegalArgumentException` | Technical exception | Signals that the requested owner cannot be resolved from the supplied identifier |
| create flow | Business term | User journey where a new owner is being initialized for entry |
| edit/display flow | Business term | User journey where an existing owner is being loaded for viewing or updating |
| Petclinic | Application domain | Sample veterinary clinic management application |
