---
# (DD27) Business Logic — OwnerController.findPaginatedForOwnersLastName() [5 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.findPaginatedForOwnersLastName()

This method implements the pagination-and-search slice for the owner maintenance flow in the PetClinic application. Its business purpose is to retrieve a page of owners whose last name begins with the user-supplied search text, so the UI can display a manageable, ordered subset instead of the full owner list. It acts as a thin controller-level orchestration method: it does not perform business rule evaluation, persistence logic, or transformation beyond preparing pagination metadata and delegating the actual query to the repository layer.

The method follows a delegation pattern and a simple pagination adapter pattern. It converts the one-based page number used by the web layer into the zero-based page index required by Spring Data, fixes the page size at five records, and then dispatches the request to `OwnerRepository.findByLastNameStartingWith(...)`. In the larger system, this method is part of the Owner search experience and serves as the entry point that translates screen input into a pageable query result for the list view.

There are no conditional branches inside the method itself. All variability is driven by the caller-provided `page` and `lastname` inputs and by the repository implementation that resolves the actual matching owners.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
START["findPaginatedForOwnersLastName(page, lastname)"]
STEP1["Set pageSize = 5"]
STEP2["Create Pageable with PageRequest.of(page - 1, pageSize)"]
STEP3["Call owners.findByLastNameStartingWith(lastname, pageable)"]
END_NODE["Return Page<Owner>"]
START --> STEP1
STEP1 --> STEP2
STEP2 --> STEP3
STEP3 --> END_NODE
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `page` | `int` | Requested page number from the owner list screen. It represents which slice of the matching owner set should be displayed to the user. The value is expected to be one-based in the web layer and is converted to a zero-based index for Spring Data pagination; invalid values can produce incorrect paging behavior. |
| 2 | `lastname` | `String` | Last-name search prefix entered by the user when filtering owners. It narrows the owner list to records whose last name starts with the supplied text, enabling incremental lookup in the UI. Empty or partial values broaden the result set, while a more specific prefix reduces it. |

Instance fields and external state read by the method:
- `owners`: the injected `OwnerRepository` used to perform the paginated lookup.
- `PageRequest.of(...)`: Spring Data pagination factory used to convert the page number and fixed size into a `Pageable` request.

## 4. CRUD Operations / Called Services

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `findByLastNameStartingWith` | N/A | `Owner` / owner repository query | Reads a page of owner records whose last name starts with the provided prefix, returning only the requested slice for the list screen. |

The method performs one business-level read operation through the repository. `PageRequest.of(...)` is a framework-level pagination setup call and is not treated as a CRUD operation.

## 5. Dependency Trace

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

| # | Caller (Screen/Batch) | Call Chain (Full Path to Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|----------------------------------|-------------------------------|
| 1 | Controller:OwnerController | `OwnerController.processFindForm` -> `OwnerController.findPaginatedForOwnersLastName` | `findByLastNameStartingWith [R] Owner` |

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 | Controller:OwnerController | `OwnerController.processFindForm` -> `OwnerController.findPaginatedForOwnersLastName` | `findByLastNameStartingWith [R] Owner` |

## 6. Per-Branch Detail Blocks

There are no conditional branches, loops, or switch statements in this method. The execution is a straight-line delegation sequence.

**Block 1** — [SEQUENCE] (L130-L134)

> Builds a fixed-size pagination request and delegates to the owner repository.

| # | Type | Code |
|---|------|------|
| 1 | SET | `int pageSize = 5;` // fixed page size for owner search results |
| 2 | SET | `Pageable pageable = PageRequest.of(page - 1, pageSize);` // convert one-based UI page to zero-based repository page |
| 3 | CALL | `owners.findByLastNameStartingWith(lastname, pageable);` // retrieve matching owners by last-name prefix |
| 4 | RETURN | `return owners.findByLastNameStartingWith(lastname, pageable);` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|---------------------|
| `lastname` | Field | Owner last-name search prefix entered from the UI. |
| `page` | Field | Requested page number for the paginated owner list. |
| `Page` | Technical type | Spring Data container for one page of results plus pagination metadata. |
| `Pageable` | Technical type | Spring Data request object describing page index and page size. |
| `PageRequest` | Technical type | Factory for creating a `Pageable` instance. |
| `Owner` | Domain entity | PetClinic owner record containing customer and pet ownership data. |
| `OwnerRepository` | Repository | Persistence interface used to query owner records from storage. |
| `findByLastNameStartingWith` | Repository query method | Searches for owners whose last name begins with a supplied string. |
| one-based page | Business term | Page numbering used by the web screen, where the first page is numbered 1. |
| zero-based page index | Technical term | Page numbering required by Spring Data pagination, where the first page is numbered 0. |
| last-name prefix | Business term | Partial surname text used to filter owner search results. |
| owner list screen | Business term | UI page that displays and searches owner records. |
| PetClinic | Business term | Sample clinic management application domain used by the project. |
