Class OfficerRegistrationRepository

java.lang.Object
com.ntu.fdae.group1.bto.repository.project.OfficerRegistrationRepository
All Implemented Interfaces:
IRepository<OfficerRegistration,String>, IOfficerRegistrationRepository

public class OfficerRegistrationRepository extends Object implements IOfficerRegistrationRepository
Repository implementation for managing OfficerRegistration entities in the BTO Management System.

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 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

      public OfficerRegistration findById(String registrationId)
      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 interface IRepository<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

      public Map<String,OfficerRegistration> 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 interface IRepository<OfficerRegistration,String>
      Returns:
      A map containing all entities, with IDs as keys and entity objects as values
    • save

      public void save(OfficerRegistration registration)
      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 interface IRepository<OfficerRegistration,String>
      Parameters:
      registration - The entity to save
      Throws:
      DataAccessException - if there is an error writing to the CSV file
    • saveAll

      public void saveAll(Map<String,OfficerRegistration> entities)
      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 interface IRepository<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 interface IRepository<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

      public List<OfficerRegistration> findByOfficerNric(String nric)
      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 interface IOfficerRegistrationRepository
      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

      public List<OfficerRegistration> findByProjectId(String projectId)
      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 interface IOfficerRegistrationRepository
      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