Class ConfigValueRepository

java.lang.Object
no.ntnu.principes.repository.AbstractRepository<ConfigValue,Long>
no.ntnu.principes.repository.ConfigValueRepository
All Implemented Interfaces:
BaseRepository<ConfigValue,Long>

public class ConfigValueRepository extends AbstractRepository<ConfigValue,Long>
Repository for managing application configuration values. Provides caching for config values and JavaFX property bindings to enable reactive UI updates. Supports string, integer, and boolean configuration values with default fallbacks.
  • Constructor Details

    • ConfigValueRepository

      public ConfigValueRepository(DataSource dataSource)
      Creates a new ConfigValueRepository with the specified data source.
      Parameters:
      dataSource - The JDBC data source for database connections
  • Method Details

    • findById

      public Optional<ConfigValue> findById(Long id)
      Finds a configuration value by its ID.
      Parameters:
      id - The configuration value ID
      Returns:
      An Optional containing the found config value or empty if not found
    • findAll

      public List<ConfigValue> findAll()
      Retrieves all configuration values.
      Returns:
      A list of all configuration values
    • save

      public ConfigValue save(ConfigValue config)
      Saves a configuration value, updating if it exists or creating if it doesn't. Uses the config key to determine if an update or insert is needed.
      Parameters:
      config - The configuration value to save
      Returns:
      The saved configuration value with ID populated
    • deleteById

      public void deleteById(Long id)
      Deletes a configuration value by its ID.
      Parameters:
      id - The ID of the configuration value to delete
    • mapRow

      protected ConfigValue mapRow(ResultSet rs) throws SQLException
      Maps a database result set row to a ConfigValue object.
      Specified by:
      mapRow in class AbstractRepository<ConfigValue,Long>
      Parameters:
      rs - The result set positioned at the row to map
      Returns:
      The mapped ConfigValue object
      Throws:
      SQLException - If a database error occurs during mapping
    • findByKey

      public Optional<ConfigValue> findByKey(String key)
      Finds a configuration value by its key.
      Parameters:
      key - The configuration key
      Returns:
      An Optional containing the found config value or empty if not found
    • getValueOrDefault

      public javafx.beans.property.ObjectProperty<ConfigValue> getValueOrDefault(String key, String defaultValue)
      Gets a configuration value as a JavaFX property, with a default if not found. Uses an in-memory cache to avoid repeated database lookups.
      Parameters:
      key - The configuration key
      defaultValue - The default value to use if the key is not found
      Returns:
      An ObjectProperty containing the config value or a new one with the default value
    • getValueOrDefault

      public javafx.beans.property.ObjectProperty<ConfigValue> getValueOrDefault(String key, int defaultValue)
      Gets an integer configuration value as a JavaFX property, with a default if not found.
      Parameters:
      key - The configuration key
      defaultValue - The default integer value to use if the key is not found
      Returns:
      An ObjectProperty containing the config value
    • getValueOrDefault

      public javafx.beans.property.ObjectProperty<ConfigValue> getValueOrDefault(String key, boolean defaultValue)
      Gets a boolean configuration value as a JavaFX property, with a default if not found.
      Parameters:
      key - The configuration key
      defaultValue - The default boolean value to use if the key is not found
      Returns:
      An ObjectProperty containing the config value
    • setConfigValue

      public void setConfigValue(String key, String value)
      Sets a configuration value in the database and updates the cache. Creates a new entry if the key doesn't exist.
      Parameters:
      key - The configuration key
      value - The string value to set
    • setConfigValue

      public void setConfigValue(String key, int value)
      Sets an integer configuration value.
      Parameters:
      key - The configuration key
      value - The integer value to set
    • setConfigValue

      public void setConfigValue(String key, boolean value)
      Sets a boolean configuration value.
      Parameters:
      key - The configuration key
      value - The boolean value to set