Class ChatController
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 Summary
ConstructorsConstructorDescriptionChatController
(SQLiteConnection db, User authenticatedUser) Constructs a ChatController with a database connection and authenticated user. -
Method Summary
Modifier and TypeMethodDescriptioncreateNewChat
(String name, String responseAttitude, String quizDifficulty, int quizLength, String educationLevel, String studyArea) Creates a new chat with the specified parameters.createNewChatMessage
(int chatId, String content, boolean fromUser, boolean isQuiz) Creates a new message based on user input.createNewQuestionAnswerOption
(AIUtils.Option option, QuizQuestion quizQuestion) Creates a new answer option for a quiz question.createNewQuiz
(AIUtils.ModelResponseFormat response, Message responseMessage) Creates a new quiz from an AI response message.createNewQuizQuestion
(String questionContent, Quiz quiz) Creates a new quiz question for a given quiz.generateChatMessageResponse
(Message userMessage) Generates an AI response to a user message.getChat
(int chatId) Retrieves a specific chat from the database.getChatMessages
(int chatId) Retrieves all messages for a specific chat.getQuizForMessage
(int messageId) Retrieves the quiz associated with a message.Retrieves the currently selected chat from the chat list view.Retrieves all chats for the current user from the database.void
Initializes the chat screen’s UI components and event handlers.void
Refreshes the chat list view to display updated chats.void
Sends a message and retrieves an AI response.void
setQuizMode
(boolean quizMode) Sets the chat to quiz mode.void
updateChatDetails
(int chatId, String name, String responseAttitude, String quizDifficulty, String educationLevel, String studyArea) Updates the details of a specific chat (marked as redundant, handled inChatSetupController
).void
updateChatName
(int chatId, String newName) Updates the name of a specific chat in the database.validateChatExistsForCurrentUser
(int chatId) Validates a chat exists for the current user (alternative togetChat(int)
).
-
Constructor Details
-
ChatController
public ChatController(SQLiteConnection db, User authenticatedUser) throws IllegalStateException, RuntimeException, SQLException Constructs a ChatController with a database connection and authenticated user.Initializes the
db
connection, sets thecurrentUser
, and creates instances ofUserDAO
,ChatDAO
,MessageDAO
,QuizDAO
,QuizQuestionDAO
, andAnswerOptionDAO
. Throws an exception if the user is null.- Parameters:
db
- The SQLite database connectionauthenticatedUser
- The currently authenticated user- Throws:
IllegalStateException
- If the user is nullRuntimeException
- If unexpected errors occur during setupSQLException
- If database initialization fails
-
-
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 fromchatDAO
, 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
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 viachatDAO
. Note: This method is marked TODO for removal as it’s handled inChatSetupController
??.- Parameters:
name
- The inputted chat nameresponseAttitude
- The selected response attitudequizDifficulty
- The selected quiz difficultyquizLength
- The chosen quiz lengtheducationLevel
- The selected education levelstudyArea
- The inputted study area- Returns:
- The newly created
Chat
- Throws:
IllegalArgumentException
- If the chat name, response attitude, or quiz difficulty is emptySQLException
- If database operations fail
-
getUserChats
Retrieves all chats for the current user from the database.Fetches chats using
chatDAO
based oncurrentUser
’s ID.- Returns:
- A list of all chats for the current user
- Throws:
IllegalArgumentException
- If fetching chats fails
-
getChat
Retrieves a specific chat from the database.Fetches the chat using
chatDAO
and validates it belongs tocurrentUser
.- 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 userSQLException
- If database operations fail
-
validateChatExistsForCurrentUser
public Chat validateChatExistsForCurrentUser(int chatId) throws NoSuchElementException, SQLException Validates a chat exists for the current user (alternative togetChat(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 userSQLException
- 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 inChatSetupController
).Note: This method is marked TODO for removal as it’s handled in
ChatSetupController
.- Parameters:
chatId
- The ID of the chat to updatename
- The new chat nameresponseAttitude
- The new response attitudequizDifficulty
- The new quiz difficultyeducationLevel
- The new education levelstudyArea
- The new study area- Throws:
IllegalArgumentException
- If the chat name, response attitude, or quiz difficulty is emptyNoSuchElementException
- If the chat does not existSQLException
- 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 updatenewName
- The new name for the chat- Throws:
IllegalArgumentException
- If the chat name is emptyNoSuchElementException
- If the chat does not existSQLException
- 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 viamessageDAO
.- Parameters:
chatId
- The ID of the chatcontent
- The message contentfromUser
- Indicates if the message is from the userisQuiz
- Indicates if the message requests a quiz- Returns:
- The newly created
Message
- Throws:
IllegalArgumentException
- If the message content is emptyNoSuchElementException
- If the chat does not existSQLException
- 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’sMessage
.- Parameters:
userMessage
- The user’s message- Returns:
- The AI’s response
Message
- Throws:
IllegalArgumentException
- If the message is not from a userNoSuchElementException
- If the chat does not existSQLException
- If database operations fail
-
getChatMessages
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 existSQLException
- 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 associatedQuizQuestion
andAnswerOption
objects.- Parameters:
response
- The AI response formatresponseMessage
- The response message- Returns:
- The newly created
Quiz
- Throws:
IllegalArgumentException
- If the message is null, not a quiz message, from a user, or invalidNoSuchElementException
- If the chat does not existSQLException
- 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 questionquiz
- The quiz to associate with the question- Returns:
- The newly created
QuizQuestion
- Throws:
IllegalArgumentException
- If the quiz is null or question content is emptySQLException
- 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 detailsquizQuestion
- The question to associate with the option- Returns:
- The newly created
AnswerOption
- Throws:
IllegalArgumentException
- If the question is null, option letter or text is emptyIllegalStateException
- If the answer option already existsSQLException
- 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
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 failNoSuchElementException
- If the quiz does not exist
-