Class EntityUtils
java.lang.Object
com.broadleafcommerce.data.tracking.core.mapping.EntityUtils
Dynamically perform operations related to repository domain classes.
- Author:
- Jeff Fischer
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
EntityUtils.ThrowingReturningWork<G extends Exception,
P> Perform a task and return an arbitrary object instance as response. -
Method Summary
Modifier and TypeMethodDescriptionstatic <P> P
divert
(ContextInfo contextInfo, OperationType type, Function<ContextInfo, P> op) Divert a call to the functionop
with the given context info and operation type.static Object
static Field
findIdField
(Class<?> entityClass) Find the field marked with the Spring or JPA @Id annotation in the entity class.static boolean
findIsJpaEntity
(Class<?> entityClass) Look for a@Entity
annotation on this class, or one of its superclasses.static int
getAbsoluteInteger
(int intValue) Get the absolute value of an integer.static boolean
isCreate
(Object entity, ContextInfo contextInfo) static <P,
G extends Exception>
PoptionallyTransact
(org.springframework.transaction.support.TransactionTemplate template, EntityUtils.ThrowingReturningWork<G, P> work) Optionally perform work inside of a transaction, if there is a valid TransactionTemplate available.static void
-
Method Details
-
findIdField
Find the field marked with the Spring or JPA @Id annotation in the entity class. This process will search superclasses, should an id field not be found on the provided class. Will return null if no id annotated field is found.- Parameters:
entityClass
- The class to check for the id annotated field- Returns:
- the id field or null if none found
- Throws:
IllegalArgumentException
- if unable to find an id field
-
findIsJpaEntity
Look for a@Entity
annotation on this class, or one of its superclasses. Will return true if a match is found.- Parameters:
entityClass
- The class on which to make the determination- Returns:
- whether or not this class is determined to be a JPA entity
-
findId
-
isCreate
-
setId
-
getAbsoluteInteger
public static int getAbsoluteInteger(int intValue) Get the absolute value of an integer. IfInteger.MIN_VALUE
, the absolute value would be an overflow. In this case, truncate and returnInteger.MAX_VALUE
instead.- Parameters:
intValue
- The candidate integer from which to derive the absolute value- Returns:
- The absolute value of the integer
-
optionallyTransact
public static <P,G extends Exception> P optionallyTransact(org.springframework.transaction.support.TransactionTemplate template, EntityUtils.ThrowingReturningWork<G, P> work) throws GOptionally perform work inside of a transaction, if there is a valid TransactionTemplate available. This is primarly used to provide a common codebase under different transactional requirements (e.g. a JPA flow vs another db format flow).- Type Parameters:
P
- The type to return.- Parameters:
template
- The template to use for a transaction. Can be null.work
- The work to perform.- Returns:
- The result of the work.
- Throws:
G extends Exception
-
divert
public static <P> P divert(@Nullable ContextInfo contextInfo, @NonNull OperationType type, @NonNull Function<ContextInfo, P> op) Divert a call to the functionop
with the given context info and operation type. If a context info is provided, it is used as-is with only the operation type changed. Otherwise, if no context info is provided, a newContextInfo
instance is instantiated with the type provided andignoreNarrowing
set to true. Finally, before exiting, the context info (if applicable) is set back to its original operation type. This represents an attempt to utilize temporary context info mutation with as few side effects as possible. This is primarily used when there is an imminent need to demonstrate an operation type, especially when inspection of the entity data is inconclusive for operation type discovery. This is true of domain operations when the primary key is previously set. In such a case, the system cannot definitively determine if the requested operation is a CREATE or UPDATE based on primary key state alone. At that point, the system will fall back to the context info operation type, in which case, this method is of assistance.- Type Parameters:
P
- The response type- Parameters:
contextInfo
- The current, unmodified contexttype
- The operation type used to temporarily mutate the contextop
- The operation to perform under the mutated context- Returns:
- The result of the operation
-