Class BookingRepository
- All Implemented Interfaces:
IBookingRepository
,IRepository<Booking,
String>
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfindAll()
Retrieves all entities currently managed by this repository.findByApplicantNric
(String nric) Finds all bookings made by a specific applicant.findByApplicationId
(String applicationId) Finds a booking by its associated application ID.Finds a booking by its unique identifier.findByProjectId
(String projectId) Finds all bookings associated with a specific project.loadAll()
Loads all entities from the persistent storage into memory.void
Saves an entity to the repository.void
Saves multiple entities to the repository in a batch operation.
-
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
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 interfaceIBookingRepository
- Specified by:
findById
in interfaceIRepository<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
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 interfaceIRepository<Booking,
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 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 interfaceIRepository<Booking,
String> - Parameters:
booking
- 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.
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 interfaceIRepository<Booking,
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 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 interfaceIRepository<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
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 interfaceIBookingRepository
- Parameters:
nric
- The NRIC of the applicant- Returns:
- A booking associated with the specified applicant, or null if not found
-
findByApplicationId
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 interfaceIBookingRepository
- Parameters:
applicationId
- The unique identifier of the application- Returns:
- The booking associated with the specified application ID, or null if not found
-
findByProjectId
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 interfaceIBookingRepository
- Parameters:
projectId
- The unique identifier of the project- Returns:
- A list of bookings for the specified project
-