---

# (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 performs the controller-side pagination step for the owner search experience. It accepts the requested page number and the last-name search prefix, converts the page number into the zero-based index required by Spring Data, and delegates the actual lookup to the owner repository. In business terms, it supports the “find owners by last name” flow by returning a single page of matching owner records rather than the full result set.

The method is intentionally narrow and acts as a routing/delegation point rather than a transformation engine. Its only business decision is the fixed page size of five records per page, which standardizes how many owners are shown in each search result page. The method does not branch, loop, or perform any in-memory filtering; it delegates all matching logic to `owners.findByLastNameStartingWith(...)`, which is the repository-backed read operation for the owner domain.

In the larger system, this method is a private helper used by the owner controller’s search form handler. It encapsulates pagination mechanics so that the controller action can stay focused on web flow orchestration and view rendering. The pattern used here is simple controller delegation with repository-driven querying, and it is part of the read path of the owner CRUD lifecycle.

## 2. Processing Pattern (Detailed Business Logic)

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

The method follows a linear read pattern with no conditional branches. It first establishes the business paging policy, then translates the requested page number into a Spring Data `Pageable`, and finally retrieves the matching owner page from the repository.

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `page` | `int` | Requested page number from the owner search result flow. The value is expected to be one-based from the user interface, and it is converted to a zero-based page index before querying the repository. |
| 2 | `lastname` | `String` | Owner last-name search prefix used to narrow the result set. The repository returns owners whose last names start with this value, so it controls which owners appear in the search results. |

External state read by the method:
- `owners` repository dependency injected into `OwnerController`
- fixed page size constant `5`

## 4. CRUD Operations / Called Services

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `findByLastNameStartingWith` | Spring Data repository method | `Owner` | Reads a paginated set of owners whose last names begin with the supplied search prefix. |

The method performs one repository read and no create, update, or delete activity. `PageRequest.of(...)` is a framework pagination utility and not a persistence 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` |

This helper is only invoked from the owner search form flow inside the same controller. The method’s dependency chain ends at the Spring Data repository read that returns the paginated owner result set.

## 6. Per-Branch Detail Blocks

**Block 1** — [SEQUENCE] `(linear read flow)` (L130)

> This helper executes one straight-through pagination and repository lookup sequence.

| # | Type | Code |
|---|------|------|
| 1 | SET | `int pageSize = 5;` // fixed page size for owner search results |
| 2 | SET | `Pageable pageable = PageRequest.of(page - 1, pageSize);` // convert UI page number to zero-based Spring Data page index |
| 3 | CALL | `return owners.findByLastNameStartingWith(lastname, pageable);` // fetch matching owners as a paged result |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `page` | Field | Requested result page number in the owner search flow; translated into a zero-based database query page index. |
| `lastname` | Field | Search prefix for owner last name; used to match owners whose last names begin with the supplied value. |
| `Page` | Technical term | Spring Data pagination container holding one page of results plus paging metadata. |
| `Pageable` | Technical term | Spring Data request object that carries page index and page size into the repository query. |
| `PageRequest` | Technical term | Spring Data factory used to build a `Pageable` instance. |
| `Owner` | Domain entity | PetClinic owner record representing a customer who owns one or more pets. |
| `owners` | Repository | Data access component for querying and persisting `Owner` entities. |
| `findByLastNameStartingWith` | Repository method | Derived query that returns owners whose last names begin with the provided prefix. |
| CRUD | Acronym | Create, Read, Update, Delete — standard data operation categories. |
| Controller | Layer | Web layer component that handles HTTP requests and coordinates domain read/write operations. |
| Spring Data | Technical term | Framework that generates repository queries from method names and provides pagination support. |
