Database Initialization (DataLoader)
Relevant source files
The following files were used as context for generating this wiki page:
- Part1/API/src/main/java/com/example/api/config/DataLoader.java
- Part1/API/src/main/java/com/example/api/entity/AppUser.java
- Part1/API/src/main/java/com/example/api/security/SecurityConfig.java
- Part1/API/src/main/java/com/example/api/model/Employee.java
- Part1/API/src/main/java/com/example/api/model/Department.java
The Database Initialization system in the SafeDispatch HQ Administration API is responsible for bootstrapping the application with essential seed data upon startup. This mechanism ensures that the system is immediately functional and accessible, particularly for security-related roles that are required to manage the platform via the Security Configuration.
The primary focus of the current implementation is the automated creation of administrative users within the persistent storage, bypassing the need for manual SQL scripts during the initial deployment or development phases. This process is handled by the DataLoader component, which integrates with Spring Boot's lifecycle to execute logic after the application context is loaded.
Sources: DataLoader.java:1-29, README.md
Core Initialization Logic
The initialization logic is encapsulated within a @Configuration class that defines a CommandLineRunner bean. This runner executes a check against the user repository to determine if the database is empty before proceeding with data insertion.
User Bootstrapping Process
The system creates two default administrative accounts with distinct roles: HR and MANAGER. These roles correspond to the authorization levels defined in the security layer. Each user is assigned a username, a role, and a BCrypt-encoded password.
The diagram shows the conditional logic used to prevent duplicate entries on subsequent application restarts.
Sources: DataLoader.java:13-28
Key Components
| Component | Responsibility |
|---|---|
DataLoader | Configuration class that triggers the data loading process. |
CommandLineRunner | Interface used to run code blocks after the application starts. |
UserRepository | Data access object used to persist AppUser entities. |
PasswordEncoder | Service used to secure plain-text passwords before storage. |
Sources: DataLoader.java:10-15, AppUser.java:1-29
Data Models and Entities
The initialization process populates the AppUser entity, which is specifically designed for Spring Security authentication. While the project contains other business models like Employee and Department, the DataLoader currently focuses on system access credentials.
AppUser Schema
The AppUser entity defines the structure for authentication records in the database.
| Field | Type | Constraint | Description |
|---|---|---|---|
id | Long | Primary Key | Generated automatically by the database. |
username | String | Unique, Not Null | The unique identifier for login. |
password | String | Not Null | BCrypt hashed password string. |
role | String | Not Null | Authorization level (e.g., HR, MANAGER). |
Sources: AppUser.java:10-25
Relationship to Business Models
The initialized users are intended to manage the broader data ecosystem, including employees and departments.
The diagram illustrates how AppUser entities provide the administrative layer required to interact with the core business entities.
Sources: Employee.java:12-30, Department.java:10-25, Assignment.java:8-20
Security Integration
The DataLoader relies on the SecurityConfig to provide a PasswordEncoder bean (specifically BCryptPasswordEncoder). This ensures that initialized passwords meet the security requirements of the DaoAuthenticationProvider.
Initialization Snippet
// Part1/API/src/main/java/com/example/api/config/DataLoader.java:16-25
if (userRepository.count() == 0) {
AppUser hr = new AppUser();
hr.setUsername("hr");
hr.setPassword(passwordEncoder.encode("hr123"));
hr.setRole("HR");
userRepository.save(hr);
AppUser manager = new AppUser();
manager.setUsername("manager");
manager.setPassword(passwordEncoder.encode("manager123"));
manager.setRole("MANAGER");
userRepository.save(manager);
}
Sources: DataLoader.java:16-27, SecurityConfig.java:25-30
Summary
The Database Initialization system provides a seamless setup experience by automatically configuring the primary administrative accounts required for the SafeDispatch HQ Administration API. By leveraging the DataLoader and Spring Boot's CommandLineRunner, the project ensures that security roles are correctly mapped and persistent storage is ready for use immediately upon deployment, facilitating both development and testing workflows.