Class TaskAssignmentRepository

java.lang.Object
no.ntnu.principes.repository.AbstractRepository<TaskAssignment,Long>
no.ntnu.principes.repository.TaskAssignmentRepository
All Implemented Interfaces:
BaseRepository<TaskAssignment,Long>

public class TaskAssignmentRepository extends AbstractRepository<TaskAssignment,Long>
Repository for managing the assignment of tasks to household members. Provides methods for tracking task assignments, due dates, completion status, and querying assignments by various criteria.
  • Constructor Details

    • TaskAssignmentRepository

      public TaskAssignmentRepository(DataSource dataSource)
      Creates a new TaskAssignmentRepository with the specified data source.
      Parameters:
      dataSource - The JDBC data source for database connections
  • Method Details

    • findById

      public Optional<TaskAssignment> findById(Long id)
      Finds a task assignment by its ID.
      Parameters:
      id - The task assignment ID
      Returns:
      An Optional containing the found task assignment or empty if not found
    • findAll

      public List<TaskAssignment> findAll()
      Retrieves all task assignments.
      Returns:
      A list of all task assignments
    • save

      public TaskAssignment save(TaskAssignment assignment)
      Saves a task assignment, creating a new record or updating an existing one. Handles date formatting for database storage.
      Parameters:
      assignment - The task assignment to save
      Returns:
      The saved task assignment with ID populated
    • deleteById

      public void deleteById(Long id)
      Deletes a task assignment by its ID.
      Parameters:
      id - The ID of the task assignment to delete
    • mapRow

      protected TaskAssignment mapRow(ResultSet rs) throws SQLException
      Maps a database result set row to a TaskAssignment object. Handles nullable date fields.
      Specified by:
      mapRow in class AbstractRepository<TaskAssignment,Long>
      Parameters:
      rs - The result set positioned at the row to map
      Returns:
      The mapped TaskAssignment object
      Throws:
      SQLException - If a database error occurs during mapping
    • findByMemberId

      public List<TaskAssignment> findByMemberId(Long memberId)
      Finds all task assignments for a specific member, ordered by due date.
      Parameters:
      memberId - The ID of the member to find assignments for
      Returns:
      A list of the member's task assignments
    • findByMemberIdAndStatus

      public List<TaskAssignment> findByMemberIdAndStatus(Long memberId, TaskStatus status)
      Finds task assignments for a member with a specific status, ordered by due date.
      Parameters:
      memberId - The ID of the member to find assignments for
      status - The status to filter by (e.g., TODO, DONE)
      Returns:
      A list of matching task assignments
    • findByStatus

      public List<TaskAssignment> findByStatus(TaskStatus status)
      Finds all task assignments with a specific status.
      Parameters:
      status - The status to filter by (e.g., TODO, DONE)
      Returns:
      A list of matching task assignments
    • findPendingByTaskId

      public List<TaskAssignment> findPendingByTaskId(Long taskId)
      Finds pending task assignments for a specific task.
      Parameters:
      taskId - The ID of the task to find pending assignments for
      Returns:
      A list of pending (TODO status) task assignments
    • findByTaskIds

      public List<TaskAssignment> findByTaskIds(List<Long> taskIds)
      Finds task assignments for multiple tasks.
      Parameters:
      taskIds - A list of task IDs to find assignments for
      Returns:
      A list of matching task assignments, or all assignments if taskIds is empty
    • getCompletionRateForMember

      public double getCompletionRateForMember(Long memberId)
      Calculates the completion rate for a member's task assignments. The rate is the number of completed tasks divided by the total number of tasks. Returns 0.0 if the member has no tasks or if an error occurs.
      Parameters:
      memberId - The ID of the member to calculate the completion rate for
      Returns:
      The completion rate as a value between 0.0 and 1.0
    • findByTaskAndMember

      public Optional<TaskAssignment> findByTaskAndMember(Long taskId, Long memberId)
      Finds a task assignment by both task and member IDs.
      Parameters:
      taskId - The ID of the task
      memberId - The ID of the member
      Returns:
      An Optional containing the matching assignment or empty if not found
    • findByUnassignedTask

      public List<TaskAssignment> findByUnassignedTask(Long taskId)
      Finds unassigned task assignments for a specific task.
      Parameters:
      taskId - The ID of the task to find unassigned assignments for
      Returns:
      A list of task assignments with null member IDs
    • findLastCompletedAssignment

      public Optional<TaskAssignment> findLastCompletedAssignment(Long id)
      Finds the most recently completed assignment for a task.
      Parameters:
      id - The ID of the task
      Returns:
      An Optional containing the most recently completed assignment or empty if none
    • findImmediateTasksForMember

      public List<TaskAssignment> findImmediateTasksForMember(Long memberId)
      Finds immediate tasks for a member (due within 2 days or with no due date). Results are ordered by status, completion date, and due date.
      Parameters:
      memberId - The ID of the member to find immediate tasks for
      Returns:
      A list of immediate task assignments
    • deleteByMemberId

      public int deleteByMemberId(Long memberId)
      Deletes all task assignments for a specific member.
      Parameters:
      memberId - The ID of the member whose assignments should be deleted
      Returns:
      The number of assignments deleted
    • deleteByTaskId

      public int deleteByTaskId(Long taskId)
      Deletes all assignments for a specific task.
      Parameters:
      taskId - The ID of the task whose assignments should be deleted
      Returns:
      The number of assignments deleted
    • findByMemberIdAndDueAt

      public List<TaskAssignment> findByMemberIdAndDueAt(Long memberId, LocalDate dueDate)
      Finds task assignments for a specific member with a specific due date.
      Parameters:
      memberId - The ID of the member to find assignments for
      dueDate - The due date to filter by
      Returns:
      A list of matching task assignments