Class ApplicationRepository
- All Implemented Interfaces:
IRepository<Application,
,String> IApplicationRepository
This repository manages application data, providing CRUD operations and specialized queries for application management in the BTO system. It uses a CSV file as the persistent storage mechanism, with in-memory caching for efficient access.
The repository delegates CSV file operations to a CsvRepositoryHelper and handles the conversion between Application objects and their CSV representation.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfindAll()
Retrieves all entities currently managed by this repository.findByApplicantNric
(String nric) Finds an application by the applicant's NRIC.Finds and retrieves a single entity by its unique identifier.findByProjectId
(String projectId) Finds all applications associated with a specific project.findByStatus
(ApplicationStatus status) Finds all applications with a specific status.loadAll()
Loads all entities from the persistent storage into memory.void
save
(Application application) Saves an entity to the repository.void
saveAll
(Map<String, Application> entities) Saves multiple entities to the repository in a batch operation.
-
Constructor Details
-
ApplicationRepository
public ApplicationRepository()Constructs a new ApplicationRepository.Initializes the repository with a CsvRepositoryHelper configured for Application entities and attempts to load existing application data from the CSV file. If the initial data load fails, an empty application 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 an application by its unique identifier from the in-memory cache. Returns null if no application exists with the specified ID.
- Specified by:
findById
in interfaceIRepository<Application,
String> - Parameters:
applicationId
- 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 applications map to prevent external modification of the repository's internal state.
- Specified by:
findAll
in interfaceIRepository<Application,
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 an application to both the in-memory cache and the CSV file. If the application or its ID is null, the method logs an error and returns without saving.
The method updates the in-memory cache first, then delegates the persistence to the CSV helper. If saving fails, the exception is logged and rethrown.
- Specified by:
save
in interfaceIRepository<Application,
String> - Parameters:
application
- 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.
Updates the in-memory application collection with the provided map and persists all applications to the CSV file. Creates a defensive copy of the provided map to maintain repository encapsulation.
If saving fails, the exception is logged and rethrown, but the in-memory state will already reflect the new entities.
- Specified by:
saveAll
in interfaceIRepository<Application,
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 application data from the CSV file into the in-memory cache, replacing any existing data. Returns a defensive copy of the loaded applications.
- Specified by:
loadAll
in interfaceIRepository<Application,
String> - Returns:
- A map of all loaded entities, with IDs as keys and entity objects as values
- Throws:
DataAccessException
- If an error occurs during data loading or parsing
-
findByApplicantNric
Finds an application by the applicant's NRIC.Searches for an application submitted by the specified applicant. This method assumes an applicant can have only one active application in the system at a time, returning the first match found or null if no application exists for the applicant.
The search is performed on the in-memory cache for optimal performance.
- Specified by:
findByApplicantNric
in interfaceIApplicationRepository
- Parameters:
nric
- The NRIC of the applicant to search for- Returns:
- The application associated with the specified applicant, or null if not found
-
findByProjectId
Finds all applications associated with a specific project.Retrieves all applications for a specific project. This method filters the in-memory application collection to find all applications associated with the given project ID, which is useful for project management and selection processes.
The method uses Java 8 Stream API for efficient filtering.
- Specified by:
findByProjectId
in interfaceIApplicationRepository
- Parameters:
projectId
- The ID of the project to filter by- Returns:
- A list of applications for the specified project, or an empty list if none exist
-
findByStatus
Finds all applications with a specific status.Retrieves all applications with a specific status. This method filters the in-memory application collection to find all applications with the given status (e.g., PENDING, APPROVED, REJECTED), which is useful for batch processing and status reporting.
The method uses Java 8 Stream API for efficient filtering.
- Specified by:
findByStatus
in interfaceIApplicationRepository
- Parameters:
status
- The application status to filter by- Returns:
- A list of applications with the specified status, or an empty list if none exist
-