# agrawald/elevator architecture

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

- Interactive diagram: https://gitdiagram.com/agrawald/elevator
- Repository: https://github.com/agrawald/elevator

## Overview

##### Main Components
- Frontend UI: Represented by the Angular.js components integrated in the JSP pages (e.g., “index.jsp”) and the JavaScript file (elevator.js). This is the client side interfacing with the REST API.
- REST Controllers: Include the BuildingController and ElevatorController. These handle REST endpoints for triggering elevator calls (up/down from floors) and actions from inside the elevator (go to floor, stop, get status).
- Service Layer: Consists of BuildingSvc and ElevatorSvc along with their respective implementations (BuildingSvcImpl and ElevatorSvcImpl). These abstract and manage the core business rules and coordinate interactions between the controllers and the underlying business/logical operations.
- Business/Simulation Layer: Contains the elevator logic via components like ElevatorPub, ElevatorSub, and ElevatorPubSingleton which initialize and run the elevator simulation using a publisher-subscriber pattern.
- Common Definitions: The “common” package provides shared constants, direction enumerations, and the representation for a floor call.
- Integration/3rd Party API: The services, particularly BuildingSvc and ElevatorSvc, also expose APIs to be used by external hardware companies.
- Testing Components: Represented by test classes (SimulatorTest, ElevatorFloorPubSubFactoryTest, etc.) which confirm simulation behavior and REST API functionality.

##### Relationships and Interactions
- The frontend invokes REST calls (via HTTP methods) which route to the appropriate controllers.
- The controllers translate incoming REST requests into service calls.
- The services then delegate to the business logic:
– For example, a REST call to call an elevator triggers a publisher method (ElevatorPub) to issue a floor call.
– The subscriber (ElevatorSub) for each elevator receives these events such that each elevator thread processes its assigned events.
- The business layer leverages the singleton (ElevatorPubSingleton) to ensure a single source of events for simulation.
- Additionally, external API calls made by hardware companies interact with the services.
- Testing modules simulate calls to both the simulation logic and the REST endpoints to verify correct operation.

##### Architectural Patterns & Technologies
- MVC Pattern: The Spring MVC design in the backend divides the application into Controllers, Services, and View (JSP).
- Publisher-Subscriber Pattern: Used for handling event distribution between floor call publishers (ElevatorPub) and elevator-specific subscribers (ElevatorSub).
- Singleton Pattern: Used with ElevatorPubSingleton to ensure consistent control over the simulation thread.
- Technologies: Spring MVC for server-side logic, Maven for project build, embedded Jetty as the server, Angular.js for client-side interactions, and traditional REST methods to couple the layers.

##### Final Checklist for the Principal Software Engineer
- Component (e.g., Frontend, Controllers, Services, Business Logic, Common, Test) is clearly labeled.
- HTTP call → Controller → Service → Business/Simulation layer.
- Business events flowing from ElevatorPub to ElevatorSub.
- Build or configuration artifacts (Maven’s pom.xml, Jetty configuration in web.xml and mvc-dispatcher-servlet.xml) if they affect deployment architecture.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Components
    subgraph "Frontend"
        JSP["index.jsp"]:::frontend
        Angular["elevator.js"]:::frontend
    end

    %% REST Controllers
    subgraph "REST Controllers"
        BC["BuildingController"]:::controller
        EC["ElevatorController"]:::controller
    end

    %% External Integration
    ext["External Hardware Companies"]:::external

    %% Service Layer
    subgraph "Service Layer"
        BS["BuildingSvc"]:::service
        ES["ElevatorSvc"]:::service
        BSI["BuildingSvcImpl"]:::service
        ESI["ElevatorSvcImpl"]:::service
    end

    %% Business/Simulation Layer
    subgraph "Business/Simulation"
        PUB["ElevatorPub"]:::business
        SUB["ElevatorSub"]:::business
        PUBS["ElevatorPubSingleton"]:::business
    end

    %% Common Definitions
    subgraph "Common Definitions"
        COMMON["Constants/Direction/FloorCall"]:::common
    end

    %% Testing Components
    subgraph "Testing"
        Test1["SimulatorTest"]:::testing
        Test2["ElevatorFloorPubSubFactoryTest"]:::testing
    end

    %% Data Flow / Relationships
    JSP -->|"HTTP_call"| BC
    Angular -->|"HTTP_call"| EC

    BC -->|"call_BuildingSvc"| BS
    EC -->|"invoke_goto,stop,status"| ES

    BS -->|"delegate_to_impl"| BSI
    ES -->|"delegate_to_impl"| ESI

    BSI -->|"trigger_simulation"| PUB
    ESI -->|"trigger_simulation"| PUB

    PUBS -->|"manage_publisher"| PUB
    PUBS -->|"subscribe_subscribers"| SUB

    PUB -->|"publish_floorcall"| SUB
    PUB -->|"access_shared"| COMMON
    SUB -->|"access_shared"| COMMON

    SUB -->|"update_status"| ESI
    ESI -->|"report_status"| EC

    ext -->|"API_call"| BS
    ext -->|"API_call"| ES

    Test1 -->|"simulate_simulation"| PUB
    Test1 -->|"simulate_REST"| EC
    Test2 -->|"verify_pubsub_factory"| PUB

    %% Styles
    classDef frontend fill:#cce5ff,stroke:#0066cc,stroke-width:2px;
    classDef controller fill:#d4edda,stroke:#28a745,stroke-width:2px;
    classDef service fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef business fill:#f8d7da,stroke:#dc3545,stroke-width:2px;
    classDef common fill:#d1ecf1,stroke:#0c5460,stroke-width:2px;
    classDef testing fill:#e2e3e5,stroke:#6c757d,stroke-width:2px;
    classDef external fill:#e9ecef,stroke:#333,stroke-width:2px,stroke-dasharray: 5, 5;

    %% Click Events
    click JSP "https://github.com/agrawald/elevator/blob/master/src/main/webapp/WEB-INF/jsp/index.jsp"
    click Angular "https://github.com/agrawald/elevator/blob/master/src/main/webapp/resources/elevator.js"
    click BC "https://github.com/agrawald/elevator/blob/master/src/main/java/org/elevator/controller/BuildingController.java"
    click EC "https://github.com/agrawald/elevator/blob/master/src/main/java/org/elevator/controller/ElevatorController.java"
    click BS "https://github.com/agrawald/elevator/blob/master/src/main/java/org/elevator/service/BuildingSvc.java"
    click ES "https://github.com/agrawald/elevator/blob/master/src/main/java/org/elevator/service/ElevatorSvc.java"
    click BSI "https://github.com/agrawald/elevator/blob/master/src/main/java/org/elevator/service/impl/BuildingSvcImpl.java"
    click ESI "https://github.com/agrawald/elevator/blob/master/src/main/java/org/elevator/service/impl/ElevatorSvcImpl.java"
    click PUB "https://github.com/agrawald/elevator/blob/master/src/main/java/org/elevator/business/ElevatorPub.java"
    click SUB "https://github.com/agrawald/elevator/blob/master/src/main/java/org/elevator/business/ElevatorSub.java"
    click PUBS "https://github.com/agrawald/elevator/blob/master/src/main/java/org/elevator/business/ElevatorPubSingleton.java"
    click COMMON "https://github.com/agrawald/elevator/tree/master/src/main/java/org/elevator/common"
    click Test1 "https://github.com/agrawald/elevator/blob/master/src/test/java/org/elevator/business/SimulatorTest.java"
    click Test2 "https://github.com/agrawald/elevator/blob/master/src/test/java/org/test/elevator/api/ElevatorFloorPubSubFactoryTest.java"
```
