layered-architecture.md
docs/layered-architecture.md
1# Layered Architecture23Each feature exposes a service interface and isolates JPA behind an Anti-Corruption Layer:45```6at/fhtw/swen3/paperless/<feature>/7 controller/ <Feature>Controller8 service/ <Feature>Service, <Feature>ServiceImpl9 model/ immutable domain models and enums10 dto/ request and response types11 mapper/ <Feature>Mapper — DTO ↔ model12 entity/ JPA entities13 persistence/ <Feature>Persistence — domain-facing interface14 Jpa<Feature>Persistence — Spring @Repository adapter15 repository/ plain Spring Data interfaces16 mapper/ <Feature>EntityMapper — pure model ↔ entity mapping17```1819`model/`, `entity/`, `service/`, and `persistence/` are sibling folders within each20feature. Entities stay separate from the persistence adapters and repositories.2122## Layer contract2324| Layer | May depend on | Must not depend on |25| --- | --- | --- |26| Controller / DTO mapper | Service interfaces, models, DTOs | Persistence interfaces or implementation details |27| Service | Domain persistence interfaces, models, paging types | Other services, JPA, entities, Spring Data repositories, entity mappers, DTOs |28| Persistence interface | Domain models, UUIDs, Optional, Page / Pageable | JPA, entities, Spring Data repositories, services, DTOs |29| JPA adapter | Entities, repositories, entity mappers, EntityManager | Controllers, DTOs, business services |30| Entity mapper | Models and entities | I/O, repositories, services, business rules |31| Domain model | Kotlin / Java value types | JPA and Spring framework classes |3233## Request flow3435```36Controller → DTO mapper → Service → Persistence interface37 ↓38 JPA adapter → entity mapper → Spring Data repository39```4041Adapters translate all persistence results to domain models before returning, including42optional values, lists, pages, and lazy relationships. Services never receive managed43entities and update immutable domain values with `copy`.4445Business validation, authorization, and domain not-found errors remain in services.46Adapters resolve entity references and handle database operations, mapping, flushes,47and concurrent-deletion / stale-version detection. They do not decide who may access data.48Spring Data repositories stay plain interfaces; no handwritten repository implementations.4950Services own transactions. Adapter calls participate in those transactions so lazy51relationships are materialized while the persistence context is open. Existing52pessimistic locks for user authentication and team membership changes are preserved.53The document adapter carries the domain version through mapping and compares it,54owner ID, and team ID after refresh before update or deletion. Service file cleanup55continues to depend on transaction completion and explicit flush failures.5657Services never inject or call other services, including services from the same feature.58Cross-feature operations use domain-facing persistence interfaces within the calling59service's transaction (for example account deletion checking team memberships and60revoking tokens). JPA types are confined to61entity and persistence packages, even when associations refer to another feature's entities.6263`DocumentReferencesPersistence` provides domain-valued reference lookups and team64locks to document services. Authentication owns credential validation, user locking, and token replacement.65Search owns its membership-filtered candidate lookup. `AccessToken` carries a digest,66user ID, and expiry, without leaking a user entity. New documents have a nullable ID until persisted; response mapping requires an ID.6768See [Annotations](annotations.md), [Mappers](mappers.md), and [Testing](testing.md).6970## Shared entity properties7172UUID entities inherit `id`, `createdAt`, `updatedAt`, and `version` from73`common/entity/AbstractEntity`, a JPA mapped superclass. Hibernate generates UUIDs,74maintains creation/update timestamps, and checks the inherited version on writes.75Domain models and entity mappers carry the loaded version through updates to preserve76optimistic locking. Membership updates mutate the managed entity and retain its version.77Flyway V2 adds and backfills the new columns for existing databases.78`AccessTokenEntity` keeps its digest primary key and expiration lifecycle; it does not79inherit a generated UUID identity.80