Class OfficerRegistrationRepository
- All Implemented Interfaces:
IRepository<OfficerRegistration,
,String> IOfficerRegistrationRepository
This class handles the persistence and retrieval of OfficerRegistration records using a CSV file as the backing store. It provides methods for finding registrations by various criteria and implements the standard repository operations defined in the IOfficerRegistrationRepository interface.
The repository maintains an in-memory cache of registrations for quick access while ensuring that changes are persisted to the CSV file. It uses the CsvRepositoryHelper to handle the low-level file operations and serialization/deserialization.
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new OfficerRegistrationRepository. -
Method Summary
Modifier and TypeMethodDescriptionfindAll()
Retrieves all entities currently managed by this repository.Finds and retrieves a single entity by its unique identifier.findByOfficerNric
(String nric) Retrieves all project registrations for a specific officer.findByProjectId
(String projectId) Retrieves all officer registrations for a specific project.loadAll()
Loads all entities from the persistent storage into memory.void
save
(OfficerRegistration registration) Saves an entity to the repository.void
saveAll
(Map<String, OfficerRegistration> entities) Saves multiple entities to the repository in a batch operation.
-
Constructor Details
-
OfficerRegistrationRepository
public OfficerRegistrationRepository()Constructs a new OfficerRegistrationRepository.Initializes the CSV helper with appropriate serializers/deserializers and loads the initial data from the CSV file. If loading fails, it starts with an empty registration collection.
-
-
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.
Returns the registration with the specified ID from the in-memory cache, or null if no registration with that ID exists.
- Specified by:
findById
in interfaceIRepository<OfficerRegistration,
String> - Parameters:
registrationId
- 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 in-memory registration map to prevent external modification of the repository's internal state.
- Specified by:
findAll
in interfaceIRepository<OfficerRegistration,
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 single registration to both the in-memory cache and the CSV file. Validates that neither the registration nor its ID is null before saving.
- Specified by:
save
in interfaceIRepository<OfficerRegistration,
String> - Parameters:
registration
- The entity to save- Throws:
DataAccessException
- if there is an error writing to the CSV file
-
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 the entire in-memory registration collection with the provided map and persists all registrations to the CSV file.
- Specified by:
saveAll
in interfaceIRepository<OfficerRegistration,
String> - Parameters:
entities
- A map of entities to save, with IDs as keys and entity objects as values- Throws:
DataAccessException
- if there is an error writing to the CSV file
-
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 registrations from the CSV file, refreshing the in-memory cache. This is useful when external processes might have modified the CSV file.
- Specified by:
loadAll
in interfaceIRepository<OfficerRegistration,
String> - Returns:
- A map of all loaded entities, with IDs as keys and entity objects as values
- Throws:
DataAccessException
- if there is an error reading from the CSV file
-
findByOfficerNric
Retrieves all project registrations for a specific officer.This method finds all registration records for the specified officer, allowing the system to determine which projects an officer is associated with and their registration status for each project.
Filters the in-memory registrations to find those associated with the specified officer. Uses Java 8 Stream API for efficient filtering.
- Specified by:
findByOfficerNric
in interfaceIOfficerRegistrationRepository
- Parameters:
nric
- The NRIC of the officer whose registrations should be retrieved- Returns:
- A list of registration records for the specified officer, or an empty list if none exist
-
findByProjectId
Retrieves all officer registrations for a specific project.This method finds all registration records for the specified project, allowing the system to determine which officers are associated with the project and their registration status.
Filters the in-memory registrations to find those associated with the specified project. Uses Java 8 Stream API for efficient filtering.
- Specified by:
findByProjectId
in interfaceIOfficerRegistrationRepository
- Parameters:
projectId
- The ID of the project whose officer registrations should be retrieved- Returns:
- A list of registration records for the specified project, or an empty list if none exist
-