Class ChatDAO
- All Implemented Interfaces:
IChatDAO
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 Summary
ConstructorsConstructorDescriptionChatDAO
(SQLiteConnection sqliteConnection) Constructor forChatDAO
initializing chats table. -
Method Summary
Modifier and TypeMethodDescriptionvoid
createChat
(Chat chat) Saves a newChat
to the database.void
deleteChat
(Chat chat) Deletes aChat
from the database.getAllUserChats
(int userId) Retrieves allChat
objects for a specific user.getChat
(int id) Retrieves aChat
by its ID.void
updateChat
(Chat chat) Updates an existingChat
row in the database.void
updateChatName
(Chat chat) Updates the name of an existingChat
in the database.
-
Constructor Details
-
ChatDAO
Constructor forChatDAO
initializing chats table.- Parameters:
sqliteConnection
- theSQLiteConnection
instance for database access- Throws:
SQLException
- if a database error occurs during table creationRuntimeException
- if the SQLite connection cannot be established
-
-
Method Details
-
createChat
Saves a newChat
to the database.This method inserts a
Chat
into thechats
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 theChat
object to ensure uniqueness.- Specified by:
createChat
in interfaceIChatDAO
- Parameters:
chat
- theChat
object to save- Throws:
SQLException
- if a database error occurs during insertionIllegalArgumentException
-
updateChat
Updates an existingChat
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 interfaceIChatDAO
- Parameters:
chat
- theChat
object with updated fields- Throws:
SQLException
- if a database error occurs during update
-
updateChatName
Updates the name of an existingChat
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 interfaceIChatDAO
- Parameters:
chat
- theChat
object with the updated name- Throws:
IllegalArgumentException
- ifchat
isnull
or has an invalid nameSQLException
- if a database error occurs during update
-
deleteChat
Deletes aChat
from the database.This method removes a
Chat
identified by its ID from thechats
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 interfaceIChatDAO
- Parameters:
chat
- theChat
object to delete- Throws:
SQLException
- if a database error occurs during deletion
-
getChat
Retrieves aChat
by its ID.This method fetches a
Chat
from thechats
table using the provided ID. It returns the chat with all its attributes, ornull
if no chat exists, supporting the retrieval of user-specific chat sessions.- Specified by:
getChat
in interfaceIChatDAO
- Parameters:
id
- the unique identifier of theChat
- Returns:
- the
Chat
object if found, ornull
if no chat exists - Throws:
IllegalArgumentException
- ifid
is negativeSQLException
- if a database error occurs during retrieval
-
getAllUserChats
Retrieves allChat
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 interfaceIChatDAO
- Parameters:
userId
- the ID of the user- Returns:
- a
List
ofChat
objects for the user, or an empty list if none exist - Throws:
IllegalArgumentException
- ifuserId
is negativeSQLException
- if a database error occurs during retrieval
-