Class ProjectRepository

java.lang.Object
com.ntu.fdae.group1.bto.repository.project.ProjectRepository
All Implemented Interfaces:
IRepository<Project,String>, IProjectRepository

public class ProjectRepository extends Object implements IProjectRepository
Repository implementation for managing Project entities in the BTO Management System.

This class handles the persistence and retrieval of Project records using two CSV files as the backing store - one for project metadata and one for flat type information. It provides methods for finding projects, adding/removing projects, and managing associated flat information.

The repository maintains the relationship between Project objects and their associated ProjectFlatInfo objects, ensuring that changes to either are properly synchronized and persisted.

  • Constructor Details

    • ProjectRepository

      public ProjectRepository()
      Constructs a new ProjectRepository.

      Initializes the CSV helper with appropriate serializers/deserializers and loads the initial project data from both CSV files. The repository maintains an in-memory cache of projects which includes their associated flat information.

      If loading fails, the repository starts with an empty project collection.

  • Method Details

    • findById

      public Project findById(String projectId)
      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 project with the specified ID from the in-memory cache, or null if no project with that ID exists.

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

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

      public void save(Project project)
      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 project and its associated flat information to both the in-memory cache and the CSV files. The method validates that neither the project nor its ID is null before saving.

      This method delegates to the CSV helper which in turn calls serializeProjectsAndFlatInfo to handle saving data to both the project file and the flat info file.

      Specified by:
      save in interface IRepository<Project,String>
      Parameters:
      project - The entity to save
      Throws:
      DataAccessException - if there is an error writing to either CSV file
    • saveAll

      public void saveAll(Map<String,Project> 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 project collection with the provided map and persists all projects and their flat information to the CSV files.

      This operation is atomic - either all projects are saved successfully, or an exception is thrown and no changes are made to the files.

      Specified by:
      saveAll in interface IRepository<Project,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 either CSV file
    • loadAll

      public Map<String,Project> 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 projects and their associated flat information from the CSV files into the in-memory cache. Returns a defensive copy of the loaded projects.

      Specified by:
      loadAll in interface IRepository<Project,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 either CSV file
    • findAllFlatInfoIds

      public Set<String> findAllFlatInfoIds() throws DataAccessException
      Retrieves all unique flat information IDs associated with projects across the system. This method collects the IDs of all flat information objects from all projects in the repository. It's particularly useful for:
      • Generating unique IDs for new flat information entities
      • Validating the existence of flat information references
      • Tracking all flat types across projects

      Returns a set of all flat info IDs that have been loaded from the CSV file. If the projects and flat info IDs are not already loaded, this method will trigger a load operation.

      Specified by:
      findAllFlatInfoIds in interface IProjectRepository
      Returns:
      A set of unique flat information IDs across all projects
      Throws:
      DataAccessException - if there is an error reading from either CSV file
    • deleteById

      public void deleteById(String id) throws DataAccessException
      Deletes the Project with the specified ID. If the ID does not exist, the method might do nothing or throw an exception,

      Deletes the project with the specified ID from the in-memory cache and persists the change to the CSV files. If the project ID is null or empty, the method logs a warning and returns without making any changes.

      Specified by:
      deleteById in interface IProjectRepository
      Parameters:
      id - The ID of the entity to delete.
      Throws:
      DataAccessException - if there is an error writing to either CSV file