Package server
Class DatabaseHandler
java.lang.Object
server.DatabaseHandler
- All Implemented Interfaces:
ChatDatabase
Handles all database operations for the chat server.
This class manages the connection to a local SQLite database and provides methods for creating tables, inserting and retrieving users, chats, and messages.
The database schema consists of four tables:
- users – Stores registered usernames.
- chats – Stores available chat rooms.
- messages – Stores messages sent in chats.
- chat_users – Maps users to the chats they participate in.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddMessage(String chatName, client.Message message) Inserts a message into the database.voidaddUserToChat(client.User user, String chatname) Adds a user to a specific chat.voidCloses the active database connection.getAllChats(client.User user) Retrieves the names of all chats that a user participates in.client.ChatRetrieves a complete Chat object from the database.ArrayList<client.Message> getMessages(String chatname, Instant time) Retrieves all messages from a specific chat that were sent after a given timestamp.static voidvoidremoveUserFromChat(client.User user, String chatName) Removes a user from a specific chat.
-
Constructor Details
-
DatabaseHandler
public DatabaseHandler()
-
-
Method Details
-
closeConnection
public void closeConnection()Closes the active database connection.Should be called when the server shuts down to release resources.
- Specified by:
closeConnectionin interfaceChatDatabase
-
removeUserFromChat
Removes a user from a specific chat.- Specified by:
removeUserFromChatin interfaceChatDatabase- Parameters:
user- the user to removechatName- the name of the chat
-
addUserToChat
Adds a user to a specific chat.If the user is already a member of the chat, the operation is ignored.
- Specified by:
addUserToChatin interfaceChatDatabase- Parameters:
user- the user to addchatname- the name of the chat
-
addMessage
Inserts a message into the database.- Specified by:
addMessagein interfaceChatDatabase- Parameters:
chatName- the name of the chat where the message was sentmessage- the message object containing content, sender, timestamp, and image flag
-
getMessages
Retrieves all messages from a specific chat that were sent after a given timestamp.- Specified by:
getMessagesin interfaceChatDatabase- Parameters:
chatname- the name of the chattime- only messages sent after this time are returned- Returns:
- a list of messages ordered by timestamp (ascending)
-
getChat
Retrieves a complete Chat object from the database.The returned Chat includes:
- All users in the chat
- All messages in the chat
- Specified by:
getChatin interfaceChatDatabase- Parameters:
chatname- the name of the chat- Returns:
- a fully populated Chat object
-
getAllChats
Retrieves the names of all chats that a user participates in.- Specified by:
getAllChatsin interfaceChatDatabase- Parameters:
user- the user whose chats should be retrieved- Returns:
- a list of chat names
-
main
-