# (DD14) Business Logic — BookRepository.searchByTitle() [5 LOC]

| Field | Value |
|-------|-------|
| Fully Qualified Name | `repository.BookRepository` |
| Layer | DAO |
| Module | `repository` (Package: `repository`) |

## 1. Role

### BookRepository.searchByTitle()

This method performs a title-based book lookup within the in-memory repository collection. Its business responsibility is to support keyword search over the catalog by returning only the books whose titles contain the provided search term, regardless of letter case. In practical terms, it behaves as a simple catalog search facility for end users who want to locate books by partial title rather than exact identifier.

The method implements a stream-based filtering pattern over the internal `books` collection. It does not create, update, or delete book records; instead, it reads the current repository state and produces a filtered result set. Because the comparison is case-insensitive, it supports flexible user-entered search terms and avoids strict matching behavior that would make the search less usable.

Within the larger system, this method is a reusable repository-level query helper. It is invoked by `LibraryService.searchBooks(String keyword)`, making it the persistence-adjacent lookup operation that underpins higher-level search behavior.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["searchByTitle(keyword)"])
    READ_BOOKS(["Read books list from repository field books"])
    STREAM_START(["Start stream over books"])
    FILTER_TITLE(["Filter books by title matching keyword case-insensitively"])
    LOWER_TITLE(["Convert book title to lowercase"])
    LOWER_KEYWORD(["Convert keyword to lowercase"])
    CONTAINS_CHECK{"Title contains keyword"}
    COLLECT(["Collect matching books into List<Book>"])
    END_NODE(["Return List<Book>"])
    START --> READ_BOOKS
    READ_BOOKS --> STREAM_START
    STREAM_START --> FILTER_TITLE
    FILTER_TITLE --> LOWER_TITLE
    FILTER_TITLE --> LOWER_KEYWORD
    LOWER_TITLE --> CONTAINS_CHECK
    LOWER_KEYWORD --> CONTAINS_CHECK
    CONTAINS_CHECK -->|Yes| COLLECT
    CONTAINS_CHECK -->|No| COLLECT
    COLLECT --> END_NODE
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `keyword` | `String` | User-entered search keyword used to find books by partial title match. It may be any text value, and the method treats it case-insensitively by converting it to lowercase before comparison. If the keyword is too broad, the result set grows; if it is more specific, fewer books are returned. |

Instance fields / external state used by the method:
- `books` — the in-memory repository list that holds all currently registered `Book` objects.

## 4. CRUD Operations / Called Services

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `books.stream().filter(...).toList()` | N/A | `books` in-memory collection | Reads the current book list and returns only entries whose titles contain the requested keyword. |

## 5. Dependency Trace

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Service: `LibraryService` | `LibraryService.searchBooks(String keyword)` -> `BookRepository.searchByTitle(String keyword)` | `books.stream().filter(...).toList() [R] books` |

## 6. Per-Branch Detail Blocks

**Block 1** — [SINGLE PATH] `(searchByTitle(keyword))` (L23)

> This block reads the repository state and begins keyword-based title filtering.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `books.stream()`; start stream processing over the repository collection |

**Block 2** — [CALL] `(stream().filter(...))` (L24-L26)

> This block applies a title-match filter to each book in the stream.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `b.getTitle()`; retrieve the book title |
| 2 | EXEC | `b.getTitle().toLowerCase()`; normalize the title for case-insensitive matching |
| 3 | EXEC | `keyword.toLowerCase()`; normalize the search keyword for case-insensitive matching |
| 4 | EXEC | `contains(...)`; check whether the normalized title includes the normalized keyword |
| 5 | RETURN | `filter(...)`; keep the book when the title contains the keyword |

**Block 3** — [RETURN] `(toList())` (L27)

> This block materializes the filtered stream into a returnable list.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `.toList()`; collect all matching books into `List<Book>` |
| 2 | RETURN | `return ...;` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `Book` | Domain object | Book master record used by the catalog. |
| `books` | Field | In-memory repository collection holding all registered books. |
| `keyword` | Field/Parameter | Search text entered by the user to locate books by title. |
| `title` | Field | Book title used as the matching target for search. |
| case-insensitive matching | Business term | Comparison logic that ignores letter case so users can search without exact capitalization. |
| repository | Layer term | Data access layer component that stores and retrieves domain objects. |
| DAO | Acronym | Data Access Object — a layer responsible for reading and writing domain data. |
| stream | Technical term | Java collection processing pipeline used here to filter books. |
| `LibraryService` | Service class | Higher-level application service that delegates book search to the repository. |
