# (DD43) Business Logic — PetController.initCreationForm() [6 LOC]

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

## 1. Role

### PetController.initCreationForm()

This method prepares the initial user interface state for creating a new pet under an existing owner record. In business terms, it is the entry point for the “add pet” workflow and ensures the owner’s aggregate is immediately extended with a newly instantiated pet object before the form is rendered. The method does not persist data or perform validation; instead, it establishes a fresh in-memory pet model that the web layer can bind to when the user fills out the creation screen.

The method follows a simple routing and initialization pattern: it creates a new `Pet`, attaches it to the current `Owner`, and returns the view name for the shared pet create-or-update form. This shared form reuse indicates that the method supports a common presentation template used by both creation and update scenarios, with the actual operation context determined elsewhere in the controller flow. There are no conditional branches in this method, so the business behavior is deterministic and single-path.

At the system level, this method acts as a controller-side orchestration step between the owner context resolved by the request mapping and the HTML form that the user sees. Its role is to prepare the domain model in a state suitable for binding, while delegating persistence to the later POST handler. Because it returns a view name rather than domain data, it belongs to the web presentation layer and serves as a lightweight entry point for initiating pet registration.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["initCreationForm(owner, model)"])
    CREATE_PET["Create new Pet instance"]
    ADD_PET["owner.addPet(pet)"]
    RETURN_VIEW["Return pets/createOrUpdatePetForm"]
    START --> CREATE_PET
    CREATE_PET --> ADD_PET
    ADD_PET --> RETURN_VIEW
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `owner` | `Owner` | The currently selected owner whose household is being expanded with a new pet registration. It represents the parent business aggregate for the pet creation flow and receives the newly created `Pet` instance so the form can bind the child record to the correct owner context. |
| 2 | `model` | `ModelMap` | The web presentation model for the current request. In this method it is not directly modified, but it is part of the MVC signature and indicates that the controller is preparing state for a server-rendered form view. |

Instance fields and external state read by the method: none. The method does not consult controller fields, repositories, or request-scoped data beyond the injected `owner` argument.

## 4. CRUD Operations / Called Services

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|-----------------------|
| C | `owner.addPet(pet)` | N/A | `Owner` / `Pet` domain aggregate | Creates a new pet object in memory and associates it with the selected owner so the create form can bind against a fresh child record. |

The method contains no repository calls, no database access, and no external service invocations. Its only business action is an in-memory create/associate operation on the owner aggregate.

## 5. Dependency Trace

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Screen: unknown MVC GET request for `/owners/{ownerId}/pets/new` | HTTP GET `/owners/{ownerId}/pets/new` -> `PetController.initCreationForm` | `owner.addPet(pet) [C] Owner/Pet` |
| 2 | Controller: `PetControllerTests` | `PetControllerTests.initCreationForm()` -> `PetController.initCreationForm` | `owner.addPet(pet) [C] Owner/Pet` |

This method is invoked as the GET handler for the pet creation screen under an existing owner context. The source search also shows a corresponding test method, which verifies the controller entry point at the unit level.

## 6. Per-Branch Detail Blocks

**Block 1** — [SEQUENCE] `(straight-through execution)` (L100-L103)

> Initializes a blank pet and binds it to the owner before returning the shared form view.

| # | Type | Code |
|---|------|------|
| 1 | SET | `Pet pet = new Pet();` |
| 2 | CALL | `owner.addPet(pet);` |
| 3 | RETURN | `return VIEWS_PETS_CREATE_OR_UPDATE_FORM;` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `owner` | Field/Domain object | Pet owner record — the parent household or customer account that owns one or more pets. |
| `pet` | Domain object | Pet registration record — the animal being created or updated in the owner’s profile. |
| `ModelMap` | Technical term | Spring MVC presentation model container used to pass server-side data to the rendered view. |
| MVC | Acronym | Model-View-Controller — web application pattern separating request handling, presentation, and domain data. |
| Controller | Layer | Web entry layer that receives HTTP requests and prepares data for views or downstream business logic. |
| `createOrUpdatePetForm` | View name | Shared pet form screen used for both creating a new pet and editing an existing pet. |
| GET | HTTP method | Read-oriented request used here to display the pet creation form without saving data. |
| Owner aggregate | Business term | The owner and all related pets treated as one related business object in memory. |
