Class AuditableDataEntity<ID extends Serializable>

java.lang.Object
com.ultikits.ultitools.abstracts.data.BaseDataEntity<ID>
com.ultikits.ultitools.abstracts.data.AuditableDataEntity<ID>
Type Parameters:
ID - the type of the entity identifier
All Implemented Interfaces:
Serializable

public abstract class AuditableDataEntity<ID extends Serializable> extends BaseDataEntity<ID>
Data entity with audit fields for tracking creation and modification. Automatically manages audit timestamps and user information.
Since:
6.2.0
Version:
2.0.0
Author:
wisdomme
See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      See Also:
    • createdAt

      private LocalDateTime createdAt
    • updatedAt

      private LocalDateTime updatedAt
    • createdBy

      private UUID createdBy
    • updatedBy

      private UUID updatedBy
    • CURRENT_USER

      private static final ThreadLocal<UUID> CURRENT_USER
      Sets the current user context for audit purposes. Thread-local storage for the current operation's user.
  • Constructor Details

    • AuditableDataEntity

      public AuditableDataEntity()
  • Method Details

    • setCurrentUser

      public static void setCurrentUser(UUID userId)
      Sets the current user for audit tracking. Call this before performing data operations.

      BaseCommandExecutor (Phase 5's file; the current-user wrapper was added there in 02-08) calls this once, inside the runnable that actually invokes a command's matched method, when the resolved sender is a Player -- so a command handler's data operations record that player's UUID in createdBy/updatedBy. Nothing else in the framework calls this: a module performing data operations from outside a command handler (a scheduled task, a listener, an external plugin via the External Plugin API) is responsible for setting the context itself, or its writes record null actors.

      Parameters:
      userId - the current user's UUID
    • clearCurrentUser

      public static void clearCurrentUser()
      Clears the current user context. Call this after completing data operations.

      BaseCommandExecutor calls this in a finally around the same invocation it wraps with setCurrentUser(UUID), so it runs whether the handler returns normally or throws. It calls this method -- not setCurrentUser(null) -- specifically because this removes the ThreadLocal entry entirely rather than leaving a null mapping behind, which matters on a pooled Bukkit worker thread that gets reused for a later, unrelated command.

    • getCurrentUser

      protected static UUID getCurrentUser()
      Gets the current user from the thread-local context.
      Returns:
      the current user's UUID, or null if not set
    • onCreate

      public void onCreate()
      Sets createdAt/updatedAt to now and, if a current user is set, createdBy/updatedBy to it.

      As of 6.3.0 (02-08), every relational and JSON data operator in this framework actually calls this before an entity is inserted -- previously it was reachable only by calling it directly, which nothing in the framework did, so createdAt/createdBy stayed null on every persisted row regardless of this method's own correctness. A later onUpdate() does not touch createdAt/createdBy, so the values this method writes on insert persist unchanged through every subsequent update.

      Overrides:
      onCreate in class BaseDataEntity<ID extends Serializable>
    • onUpdate

      public void onUpdate()
      Sets updatedAt to now and, if a current user is set, updatedBy to it. Deliberately does not touch createdAt/createdBy -- see onCreate().

      As of 6.3.0 (02-08), every relational and JSON data operator in this framework actually calls this before an entity is updated; see onCreate()'s note on why that was not previously true.

      Overrides:
      onUpdate in class BaseDataEntity<ID extends Serializable>
    • getAge

      public Duration getAge()
      Gets the age of this entity since creation.
      Returns:
      the duration since creation, or null if not persisted
    • getTimeSinceUpdate

      public Duration getTimeSinceUpdate()
      Gets the time since the last modification.
      Returns:
      the duration since last update, or null if not persisted
    • wasModified

      public boolean wasModified()
      Checks if this entity was modified after creation.
      Returns:
      true if modified after creation