---
# (DD16) Business Logic — Owner.addVisit() [11 LOC]

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

## 1. Role

### Owner.addVisit()

`addVisit` is a domain-level helper that attaches a new `Visit` record to one of the owner’s pets by pet identifier. Its business purpose is to preserve the Petclinic aggregate relationship: an owner can have multiple pets, and each pet can accumulate a history of veterinary visits. The method does not create or persist a visit on its own; instead, it delegates to the selected `Pet` so that the visit is stored on the correct child entity in the ownership hierarchy.

This method is a routing/delegation operation rather than a decision-heavy workflow. It performs a targeted lookup of the pet inside the owner aggregate, then forwards the visit to that pet. In the larger system, it acts as a convenience entry point for screens or services that already know the pet identifier but do not want to manipulate the pet collection directly. The logic assumes a single matching pet and relies on the underlying `Pet` behavior to append the visit to that pet’s visit history.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["addVisit(petId, visit)"])
    GET_PET["Call Owner.getPet(petId)"]
    IS_PET_FOUND{"pet is found?"}
    ADD_VISIT["Call Pet.addVisit(visit)"]
    END_NODE(["Return / Next"])

    START --> GET_PET
    GET_PET --> IS_PET_FOUND
    IS_PET_FOUND -- "yes" --> ADD_VISIT
    ADD_VISIT --> END_NODE
    IS_PET_FOUND -- "no" --> END_NODE
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `petId` | `Integer` | Identifier of the pet that should receive the new visit entry within the owner’s pet collection. |
| 2 | `visit` | `Visit` | Visit record to attach to the selected pet, representing the medical appointment details being recorded. |

External state read by the method:
- The owner’s in-memory pet collection through `getPet(petId)`.
- The selected `Pet` aggregate, which receives the visit.

## 4. CRUD Operations / Called Services

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

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `Owner.getPet` | Owner | `Pet` | Locates the pet inside the owner aggregate by identifier. |

Analyze all method calls within this method and classify each as a CRUD operation.

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `getPet` | Owner | `Pet` | Reads the owner’s pet collection to find the target pet. |
| C | `addVisit` | Pet | `Visit` | Adds the visit to the selected pet’s visit history. |

## 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: `OwnerController` | `OwnerController.processNewVisitForm` -> `Owner.addVisit` | `getPet [R] Pet` -> `addVisit [C] Visit` |

## 6. Per-Branch Detail Blocks

**Block 1** — [EXEC] `(method entry and pet lookup)` (L164-L166)

> Locates the target pet inside the owner aggregate.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `Pet pet = getPet(petId);` // resolves the target pet from the owner |

**Block 2** — [EXEC] `(delegate visit registration to pet)` (L167-L173)

> Forwards the visit to the selected pet so the pet owns its own visit history.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `pet.addVisit(visit);` // appends the visit to the pet’s visit list |
| 2 | RETURN | `return;` // method completes with no explicit result |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `Owner` | Domain entity | Customer/animal owner in Petclinic; aggregate root that groups owned pets. |
| `Pet` | Domain entity | Animal belonging to an owner and carrying its own medical history. |
| `Visit` | Domain entity | Veterinary appointment or consultation recorded for a pet. |
| `petId` | Field | Identifier used to select the specific pet within an owner’s pet collection. |
| `visit` | Field | The visit details to be recorded against the chosen pet. |
| Aggregate root | Architectural term | Domain object that manages access to its child entities in a consistent way. |
| Domain model | Architectural term | Business object layer where owner/pet/visit relationships are represented. |
