java.lang.Object
ai.tutor.cab302exceptionalhandlers.model.ChatDAO
All Implemented Interfaces:
IChatDAO

public class ChatDAO extends Object implements IChatDAO
Responsible for conducting database CRUD operations for chat sessions.

This Data Access Object (DAO) provides methods to perform CRUD operations on the chats table in the SQLite database. Each chat is associated with a user and stores their name and preferences such as response attitude, quiz difficulty, quizLength, educationLevel and study area.

Author:
Joshua M.
  • Constructor Details

  • Method Details

    • createChat

      public void createChat(Chat chat) throws IllegalArgumentException, SQLException
      Saves a new Chat to the database.

      This method inserts a Chat into the chats table, storing its user ID, name, response attitude, quiz difficulty, quiz length, education level, and study area. The chat ID is automatically generated by the SQLite database and set on the Chat object to ensure uniqueness.

      Specified by:
      createChat in interface IChatDAO
      Parameters:
      chat - the Chat object to save
      Throws:
      SQLException - if a database error occurs during insertion
      IllegalArgumentException
    • updateChat

      public void updateChat(Chat chat) throws SQLException
      Updates an existing Chat row in the database.

      This method updates the name, response attitude, quiz difficulty, quiz length, education level, and study area of a Chat identified by its ID. It ensures the chat’s preferences are kept up-to-date when new values are set by the user.

      Specified by:
      updateChat in interface IChatDAO
      Parameters:
      chat - the Chat object with updated fields
      Throws:
      SQLException - if a database error occurs during update
    • updateChatName

      public void updateChatName(Chat chat) throws SQLException
      Updates the name of an existing Chat in the database.

      This method modifies only the name of a Chat identified by its ID, allowing users to rename chat sessions without affecting other preferences.

      Specified by:
      updateChatName in interface IChatDAO
      Parameters:
      chat - the Chat object with the updated name
      Throws:
      IllegalArgumentException - if chat is null or has an invalid name
      SQLException - if a database error occurs during update
    • deleteChat

      public void deleteChat(Chat chat) throws SQLException
      Deletes a Chat from the database.

      This method removes a Chat identified by its ID from the chats table. Cascading deletion, configured in the database schema, ensures associated data such as quiz questions and answer options is also removed.

      Specified by:
      deleteChat in interface IChatDAO
      Parameters:
      chat - the Chat object to delete
      Throws:
      SQLException - if a database error occurs during deletion
    • getChat

      public Chat getChat(int id) throws IllegalArgumentException, SQLException
      Retrieves a Chat by its ID.

      This method fetches a Chat from the chats table using the provided ID. It returns the chat with all its attributes, or null if no chat exists, supporting the retrieval of user-specific chat sessions.

      Specified by:
      getChat in interface IChatDAO
      Parameters:
      id - the unique identifier of the Chat
      Returns:
      the Chat object if found, or null if no chat exists
      Throws:
      IllegalArgumentException - if id is negative
      SQLException - if a database error occurs during retrieval
    • getAllUserChats

      public List<Chat> getAllUserChats(int userId) throws IllegalArgumentException, SQLException
      Retrieves all Chat objects for a specific user.

      This method fetches all chats associated with a given user ID from the chats table. It returns a list of chats, which may be empty if no chats exist.

      Specified by:
      getAllUserChats in interface IChatDAO
      Parameters:
      userId - the ID of the user
      Returns:
      a List of Chat objects for the user, or an empty list if none exist
      Throws:
      IllegalArgumentException - if userId is negative
      SQLException - if a database error occurs during retrieval