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.static UUIDswapCurrentUser(UUID userId) Replaces the current user and returns the one it replaces, so a caller can put it back.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.BaseCommandExecutorsets the command sender's UUID around the body that actually invokes a command's matched method, when the resolved sender is aPlayer-- throughswapCurrentUser(UUID)as of 6.3.0, which also restores the previous user afterwards -- so a command handler's data operations record that player's UUID increatedBy/updatedBy. Nothing else in the framework sets it: 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.This removes the
ThreadLocalentry entirely rather than leaving anullmapping behind, which matters on a pooled Bukkit worker thread that gets reused for a later, unrelated command.BaseCommandExecutorno longer calls it after a command body (as of 6.3.0, #541): it restores the user that was current before the body throughswapCurrentUser(UUID), which removes the entry the same way when there was none. -
swapCurrentUser
Replaces the current user and returns the one it replaces, so a caller can put it back.BaseCommandExecutorcalls this around every command body (as of 6.3.0, #541): once with the sender's UUID (ornullfor a sender that is not a player) before the body, and once with the returned value in afinallyafter it. A command body runs at dispatch on the server thread, so a body that dispatches another command runs the nested body on the same thread before its own has finished; saving and restoring keeps each body's own user, and a thread that carried no user before the outermost command carries none after it. Anullargument removes theThreadLocalentry, asclearCurrentUser()does, rather than storingnull.- Parameters:
userId- the user to make current, ornullfor none- Returns:
- the user that was current before, or
nullif there was none - Since:
- 6.3.0
-
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
-