# bhits/document-validator architecture

> Architecture diagram of the GitHub repository bhits/document-validator, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/bhits/document-validator
- Repository: https://github.com/bhits/document-validator

## Overview

##### Project Type and Purpose
- Type: A standalone RESTful microservice (no UI) built on Spring Boot + external Tomcat, packaged as a WAR/Docker image.
- Purpose: Exposes a REST API for validating C32 and C-CDA clinical documents by invoking MDHT libraries, returning schema and schematron validation results.  It is consumed by clients like the DSS (Document Segmentation Service).

##### Main Components
A. External Clients
– DSS or any HTTP client that submits a document for validation
B. Network/API Layer
– Spring MVC Controller (DocumentValidationController)
– REST endpoints under `/document-validator` context path
C. Business Logic Layer
– DocumentValidationService (service interface + impl)
– DocumentTypeResolver (identifies schema/CCDA version)
D. Infrastructure/Validation Engine
– CcdaValidator (interface + impl) wrapping MDHT libraries
– MDHT JARs (schema + schematron engines)
E. Configuration
– Spring Cloud Config Server (external)
– Local bootstrap.yml & application.yml
F. Application Server & Packaging
– Apache Tomcat 8 (external container)
– Docker image `bhits/document-validator:latest`
G. Deployment Artifacts
– WAR file `document-validator.war`
– Docker image, mounted SSL keystore & custom cacerts
H. Logging
– logback-spring.xml (for structured logs)

##### Relationships & Data Flows
1. Client → HTTP POST `/validate`
2. Controller invokes
→ DocumentValidationService
3. Service uses
→ DocumentTypeResolver to detect document type
→ CcdaValidator to perform MDHT validation
4. CcdaValidator loads MDHT schema + schematron rules, returns ValidationResponseDto
5. Controller returns HTTP response with ValidationResponseDto JSON
6. At startup, application fetches external config from Config Server (or falls back to local YML files)
7. Packaging path: source → Maven build → WAR → deploy to Tomcat OR Maven‐Docker plugin → Docker image

