java.lang.Object
ai.tutor.cab302exceptionalhandlers.controller.ChatController

public class ChatController extends Object
Controls the main chat screen in the AI tutor application.

Manages the chat interface, allowing users to select chats, send/receive messages, generate quizzes, and navigate to other screens (e.g., chat setup, user settings, login). Integrates with the database via ChatDAO, MessageDAO, QuizDAO, QuizQuestionDAO, and AnswerOptionDAO, and uses AIUtils for AI-generated responses.

See Also:
  • Constructor Details

  • Method Details

    • initialize

      public void initialize()
      Initializes the chat screen’s UI components and event handlers.

      Sets up chat selection listener (setupChatSelectionListener()), chat list view (setupChatListView()), refreshes the chat list (refreshChatListView()), and configures buttons for editing chat names (setupEditChatNameButton()), activating edits (setupActivateEdit()), sending messages (setupMessageSendActions()), expanding the message input (setupExpandingMessageInput()), creating chats (setupCreateChatButton()), chat settings (setupChatSettingsButton()), toggling chat mode (setupToggleChatMode()), toggling quiz mode (setupToggleQuizMode()), logout (setupLogoutButton()), and user details (setupUserDetailsButton()). Called automatically by JavaFX when the FXML is loaded.

    • refreshChatListView

      public void refreshChatListView()
      Refreshes the chat list view to display updated chats.

      Clears and repopulates chatsListView with chats from chatDAO, preserving the selected chat if it exists.

    • SendAndReceiveMessage

      public void SendAndReceiveMessage()
      Sends a message and retrieves an AI response.

      Validates AI availability, chat selection, and message content, creates a user message, and triggers an asynchronous task to generate an AI response.

    • getSelectedChat

      public Chat getSelectedChat()
      Retrieves the currently selected chat from the chat list view.

      Returns the selected item from chatsListView.

      Returns:
      The selected Chat or null if none is selected
    • createNewChat

      public Chat createNewChat(String name, String responseAttitude, String quizDifficulty, int quizLength, String educationLevel, String studyArea) throws IllegalArgumentException, SQLException
      Creates a new chat with the specified parameters.

      Validates inputs and creates a Chat object, saving it to the database via chatDAO. Note: This method is marked TODO for removal as it’s handled in ChatSetupController ??.

      Parameters:
      name - The inputted chat name
      responseAttitude - The selected response attitude
      quizDifficulty - The selected quiz difficulty
      quizLength - The chosen quiz length
      educationLevel - The selected education level
      studyArea - The inputted study area
      Returns:
      The newly created Chat
      Throws:
      IllegalArgumentException - If the chat name, response attitude, or quiz difficulty is empty
      SQLException - If database operations fail
    • getUserChats

      public List<Chat> getUserChats()
      Retrieves all chats for the current user from the database.

      Fetches chats using chatDAO based on currentUser’s ID.

      Returns:
      A list of all chats for the current user
      Throws:
      IllegalArgumentException - If fetching chats fails
    • getChat

      public Chat getChat(int chatId) throws NoSuchElementException, SQLException
      Retrieves a specific chat from the database.

      Fetches the chat using chatDAO and validates it belongs to currentUser.

      Parameters:
      chatId - The ID of the chat to retrieve
      Returns:
      The requested Chat
      Throws:
      NoSuchElementException - If the chat does not exist or doesn’t belong to the user
      SQLException - If database operations fail
    • validateChatExistsForCurrentUser

      public Chat validateChatExistsForCurrentUser(int chatId) throws NoSuchElementException, SQLException
      Validates a chat exists for the current user (alternative to getChat(int)).

      Provides clarity and consistency by reusing getChat(int).

      Parameters:
      chatId - The ID of the chat to validate
      Returns:
      The requested Chat
      Throws:
      NoSuchElementException - If the chat does not exist or doesn’t belong to the user
      SQLException - If database operations fail
    • updateChatDetails

      public void updateChatDetails(int chatId, String name, String responseAttitude, String quizDifficulty, String educationLevel, String studyArea) throws IllegalArgumentException, NoSuchElementException, SQLException
      Updates the details of a specific chat (marked as redundant, handled in ChatSetupController).

      Note: This method is marked TODO for removal as it’s handled in ChatSetupController.

      Parameters:
      chatId - The ID of the chat to update
      name - The new chat name
      responseAttitude - The new response attitude
      quizDifficulty - The new quiz difficulty
      educationLevel - The new education level
      studyArea - The new study area
      Throws:
      IllegalArgumentException - If the chat name, response attitude, or quiz difficulty is empty
      NoSuchElementException - If the chat does not exist
      SQLException - If database operations fail
    • updateChatName

      public void updateChatName(int chatId, String newName) throws IllegalArgumentException, NoSuchElementException, SQLException
      Updates the name of a specific chat in the database.

      Validates the new name and updates the chat using chatDAO.

      Parameters:
      chatId - The ID of the chat to update
      newName - The new name for the chat
      Throws:
      IllegalArgumentException - If the chat name is empty
      NoSuchElementException - If the chat does not exist
      SQLException - If database operations fail
    • createNewChatMessage

      public Message createNewChatMessage(int chatId, String content, boolean fromUser, boolean isQuiz) throws NoSuchElementException, SQLException
      Creates a new message based on user input.

      Validates the content and creates a Message object, saving it to the database via messageDAO.

      Parameters:
      chatId - The ID of the chat
      content - The message content
      fromUser - Indicates if the message is from the user
      isQuiz - Indicates if the message requests a quiz
      Returns:
      The newly created Message
      Throws:
      IllegalArgumentException - If the message content is empty
      NoSuchElementException - If the chat does not exist
      SQLException - If database operations fail
    • generateChatMessageResponse

      public Message generateChatMessageResponse(Message userMessage) throws IllegalArgumentException, NoSuchElementException, SQLException
      Generates an AI response to a user message.

      Validates the message is from a user, generates a response using generateAIResponse(Message), and returns the AI’s Message.

      Parameters:
      userMessage - The user’s message
      Returns:
      The AI’s response Message
      Throws:
      IllegalArgumentException - If the message is not from a user
      NoSuchElementException - If the chat does not exist
      SQLException - If database operations fail
    • getChatMessages

      public List<Message> getChatMessages(int chatId) throws NoSuchElementException, SQLException
      Retrieves all messages for a specific chat.

      Validates the chat exists and fetches messages using messageDAO.

      Parameters:
      chatId - The ID of the chat
      Returns:
      A list of all messages for the chat
      Throws:
      NoSuchElementException - If the chat does not exist
      SQLException - If database operations fail
    • createNewQuiz

      public Quiz createNewQuiz(AIUtils.ModelResponseFormat response, Message responseMessage) throws IllegalArgumentException, NoSuchElementException, SQLException
      Creates a new quiz from an AI response message.

      Validates the response and creates a Quiz with associated QuizQuestion and AnswerOption objects.

      Parameters:
      response - The AI response format
      responseMessage - The response message
      Returns:
      The newly created Quiz
      Throws:
      IllegalArgumentException - If the message is null, not a quiz message, from a user, or invalid
      NoSuchElementException - If the chat does not exist
      SQLException - If database operations fail
    • createNewQuizQuestion

      public QuizQuestion createNewQuizQuestion(String questionContent, Quiz quiz) throws IllegalArgumentException, SQLException
      Creates a new quiz question for a given quiz.

      Validates the quiz and question content, creating a QuizQuestion object.

      Parameters:
      questionContent - The content of the question
      quiz - The quiz to associate with the question
      Returns:
      The newly created QuizQuestion
      Throws:
      IllegalArgumentException - If the quiz is null or question content is empty
      SQLException - If database operations fail
    • createNewQuestionAnswerOption

      public AnswerOption createNewQuestionAnswerOption(AIUtils.Option option, QuizQuestion quizQuestion) throws IllegalStateException, IllegalStateException, IllegalArgumentException, SQLException
      Creates a new answer option for a quiz question.

      Validates the option and creates an AnswerOption object.

      Parameters:
      option - The option details
      quizQuestion - The question to associate with the option
      Returns:
      The newly created AnswerOption
      Throws:
      IllegalArgumentException - If the question is null, option letter or text is empty
      IllegalStateException - If the answer option already exists
      SQLException - If database operations fail
    • setQuizMode

      public void setQuizMode(boolean quizMode)
      Sets the chat to quiz mode.

      Updates isQuiz to enable or disable quiz generation.

      Parameters:
      quizMode - Whether to enable quiz mode
    • getQuizForMessage

      public Quiz getQuizForMessage(int messageId) throws SQLException, NoSuchElementException
      Retrieves the quiz associated with a message.

      Fetches the quiz from quizDAO based on the message ID.

      Parameters:
      messageId - The ID of the message
      Returns:
      The associated Quiz or null if none exists
      Throws:
      SQLException - If database operations fail
      NoSuchElementException - If the quiz does not exist