public class ContextStateBuilder extends Object
Modifier and Type | Field and Description |
---|---|
protected static Comparator<ChangeDetail> |
DETAIL_LEVEL_COMPARATOR |
protected static Comparator<ChangeDetail> |
DETAIL_TIMESTAMP_COMPARATOR |
protected static Comparator<FieldChange> |
FIELD_LEVEL_COMPARATOR |
Constructor and Description |
---|
ContextStateBuilder(com.broadleafcommerce.common.extension.TypeFactory factory,
TrackableBehaviorUtil behaviorUtil,
com.fasterxml.jackson.databind.ObjectMapper fieldChangeMapper,
org.modelmapper.ModelMapper cloneMapper) |
Modifier and Type | Method and Description |
---|---|
protected String |
buildBusinessCollectionAccessorName(String fieldName,
Object businessItem,
String itemId)
Builds the name of the accessor of one of the businessItem's embedded collections to invoke
in order to obtain the value of that field when setting
ContextState on embedded
collections of ContextStateAware entities . |
ContextState |
buildContextState(Trackable domain,
ContextInfo contextInfo)
Builds
ContextState information for a ContextStateAware business instance corresponding to the given Trackable
domain instance that can be inspected for additional metadata around the entity in relation
to the context of the requester. |
protected List<FieldChange> |
buildContextStateFieldChanges(Trackable domain)
Builds a list of
FieldChanges for the ContextState of a
ContextStateAware business instance corresponding to a given
Trackable domain instance . |
protected <T> List<T> |
deserializeCollectionValue(String collectionName,
Class<?> collectionType,
com.fasterxml.jackson.databind.JavaType javaType,
Class<?> parentType,
String parentId,
String serializedValue)
Deserializes either the afterValue or beforeValue of a
ChangeDetail for an embedded
collection field, depending on which is passed as the serializedValue param, when setting
ContextState on embedded collections of ContextStateAware
entities . |
protected boolean |
determineApplicationContextStateMutability(Trackable domain,
ContextInfo contextInfo)
Determines whether the domain is mutable given the requesting context based on the
application and tenancy state of the domain and context.
|
protected Boolean |
determineArchived(Trackable domain)
Determines the
ContextState.getArchived() . |
protected Integer |
determineContextStateLevel(Trackable domain)
Determines the
ContextState.getLevel() . |
protected boolean |
determineContextStateMutability(Trackable domain,
ContextInfo contextInfo)
Determines whether the domain is mutable given the requesting context.
|
protected com.fasterxml.jackson.databind.JavaType |
determinePersistentCollectionJavaType(ChangeDetail detail,
Class<?> collectionType,
Class<?> memberType,
Class<?> parentType,
String parentId)
Determines the
JavaType of collections on persistent domains that correspond to
ContextStateAware entities when deserializing the before and after
values of a ChangeDetail when setting ContextState on embedded collections of
ContextStateAware entities . |
protected boolean |
determinePolicyContextStateMutability(Trackable domain,
ContextInfo contextInfo)
Determines whether the domain is mutable given the requesting context based on permissions
and policy validation.
|
protected String |
determineSandboxChangeType(Trackable domain)
Returns the
tracking's object's sandbox change type. |
protected TrackableBehaviorUtil |
getBehaviorUtil() |
protected org.modelmapper.ModelMapper |
getCloneMapper() |
protected Field |
getCollectionField(String fieldName,
Class<?> parentType) |
protected com.broadleafcommerce.common.extension.TypeFactory |
getFactory() |
protected com.fasterxml.jackson.databind.ObjectMapper |
getFieldChangeMapper() |
protected <T> List<T> |
getOtherBusinessCollectionValue(String fieldName,
Class<T> collectionMemberType,
Object businessItem,
String itemId)
Extension point for getting the values of an embedded collection not supported by default on
a business item implementing
ContextStateAware where the collection's members also
implement ContextStateAware when setting ContextState on embedded collections
of ContextStateAware entities . |
protected Optional<Class<?>> |
getOtherCollectionMemberType(String fieldName,
Class<?> parentType,
String parentId)
Extension point for determining the member type of a collection not supported by default when
setting
EmbeddedContextState on embedded collection member items of
ContextStateAware entities . |
protected PolicyUtils |
getPolicyUtils() |
protected boolean |
isLatestChangeToFieldAtAnyLevel(ChangeDetail detail,
Collection<? extends ChangeDetail> otherDetails)
Checks that the given
ChangeDetail any of:
The only change for a field
The highest ChangeDetail.getLevel() of any change for a field
The newest change for that field (comparing ChangeDetail.getTimestamp()
All obsolete changes are automatically excluded (see ChangeDetail.getObsolete() ). |
protected boolean |
isNotObsoleteChange(ChangeDetail detail) |
protected boolean |
isNotProductionChange(ChangeDetail detail) |
protected boolean |
isRebasedChange(ChangeDetail detail) |
protected void |
setApplicationInfo(Trackable domain,
ContextState contextState,
TrackableBehaviorPackage behavior) |
protected void |
setCatalogInfo(Trackable domain,
ContextState contextState,
TrackableBehaviorPackage behavior) |
void |
setChangeStateForEmbeddedCollections(Trackable domain,
ContextStateAware businessItem,
org.modelmapper.ModelMapper fromMeMapper)
Sets the
EmbeddedContextState on a businessItem's embedded collections where the
member items are EmbeddedContextStateAware . |
protected void |
setCollectionItemContextState(EmbeddedContextStateAware item,
List<String> fieldsChanged)
Builds and sets a
ContextState onto the given EmbeddedContextStateAware collection item . |
protected void |
setContextStateTrackingInfo(Trackable domain,
ContextState contextState)
Adds additional info to a
ContextState being built for a ContextStateAware business instance corresponding to a given Trackable
domain instance . |
protected void |
setCustomerContext(Trackable domain,
ContextState contextState,
TrackableBehaviorPackage behavior) |
void |
setPolicyUtils(PolicyUtils policyUtils) |
protected void |
setTenant(Trackable domain,
ContextState contextState,
TrackableBehaviorPackage behavior) |
protected static final Comparator<ChangeDetail> DETAIL_LEVEL_COMPARATOR
protected static final Comparator<FieldChange> FIELD_LEVEL_COMPARATOR
protected static final Comparator<ChangeDetail> DETAIL_TIMESTAMP_COMPARATOR
public ContextStateBuilder(com.broadleafcommerce.common.extension.TypeFactory factory, TrackableBehaviorUtil behaviorUtil, com.fasterxml.jackson.databind.ObjectMapper fieldChangeMapper, org.modelmapper.ModelMapper cloneMapper)
@Autowired(required=false) public void setPolicyUtils(@Nullable PolicyUtils policyUtils)
@NonNull public ContextState buildContextState(@NonNull Trackable domain, @Nullable ContextInfo contextInfo)
ContextState information
for a ContextStateAware business instance
corresponding to the given Trackable
domain instance
that can be inspected for additional metadata around the entity in relation
to the context of the requester. This information can be used to make determination around
mutability, as well as details around the nature of field change state (if any).domain
- A repository domain instance
to use as the basis for the
ContextState
being built.contextInfo
- The context information used to determine sandbox and multitenant state in
trackingContextState information
for a ContextStateAware business instance
corresponding to the given Trackable domain instance
.@Nullable protected String determineSandboxChangeType(Trackable domain)
tracking's object's
sandbox change type.domain
- The Trackable instance
to retrieve the sandbox change type
from.SandboxInfo.getChangeType()
,
Tracking.getSandbox()
public void setChangeStateForEmbeddedCollections(@NonNull Trackable domain, @NonNull ContextStateAware businessItem, @Nullable org.modelmapper.ModelMapper fromMeMapper)
EmbeddedContextState
on a businessItem's embedded collections where the
member items are EmbeddedContextStateAware
.
By default, checks for ChangeDetails
related to embedded collections on
the businessItem. Then, deserializes and compares ChangeDetail.getBeforeValue()
and
ChangeDetail.getAfterValue()
to determine which member items within the collections
have changed. Then, updates their ContextState
, assuming those items are
EmbeddedContextStateAware
. Filters out all changes that are obsolete or older than
another change for the same field in the same or higher tracking level
(e.g., if 2 changes are found to a 'name' field and both are in
the USER level
, the older one is filtered out).
domain
- The Trackable instance
representing the persistent entity
from which to glean ChangeDetails
businessItem
- The business item corresponding to the domain whose embedded collections
should have their ContextStates
set.fromMeMapper
- if domain
is ModelMapperMappable
, this should contain the
result of ModelMapperMappable.fromMe()
. This mapper is used to convert the
embedded items from persisted instances to business instances. If domain
is
not ModelMapperMappable
, then this should be left null.IllegalContextStateException
- Thrown when a problem in the context state is found.
This is normally because of one of the following scenarios:
ContextStateProcessingException
- Thrown when an error occurs when attempting to set
the ContextState
on the members of a businessItem's embedded collection. Some
examples:
ChangeDetail's
before or after values cannot be
deserialized once a change for a collection has been detected.protected boolean determineContextStateMutability(Trackable domain, @Nullable ContextInfo contextInfo)
domain
- The Trackable domain instance
under consideration for
mutabilitycontextInfo
- The context information used to determine sandbox and multitenant state in
trackingprotected boolean determineApplicationContextStateMutability(Trackable domain, @Nullable ContextInfo contextInfo)
If the domain doesn't belong to the requesting context's application, then it should not be mutable.
domain
- The Trackable domain instance
under consideration for
mutabilitycontextInfo
- The context information used to determine sandbox and multitenant state in
trackingprotected boolean determinePolicyContextStateMutability(Trackable domain, @Nullable ContextInfo contextInfo)
domain
- The Trackable domain instance
under consideration for
mutabilitycontextInfo
- The context information used to determine sandbox and multitenant state in
trackingprotected Integer determineContextStateLevel(Trackable domain)
ContextState.getLevel()
. This is generally the same as the given
Trackable domain instance's tracking
level
.domain
- A domain instance
being used as the basis for building a
ContextState
.TrackingLevel
to set on the ContextState
.protected Boolean determineArchived(Trackable domain)
ContextState.getArchived()
. This is generally the same as the given
Trackable domain instance's tracking
archived
.domain
- A domain instance
being used as the basis for building a
ContextState
.ContextState
.protected List<FieldChange> buildContextStateFieldChanges(Trackable domain)
FieldChanges
for the ContextState
of a
ContextStateAware business instance
corresponding to a given
Trackable domain instance
.domain
- The Trackable domain instance
being used as a basis for
building a ContextState
.FieldChanges
to set on a ContextState
.protected final boolean isRebasedChange(ChangeDetail detail)
protected final boolean isNotObsoleteChange(ChangeDetail detail)
protected final boolean isNotProductionChange(ChangeDetail detail)
protected void setContextStateTrackingInfo(Trackable domain, ContextState contextState)
ContextState
being built for a ContextStateAware business instance
corresponding to a given Trackable
domain instance
. Such info usually includes SandboxInfo
, CatalogInfo
, and
ApplicationInfo
as applicable to the given trackable.domain
- A Trackable domain instance
being used as the basis for
building a ContextState
.contextState
- A ContextState
being built for a ContextStateAware business instance
corresponding to the given trackable domain.protected void setTenant(Trackable domain, ContextState contextState, TrackableBehaviorPackage behavior)
protected void setCustomerContext(Trackable domain, ContextState contextState, TrackableBehaviorPackage behavior)
protected void setApplicationInfo(Trackable domain, ContextState contextState, TrackableBehaviorPackage behavior)
protected void setCatalogInfo(Trackable domain, ContextState contextState, TrackableBehaviorPackage behavior)
protected final boolean isLatestChangeToFieldAtAnyLevel(ChangeDetail detail, Collection<? extends ChangeDetail> otherDetails)
ChangeDetail
any of:
ChangeDetail.getLevel()
of any change for a fieldChangeDetail.getTimestamp()
ChangeDetail.getObsolete()
).protected Optional<Class<?>> getOtherCollectionMemberType(String fieldName, Class<?> parentType, String parentId)
EmbeddedContextState
on embedded collection member items of
ContextStateAware entities
. Default supported collections are
Iterable
, Map
, and arrays. The following should also be extended:
deserializeCollectionValue(String, Class, JavaType, Class, String, String)
,
getOtherBusinessCollectionValue(String, Class, Object, String)
, and
determinePersistentCollectionJavaType(ChangeDetail, Class, Class, Class, String)
fieldName
- Name of the field on an entity (business or persistent) that may be a
collection for whose member items we need the typeparentType
- The Class
of the parent entityparentId
- Id of the parent entityClass
of the member items inside of an embedded collection or
Optional.empty()
if not a collection.protected <T> List<T> getOtherBusinessCollectionValue(String fieldName, Class<T> collectionMemberType, Object businessItem, String itemId)
ContextStateAware
where the collection's members also
implement ContextStateAware
when setting ContextState
on embedded collections
of ContextStateAware entities
. Default supported collections are
Iterable
, Map
, and arrays. For more, extend this method,
deserializeCollectionValue(String, Class, JavaType, Class, String, String)
,
getOtherCollectionMemberType(String, Class, String)
, and
determinePersistentCollectionJavaType(ChangeDetail, Class, Class, Class, String)
fieldName
- Name of the field on an business entity that is an unsupported type of
collectioncollectionMemberType
- The Class
of the collection's member itembusinessItem
- The business item that owns the embedded collectionitemId
- The id of the business item that owns the embedded collectionContextStateAware
where the collection's members also implement
ContextStateAware
and require their ContextStates
to be
set.protected String buildBusinessCollectionAccessorName(String fieldName, Object businessItem, String itemId)
ContextState
on embedded
collections of ContextStateAware entities
.fieldName
- Name of the embedded collection on the businessItembusinessItem
- Business instance of a persistent entity that is having its embedded
collections' ContextStates
set.itemId
- ID of the businessItemprotected com.fasterxml.jackson.databind.JavaType determinePersistentCollectionJavaType(ChangeDetail detail, Class<?> collectionType, Class<?> memberType, Class<?> parentType, String parentId)
JavaType
of collections on persistent domains that correspond to
ContextStateAware entities
when deserializing the before and after
values of a ChangeDetail
when setting ContextState
on embedded collections of
ContextStateAware entities
.
The JavaType
returned is used to inform an ObjectMapper
of how to read the
serialized before or after value from the ChangeDetail: see
ObjectMapper.readValue(String, JavaType)
.
Default supported collections are Iterable
, Map
, and arrays. If other types
should be supported, extend this method,
deserializeCollectionValue(String, Class, JavaType, Class, String, String)
,
getOtherCollectionMemberType(String, Class, String)
, and
getOtherBusinessCollectionValue(String, Class, Object, String)
detail
- the ChangeDetail
with info on the changes to an embedded collection
that should be used to set the ContextState
on a member of the corresponding
business domain's collection.collectionType
- the Class
of the persistent embedded collection to which the
ChangeDetail
belongs.memberType
- the Class
of the member items within the embedded collectionparentType
- The Class
of the persistent entity that owns the embedded
collectionparentId
- The id of the persistent entity that owns the embedded collectionJavaType
of the persistent collection required for deserialization.protected <T> List<T> deserializeCollectionValue(String collectionName, Class<?> collectionType, com.fasterxml.jackson.databind.JavaType javaType, Class<?> parentType, String parentId, String serializedValue)
ChangeDetail
for an embedded
collection field, depending on which is passed as the serializedValue param, when setting
ContextState
on embedded collections of ContextStateAware
entities
. If a collection type other than array, Map
, or Iterable
should be
supported, extend this method,
determinePersistentCollectionJavaType(ChangeDetail, Class, Class, Class, String)
,
getOtherCollectionMemberType(String, Class, String)
, and
getOtherBusinessCollectionValue(String, Class, Object, String)
collectionName
- The name of the collection field to which values belongcollectionType
- The Class
of the collectionjavaType
- The JavaType
to pass to the ObjectMapper
into which to
deserialize the serializedValueparentType
- The Class
of the entity that owns the collectionparentId
- The id of the entity that owns the collectionserializedValue
- The before or after value from a ChangeDetail
to deserializeContextStateProcessingException
- Thrown if deserialization fails for any reason.protected void setCollectionItemContextState(EmbeddedContextStateAware item, List<String> fieldsChanged)
ContextState
onto the given EmbeddedContextStateAware collection item
.item
- The EmbeddedContextStateAware collection item
.fieldsChanged
- List of the names of the fields on the changed item that were actually
changed@NonNull protected com.broadleafcommerce.common.extension.TypeFactory getFactory()
@NonNull protected TrackableBehaviorUtil getBehaviorUtil()
@NonNull protected com.fasterxml.jackson.databind.ObjectMapper getFieldChangeMapper()
@NonNull protected org.modelmapper.ModelMapper getCloneMapper()
@Nullable protected PolicyUtils getPolicyUtils()
Copyright © 2021. All rights reserved.