Class OperationContext
- java.lang.Object
-
- org.silverpeas.core.persistence.datasource.OperationContext
-
public class OperationContext extends Object
This class permits to give details about operation actions (save action for now), especially the user who is the trigger of the operation. It is usually used from service method implementation. It is used by JPA Silverpeas API to set the technical entity information on:
- entity create
- entity update
As each operation in Silverpeas is relative to a thread, such an operation context is then attached to the thread that processes the operation. So, the operation context can be get by any processing actors within the same thread without being worried on the context transmission between them.
- Author:
- Yohann Chastagnier
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
OperationContext.State
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static void
addStates(OperationContext.State... states)
Adds states into context.static OperationContext
fromCurrentRequester()
Creates an instance from the current requester (User.getCurrentRequester()
).static OperationContext
fromUser(String userId)
Creates an instance from the given identifier which aims a user.static OperationContext
fromUser(User user)
Creates an instance from the given user.static OperationContext
getFromCache()
Get the currentOperationContext
instance from a cache (thread cache (request cache exactly)).<T extends PersistenceOperation>
TgetPersistenceOperation(Class<T> operationType)
Gets the current active persistence operation from this context.User
getUser()
Gets the user behind the operation.boolean
isUpdatingInCaseOfCreation()
Indicates if the date update is performed in a case of creation.static void
removeStates(OperationContext.State... states)
Removes states from context.OperationContext
setUpdatingInCaseOfCreation()
Calling this method to indicates that the current data update is performed in a case of a creation.OperationContext
setUpdatingInCaseOfCreation(boolean updatingInCaseOfCreation)
Calling this method to indicates that the current data update is performed in a case of a creation or not.static boolean
statesOf(OperationContext.State... states)
Indicates if the given states are well set into context.OperationContext
withUser(User user)
Sets the user associated to the save operation.
-
-
-
Method Detail
-
fromUser
public static OperationContext fromUser(String userId)
Creates an instance from the given identifier which aims a user. If an operation context already exists for the current thread, then sets it with the specified user and returns it.- Parameters:
userId
- the unique identifier of the user at the source of the operation.- Returns:
- a new
OperationContext
for the specified user identifier.
-
fromUser
public static OperationContext fromUser(User user)
Creates an instance from the given user. If an operation context already exists for the current thread, then sets it with the specified user and returns it.- Parameters:
user
- the user at the source of the operation.- Returns:
- a new
OperationContext
for the specified user.
-
fromCurrentRequester
public static OperationContext fromCurrentRequester()
Creates an instance from the current requester (User.getCurrentRequester()
). If an operation context exists for the current thread, then returns it. Otherwise, constructs a new one from the current requester of the operation (if any). If there is no user set, then set it with the current requester before returning it.- Returns:
- a new
OperationContext
for the current user.
-
getFromCache
public static OperationContext getFromCache()
Get the currentOperationContext
instance from a cache (thread cache (request cache exactly)). The call of this method is automatically done by technical JPA tools.- Returns:
- the current
OperationContext
instance from the cache.
-
statesOf
public static boolean statesOf(OperationContext.State... states)
Indicates if the given states are well set into context.- Parameters:
states
- the states to verify.- Returns:
- true if the specified states exists into the context, false otherwise.
-
addStates
public static void addStates(OperationContext.State... states)
Adds states into context.- Parameters:
states
- the states to add.
-
removeStates
public static void removeStates(OperationContext.State... states)
Removes states from context.- Parameters:
states
- the states to remove.
-
withUser
public OperationContext withUser(User user)
Sets the user associated to the save operation.- Parameters:
user
- the user to set.- Returns:
- itself.
-
setUpdatingInCaseOfCreation
public OperationContext setUpdatingInCaseOfCreation()
Calling this method to indicates that the current data update is performed in a case of a creation. Indeed, in some cases, the creation of a resource into database is done by a chaining of inserts and updates.- Returns:
- itself.
-
isUpdatingInCaseOfCreation
public boolean isUpdatingInCaseOfCreation()
Indicates if the date update is performed in a case of creation.- Returns:
- true if the date is updated, false otherwise.
-
setUpdatingInCaseOfCreation
public OperationContext setUpdatingInCaseOfCreation(boolean updatingInCaseOfCreation)
Calling this method to indicates that the current data update is performed in a case of a creation or not. Indeed, in some cases, the creation of a resource into database is done by a chaining of inserts and updates.- Parameters:
updatingInCaseOfCreation
- true to specify that the update is performed in a case of data creation.- Returns:
- itself
-
getUser
public User getUser()
Gets the user behind the operation.- Returns:
- the user concerned by the operation.
-
getPersistenceOperation
public <T extends PersistenceOperation> T getPersistenceOperation(Class<T> operationType)
Gets the current active persistence operation from this context.- Type Parameters:
T
- the concrete type of the implementation ofPersistenceOperation
class.- Parameters:
operationType
- the type of the expected persistence operation.- Returns:
- a persistence operation matching the expected type. Null if no such type exists.
-
-