java.lang.Object
de.seggebaeing.sqlanalyzer.persistence.dao.DTODAO<T>
Type Parameters:
T - the DTO type
All Implemented Interfaces:
DAO<T>
Direct Known Subclasses:
GeneratedQueryDAOImpl, LLMDAOImpl, PromptDAOImpl, PromptTypeDAOImpl, SampleQueryDAOImpl

public abstract class DTODAO<T extends Persistable> extends Object implements DAO<T>
Base DAO implementation for Persistable DTOs backed by the file system.

Provides a simple in-memory cache (id → DTO) synchronized from disk via PersistenceHelper. Subclasses specify the DTO type by implementing getDtoClass().

Notes: Cache synchronization is eager on construction and on selected operations; this class is not thread-safe.

Since:
1.0
Author:
Felix Seggebäing
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final Map<Integer,T>
    In-memory cache mapping DTO identifiers to their instances.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Constructs a new DAO and initializes the in-memory cache by synchronizing with the de.seggebaeing.sqlanalyzer.persistence layer.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    delete(T dto)
    Deletes the given entity from the de.seggebaeing.sqlanalyzer.persistence layer and removes it from the cache.
    Returns all cached entities.
    getByID(int id)
    Retrieves an entity by its identifier.
    protected abstract Class<T>
    Returns the DTO class managed by this DAO.
    int
    Generates a free identifier not currently used in the cache.
    void
    Persists or updates the given entity and refreshes the cache entry.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • cache

      protected final Map<Integer,T extends Persistable> cache
      In-memory cache mapping DTO identifiers to their instances.

      Keeps recently loaded or persisted objects to reduce file system access.

  • Constructor Details

    • DTODAO

      protected DTODAO()
      Constructs a new DAO and initializes the in-memory cache by synchronizing with the de.seggebaeing.sqlanalyzer.persistence layer.
  • Method Details

    • getAll

      public Set<T> getAll()
      Returns all cached entities.

      The result is a snapshot copy of the current cache contents.

      Specified by:
      getAll in interface DAO<T extends Persistable>
      Returns:
      a set of all entities; may be empty if none are cached
    • getByID

      public T getByID(int id)
      Retrieves an entity by its identifier.

      First checks the in-memory cache; if not present, attempts to load the entity from the de.seggebaeing.sqlanalyzer.persistence layer and refreshes the cache. Returns null if the id is -1, not found, or loading fails.

      Specified by:
      getByID in interface DAO<T extends Persistable>
      Parameters:
      id - the identifier of the entity
      Returns:
      the entity with the given id, or null if not found or load failed
    • delete

      public void delete(T dto)
      Deletes the given entity from the de.seggebaeing.sqlanalyzer.persistence layer and removes it from the cache.

      Logs a warning if deletion fails.

      Specified by:
      delete in interface DAO<T extends Persistable>
      Parameters:
      dto - the entity to delete, must not be null
      Throws:
      NullPointerException - if dto is null
    • saveOrUpdate

      public void saveOrUpdate(T dto)
      Persists or updates the given entity and refreshes the cache entry.

      Logs a warning if the operation fails.

      Specified by:
      saveOrUpdate in interface DAO<T extends Persistable>
      Parameters:
      dto - the entity to save or update, must not be null
      Throws:
      NullPointerException - if dto is null
    • getFreeId

      public int getFreeId()
      Generates a free identifier not currently used in the cache.

      Uses random numbers in the positive int range until an unused id is found. This implementation is simple but not guaranteed to be efficient.

      Returns:
      a free identifier
    • getDtoClass

      protected abstract Class<T> getDtoClass()
      Returns the DTO class managed by this DAO.
      Returns:
      the class object of the managed DTO type