---
# (DD20) Business Logic — Owner.addPet() [5 LOC]

| Field | Value |
|-------|-------|
| Fully Qualified Name | `org.springframework.samples.petclinic.owner.Owner` |
| Layer | Domain Model / Entity |
| Module | `owner` (Package: `org.springframework.samples.petclinic.owner`) |

## 1. Role

### Owner.addPet()

`Owner.addPet(Pet pet)` is a domain-level association helper that attaches a `Pet` to the current `Owner` only when the `Pet` is new. In business terms, it protects the integrity of the owner-to-pet relationship by preventing existing persisted pets from being redundantly re-added through this helper. The method acts as a simple routing/guard pattern: it evaluates the lifecycle state of the incoming `Pet` and conditionally appends it to the owner's pet collection. Because it sits on the aggregate root `Owner`, this method is a shared consistency point used by multiple screens and tests that need to register pets against an owner before persistence or further editing. There are no alternate business branches beyond the new-pet check; if the pet is not new, the method intentionally performs no action.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["addPet(Pet pet)"])
    CHECK_NEW{"pet.isNew() ?"}
    ADD_PET["getPets().add(pet)"]
    NO_OP["Do nothing"]
    END_NODE(["Return / Next"])

    START --> CHECK_NEW
    CHECK_NEW -- "true" --> ADD_PET
    CHECK_NEW -- "false" --> NO_OP
    ADD_PET --> END_NODE
    NO_OP --> END_NODE
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `pet` | `Pet` | The pet domain object to be associated with the owner. In business terms, this represents a pet record being registered or prepared for ownership; only pets that are still new are added to the owner's collection. |

**Instance fields / external state read by the method:**
- The method reads the incoming `pet` object state via `pet.isNew()`.
- The method reads the owner's pet collection indirectly through `getPets()`.
- No other instance fields or external system state are accessed.

## 4. CRUD Operations / Called Services

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

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `Owner.getPets` | Owner | - | Calls `getPets` in `Owner` |

Analyze all method calls within this method and classify each as a CRUD operation.
Use the pre-computed evidence above. If SC Code or Entity/DB is missing, try to infer from:
- The **SC Code** (Service Component code, e.g., `EKK0361A010SC`, `EKK1081D010CBS`) — look at the class name of the called method or its containing class.
- The **Entity/DB tables** — search for table name constants (often `KK_T_*` pattern), SQL references, or entity names in the called method's source code.

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `Owner.getPets` | Owner | Owner.pets collection | Reads the owner's pet collection in order to append the incoming pet when the business rule is satisfied. |
| C | `List.add` (via `getPets().add`) | - | Owner.pets collection | Adds the new pet to the in-memory owner aggregate collection. No database write occurs inside this method. |

## 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:PetController | `PetController` -> `Owner.addPet` | `Owner.getPets [R] Owner.pets collection` |
| 2 | Screen:OwnerControllerTests | `OwnerControllerTests` -> `Owner.addPet` | `Owner.getPets [R] Owner.pets collection` |
| 3 | Screen:PetControllerTests | `PetControllerTests` -> `Owner.addPet` | `Owner.getPets [R] Owner.pets collection` |
| 4 | Screen:VisitControllerTests | `VisitControllerTests` -> `Owner.addPet` | `Owner.getPets [R] Owner.pets collection` |
| 5 | Screen:ClinicServiceTests | `ClinicServiceTests` -> `Owner.addPet` | `Owner.getPets [R] Owner.pets collection` |

## 6. Per-Branch Detail Blocks

**Block 1** — [IF] `(pet.isNew())` (L97)

> Business guard that allows only newly created pets to be attached to the owner.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `pet.isNew();` // checks whether the pet has not been persisted yet |

**Block 1.1** — [THEN] `(pet is new)` (L98)

| # | Type | Code |
|---|------|------|
| 1 | CALL | `getPets().add(pet);` // appends the new pet to the owner's pet list |

**Block 1.1.1** — [EXEC] `(collection append)` (L98)

| # | Type | Code |
|---|------|------|
| 1 | CALL | `getPets();` // obtains the owner's current pet collection |
| 2 | EXEC | `.add(pet);` // mutates the collection by inserting the new pet |

**Block 1.2** — [ELSE] `(pet is not new)` (L97-100)

> No action is taken when the pet already exists or is not eligible for addition through this helper.

| # | Type | Code |
|---|------|------|
| 1 | RETURN | `return;` // implicit fall-through with no state change |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `Owner` | Domain entity | The customer or person who owns one or more pets in the PetClinic business model. |
| `Pet` | Domain entity | An animal record associated with an owner and managed as part of the clinic's pet care workflow. |
| `isNew()` | Domain behavior | Lifecycle check that indicates whether the pet has not yet been persisted. |
| `getPets()` | Collection accessor | Returns the owner's in-memory pet list used to manage owner-to-pet associations. |
| `addPet` | Domain method | Helper that conditionally registers a new pet under the owner. |
| `PetController` | Web controller | MVC entry point that prepares owner/pet state for add/edit operations. |
| `OwnerControllerTests` | Test class | Unit/integration test coverage for owner-related behavior, including pet association. |
| `PetControllerTests` | Test class | Test coverage for pet management flows that invoke the domain association helper. |
| `VisitControllerTests` | Test class | Test coverage for visit flows that rely on owner-pet setup. |
| `ClinicServiceTests` | Test class | Service-level tests that construct owner and pet aggregates for clinic scenarios. |
| `Owner.pets collection` | Domain collection | The set of pets currently associated with the owner aggregate. |
| CRUD | Technical acronym | Create, Read, Update, Delete — standard classification of data operations. |
| Domain model | Architecture term | Business object layer that encapsulates core rules and relationships without direct UI or persistence concerns. |
| Aggregate root | Domain term | The primary consistency boundary object that controls related entities such as pets. |

