Class SqliteWorldRepository

java.lang.Object
za.co.wethinkcode.robots.server.persistence.SqliteWorldRepository
All Implemented Interfaces:
WorldRepository

public class SqliteWorldRepository extends Object implements WorldRepository
SQLite implementation of the WorldRepository. Acts as a facade orchestrating transactions across three separate DAOs (SqliteSnapshotDao, SqliteObjectDao, SqliteRobotDao). This ensures strict separation of concerns and adheres to Single Responsibility OOP principles. Opens a new database connection for each operation and ensures foreign keys are explicitly enabled for ON DELETE CASCADE features to function.
  • Constructor Details

    • SqliteWorldRepository

      public SqliteWorldRepository(String databaseFilePath)
      Constructs a new repository and ensures the v1 database schema is created.
      Parameters:
      databaseFilePath - path to the SQLite database file (created if it doesn't exist)
      Throws:
      IllegalStateException - if the database schema fails to initialize
  • Method Details

    • save

      public void save(SavedWorld world) throws SQLException
      Saves a full snapshot of the world into the database as a single transaction. Overwrites any existing snapshot that shares the same name.
      Specified by:
      save in interface WorldRepository
      Parameters:
      world - the world snapshot containing metadata, objects, and robots
      Throws:
      SQLException - if any part of the save transaction fails
    • restore

      public Optional<SavedWorld> restore(String name) throws SQLException
      Restores a SavedWorld by looking up its most recent snapshot by name. Reconstructs the world along with its associated objects and robots.
      Specified by:
      restore in interface WorldRepository
      Parameters:
      name - the unique name of the world to restore
      Returns:
      an Optional containing the SavedWorld if found, or empty if it does not exist
      Throws:
      SQLException - if a database query fails
    • exists

      public boolean exists(String name) throws SQLException
      Checks if a world with the specified name exists in the database.
      Specified by:
      exists in interface WorldRepository
      Parameters:
      name - the name of the world to look for
      Returns:
      true if it exists, false otherwise
      Throws:
      SQLException - if the database query fails
    • listAll

      public List<String> listAll() throws SQLException
      Returns a list of all world names currently saved in the database.
      Specified by:
      listAll in interface WorldRepository
      Returns:
      a list of unique world names, sorted alphabetically
      Throws:
      SQLException - if the database query fails