public final class EntityUtils extends Object
Modifier and Type | Class and Description |
---|---|
static interface |
EntityUtils.ThrowingReturningWork<G extends Exception,P>
Perform a task and return an arbitrary object instance as response.
|
Modifier and Type | Method and Description |
---|---|
static <P> P |
divert(ContextInfo contextInfo,
OperationType type,
Function<ContextInfo,P> op)
Divert a call to the function
op with the given context info and operation type. |
static Object |
findId(Object entity) |
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> |
optionallyTransact(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 |
setId(Object entity,
Object id) |
public static Field findIdField(Class<?> entityClass)
entityClass
- The class to check for the id annotated fieldIllegalArgumentException
- if unable to find an id fieldpublic static boolean findIsJpaEntity(Class<?> entityClass)
@Entity
annotation on this class, or one of its superclasses. Will return
true if a match is found.entityClass
- The class on which to make the determinationpublic static boolean isCreate(Object entity, @Nullable ContextInfo contextInfo)
public static int getAbsoluteInteger(int intValue)
Integer.MIN_VALUE
, the absolute value would
be an overflow. In this case, truncate and return Integer.MAX_VALUE
instead.intValue
- The candidate integer from which to derive the absolute valuepublic static <P,G extends Exception> P optionallyTransact(org.springframework.transaction.support.TransactionTemplate template, EntityUtils.ThrowingReturningWork<G,P> work) throws G extends Exception
P
- The type to return.template
- The template to use for a transaction. Can be null.work
- The work to perform.G extends Exception
public static <P> P divert(@Nullable ContextInfo contextInfo, @NonNull OperationType type, @NonNull Function<ContextInfo,P> op)
op
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 new ContextInfo
instance is instantiated with the
type provided and ignoreNarrowing
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.
The behavior is pass through for origination operation types of READ and DELETE, since those types do not drive inconclusive decisions when examining data. If a ContextInfo is passed and has one of these types assigned as the operation type, it is used as-is WITHOUT mutation.
P
- The response typecontextInfo
- The current, unmodified contexttype
- The operation type used to temporarily mutate the contextop
- The operation to perform under the mutated contextCopyright © 2021. All rights reserved.