Class BookingRepository

java.lang.Object
com.ntu.fdae.group1.bto.repository.booking.BookingRepository
All Implemented Interfaces:
IBookingRepository, IRepository<Booking,String>

public class BookingRepository extends Object implements IBookingRepository
Implementation of the IBookingRepository interface that persists Booking entities to a CSV file.

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

The repository maintains thread safety for its internal state and handles serialization and deserialization of booking data to and from the CSV format.

  • Constructor Details

    • BookingRepository

      public BookingRepository()
      Constructs a new BookingRepository.

      Initializes the repository with a CsvRepositoryHelper configured for Booking entities and attempts to load existing booking data from the CSV file. If the initial data load fails, an empty booking collection is created.

  • Method Details

    • findById

      public Booking findById(String bookingId)
      Finds a booking by its unique identifier.

      Retrieves a booking by its unique identifier from the in-memory cache. Returns null if no booking exists with the specified ID.

      Specified by:
      findById in interface IBookingRepository
      Specified by:
      findById in interface IRepository<Booking,String>
      Parameters:
      bookingId - The unique identifier of the booking to retrieve
      Returns:
      The booking with the specified ID, or null if not found
    • findAll

      public Map<String,Booking> 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 bookings map to prevent external modification of the repository's internal state.

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

      public void save(Booking booking)
      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 booking to both the in-memory cache and the CSV file. Validates that the booking and its ID are not null before saving. The method will terminate early without throwing an exception if validation fails, but will throw any DataAccessExceptions from the underlying storage mechanism.

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

      public void saveAll(Map<String,Booking> 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 all existing bookings with the provided collection and persists them to the CSV file. Creates a defensive copy of the provided map to maintain repository encapsulation.

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

      public Map<String,Booking> 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.

      Reloads all booking data from the CSV file into the in-memory cache, replacing any existing data. Returns a defensive copy of the loaded bookings.

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

      public Booking findByApplicantNric(String nric)
      Finds all bookings made by a specific applicant.

      Searches the in-memory booking collection for a booking with the specified applicant NRIC. Returns the first matching booking or null if none is found.

      Specified by:
      findByApplicantNric in interface IBookingRepository
      Parameters:
      nric - The NRIC of the applicant
      Returns:
      A booking associated with the specified applicant, or null if not found
    • findByApplicationId

      public Booking findByApplicationId(String applicationId)
      Finds a booking by its associated application ID.

      Searches the in-memory booking collection for a booking with the specified application ID. Returns the first matching booking or null if none is found.

      Specified by:
      findByApplicationId in interface IBookingRepository
      Parameters:
      applicationId - The unique identifier of the application
      Returns:
      The booking associated with the specified application ID, or null if not found
    • findByProjectId

      public List<Booking> findByProjectId(String projectId)
      Finds all bookings associated with a specific project.

      Searches the in-memory booking collection for bookings associated with the specified project ID. Returns a list of matching bookings.

      Specified by:
      findByProjectId in interface IBookingRepository
      Parameters:
      projectId - The unique identifier of the project
      Returns:
      A list of bookings for the specified project