##### Architectural Patterns & Design Principles
- Layered (Controller → Service → Infrastructure)
- Dependency Injection via Spring Boot
- Externalized Configuration (Spring Cloud Config + bootstrap.yml)
- Adapter/Wrapper for MDHT libraries
- Dockerization for consistent deployment
- No persistent datastore—stateless service

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Layer
    subgraph "External Systems"
        direction TB
        Client["DSS / HTTP Client"]:::external
        ConfigServer["Spring Cloud Config Server"]:::external
    end

    %% API Layer
    subgraph "API Layer"
        direction TB
        DocController["DocumentValidationController\n(Spring MVC)"]:::internal
    end

    %% Business Logic Layer
    subgraph "Business Layer"
        direction TB
        DocServiceIntf["DocumentValidationService\n(interface)"]:::internal
        DocServiceImpl["DocumentValidationServiceImpl\n(@Service)"]:::internal
        DocTypeResolverIntf["DocumentTypeResolver\n(interface)"]:::internal
        DocTypeResolverImpl["DocumentTypeResolverImpl"]:::internal
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        direction TB
        CcdaValidatorIntf["CcdaValidator\n(interface)"]:::infra
        CcdaValidatorImpl["CcdaValidatorImpl"]:::infra
        MDHT["MDHT Libraries\n(schema & schematron)"]:::lib
        ValidationCriteria["ValidationCriteria"]:::infra
        ValidationType["ValidationType"]:::infra
        CcdaValEx["CcdaValidatorRunningException"]:::exception
        C32ValEx["C32ValidatorRunningException"]:::exception
    end

    %% DTOs and Schema
    subgraph "Service DTOs & Schema"
        direction TB
        ReqDto["ValidationRequestDto"]:::dto
        ResDto["ValidationResponseDto"]:::dto
        CCDAVersion["CCDAVersion"]:::dto
        DocumentTypeSchema["DocumentType"]:::dto
        TemplateId["TemplateId"]:::dto
    end

    %% Util Layer
    DocumentHandler["DocumentHandler"]:::internal

    %% Configuration Layer
    subgraph "Configuration"
        direction TB
        BootstrapYml["bootstrap.yml"]:::config
        AppYml["application.yml"]:::config
        Props["DocumentValidatorProperties"]:::config
        AppCtx["ApplicationContextConfig"]:::config
    end

    %% Logging
    Logback["logback-spring.xml"]:::config

    %% Application Entry
    DocApp["DocumentValidatorApplication"]:::deployment

    %% Packaging & Deployment
    subgraph "Deployment Artifacts"
        direction TB
        Dockerfile["Dockerfile"]:::deployment
        Pom["pom.xml"]:::deployment
        WAR["document-validator.war"]:::deployment
    end

    %% Data Flows
    Client -->|"HTTP POST /document-validator/validate"| DocController
    DocController -->|"invoke validate()"| DocServiceIntf
    DocServiceIntf --> DocServiceImpl
    DocServiceImpl -->|"resolve type"| DocTypeResolverIntf
    DocTypeResolverIntf --> DocTypeResolverImpl
    DocServiceImpl -->|"validate document"| CcdaValidatorIntf
    CcdaValidatorIntf --> CcdaValidatorImpl
    CcdaValidatorImpl --> MDHT
    CcdaValidatorImpl --> ValidationCriteria
    CcdaValidatorImpl --> ValidationType
    CcdaValidatorImpl --> CcdaValEx
    CcdaValidatorImpl --> C32ValEx
    CcdaValidatorImpl --> ResDto
    DocServiceImpl --> ReqDto
    DocServiceImpl --> ResDto
    DocController -->|"return JSON"| ResDto

    DocApp -->|"fetch config"| ConfigServer
    DocApp --> BootstrapYml
    DocApp --> AppYml
    DocApp --> Props
    DocApp --> AppCtx
    DocApp --> Logback

    %% Packaging Flow
    Pom --> WAR
    Dockerfile --> WAR
    WAR -->|"deploy to Tomcat8"| DocApp

    %% Util
    DocServiceImpl --> DocumentHandler

    %% Click Events
    click DocController "https://github.com/bhits/document-validator/blob/master/document-validator/src/main/java/gov/samhsa/c2s/documentvalidator/web/DocumentValidationController.java"
    click DocServiceIntf "https://github.com/bhits/document-validator/blob/master/document-validator/src/main/java/gov/samhsa/c2s/documentvalidator/service/DocumentValidationService.java"
    click DocServiceImpl "https://github.com/bhits/document-validator/blob/master/document-validator/src/main/java/gov/samhsa/c2s/documentvalidator/service/DocumentValidationServiceImpl.java"
    click DocTypeResolverIntf "https://github.com/bhits/document-validator/blob/master/document-validator/src/main/java/gov/samhsa/c2s/documentvalidator/service/DocumentTypeResolver.java"
    click DocTypeResolverImpl "https://github.com/bhits/document-validator/blob/master/document-validator/src/main/java/gov/samhsa/c2s/documentvalidator/service/DocumentTypeResolverImpl.java"
    click CcdaValidatorIntf "https://github.com/bhits/document-validator/blob/master/document-validator/src/main/java/gov/samhsa/c2s/documentvalidator/infrastructure/CcdaValidator.java"
    click CcdaValidatorImpl "https://github.com/bhits/document-validator/blob/master/document-validator/src/main/java/gov/samhsa/c2s/documentvalidator/infrastructure/CcdaValidatorImpl.java"
    click ValidationCriteria "https://github.com/bhits/document-validator/blob/master/document-validator/src/main/java/gov/samhsa/c2s/documentvalidator/infrastructure/ValidationCriteria.java"
    click ValidationType "https://github.com/bhits/document-validator/blob/master/document-validator/src/main/java/gov/samhsa/c2s/documentvalidator/infrastructure/ValidationType.java"
    click BootstrapYml "https://github.com/bhits/document-validator/blob/master/document-validator/src/main/resources/bootstrap.yml"
    click AppYml "https://github.com/bhits/document-validator/blob/master/document-validator/src/main/resources/application.yml"
    click Props "https://github.com/bhits/document-validator/blob/master/document-validator/src/main/java/gov/samhsa/c2s/documentvalidator/config/DocumentValidatorProperties.java"
    click AppCtx "https://github.com/bhits/document-validator/blob/master/document-validator/src/main/java/gov/samhsa/c2s/documentvalidator/config/ApplicationContextConfig.java"
    click DocApp "https://github.com/bhits/document-validator/blob/master/document-validator/src/main/java/gov/samhsa/c2s/documentvalidator/DocumentValidatorApplication.java"
    click Logback "https://github.com/bhits/document-validator/blob/master/document-validator/src/main/resources/logback-spring.xml"
    click Dockerfile "https://github.com/bhits/document-validator/tree/master/document-validator/src/main/docker/Dockerfile"
    click Pom "https://github.com/bhits/document-validator/blob/master/document-validator/pom.xml"
    click CcdaValEx "https://github.com/bhits/document-validator/blob/master/document-validator/src/main/java/gov/samhsa/c2s/documentvalidator/infrastructure/exception/CcdaValidatorRunningException.java"
    click C32ValEx "https://github.com/bhits/document-validator/blob/master/document-validator/src/main/java/gov/samhsa/c2s/documentvalidator/infrastructure/exception/C32ValidatorRunningException.java"
    click ReqDto "https://github.com/bhits/document-validator/blob/master/document-validator/src/main/java/gov/samhsa/c2s/documentvalidator/service/dto/ValidationRequestDto.java"
    click ResDto "https://github.com/bhits/document-validator/blob/master/document-validator/src/main/java/gov/samhsa/c2s/documentvalidator/service/dto/ValidationResponseDto.java"
    click CCDAVersion "https://github.com/bhits/document-validator/blob/master/document-validator/src/main/java/gov/samhsa/c2s/documentvalidator/service/schema/CCDAVersion.java"
    click DocumentTypeSchema "https://github.com/bhits/document-validator/blob/master/document-validator/src/main/java/gov/samhsa/c2s/documentvalidator/service/schema/DocumentType.java"
    click TemplateId "https://github.com/bhits/document-validator/blob/master/document-validator/src/main/java/gov/samhsa/c2s/documentvalidator/service/schema/TemplateId.java"
    click DocumentHandler "https://github.com/bhits/document-validator/blob/master/document-validator/src/main/java/gov/samhsa/c2s/documentvalidator/service/util/DocumentHandler.java"

    %% Styles
    classDef external fill:#f96,stroke:#633,stroke-width:1px
    classDef internal fill:#bbf,stroke:#44f,stroke-width:1px
    classDef infra fill:#8f8,stroke:#080,stroke-width:1px
    classDef lib fill:#ccc,stroke:#888,stroke-width:1px
    classDef config fill:#fc9,stroke:#c70,stroke-width:1px
    classDef deployment fill:#ddf,stroke:#55f,stroke-width:1px
    classDef dto fill:#ccf,stroke:#77f,stroke-width:1px
    classDef exception fill:#fbb,stroke:#f33,stroke-width:1px
```
