Class ContextStateBuilder
- Direct Known Subclasses:
AlwaysMutableContextStateBuilder
- Author:
- Nathan Moore (nathandmoore)
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final Comparator<ChangeDetail>protected static final Comparator<ChangeDetail>protected static final Comparator<FieldChange> -
Constructor Summary
ConstructorsConstructorDescriptionContextStateBuilder(com.broadleafcommerce.common.extension.TypeFactory factory, TrackableBehaviorUtil behaviorUtil, com.fasterxml.jackson.databind.ObjectMapper fieldChangeMapper, org.modelmapper.ModelMapper cloneMapper) -
Method Summary
Modifier and TypeMethodDescriptionprotected StringbuildBusinessCollectionAccessorName(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 settingContextStateon embedded collections ofContextStateAware entities.buildContextState(Trackable domain, ContextInfo contextInfo) BuildsContextState informationfor aContextStateAware business instancecorresponding to the givenTrackable domain instancethat can be inspected for additional metadata around the entity in relation to the context of the requester.protected List<FieldChange>Builds a list ofFieldChangesfor theContextStateof aContextStateAware business instancecorresponding to a givenTrackable 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 aChangeDetailfor an embedded collection field, depending on which is passed as the serializedValue param, when settingContextStateon embedded collections ofContextStateAware entities.protected booleandetermineApplicationContextStateMutability(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 BooleandetermineArchived(Trackable domain) Determines theContextState.getArchived().protected IntegerdetermineContextStateLevel(Trackable domain) Determines theContextState.getLevel().protected booleandetermineContextStateMutability(Trackable domain, ContextInfo contextInfo) Determines whether the domain is mutable given the requesting context.protected com.fasterxml.jackson.databind.JavaTypedeterminePersistentCollectionJavaType(ChangeDetail detail, Class<?> collectionType, Class<?> memberType, Class<?> parentType, String parentId) Determines theJavaTypeof collections on persistent domains that correspond toContextStateAware entitieswhen deserializing the before and after values of aChangeDetailwhen settingContextStateon embedded collections ofContextStateAware entities.protected booleandeterminePolicyContextStateMutability(Trackable domain, ContextInfo contextInfo) Determines whether the domain is mutable given the requesting context based on permissions and policy validation.protected StringdetermineSandboxChangeType(Trackable domain) Returns thetracking's object'ssandbox change type.protected TrackableBehaviorUtilprotected org.modelmapper.ModelMapperprotected FieldgetCollectionField(String fieldName, Class<?> parentType) protected com.broadleafcommerce.common.extension.TypeFactoryprotected com.fasterxml.jackson.databind.ObjectMapperprotected <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 implementingContextStateAwarewhere the collection's members also implementContextStateAwarewhen settingContextStateon embedded collections ofContextStateAware entities.getOtherCollectionMemberType(String fieldName, Class<?> parentType, String parentId) Extension point for determining the member type of a collection not supported by default when settingEmbeddedContextStateon embedded collection member items ofContextStateAware entities.protected PolicyUtilsprotected final booleanisLatestChangeToFieldAtAnyLevel(ChangeDetail detail, Collection<? extends ChangeDetail> otherDetails) Checks that the givenChangeDetailany of: The only change for a field The highestChangeDetail.getLevel()of any change for a field The newest change for that field (comparingChangeDetail.getTimestamp()All obsolete changes are automatically excluded (seeChangeDetail.getObsolete()).protected final booleanisNotObsoleteChange(ChangeDetail detail) protected final booleanisNotProductionChange(ChangeDetail detail) protected final booleanisRebasedChange(ChangeDetail detail) protected voidsetApplicationInfo(Trackable domain, ContextState contextState, TrackableBehaviorPackage behavior) protected voidsetAuditInfo(Trackable domain, ContextState contextState, TrackableBehaviorPackage behavior) protected voidsetCatalogInfo(Trackable domain, ContextState contextState, TrackableBehaviorPackage behavior) voidsetChangeStateForEmbeddedCollections(Trackable domain, ContextStateAware businessItem, org.modelmapper.ModelMapper fromMeMapper) Sets theEmbeddedContextStateon a businessItem's embedded collections where the member items areEmbeddedContextStateAware.protected voidsetCollectionItemContextState(EmbeddedContextStateAware item, List<String> fieldsChanged) Builds and sets aContextStateonto the givenEmbeddedContextStateAware collection item.protected voidsetContextStateTrackingInfo(Trackable domain, ContextState contextState) Adds additional info to aContextStatebeing built for aContextStateAware business instancecorresponding to a givenTrackable domain instance.protected voidsetCustomerContext(Trackable domain, ContextState contextState, TrackableBehaviorPackage behavior) voidsetPolicyUtils(PolicyUtils policyUtils) protected voidsetTenant(Trackable domain, ContextState contextState, TrackableBehaviorPackage behavior)
-
Field Details
-
DETAIL_LEVEL_COMPARATOR
-
FIELD_LEVEL_COMPARATOR
-
DETAIL_TIMESTAMP_COMPARATOR
-
-
Constructor Details
-
ContextStateBuilder
public ContextStateBuilder(com.broadleafcommerce.common.extension.TypeFactory factory, TrackableBehaviorUtil behaviorUtil, com.fasterxml.jackson.databind.ObjectMapper fieldChangeMapper, org.modelmapper.ModelMapper cloneMapper)
-
-
Method Details
-
setPolicyUtils
-
buildContextState
@NonNull public ContextState buildContextState(@NonNull Trackable domain, @Nullable ContextInfo contextInfo) BuildsContextState informationfor aContextStateAware business instancecorresponding to the givenTrackable domain instancethat 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).- Parameters:
domain- Arepository domain instanceto use as the basis for theContextStatebeing built.contextInfo- The context information used to determine sandbox and multitenant state in tracking- Returns:
- The
ContextState informationfor aContextStateAware business instancecorresponding to the givenTrackable domain instance.
-
determineSandboxChangeType
Returns thetracking's object'ssandbox change type.- Parameters:
domain- TheTrackable instanceto retrieve the sandbox change type from.- Returns:
- The sandbox change type, or null if there is no sandbox change type.
- See Also:
-
setChangeStateForEmbeddedCollections
public void setChangeStateForEmbeddedCollections(@NonNull Trackable domain, @NonNull ContextStateAware businessItem, @Nullable org.modelmapper.ModelMapper fromMeMapper) Sets theEmbeddedContextStateon a businessItem's embedded collections where the member items areEmbeddedContextStateAware.By default, checks for
ChangeDetailsrelated to embedded collections on the businessItem. Then, deserializes and comparesChangeDetail.getBeforeValue()andChangeDetail.getAfterValue()to determine which member items within the collections have changed. Then, updates theirContextState, assuming those items areEmbeddedContextStateAware. Filters out all changes that are obsolete or older than another change for the same field in the same or highertracking level(e.g., if 2 changes are found to a 'name' field and both are inthe USER level, the older one is filtered out).- Parameters:
domain- TheTrackable instancerepresenting the persistent entity from which to gleanChangeDetailsbusinessItem- The business item corresponding to the domain whose embedded collections should have theirContextStatesset.fromMeMapper- ifdomainisModelMapperMappable, this should contain the result ofModelMapperMappable.fromMe(). This mapper is used to convert the embedded items from persisted instances to business instances. Ifdomainis notModelMapperMappable, then this should be left null.- Throws:
IllegalContextStateException- Thrown when a problem in the context state is found. This is normally because of one of the following scenarios:- a collection is found on the businessItem but the corresponding field on the domain is not a collection.
- the accessor for a collection field on the businessItem cannot be invoked because of access security or the name of the accessor is unknown.
ContextStateProcessingException- Thrown when an error occurs when attempting to set theContextStateon the members of a businessItem's embedded collection. Some examples:- when invoking (not finding) the correct accessor of a collection field on the businessItem encounters an error.
- when a
ChangeDetail'sbefore or after values cannot be deserialized once a change for a collection has been detected.
-
determineContextStateMutability
protected boolean determineContextStateMutability(Trackable domain, @Nullable ContextInfo contextInfo) Determines whether the domain is mutable given the requesting context. Generally, factors such as permissions, application tenancy, and catalog mutability are considered.- Parameters:
domain- TheTrackable domain instanceunder consideration for mutabilitycontextInfo- The context information used to determine sandbox and multitenant state in tracking- Returns:
- whether the given domain instance is mutable in the current request context.
-
determineApplicationContextStateMutability
protected boolean determineApplicationContextStateMutability(Trackable domain, @Nullable ContextInfo contextInfo) Determines whether the domain is mutable given the requesting context based on the application and tenancy state of the domain and context.If the domain doesn't belong to the requesting context's application, then it should not be mutable.
- Parameters:
domain- TheTrackable domain instanceunder consideration for mutabilitycontextInfo- The context information used to determine sandbox and multitenant state in tracking- Returns:
- whether the given domain instance is mutable in the current request context according to application tenancy
-
determinePolicyContextStateMutability
protected boolean determinePolicyContextStateMutability(Trackable domain, @Nullable ContextInfo contextInfo) Determines whether the domain is mutable given the requesting context based on permissions and policy validation.- Parameters:
domain- TheTrackable domain instanceunder consideration for mutabilitycontextInfo- The context information used to determine sandbox and multitenant state in tracking- Returns:
- whether the given domain instance is mutable in the current request context according to permissions and policies
-
determineContextStateLevel
Determines theContextState.getLevel(). This is generally the same as the givenTrackable domain instance's trackinglevel.- Parameters:
domain- Adomain instancebeing used as the basis for building aContextState.- Returns:
- The
TrackingLevelto set on theContextState.
-
determineArchived
Determines theContextState.getArchived(). This is generally the same as the givenTrackable domain instance's trackingarchived.- Parameters:
domain- Adomain instancebeing used as the basis for building aContextState.- Returns:
- The archived status to set on the
ContextState.
-
buildContextStateFieldChanges
Builds a list ofFieldChangesfor theContextStateof aContextStateAware business instancecorresponding to a givenTrackable domain instance.- Parameters:
domain- TheTrackable domain instancebeing used as a basis for building aContextState.- Returns:
- A list of
FieldChangesto set on aContextState.
-
isRebasedChange
-
isNotObsoleteChange
-
isNotProductionChange
-
setContextStateTrackingInfo
Adds additional info to aContextStatebeing built for aContextStateAware business instancecorresponding to a givenTrackable domain instance. Such info usually includesSandboxInfo,CatalogInfo, andApplicationInfoas applicable to the given trackable.- Parameters:
domain- ATrackable domain instancebeing used as the basis for building aContextState.contextState- AContextStatebeing built for aContextStateAware business instancecorresponding to the given trackable domain.
-
setTenant
protected void setTenant(Trackable domain, ContextState contextState, TrackableBehaviorPackage behavior) -
setCustomerContext
protected void setCustomerContext(Trackable domain, ContextState contextState, TrackableBehaviorPackage behavior) -
setApplicationInfo
protected void setApplicationInfo(Trackable domain, ContextState contextState, TrackableBehaviorPackage behavior) -
setCatalogInfo
protected void setCatalogInfo(Trackable domain, ContextState contextState, TrackableBehaviorPackage behavior) -
setAuditInfo
protected void setAuditInfo(Trackable domain, ContextState contextState, TrackableBehaviorPackage behavior) -
isLatestChangeToFieldAtAnyLevel
protected final boolean isLatestChangeToFieldAtAnyLevel(ChangeDetail detail, Collection<? extends ChangeDetail> otherDetails) Checks that the givenChangeDetailany 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()
ChangeDetail.getObsolete()). -
getCollectionField
-
getOtherCollectionMemberType
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 settingEmbeddedContextStateon embedded collection member items ofContextStateAware entities. Default supported collections areIterable,Map, and arrays. The following should also be extended:deserializeCollectionValue(String, Class, JavaType, Class, String, String),getOtherBusinessCollectionValue(String, Class, Object, String), anddeterminePersistentCollectionJavaType(ChangeDetail, Class, Class, Class, String)- Parameters:
fieldName- Name of the field on an entity (business or persistent) that may be a collection for whose member items we need the typeparentType- TheClassof the parent entityparentId- Id of the parent entity- Returns:
- the
Classof the member items inside of an embedded collection orOptional.empty()if not a collection.
-
getOtherBusinessCollectionValue
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 implementingContextStateAwarewhere the collection's members also implementContextStateAwarewhen settingContextStateon embedded collections ofContextStateAware entities. Default supported collections areIterable,Map, and arrays. For more, extend this method,deserializeCollectionValue(String, Class, JavaType, Class, String, String),getOtherCollectionMemberType(String, Class, String), anddeterminePersistentCollectionJavaType(ChangeDetail, Class, Class, Class, String)- Parameters:
fieldName- Name of the field on an business entity that is an unsupported type of collectioncollectionMemberType- TheClassof the collection's member itembusinessItem- The business item that owns the embedded collectionitemId- The id of the business item that owns the embedded collection- Returns:
- The values of an embedded collection on a business item that implements
ContextStateAwarewhere the collection's members also implementContextStateAwareand require theirContextStatesto be set.
-
buildBusinessCollectionAccessorName
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 settingContextStateon embedded collections ofContextStateAware entities.- Parameters:
fieldName- Name of the embedded collection on the businessItembusinessItem- Business instance of a persistent entity that is having its embedded collections'ContextStatesset.itemId- ID of the businessItem- Returns:
- name of the accessor of one of the businessItem's embedded collections
-
determinePersistentCollectionJavaType
protected com.fasterxml.jackson.databind.JavaType determinePersistentCollectionJavaType(ChangeDetail detail, Class<?> collectionType, Class<?> memberType, Class<?> parentType, String parentId) Determines theJavaTypeof collections on persistent domains that correspond toContextStateAware entitieswhen deserializing the before and after values of aChangeDetailwhen settingContextStateon embedded collections ofContextStateAware entities.The
JavaTypereturned is used to inform anObjectMapperof how to read the serialized before or after value from the ChangeDetail: seeObjectMapper.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), andgetOtherBusinessCollectionValue(String, Class, Object, String)- Parameters:
detail- theChangeDetailwith info on the changes to an embedded collection that should be used to set theContextStateon a member of the corresponding business domain's collection.collectionType- theClassof the persistent embedded collection to which theChangeDetailbelongs.memberType- theClassof the member items within the embedded collectionparentType- TheClassof the persistent entity that owns the embedded collectionparentId- The id of the persistent entity that owns the embedded collection- Returns:
- The
JavaTypeof the persistent collection required for deserialization.
-
deserializeCollectionValue
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 aChangeDetailfor an embedded collection field, depending on which is passed as the serializedValue param, when settingContextStateon embedded collections ofContextStateAware entities. If a collection type other than array,Map, orIterableshould be supported, extend this method,determinePersistentCollectionJavaType(ChangeDetail, Class, Class, Class, String),getOtherCollectionMemberType(String, Class, String), andgetOtherBusinessCollectionValue(String, Class, Object, String)- Parameters:
collectionName- The name of the collection field to which values belongcollectionType- TheClassof the collectionjavaType- TheJavaTypeto pass to theObjectMapperinto which to deserialize the serializedValueparentType- TheClassof the entity that owns the collectionparentId- The id of the entity that owns the collectionserializedValue- The before or after value from aChangeDetailto deserialize- Returns:
- A List representing the deserialized collection
- Throws:
ContextStateProcessingException- Thrown if deserialization fails for any reason.
-
setCollectionItemContextState
protected void setCollectionItemContextState(EmbeddedContextStateAware item, List<String> fieldsChanged) Builds and sets aContextStateonto the givenEmbeddedContextStateAware collection item.- Parameters:
item- TheEmbeddedContextStateAware collection item.fieldsChanged- List of the names of the fields on the changed item that were actually changed
-
getFactory
@NonNull protected com.broadleafcommerce.common.extension.TypeFactory getFactory() -
getBehaviorUtil
-
getFieldChangeMapper
@NonNull protected com.fasterxml.jackson.databind.ObjectMapper getFieldChangeMapper() -
getCloneMapper
@NonNull protected org.modelmapper.ModelMapper getCloneMapper() -
getPolicyUtils
-