Class SqliteDAO

java.lang.Object
com.example.cab302project.SqliteDAO
All Implemented Interfaces:
IAppDAO

public class SqliteDAO extends Object implements IAppDAO
SQLite-based implementation of the IAppDAO interface. This class handles all persistent data storage for the application using an embedded SQLite database. It manages table creation, data mapping, and CRUD operations for users and crime records.

The database connection is managed via the SqliteConnection singleton.

  • Constructor Details

    • SqliteDAO

      public SqliteDAO()
      Initializes the SQLite DAO. Establishes a connection to the database and ensures that the required 'users' and 'crimes' tables exist.
  • Method Details

    • validateUser

      public User validateUser(String username, String plaintextPassword)
      Validates a user's credentials against the data store. Typically used during the login process to verify identity.
      Specified by:
      validateUser in interface IAppDAO
      Parameters:
      username - The unique username to validate.
      plaintextPassword - The password provided for validation.
      Returns:
      A User object if credentials are correct; null if validation fails.
    • addUser

      public boolean addUser(User user)
      Persists a new user in the data store.
      Specified by:
      addUser in interface IAppDAO
      Parameters:
      user - The User object containing the account details to be saved.
      Returns:
      true if the user was successfully added; false if the username is taken or an error occurred.
    • updateUser

      public boolean updateUser(User user)
      Updates an existing user's information in the data store.
      Specified by:
      updateUser in interface IAppDAO
      Parameters:
      user - The User object containing the updated profile data.
      Returns:
      true if the update was successful; false otherwise.
    • addCrime

      public boolean addCrime(CrimeRecord crime)
      Records a new crime incident in the data store.
      Specified by:
      addCrime in interface IAppDAO
      Parameters:
      crime - The CrimeRecord object containing the incident details.
      Returns:
      true if the record was successfully created; false if an error occurred.
    • updateCrime

      public boolean updateCrime(CrimeRecord crime)
      Updates the details of an existing crime record.
      Specified by:
      updateCrime in interface IAppDAO
      Parameters:
      crime - The CrimeRecord object containing the updated incident data.
      Returns:
      true if the record was found and updated successfully; false otherwise.
    • deleteCrime

      public boolean deleteCrime(int id)
      Removes a crime record from the data store based on its unique identifier.
      Specified by:
      deleteCrime in interface IAppDAO
      Parameters:
      id - The unique primary key of the crime record to be removed.
      Returns:
      true if the record was deleted; false if the ID was not found or an error occurred.
    • getCrimeById

      public CrimeRecord getCrimeById(int id)
      Retrieves a specific crime record by its unique identifier.
      Specified by:
      getCrimeById in interface IAppDAO
      Parameters:
      id - The unique primary key of the desired crime.
      Returns:
      The CrimeRecord matching the ID, or null if no such record exists.
    • getCrimesByUser

      public List<CrimeRecord> getCrimesByUser(String username)
      Retrieves all crime incidents reported by a specific user.
      Specified by:
      getCrimesByUser in interface IAppDAO
      Parameters:
      username - The username of the reporter to filter by.
      Returns:
      A List of CrimeRecord objects; returns an empty list if the user has no reports.
    • getAllCrimes

      public List<CrimeRecord> getAllCrimes()
      Retrieves every crime record currently stored in the system.
      Specified by:
      getAllCrimes in interface IAppDAO
      Returns:
      A List containing all CrimeRecord objects in the data store.