Class UserRepository
- All Implemented Interfaces:
IRepository<User,
,String> IUserRepository
This repository manages user data for all user types in the BTO Management System, including Applicants, HDB Officers, and HDB Managers. It provides CRUD operations and handles the persistence of user data to a CSV file, with in-memory caching for efficient access.
The repository uses a CsvRepositoryHelper to handle the serialization and deserialization of user data between Java objects and CSV format. It maintains data consistency between the in-memory cache and the persistent storage.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfindAll()
Retrieves all entities currently managed by this repository.Finds and retrieves a single entity by its unique identifier.loadAll()
Loads all entities from the persistent storage into memory.void
Saves an entity to the repository.void
Saves multiple entities to the repository in a batch operation.
-
Constructor Details
-
UserRepository
public UserRepository()Constructs a new UserRepository.Initializes the repository with a CsvRepositoryHelper configured for User entities and attempts to load existing user data from the CSV file. If the initial data load fails, an empty user collection is created.
-
-
Method Details
-
findById
Finds and retrieves a single entity by its unique identifier.This method attempts to locate an entity with the specified ID in the data store. If no matching entity is found, the method returns null.
Retrieves a user by their NRIC from the in-memory cache. Returns null if no user exists with the specified NRIC.
- Specified by:
findById
in interfaceIRepository<User,
String> - Parameters:
id
- The unique identifier of the entity to retrieve- Returns:
- The entity if found, or null if no entity exists with the given ID
-
findAll
Retrieves all entities currently managed by this repository.This method returns a map of all entities, with entity IDs as keys and entity objects as values. If the repository is empty, an empty map is returned.
Returns a defensive copy of the users map to prevent external modification of the repository's internal state. Users are keyed by their NRIC.
- Specified by:
findAll
in interfaceIRepository<User,
String> - Returns:
- A map containing all entities, with IDs as keys and entity objects as values
-
save
Saves an entity to the repository.If the entity already exists in the repository (based on its ID), the existing entity will be updated with the new values. If the entity does not exist, it will be added as a new entry. The repository implementation is responsible for determining how to extract the ID from the entity.
Saves a user to both the in-memory cache and the CSV file. The user's NRIC is used as the key in the users map.
If the user or their NRIC is null, the method logs an error and returns without saving. If saving to the persistent store fails, an error is logged and the exception is propagated.
- Specified by:
save
in interfaceIRepository<User,
String> - Parameters:
entity
- The entity to save
-
saveAll
Saves multiple entities to the repository in a batch operation.This method allows for more efficient bulk saving of entities compared to calling save() repeatedly. It takes a map of entities keyed by their IDs and persists them all, potentially in a single operation depending on the implementation.
Replaces all existing users with the provided collection and persists them to the CSV file. Creates a defensive copy of the provided map to maintain repository encapsulation.
If saving to the persistent store fails, an error is logged and the exception is propagated.
- Specified by:
saveAll
in interfaceIRepository<User,
String> - Parameters:
entities
- A map of entities to save, with IDs as keys and entity objects as values
-
loadAll
Loads all entities from the persistent storage into memory.This method is typically called during application initialization to populate the repository with data from the persistent store. Implementations should handle parsing and conversion from the storage format to entity objects.
Reloads all user data from the CSV file into the in-memory cache, replacing any existing data. Returns a defensive copy of the loaded users map.
- Specified by:
loadAll
in interfaceIRepository<User,
String> - Returns:
- A map of all loaded entities, with IDs as keys and entity objects as values
-