Class AuditableDataEntity<ID extends Serializable>
- Type Parameters:
ID- the type of the entity identifier
- All Implemented Interfaces:
Serializable
- Since:
- 6.2.0
- Version:
- 2.0.0
- Author:
- wisdomme
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate LocalDateTimeprivate UUIDprivate static final ThreadLocal<UUID> Sets the current user context for audit purposes.private static final longprivate LocalDateTimeprivate UUID -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidClears the current user context.getAge()Gets the age of this entity since creation.protected static UUIDGets the current user from the thread-local context.Gets the time since the last modification.voidonCreate()voidonUpdate()static voidsetCurrentUser(UUID userId) Sets the current user for audit tracking.booleanChecks if this entity was modified after creation.Methods inherited from class com.ultikits.ultitools.abstracts.data.BaseDataEntity
clone, copyWithoutId, getId, getValidationErrors, isNew, onDelete, onLoad, setId, validate
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
createdAt
-
updatedAt
-
createdBy
-
updatedBy
-
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
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 aPlayer-- so a command handler's data operations record that player's UUID increatedBy/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 recordnullactors.- Parameters:
userId- the current user's UUID
-
clearCurrentUser
public static void clearCurrentUser()Clears the current user context. Call this after completing data operations.BaseCommandExecutorcalls this in afinallyaround the same invocation it wraps withsetCurrentUser(UUID), so it runs whether the handler returns normally or throws. It calls this method -- notsetCurrentUser(null)-- specifically because this removes theThreadLocalentry entirely rather than leaving anullmapping behind, which matters on a pooled Bukkit worker thread that gets reused for a later, unrelated command. -
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()SetscreatedAt/updatedAtto now and, if acurrent useris set,createdBy/updatedByto 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/createdBystayednullon every persisted row regardless of this method's own correctness. A lateronUpdate()does not touchcreatedAt/createdBy, so the values this method writes on insert persist unchanged through every subsequent update.- Overrides:
onCreatein classBaseDataEntity<ID extends Serializable>
-
onUpdate
public void onUpdate()SetsupdatedAtto now and, if acurrent useris set,updatedByto it. Deliberately does not touchcreatedAt/createdBy-- seeonCreate().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:
onUpdatein classBaseDataEntity<ID extends Serializable>
-
getAge
Gets the age of this entity since creation.- Returns:
- the duration since creation, or null if not persisted
-
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
-