public class MappableCrudEntityHelper extends Object
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 MappableCrudEntityServices
.
Most of the methods require supplying the MappableCrudRepository
from those services.
Typically, it is sufficient to extend BaseMappableCrudEntityService
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 MappableCrudEntityHelper<MyPayload> helper;
private final MyCrudRepository repository;
public DefaultMyService(MyCrudRepository repository, DomainMapperManager mapper) { this.helper = new MappableCrudEntityHelper(repository, mapper); this.repository = repository; } }
Constructor and Description |
---|
MappableCrudEntityHelper(DomainMapperManager mapper,
List<SortTransformer> sortTransformers,
EntityValidatorManager validator) |
Modifier and Type | Method and Description |
---|---|
protected <P,D> D |
applyReplace(String entityToReplaceId,
P replacement,
Map<String,D> foundEntitiesById,
List<BulkPersistenceResponse.PersistenceFailure<P>> failures) |
protected <P,D> org.springframework.data.util.Pair<P,D> |
applyUpdate(String entityToUpdateId,
P change,
Map<String,D> foundEntitiesById,
List<BulkPersistenceResponse.PersistenceFailure<P>> failures) |
<P,D> P |
convertFromPersistentDomain(D domain)
Convenience method to use the
mapper to convert a persistent domain instance to its
business equivalent. |
<P,D,R extends org.springframework.data.repository.CrudRepository<D,String> & com.broadleafcommerce.common.extension.DomainTypeAware> |
create(P businessInstance,
R repository)
Create an instance of an entity based on a business instance containing relevant property
values.
|
<P,D,R extends org.springframework.data.repository.CrudRepository<D,String> & com.broadleafcommerce.common.extension.DomainTypeAware> |
createAll(List<P> businessInstances,
R repository) |
<P,D,R extends org.springframework.data.repository.CrudRepository<D,String> & com.broadleafcommerce.common.extension.DomainTypeAware> |
createAllAllowingPartialSuccess(List<P> businessInstances,
R repository) |
void |
delete(String id,
org.springframework.data.repository.CrudRepository<?,String> repository)
Perform a delete operation on the entity identified by the id.
|
DomainMapperManager |
getMapper()
Provides access to the internal
DomainMapperManager |
protected List<SortTransformer> |
getSortTransformers() |
protected EntityValidatorManager |
getValidator() |
<P,D> List<P> |
readAll(org.springframework.data.repository.CrudRepository<D,String> repository)
Read a list of instances in the form of the business instance.
|
<P,D,R extends org.springframework.data.repository.PagingAndSortingRepository<D,String> & com.broadleafcommerce.common.extension.DomainTypeAware> |
readAll(org.springframework.data.domain.Pageable pageable,
R repository)
Read a page of entity instances in the form of business instances.
|
<P,D,R extends org.springframework.data.repository.PagingAndSortingRepository<D,String> & com.broadleafcommerce.common.extension.DomainTypeAware> |
readAll(org.springframework.data.domain.Sort sort,
R repository)
Read a list of instances in the form of the business instance.
|
<P,D> List<P> |
readAllByIds(Iterable<String> ids,
org.springframework.data.repository.CrudRepository<D,String> repository)
Reads all entity instances matching the provided ids.
|
<P,D> P |
readById(String id,
org.springframework.data.repository.CrudRepository<D,String> repository)
Read a single instance of an entity in the form of a business domain.
|
<P,D> P |
replace(String id,
P businessInstance,
org.springframework.data.repository.CrudRepository<D,String> repository)
Replace a single instance of an entity based on a business instance containing relavant
property values.
|
<P> List<P> |
replaceAll(List<Update<P>> replacements,
org.springframework.data.repository.CrudRepository<Identifiable,String> repository) |
<P> BulkPersistenceResponse<P> |
replaceAllAllowingPartialSuccess(List<Update<P>> replacements,
org.springframework.data.repository.CrudRepository<Identifiable,String> repository) |
org.springframework.data.domain.Sort |
transform(org.springframework.data.domain.Sort sort,
com.broadleafcommerce.common.extension.DomainTypeAware 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,
com.broadleafcommerce.common.extension.DomainTypeAware repository)
Transforms the sort inside of a
Pageable (if applicable). |
<P,D> P |
update(String id,
P businessInstance,
org.springframework.data.repository.CrudRepository<D,String> repository)
Update a single instance of an entity based on a business instance containing relevant
property values.
|
<P> List<P> |
updateAll(List<Update<P>> updates,
org.springframework.data.repository.CrudRepository<Identifiable,String> repository) |
<P> BulkPersistenceResponse<P> |
updateAllAllowingPartialSuccess(List<Update<P>> updates,
org.springframework.data.repository.CrudRepository<Identifiable,String> repository) |
protected <P> void |
validateAllForCreate(List<P> businessInstancesToValidate,
List<P> valid,
List<BulkPersistenceResponse.PersistenceFailure<P>> failed)
Validates each of the given business instances for creation.
|
protected <P> void |
validateAllForCreateAndThrowIfError(Iterable<P> businessInstances) |
protected <P> void |
validateAllForReplace(List<Update<P>> businessInstancesToValidate,
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) |
protected <P,D> void |
validateAllForUpdate(List<org.springframework.data.util.Pair<P,D>> businessInstancesToValidate,
List<org.springframework.data.util.Pair<P,D>> valid,
List<BulkPersistenceResponse.PersistenceFailure<P>> failed)
Validates the business instance within each pair for update.
|
protected <P> void |
validateAllForUpdateAndThrowIfError(Iterable<P> businessInstances) |
public MappableCrudEntityHelper(DomainMapperManager mapper, List<SortTransformer> sortTransformers, EntityValidatorManager validator)
public <P,D> P readById(@NonNull String id, @NonNull org.springframework.data.repository.CrudRepository<D,String> repository)
id
- The id for the entity.repository
- A CrudRepository
to perform the fetch operationEntityMissingException
if not available.public <P,D> List<P> readAllByIds(@NonNull Iterable<String> ids, @NonNull org.springframework.data.repository.CrudRepository<D,String> repository)
public <P,D,R extends org.springframework.data.repository.PagingAndSortingRepository<D,String> & com.broadleafcommerce.common.extension.DomainTypeAware> org.springframework.data.domain.Page<P> readAll(@NonNull org.springframework.data.domain.Pageable pageable, @NonNull R repository)
pageable
- information about which page of results to return. May be
Pageable.unpaged()
, may not be null
.repository
- A PagingAndSortingRepository
to perform the fetch operationpublic <P,D,R extends org.springframework.data.repository.PagingAndSortingRepository<D,String> & com.broadleafcommerce.common.extension.DomainTypeAware> List<P> readAll(@NonNull org.springframework.data.domain.Sort sort, @NonNull R repository)
sort
- Sort
instructions for the queryrepository
- A PagingAndSortingRepository
to perform the fetch operation
involving a Sort
public <P,D> List<P> readAll(@NonNull org.springframework.data.repository.CrudRepository<D,String> repository)
repository
- A CrudRepository
to perform the fetch operationpublic <P,D> P update(@NonNull String id, @NonNull P businessInstance, @NonNull org.springframework.data.repository.CrudRepository<D,String> repository)
id
- The id for the entity.businessInstance
- The payload type that domain class should be converted to. In
general, the payload is what the rest API responds with.repository
- A CrudRepository
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, @NonNull org.springframework.data.repository.CrudRepository<Identifiable,String> repository)
protected <P> void validateAllForUpdateAndThrowIfError(Iterable<P> businessInstances)
public <P> BulkPersistenceResponse<P> updateAllAllowingPartialSuccess(@NonNull List<Update<P>> updates, @NonNull org.springframework.data.repository.CrudRepository<Identifiable,String> repository)
@Nullable protected <P,D> org.springframework.data.util.Pair<P,D> applyUpdate(String entityToUpdateId, P change, Map<String,D> foundEntitiesById, List<BulkPersistenceResponse.PersistenceFailure<P>> failures)
entityToUpdateId
- the 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)foundEntitiesById
- a map of entities by 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 listfoundEntitiesById
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,D> void validateAllForUpdate(List<org.springframework.data.util.Pair<P,D>> businessInstancesToValidate, List<org.springframework.data.util.Pair<P,D>> 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 persistence-domain representation of the same instancevalid
- the list to which successfully validated entities should be addedfailed
- the list to which entities that failed validation should be addedpublic <P,D> P replace(@NonNull String id, @NonNull P businessInstance, @NonNull org.springframework.data.repository.CrudRepository<D,String> repository)
id
- The id for the entity.businessInstance
- The payload type that domain class should be converted to. In
general, the payload is what the rest API responds with.repository
- A CrudRepository
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, @NonNull org.springframework.data.repository.CrudRepository<Identifiable,String> repository)
protected <P> void validateAllForReplaceAndThrowIfError(Iterable<P> businessInstances)
public <P> BulkPersistenceResponse<P> replaceAllAllowingPartialSuccess(@NonNull List<Update<P>> replacements, @NonNull org.springframework.data.repository.CrudRepository<Identifiable,String> repository)
@Nullable protected <P,D> D applyReplace(String entityToReplaceId, P replacement, Map<String,D> foundEntitiesById, List<BulkPersistenceResponse.PersistenceFailure<P>> failures)
entityToReplaceId
- the ID of the entity which needs to be replacedreplacement
- the replacement entityfoundEntitiesById
- a map of entities by 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 listfoundEntitiesById
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, 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 replacevalid
- the list to which successfully validated entities should be addedfailed
- the list to which entities that failed validation should be addedpublic <P,D,R extends org.springframework.data.repository.CrudRepository<D,String> & com.broadleafcommerce.common.extension.DomainTypeAware> P create(@NonNull P businessInstance, @NonNull R repository)
businessInstance
- The business domain instance. In general, the
DomainMapperManager
is responsible for converting to a repository platform
persistence.repositoryDomainType
- The repository domain type that should be that should be mapped
from the businessInstance and persisted.repository
- A CrudRepository
to perform the persistence operationcom.broadleafcommerce.common.error.validation.ValidationException
- if the given businessInstance could not be createdpublic <P,D,R extends org.springframework.data.repository.CrudRepository<D,String> & com.broadleafcommerce.common.extension.DomainTypeAware> List<P> createAll(@NonNull List<P> businessInstances, @NonNull R repository)
protected <P> void validateAllForCreateAndThrowIfError(Iterable<P> businessInstances)
public <P,D,R extends org.springframework.data.repository.CrudRepository<D,String> & com.broadleafcommerce.common.extension.DomainTypeAware> BulkPersistenceResponse<P> createAllAllowingPartialSuccess(@NonNull List<P> businessInstances, @NonNull R repository)
protected <P> void validateAllForCreate(List<P> businessInstancesToValidate, 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 creationvalid
- 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, @NonNull org.springframework.data.repository.CrudRepository<?,String> repository)
id
- The id for the entity.repository
- A CrudRepository
to perform the persistence operationEntityMissingException
- if no instance was found in the backing store corresponding to
idpublic <P,D> P convertFromPersistentDomain(D domain)
mapper
to convert a persistent domain instance to its
business equivalent.P
- The business domain typedomain
- The persistent domain instanceIllegalArgumentException
- Thrown if the business type could not be determined@NonNull public org.springframework.data.domain.Sort transform(@Nullable org.springframework.data.domain.Sort sort, @NonNull com.broadleafcommerce.common.extension.DomainTypeAware 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 DomainTypeAware
repo from 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 com.broadleafcommerce.common.extension.DomainTypeAware 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 DomainTypeAware
implementing repository from which to get
information about the entity's typePageable
exposing the converted Sort
instance@NonNull public DomainMapperManager getMapper()
DomainMapperManager
DomainMapperManager
@NonNull protected List<SortTransformer> getSortTransformers()
@NonNull protected EntityValidatorManager getValidator()
Copyright © 2021. All rights reserved.