Class EnquiryRepository

java.lang.Object
com.ntu.fdae.group1.bto.repository.enquiry.EnquiryRepository
All Implemented Interfaces:
IEnquiryRepository, IRepository<Enquiry,String>

public class EnquiryRepository extends Object implements IEnquiryRepository
Implementation of the IEnquiryRepository interface that persists Enquiry entities to a CSV file.

This repository manages enquiry data, providing CRUD operations and specialized queries for enquiry management in the BTO system. It uses a CSV file as the persistent storage mechanism, with in-memory caching for efficient access.

The repository handles serialization and deserialization of enquiry data to and from the CSV format, including conversion of complex fields like timestamps.

  • Constructor Details

    • EnquiryRepository

      public EnquiryRepository()
      Constructs a new EnquiryRepository.

      Initializes the repository and loads existing enquiry data from the CSV file. If the initial data load fails, an empty enquiry collection is created.

  • Method Details

    • findById

      public Enquiry findById(String enquiryId)
      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 enquiry by its unique identifier from the in-memory cache. Returns null if no enquiry exists with the specified ID.

      Specified by:
      findById in interface IRepository<Enquiry,String>
      Parameters:
      enquiryId - 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,Enquiry> 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 enquiries map to prevent external modification of the repository's internal state.

      Specified by:
      findAll in interface IRepository<Enquiry,String>
      Returns:
      A map containing all entities, with IDs as keys and entity objects as values
    • save

      public void save(Enquiry enquiry)
      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 enquiry to both the in-memory cache and the CSV file. If the enquiry or its ID is null, the method logs an error and returns without saving.

      Specified by:
      save in interface IRepository<Enquiry,String>
      Parameters:
      enquiry - The entity to save
    • saveAll

      public void saveAll(Map<String,Enquiry> 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.

      Updates the in-memory enquiry collection with the provided map and persists all enquiries to the CSV file.

      Specified by:
      saveAll in interface IRepository<Enquiry,String>
      Parameters:
      entities - A map of entities to save, with IDs as keys and entity objects as values
    • loadAll

      public Map<String,Enquiry> loadAll() throws DataAccessException
      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.

      Loads all enquiry data from the CSV file into the in-memory cache, replacing any existing data. Returns a defensive copy of the loaded enquiries.

      Specified by:
      loadAll in interface IRepository<Enquiry,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
    • findByUserNric

      public List<Enquiry> findByUserNric(String nric)
      Retrieves all enquiries submitted by a specific user.

      This method finds all enquiry records associated with the specified user NRIC, allowing retrieval of a user's complete enquiry history.

      Retrieves all enquiries submitted by the user with the specified NRIC. Returns an empty list if no matching enquiries are found.

      Specified by:
      findByUserNric in interface IEnquiryRepository
      Parameters:
      nric - The NRIC of the user whose enquiries should be retrieved
      Returns:
      A list of all enquiries submitted by the specified user
    • findByProjectId

      public List<Enquiry> findByProjectId(String projectId)
      Retrieves all enquiries related to a specific project.

      This method finds all enquiry records associated with the specified project, which is useful for project management and responding to project-specific queries.

      Retrieves all enquiries related to the specified project. Returns an empty list if no matching enquiries are found or if projectId is null.

      Specified by:
      findByProjectId in interface IEnquiryRepository
      Parameters:
      projectId - The ID of the project whose enquiries should be retrieved
      Returns:
      A list of all enquiries related to the specified project
    • deleteById

      public void deleteById(String enquiryId) throws DataAccessException
      Deletes the entity with the specified ID. If the ID does not exist, the method might do nothing or throw an exception, depending on implementation preference.

      Removes the enquiry with the specified ID from both the in-memory cache and the CSV file. If no enquiry exists with the specified ID, no action is taken.

      Specified by:
      deleteById in interface IEnquiryRepository
      Parameters:
      enquiryId - The ID of the entity to delete.
      Throws:
      DataAccessException - If an error occurs during persistence.