# (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 Pet creation screen for an existing owner in the PetClinic owner-management flow. Its business purpose is to initialize a brand-new pet instance, attach that pet to the current owner aggregate, and then route the user to the pet maintenance form so the new pet can be completed and submitted. In practical terms, it opens the entry point for adding a pet under a specific owner rather than persisting anything immediately. The method uses a simple routing/delegation pattern: it creates a domain object, associates it to the owner through the aggregate root, and returns the logical view name for the shared create-or-update page. There are no conditional branches in this method, so the behavior is deterministic and always follows the same create-screen initialization path. The method also relies on Spring MVC model binding to supply the current `Owner` and to expose the initialized `pet` model attribute to the view layer.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["initCreationForm(owner, model)"])
    STEP1["Create new Pet domain instance"]
    STEP2["Attach new pet to Owner aggregate via owner.addPet(pet)"]
    STEP3["Return view name VIEWS_PETS_CREATE_OR_UPDATE_FORM = \"pets/createOrUpdatePetForm\""]
    END_NODE(["Render pet create/update form"])

    START --> STEP1
    STEP1 --> STEP2
    STEP2 --> STEP3
    STEP3 --> END_NODE
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `owner` | `Owner` | The currently selected pet owner aggregate root. It represents the customer record under which the new pet will be registered, and the method uses it to attach the newly created pet before the form is shown. |
| 2 | `model` | `ModelMap` | The MVC model container for view rendering. In this method signature it is available for screen composition, but the implementation does not add any explicit attributes because the owner-bound pet is already prepared through the domain model association. |

Instance fields and external state read by the method:
- `VIEWS_PETS_CREATE_OR_UPDATE_FORM` — constant logical view name used as the navigation target.
- Spring MVC binding context — the `Owner` argument is resolved by the framework before method execution.

## 4. CRUD Operations / Called Services

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| C | `new Pet()` | N/A | `Pet` entity | Creates a new in-memory pet domain object to start the registration flow. |
| C | `owner.addPet(pet)` | N/A | `Owner` / `Pet` aggregate | Adds the new pet to the owner’s collection so the form can bind the new child record under the existing owner. |

## 5. Dependency Trace

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

## 6. Per-Branch Detail Blocks

**Block 1** — [SEQUENCE] `(Pet creation form initialization)` (L100-L102)

> Initializes a blank pet object and links it to the current owner before navigating to the form.

| # | Type | Code |
|---|------|------|
| 1 | SET | `Pet pet = new Pet();` // Create a new unsaved pet domain object |
| 2 | CALL | `owner.addPet(pet);` // Attach the new pet to the owner aggregate |

**Block 2** — [RETURN] `(navigate to create/update form)` (L103)

| # | Type | Code |
|---|------|------|
| 1 | RETURN | `return VIEWS_PETS_CREATE_OR_UPDATE_FORM;` // Return the logical view name `pets/createOrUpdatePetForm` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `Owner` | Domain entity | Customer or household record that owns one or more pets in the PetClinic application. |
| `Pet` | Domain entity | Animal record associated with an owner and later enriched with details such as name, type, and birth date. |
| `ModelMap` | Framework object | Spring MVC view model container used to expose data to the template engine. |
| `VIEWS_PETS_CREATE_OR_UPDATE_FORM` | View constant | Logical view name `pets/createOrUpdatePetForm`, used for both pet creation and pet editing screens. |
| `aggregate root` | Domain pattern | The parent business object that controls lifecycle and consistency of related child entities. |
| `create/update form` | UI flow | Shared screen used to collect pet details for both initial registration and later edits. |
| `initCreationForm` | Method name | Controller entry point that prepares the pet registration screen. |
| `owner.addPet(pet)` | Domain operation | Adds a pet to the owner’s collection so the new record is linked before user input is submitted. |
