public class CrudEntityHelper extends Object implements ChangeNotifier, PropagationIntegrator
DomainMapperManager
instance, this helper is capable of mapping
a business domain object to a repository specific domain object and performing persistence
operations using that business domain instance and the repository instance. This helper is
intended to be used in support of CrudEntityServices
when interacting
with Trackable
repository domain. Most of the methods require supplying the
TrackableRepository
from those services.
Typically, it is sufficient to extend BaseCrudEntityService
in order to gain access to
this helper; however, if a service implementation spans several repository types, it is recommend
to inject this class instead:
@Service
public class DefaultMyService<P extends MyPayload> implements MyService<P>
{private final CrudEntityHelper helper;
private final MyTrackableRepository repository;
public DefaultMyService(MyTrackableRepository repository, DomainMapperManager mapper) { this.helper = new CrudEntityHelper(mapper); this repository = repository; } }
Constructor and Description |
---|
CrudEntityHelper(DomainMapperManager mapper,
List<SortTransformer> sortTransformers,
EntityValidatorManager validator,
TrackableBehaviorUtil trackableBehaviorUtil) |
Modifier and Type | Method and Description |
---|---|
protected <P> Trackable |
applyReplace(String entityToReplaceId,
P replacement,
Map<String,Trackable> foundEntitiesByContextId,
List<BulkPersistenceResponse.PersistenceFailure<P>> failures,
ContextInfo contextInfo) |
protected <P> org.springframework.data.util.Pair<P,Trackable> |
applyUpdate(String entityToUpdateId,
P change,
Map<String,Trackable> foundEntitiesByContextId,
List<BulkPersistenceResponse.PersistenceFailure<P>> failures,
ContextInfo contextInfo) |
<P> P |
convertFromPersistentDomain(Trackable domain,
ContextInfo contextInfo)
Convenience method to use the
mapper to convert a persistent domain instance to its
business equivalent. |
<P> Trackable |
convertToPersistentDomain(P businessInstance,
Class<Trackable> persistentType,
ContextInfo context)
Convenience method to use the
mapper to convert a business domain instance to its
persistent equivalent. |
<P> P |
create(P businessInstance,
ContextInfo context,
TrackableRepository<Trackable> repository)
Create an instance of an entity based on a business instance containing relavant property
values.
|
<P> List<P> |
createAll(List<P> businessInstances,
ContextInfo context,
TrackableRepository<Trackable> repository) |
<P> BulkPersistenceResponse<P> |
createAllAllowingPartialSuccess(List<P> businessInstances,
ContextInfo context,
TrackableRepository<Trackable> repository) |
void |
delete(String id,
ContextInfo context,
TrackableRepository<Trackable> repository)
Perform a delete operation on the entity identified by the context id.
|
DomainMapperManager |
getMapper()
Provides access to the internal
DomainMapperManager |
protected com.broadleafcommerce.common.messaging.notification.NotificationManager |
getNotificationManager() |
protected SingleIndexRequestMessageFactory |
getSingleIndexRequestMessageFactory() |
protected List<SortTransformer> |
getSortTransformers() |
protected TrackableBehaviorUtil |
getTrackableBehaviorUtil() |
protected EntityValidatorManager |
getValidator() |
void |
notify(Trackable domain,
ContextInfo context,
@NonNull com.broadleafcommerce.common.messaging.notification.NotificationStateRepository repository)
Notify the system of the change.
|
String |
prepare(Trackable updated,
ContextInfo context,
@NonNull OperationType operationType)
Prepare a
Trackable entity for propagation, if applicable. |
void |
propagateIfApplicable(Trackable updated,
ContextInfo context,
@NonNull OperationType operationType,
@NonNull TrackableRepository<Trackable> repository,
String propagationId)
Initiate a catalog propagation for the change.
|
<P> List<P> |
readAll(ContextInfo context,
TrackableRepository<Trackable> repository)
Read a list of narrowed instances in the form of the business instance.
|
<P> org.springframework.data.domain.Page<P> |
readAll(org.springframework.data.domain.Pageable pageable,
ContextInfo context,
TrackableRepository<Trackable> repository)
Read a page of narrowed instances in the form of the business instance.
|
<P> List<P> |
readAll(org.springframework.data.domain.Sort sort,
ContextInfo context,
TrackableRepository<Trackable> repository)
Read a list of narrowed instances in the form of the business instance.
|
<P> Iterable<P> |
readAllByContextId(Iterable<String> ids,
ContextInfo context,
TrackableRepository<Trackable> repository)
Retrieve all business instances based on a list of context ids.
|
<P> P |
readByContextId(String id,
ContextInfo context,
TrackableRepository<Trackable> repository)
Read a single instance of an entity in the form of a business domain.
|
<P> P |
replace(String id,
P businessInstance,
ContextInfo context,
TrackableRepository<Trackable> repository)
Replace a single, narrowed instance of an entity based on a business instance containing
relavant property values.
|
<P> List<P> |
replaceAll(List<Update<P>> replacements,
ContextInfo context,
TrackableRepository<Trackable> repository) |
<P> BulkPersistenceResponse<P> |
replaceAllAllowingPartialSuccess(List<Update<P>> replacements,
ContextInfo context,
TrackableRepository<Trackable> repository) |
void |
setNotificationManager(com.broadleafcommerce.common.messaging.notification.NotificationManager notificationManager) |
void |
setPropagationManager(PropagationManager propagationManager) |
void |
setSingleIndexRequestMessageFactory(com.broadleafcommerce.common.messaging.notification.MessageFactory<SingleIndexRequest<Identifiable>> singleIndexRequestMessageFactory) |
void |
setTypeFactory(com.broadleafcommerce.common.extension.TypeFactory typeFactory) |
org.springframework.data.domain.Sort |
transform(org.springframework.data.domain.Sort sort,
TrackableRepository<Trackable> repository)
Transforms the given
sort from properties targeting the projection layer into a
Sort that targets properties at the domain layer |
org.springframework.data.domain.Pageable |
transformSort(org.springframework.data.domain.Pageable pageable,
TrackableRepository<Trackable> repository)
Transforms the sort inside of a
Pageable (if applicable). |
<P> P |
update(String id,
P businessInstance,
ContextInfo context,
TrackableRepository<Trackable> repository)
Update a single, narrowed instance of an entity based on a business instance containing
relevant property values.
|
<P> List<P> |
updateAll(List<Update<P>> updates,
ContextInfo context,
TrackableRepository<Trackable> repository) |
<P> BulkPersistenceResponse<P> |
updateAllAllowingPartialSuccess(List<Update<P>> updates,
ContextInfo context,
TrackableRepository<Trackable> repository) |
<P> P |
updateSort(SortPositionRequest sortPositionRequest,
ContextInfo context,
TrackableRepository<Trackable> repository,
SortPositionStrategy<P> sortPositionStrategy)
Apply a sort field value change
|
protected <P> void |
validateAllForCreate(List<P> businessInstancesToValidate,
ContextInfo contextInfo,
List<P> valid,
List<BulkPersistenceResponse.PersistenceFailure<P>> failed)
Validates each of the given business instances for creation.
|
protected <P> void |
validateAllForCreateAndThrowIfError(Iterable<P> businessInstances,
ContextInfo contextInfo) |
protected <P> void |
validateAllForReplace(List<Update<P>> businessInstancesToValidate,
ContextInfo contextInfo,
List<Update<P>> valid,
List<BulkPersistenceResponse.PersistenceFailure<P>> failed)
Validates the business instance within each
Update for replacement. |
protected <P> void |
validateAllForReplaceAndThrowIfError(Iterable<P> businessInstances,
ContextInfo contextInfo) |
protected <P> void |
validateAllForUpdate(List<org.springframework.data.util.Pair<P,Trackable>> businessInstancesToValidate,
ContextInfo contextInfo,
List<org.springframework.data.util.Pair<P,Trackable>> valid,
List<BulkPersistenceResponse.PersistenceFailure<P>> failed)
Validates the business instance within each pair for update.
|
protected <P> void |
validateAllForUpdateAndThrowIfError(Iterable<P> businessInstances,
ContextInfo contextInfo) |
public CrudEntityHelper(DomainMapperManager mapper, List<SortTransformer> sortTransformers, EntityValidatorManager validator, TrackableBehaviorUtil trackableBehaviorUtil)
@Autowired(required=false) public void setNotificationManager(@Nullable com.broadleafcommerce.common.messaging.notification.NotificationManager notificationManager)
@Autowired(required=false) public void setSingleIndexRequestMessageFactory(@Nullable com.broadleafcommerce.common.messaging.notification.MessageFactory<SingleIndexRequest<Identifiable>> singleIndexRequestMessageFactory)
public void notify(@Nullable Trackable domain, @Nullable ContextInfo context, @NonNull @NonNull @NonNull com.broadleafcommerce.common.messaging.notification.NotificationStateRepository repository)
NotificationManager.handle(NotificationStateRepository, NotificationStateAware, String)
for the following:
PersistenceProducer.TYPE
in all casesChangeSummaryProducer.TYPE
if the given instance is a sandbox recordSingleIndexRequestProducer.TYPE
if the given instance is Indexable
notify
in interface ChangeNotifier
domain
- The Trackable
repository domain instance that was changed. May also
wrap with a proxy exposing the MessageAdditionalInformationAware
interface to
engage message creation at MessageFactory.createMessage(Object, Map)
.context
- context information around sandbox and authorrepository
- The service responsible for making the state change. Usually a
Repository
instance.public void propagateIfApplicable(@Nullable Trackable updated, @Nullable ContextInfo context, @NonNull @NonNull @NonNull OperationType operationType, @NonNull @NonNull @NonNull TrackableRepository<Trackable> repository, @Nullable String propagationId)
PropagationIntegrator
Trackable
domain that use TrackableBehavior.CATALOG
, but explicitly omit
TrackableBehavior.SANDBOX
. This can also apply to catalog behavior supporting domain
that also supports sandbox behavior, but has explicitly omitted the sandbox identification in
the ContextRequest
in the ContextInfo
. This is a special case, and normally
propagation is automatically performed for catalog and sandbox behavior domain during
WorkflowTransitionHelper.deploy(WorkflowDeployRequest)
.propagateIfApplicable
in interface PropagationIntegrator
updated
- The Trackable
repository domain instance that was changedcontext
- context information around sandbox and authoroperationType
- The type of change operation performedrepository
- The service responsible for making the state change. Usually a
Repository
instance.propagationId
- The unique id identifying this propagation. Usually returned from a call
to PropagationIntegrator.prepare(Trackable, ContextInfo, OperationType)
@Nullable public String prepare(@Nullable Trackable updated, @Nullable ContextInfo context, @NonNull @NonNull @NonNull OperationType operationType)
PropagationIntegrator
Trackable
entity for propagation, if applicable. Usually involves preparing
ChangeDetail
instances and FieldOverride
instances contained therein.prepare
in interface PropagationIntegrator
updated
- The Trackable
repository domain instance that was changedcontext
- Context information around sandbox and authoroperationType
- The type of change operation performedpublic <P> P readByContextId(@NonNull String id, @Nullable ContextInfo context, @NonNull TrackableRepository<Trackable> repository)
id
- The context id for the entity. This is the id by which the business recognizes the
item.context
- Context information used to discriminate the correct version of an entity by
context id when multiple verions may be available across sandboxes, catalogs and
applications.repository
- A TrackableRepository
to perform the persistence operationEntityMissingException
if not available.public <P> Iterable<P> readAllByContextId(@NonNull Iterable<String> ids, @Nullable ContextInfo context, @NonNull TrackableRepository<Trackable> repository)
ids
- A list of context ids for the entitys. This is the id by which the business
recognizes the item.context
- Context information used to discriminate the correct version of an entity by
context id when multiple verions may be available across sandboxes, catalogs and
applications.repository
- A TrackableRepository
to perform the persistence operationpublic <P> org.springframework.data.domain.Page<P> readAll(@NonNull org.springframework.data.domain.Pageable pageable, @Nullable ContextInfo context, @NonNull TrackableRepository<Trackable> repository)
pageable
- The current page informationcontext
- Context information used to discriminate the correct version of an entity by
context id when multiple verions may be available across sandboxes, catalogs and
applications.repository
- A TrackableRepository
to perform the persistence operationpublic <P> List<P> readAll(@Nullable ContextInfo context, @NonNull TrackableRepository<Trackable> repository)
context
- Context information used to discriminate the correct version of an entity by
context id when multiple versions may be available across sandboxes, catalogs and
applications.repository
- A TrackableRepository
to perform the persistence operationpublic <P> List<P> readAll(@NonNull org.springframework.data.domain.Sort sort, @Nullable ContextInfo context, @NonNull TrackableRepository<Trackable> repository)
sort
- The sort to be performed on the resultscontext
- Context information used to discriminate the correct version of an entity by
context id when multiple versions may be available across sandboxes, catalogs and
applications.repository
- A TrackableRepository
to perform the persistence operationpublic <P> P update(@NonNull String id, @NonNull P businessInstance, @Nullable ContextInfo context, @NonNull TrackableRepository<Trackable> repository)
TrackableRepository
for more information on entity
persistence behavior in light of sandboxing and multitenant concerns.id
- The context id for the entity. This is the id by which the business recognizes the
item.businessInstance
- The payload type that domain class should be converted to. In
general, the payload is what the rest API responds with.context
- Context information used to discriminate the correct version of an entity by
context id when multiple versions may be available across sandboxes, catalogs and
applications.repository
- A TrackableRepository
to perform the persistence operationEntityMissingException
if not available.com.broadleafcommerce.common.error.validation.ValidationException
- if the businessInstance failed validationEntityMissingException
- if a managed instance corresponding to the given id
could not be foundpublic <P> List<P> updateAll(@NonNull List<Update<P>> updates, @Nullable ContextInfo context, @NonNull TrackableRepository<Trackable> repository)
protected <P> void validateAllForUpdateAndThrowIfError(Iterable<P> businessInstances, ContextInfo contextInfo)
public <P> BulkPersistenceResponse<P> updateAllAllowingPartialSuccess(@NonNull List<Update<P>> updates, @Nullable ContextInfo context, @NonNull TrackableRepository<Trackable> repository)
@Nullable protected <P> org.springframework.data.util.Pair<P,Trackable> applyUpdate(String entityToUpdateId, P change, Map<String,Trackable> foundEntitiesByContextId, List<BulkPersistenceResponse.PersistenceFailure<P>> failures, @Nullable ContextInfo contextInfo)
entityToUpdateId
- the context ID of the entity to which the change should be appliedchange
- the changes to apply to the entity (only non-null values from this object will
be applied)foundEntitiesByContextId
- a map of entities by context ID. This will be used to get the
entity to which the change will be applied.failures
- if the update cannot be applied for whatever reason, the details of the
failure will be added to this listcontextInfo
- context information surrounding sandboxing and multitenant statefoundEntitiesByContextId
or if there was some error in mapping the updates,
the return value will be null
and the failure details will be added to
failures
.protected <P> void validateAllForUpdate(List<org.springframework.data.util.Pair<P,Trackable>> businessInstancesToValidate, ContextInfo contextInfo, List<org.springframework.data.util.Pair<P,Trackable>> valid, List<BulkPersistenceResponse.PersistenceFailure<P>> failed)
valid
list. If it fails validation, a BulkPersistenceResponse.PersistenceFailure
is created with the business instance and that failure is added to the failed
list.businessInstancesToValidate
- a list of pairs containing the business instances to
validate for update, and the trackable representation of the same instancecontextInfo
- the sandboxing/multitenant context for the persistence operationvalid
- the list to which successfully validated entities should be addedfailed
- the list to which entities that failed validation should be addedpublic <P> P replace(@NonNull String id, @NonNull P businessInstance, @Nullable ContextInfo context, TrackableRepository<Trackable> repository)
id
- The context id for the entity. This is the id by which the business recognizes the
item.businessInstance
- The payload type that domain class should be converted to. In
general, the payload is what the rest API responds with.context
- Context information used to discriminate the correct version of an entity by
context id when multiple versions may be available across sandboxes, catalogs and
applications.repository
- A TrackableRepository
to perform the persistence operationEntityMissingException
if not available.com.broadleafcommerce.common.error.validation.ValidationException
- if the given businessInstance failed validation on
replacementEntityMissingException
- if a managed instance corresponding to the given id
could not be foundpublic <P> List<P> replaceAll(@NonNull List<Update<P>> replacements, @Nullable ContextInfo context, @NonNull TrackableRepository<Trackable> repository)
protected <P> void validateAllForReplaceAndThrowIfError(Iterable<P> businessInstances, ContextInfo contextInfo)
public <P> BulkPersistenceResponse<P> replaceAllAllowingPartialSuccess(@NonNull List<Update<P>> replacements, @Nullable ContextInfo context, @NonNull TrackableRepository<Trackable> repository)
@Nullable protected <P> Trackable applyReplace(String entityToReplaceId, P replacement, Map<String,Trackable> foundEntitiesByContextId, List<BulkPersistenceResponse.PersistenceFailure<P>> failures, @Nullable ContextInfo contextInfo)
entityToReplaceId
- the context ID of the entity which needs to be replacedreplacement
- the replacement entityfoundEntitiesByContextId
- a map of entities by context ID. This will be used to get the
entity to which the change will be applied.failures
- if the update cannot be applied for whatever reason, the details of the
failure will be added to this listcontextInfo
- context information surrounding sandboxing and multitenant statefoundEntitiesByContextId
or if there was some error in mapping the change,
the return value will be null
and the failure details will be added to
failures
.protected <P> void validateAllForReplace(List<Update<P>> businessInstancesToValidate, ContextInfo contextInfo, List<Update<P>> valid, List<BulkPersistenceResponse.PersistenceFailure<P>> failed)
Update
for replacement. If it passes
validation, the update is added to the valid
list. If it fails validation, a
BulkPersistenceResponse.PersistenceFailure
is created with the business instance and that failure is added to
the failed
list.businessInstancesToValidate
- a list of updates containing the business instances to
validate for replacecontextInfo
- the sandboxing/multitenant context for the persistence operationvalid
- the list to which successfully validated entities should be addedfailed
- the list to which entities that failed validation should be addedpublic <P> P create(@NonNull P businessInstance, @Nullable ContextInfo context, @NonNull TrackableRepository<Trackable> repository)
businessInstance
- The business domain instance. In general, the
DomainMapperManager
is responsible for converting to a repository platform
type for persistence.context
- Context information used to discriminate the correct version of an entity by
context id when multiple versions may be available across sandboxes, catalogs and
applications.repository
- A TrackableRepository
to perform the persistence operationcom.broadleafcommerce.common.error.validation.ValidationException
- if the given businessInstance could not be createdpublic <P> List<P> createAll(@NonNull List<P> businessInstances, @Nullable ContextInfo context, @NonNull TrackableRepository<Trackable> repository)
protected <P> void validateAllForCreateAndThrowIfError(Iterable<P> businessInstances, ContextInfo contextInfo)
public <P> BulkPersistenceResponse<P> createAllAllowingPartialSuccess(@NonNull List<P> businessInstances, @Nullable ContextInfo context, @NonNull TrackableRepository<Trackable> repository)
protected <P> void validateAllForCreate(List<P> businessInstancesToValidate, ContextInfo contextInfo, List<P> valid, List<BulkPersistenceResponse.PersistenceFailure<P>> failed)
valid
list. If it fails validation, a BulkPersistenceResponse.PersistenceFailure
is
created for it and that failure is added to the failed
list.businessInstancesToValidate
- the business instances to validate for creationcontextInfo
- the sandboxing/multitenant context for the persistence operationvalid
- the list to which successfully validated entities should be addedfailed
- the list to which entities that failed validation should be addedpublic void delete(@NonNull String id, @Nullable ContextInfo context, @NonNull TrackableRepository<Trackable> repository)
DomainMapper.deleteMap(Object, ContextInfo)
.id
- The context id for the entity. This is the id by which the business recognizes the
item.context
- Context information used to discriminate the correct version of an entity by
context id when multiple verions may be available across sandboxes, catalogs and
applications.repository
- A TrackableRepository
to perform the persistence operationEntityMissingException
- if no instance was found in the backing store corresponding to
idpublic <P> P updateSort(@NonNull SortPositionRequest sortPositionRequest, @Nullable ContextInfo context, @NonNull TrackableRepository<Trackable> repository, @NonNull SortPositionStrategy<P> sortPositionStrategy)
sortPositionRequest
- Information about the item being moved and the position to which
it's movingcontext
- context information used to discriminate the correct version of an entity by
context id when multiple versions may be available across sandboxes, catalogs and
applications.repository
- A TrackableRepository
to perform the persistence operationsortPositionStrategy
- The SortPositionStrategy
to use when updating the sort@NonNull public org.springframework.data.domain.Sort transform(@Nullable org.springframework.data.domain.Sort sort, @NonNull TrackableRepository<Trackable> repository)
sort
from properties targeting the projection layer into a
Sort
that targets properties at the domain layersort
- the projection-targeted sort, can be nullrepository
- A TrackableRepository
form which to get information about the
entity's typesort
is null, otherwise a Sort
targeting the repository provider domainSortTransformer
@NonNull public org.springframework.data.domain.Pageable transformSort(@NonNull org.springframework.data.domain.Pageable pageable, @NonNull TrackableRepository<Trackable> repository)
Pageable
(if applicable). Convert the sort
from properties targeting the projection layer into a Sort
that targets properties at
the domain layerpageable
- The Pageable
containing the Sort
instance to convertrepository
- A TrackableRepository
from which to get information about the
entity's typePageable
exposing the converted Sort
instancepublic <P> Trackable convertToPersistentDomain(P businessInstance, Class<Trackable> persistentType, ContextInfo context)
mapper
to convert a business domain instance to its
persistent equivalent.P
- The business domain typebusinessInstance
- The business domain instancepersistentType
- The persistent domain typecontext
- Context information used to discriminate the correct version of an entity by
context id when multiple verions may be available across sandboxes, catalogs and
applications.IllegalArgumentException
- Thrown if the business type could not be determinedpublic <P> P convertFromPersistentDomain(Trackable domain, ContextInfo contextInfo)
mapper
to convert a persistent domain instance to its
business equivalent.domain
- The persistent domain instance@NonNull public DomainMapperManager getMapper()
DomainMapperManager
DomainMapperManager
@NonNull protected List<SortTransformer> getSortTransformers()
@NonNull protected EntityValidatorManager getValidator()
@Nullable protected com.broadleafcommerce.common.messaging.notification.NotificationManager getNotificationManager()
@NonNull protected TrackableBehaviorUtil getTrackableBehaviorUtil()
@Nullable protected SingleIndexRequestMessageFactory getSingleIndexRequestMessageFactory()
@Autowired public void setPropagationManager(@Nullable PropagationManager propagationManager)
@Autowired public void setTypeFactory(@Nullable com.broadleafcommerce.common.extension.TypeFactory typeFactory)
Copyright © 2021. All rights reserved.