---

# (DD23) Business Logic — OwnerController.processCreationForm() [11 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.processCreationForm()

This method is the POST submission handler for the owner creation screen. It accepts a validated `Owner` form object, checks whether the submitted business data satisfies the binding and validation rules, and then either returns the user to the same owner form with an error message or persists the new owner record and redirects the user to the newly created owner detail page. In business terms, it is the commit point for registering a new pet owner in the PetClinic system.

The method follows a classic controller routing and validation pattern: it dispatches on the presence of validation errors, stores user-facing feedback as flash attributes, and delegates persistence to the owner repository. It does not perform business transformations beyond this workflow control, so its primary role is to orchestrate the create operation rather than implement domain logic itself. The method has two branches: an error branch that preserves the entry form for correction, and a success branch that saves the owner and confirms creation to the user.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["processCreationForm(owner, result, redirectAttributes)"])
    CHECK_ERRORS{"result.hasErrors()"}
    ADD_ERROR(["redirectAttributes.addFlashAttribute(\"error\", \"There was an error in creating the owner.\")"])
    RETURN_FORM(["Return views owner create or update form"])
    SAVE_OWNER(["owners.save(owner)"])
    ADD_MESSAGE(["redirectAttributes.addFlashAttribute(\"message\", \"New Owner Created\")"])
    RETURN_REDIRECT(["Return redirect to /owners/{ownerId}"])

    START --> CHECK_ERRORS
    CHECK_ERRORS -->|true| ADD_ERROR
    ADD_ERROR --> RETURN_FORM
    CHECK_ERRORS -->|false| SAVE_OWNER
    SAVE_OWNER --> ADD_MESSAGE
    ADD_MESSAGE --> RETURN_REDIRECT
```

**Constant resolution:** No application constants are referenced in this method. The view name is stored in the local class constant `VIEWS_OWNER_CREATE_OR_UPDATE_FORM`, which resolves to `owners/createOrUpdateOwnerForm`.

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `owner` | `@Valid Owner` | The owner registration form data submitted from the UI. It contains the customer profile that will become the new owner record if validation succeeds. |
| 2 | `result` | `BindingResult` | The validation outcome for the submitted owner profile. It carries field-level and object-level errors that determine whether the request can be committed or must be returned to the form. |
| 3 | `redirectAttributes` | `RedirectAttributes` | Flash-message carrier used to communicate success or failure to the next page after a redirect or form re-display. It preserves user feedback across the request boundary. |

**Instance fields / external state read by the method:**
- `owners` — the injected `OwnerRepository` used to persist the new owner.
- `VIEWS_OWNER_CREATE_OR_UPDATE_FORM` — local view name constant returned when validation fails.

## 4. CRUD Operations / Called Services

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `result.hasErrors()` | N/A | Binding / validation state | Reads the validation state of the submitted owner form to determine whether creation can continue. |
| C | `owners.save(owner)` | N/A | `Owner` / owner persistence store | Creates a new owner record in the repository after successful validation. |
| R | `owner.getId()` | N/A | `Owner` | Reads the generated owner identifier so the controller can build the redirect target. |

## 5. Dependency Trace

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Screen:Owner create form (`owners/createOrUpdateOwnerForm`) | Browser POST `/owners/new` -> `OwnerController.processCreationForm` | `owners.save(owner) [C] Owner` |

### Downstream call outcome
- Validation branch: `result.hasErrors()` determines whether the method returns the form view or continues to persistence.
- Success branch: `owners.save(owner)` persists the new owner, then `owner.getId()` supplies the identifier for the redirect URL.

## 6. Per-Branch Detail Blocks

**Block 1** — IF `(result.hasErrors())` (L79)

> Checks whether the submitted owner registration data failed validation or binding rules.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `result.hasErrors();` |

**Block 1.1** — TRUE branch `(validation failed)` (L80-L81)

> Stores an error flash message and returns the owner entry form for correction.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `redirectAttributes.addFlashAttribute("error", "There was an error in creating the owner.");` |
| 2 | RETURN | `return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;` // `owners/createOrUpdateOwnerForm` |

**Block 1.2** — FALSE branch `(validation passed)` (L84-L86)

> Persists the new owner and redirects the user to the created owner profile page.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `this.owners.save(owner);` |
| 2 | EXEC | `redirectAttributes.addFlashAttribute("message", "New Owner Created");` |
| 3 | RETURN | `return "redirect:/owners/" + owner.getId();` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `Owner` | Domain entity | Pet owner profile stored in the system, representing the customer who owns pets. |
| `BindingResult` | Technical term | Validation and binding result object that records input errors from the submitted form. |
| `RedirectAttributes` | Technical term | Spring MVC flash attribute container used to pass messages across redirects. |
| `owners` | Repository | Data access component used to persist and retrieve owner records. |
| `save` | CRUD operation | Create/persist operation that stores a new owner record. |
| `redirect:/owners/{id}` | Web flow term | Redirect target that opens the newly created owner detail page. |
| `owners/createOrUpdateOwnerForm` | View name | Thymeleaf page used for both owner creation and owner editing. |
| validation | Business rule term | Input quality check that ensures required owner fields are present and well-formed before creation. |
| flash attribute | Web term | Temporary message stored for display on the next request after redirect or form return. |
| owner registration | Business term | The act of creating a new pet owner profile in PetClinic. |
