Package server

Class DatabaseHandler

java.lang.Object
server.DatabaseHandler
All Implemented Interfaces:
ChatDatabase

public class DatabaseHandler extends Object implements 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.

Foreign key constraints are enabled to maintain referential integrity.
  • 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:
      closeConnection in interface ChatDatabase
    • removeUserFromChat

      public void removeUserFromChat(client.User user, String chatName)
      Removes a user from a specific chat.
      Specified by:
      removeUserFromChat in interface ChatDatabase
      Parameters:
      user - the user to remove
      chatName - the name of the chat
    • addUserToChat

      public void addUserToChat(client.User user, String chatname)
      Adds a user to a specific chat.

      If the user is already a member of the chat, the operation is ignored.

      Specified by:
      addUserToChat in interface ChatDatabase
      Parameters:
      user - the user to add
      chatname - the name of the chat
    • addMessage

      public void addMessage(String chatName, client.Message message)
      Inserts a message into the database.
      Specified by:
      addMessage in interface ChatDatabase
      Parameters:
      chatName - the name of the chat where the message was sent
      message - the message object containing content, sender, timestamp, and image flag
    • getMessages

      public ArrayList<client.Message> getMessages(String chatname, Instant time)
      Retrieves all messages from a specific chat that were sent after a given timestamp.
      Specified by:
      getMessages in interface ChatDatabase
      Parameters:
      chatname - the name of the chat
      time - only messages sent after this time are returned
      Returns:
      a list of messages ordered by timestamp (ascending)
    • getChat

      public client.Chat getChat(String chatname)
      Retrieves a complete Chat object from the database.

      The returned Chat includes:

      • All users in the chat
      • All messages in the chat

      Specified by:
      getChat in interface ChatDatabase
      Parameters:
      chatname - the name of the chat
      Returns:
      a fully populated Chat object
    • getAllChats

      public ArrayList<String> getAllChats(client.User user)
      Retrieves the names of all chats that a user participates in.
      Specified by:
      getAllChats in interface ChatDatabase
      Parameters:
      user - the user whose chats should be retrieved
      Returns:
      a list of chat names
    • main

      public static void main(String[] args)