Class MappableCrudEntityHelper
- java.lang.Object
-
- com.broadleafcommerce.data.tracking.core.service.MappableCrudEntityHelper
-
public class MappableCrudEntityHelper extends Object
Helper class responsible for providing basic CRUD support for a business domain type. Using a repository instance and aDomainMapperManager
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 ofMappableCrudEntityServices
. Most of the methods require supplying theMappableCrudRepository
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; } }- Author:
- Jeff Fischer, Nathan Moore (nathandmoore)
-
-
Constructor Summary
Constructors Constructor Description MappableCrudEntityHelper(DomainMapperManager mapper, List<SortTransformer> sortTransformers, EntityValidatorManager validator)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected <P,D>
DapplyReplace(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>
PconvertFromPersistentDomain(D domain)
Convenience method to use themapper
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>
Pcreate(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>
List<P>createAll(List<P> businessInstances, R repository)
<P,D,R extends org.springframework.data.repository.CrudRepository<D,String> & com.broadleafcommerce.common.extension.DomainTypeAware>
BulkPersistenceResponse<P>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 internalDomainMapperManager
protected List<SortTransformer>
getSortTransformers()
protected EntityValidatorManager
getValidator()
<P,D,R extends org.springframework.data.repository.PagingAndSortingRepository<D,String> & com.broadleafcommerce.common.extension.DomainTypeAware>
org.springframework.data.domain.Page<P>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>
List<P>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>readAll(org.springframework.data.repository.CrudRepository<D,String> 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>
PreadById(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>
Preplace(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 givensort
from properties targeting the projection layer into aSort
that targets properties at the domain layerorg.springframework.data.domain.Pageable
transformSort(org.springframework.data.domain.Pageable pageable, com.broadleafcommerce.common.extension.DomainTypeAware repository)
Transforms the sort inside of aPageable
(if applicable).<P,D>
Pupdate(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 eachUpdate
for replacement.protected <P> void
validateAllForReplaceAndThrowIfError(Iterable<P> businessInstances)
protected <P,D>
voidvalidateAllForUpdate(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)
-
-
-
Constructor Detail
-
MappableCrudEntityHelper
public MappableCrudEntityHelper(DomainMapperManager mapper, List<SortTransformer> sortTransformers, EntityValidatorManager validator)
-
-
Method Detail
-
readById
public <P,D> P readById(@NonNull String id, @NonNull org.springframework.data.repository.CrudRepository<D,String> repository)
Read a single instance of an entity in the form of a business domain.- Parameters:
id
- The id for the entity.repository
- ACrudRepository
to perform the fetch operation- Returns:
- The entity instance in the form of a business instance, or
EntityMissingException
if not available.
-
readAllByIds
public <P,D> List<P> readAllByIds(@NonNull Iterable<String> ids, @NonNull org.springframework.data.repository.CrudRepository<D,String> repository)
Reads all entity instances matching the provided ids.
-
readAll
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)
Read a page of entity instances in the form of business instances.- Parameters:
pageable
- information about which page of results to return. May bePageable.unpaged()
, may not benull
.repository
- APagingAndSortingRepository
to perform the fetch operation- Returns:
- the requested page of entity instances in the form of business instances
-
readAll
public <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)
Read a list of instances in the form of the business instance.- Parameters:
sort
-Sort
instructions for the queryrepository
- APagingAndSortingRepository
to perform the fetch operation involving aSort
- Returns:
- The list of data in the form of business instances
-
readAll
public <P,D> List<P> readAll(@NonNull org.springframework.data.repository.CrudRepository<D,String> repository)
Read a list of instances in the form of the business instance.- Parameters:
repository
- ACrudRepository
to perform the fetch operation- Returns:
- The list of data in the form of business instances
-
update
public <P,D> P update(@NonNull String id, @NonNull P businessInstance, @NonNull org.springframework.data.repository.CrudRepository<D,String> repository)
Update a single instance of an entity based on a business instance containing relevant property values.- Parameters:
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
- ACrudRepository
to perform the persistence operation- Returns:
- The updated entity instance in the form of a payload instance, or
EntityMissingException
if not available. - Throws:
com.broadleafcommerce.common.error.validation.ValidationException
- if the businessInstance failed validationEntityMissingException
- if a managed instance corresponding to the given id could not be found
-
updateAll
public <P> List<P> updateAll(@NonNull List<Update<P>> updates, @NonNull org.springframework.data.repository.CrudRepository<Identifiable,String> repository)
-
validateAllForUpdateAndThrowIfError
protected <P> void validateAllForUpdateAndThrowIfError(Iterable<P> businessInstances)
-
updateAllAllowingPartialSuccess
public <P> BulkPersistenceResponse<P> updateAllAllowingPartialSuccess(@NonNull List<Update<P>> updates, @NonNull org.springframework.data.repository.CrudRepository<Identifiable,String> repository)
-
applyUpdate
@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)
- Parameters:
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 list- Returns:
- a pair of the business-domain and persistence-domain representations of the entity
after successfully applying the update. If the entity to update could not be found in
foundEntitiesById
or if there was some error in mapping the updates, the return value will benull
and the failure details will be added tofailures
.
-
validateAllForUpdate
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. If it passes validation, the pair is added to thevalid
list. If it fails validation, aBulkPersistenceResponse.PersistenceFailure
is created with the business instance and that failure is added to thefailed
list.- Parameters:
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 added
-
replace
public <P,D> P replace(@NonNull String id, @NonNull P businessInstance, @NonNull org.springframework.data.repository.CrudRepository<D,String> repository)
Replace a single instance of an entity based on a business instance containing relavant property values.- Parameters:
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
- ACrudRepository
to perform the persistence operation- Returns:
- The replaced entity instance in the form of a payload instance, or
EntityMissingException
if not available. - Throws:
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 found
-
replaceAll
public <P> List<P> replaceAll(@NonNull List<Update<P>> replacements, @NonNull org.springframework.data.repository.CrudRepository<Identifiable,String> repository)
-
validateAllForReplaceAndThrowIfError
protected <P> void validateAllForReplaceAndThrowIfError(Iterable<P> businessInstances)
-
replaceAllAllowingPartialSuccess
public <P> BulkPersistenceResponse<P> replaceAllAllowingPartialSuccess(@NonNull List<Update<P>> replacements, @NonNull org.springframework.data.repository.CrudRepository<Identifiable,String> repository)
-
applyReplace
@Nullable protected <P,D> D applyReplace(String entityToReplaceId, P replacement, Map<String,D> foundEntitiesById, List<BulkPersistenceResponse.PersistenceFailure<P>> failures)
- Parameters:
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 list- Returns:
- the persistence representation of the entity after successfully applying the
replacement. If the entity to replace could not be found in
foundEntitiesById
or if there was some error in mapping the change, the return value will benull
and the failure details will be added tofailures
.
-
validateAllForReplace
protected <P> void validateAllForReplace(List<Update<P>> businessInstancesToValidate, List<Update<P>> valid, List<BulkPersistenceResponse.PersistenceFailure<P>> failed)
Validates the business instance within eachUpdate
for replacement. If it passes validation, the update is added to thevalid
list. If it fails validation, aBulkPersistenceResponse.PersistenceFailure
is created with the business instance and that failure is added to thefailed
list.- Parameters:
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 added
-
create
public <P,D,R extends org.springframework.data.repository.CrudRepository<D,String> & com.broadleafcommerce.common.extension.DomainTypeAware> P create(@NonNull P businessInstance, @NonNull R repository)
Create an instance of an entity based on a business instance containing relevant property values.- Parameters:
businessInstance
- The business domain instance. In general, theDomainMapperManager
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
- ACrudRepository
to perform the persistence operation- Returns:
- The new entity instance in the form of a business type instance.
- Throws:
com.broadleafcommerce.common.error.validation.ValidationException
- if the given businessInstance could not be created
-
createAll
public <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)
-
validateAllForCreateAndThrowIfError
protected <P> void validateAllForCreateAndThrowIfError(Iterable<P> businessInstances)
-
createAllAllowingPartialSuccess
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)
-
validateAllForCreate
protected <P> void validateAllForCreate(List<P> businessInstancesToValidate, List<P> valid, List<BulkPersistenceResponse.PersistenceFailure<P>> failed)
Validates each of the given business instances for creation. If it passes validation, it is added to thevalid
list. If it fails validation, aBulkPersistenceResponse.PersistenceFailure
is created for it and that failure is added to thefailed
list.- Parameters:
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 added
-
delete
public void delete(@NonNull String id, @NonNull org.springframework.data.repository.CrudRepository<?,String> repository)
Perform a delete operation on the entity identified by the id.- Parameters:
id
- The id for the entity.repository
- ACrudRepository
to perform the persistence operation- Throws:
EntityMissingException
- if no instance was found in the backing store corresponding to id
-
convertFromPersistentDomain
public <P,D> P convertFromPersistentDomain(D domain)
Convenience method to use themapper
to convert a persistent domain instance to its business equivalent.- Type Parameters:
P
- The business domain type- Parameters:
domain
- The persistent domain instance- Returns:
- The business domain instance equivalent to the given persistent instance.
- Throws:
IllegalArgumentException
- Thrown if the business type could not be determined
-
transform
@NonNull public org.springframework.data.domain.Sort transform(@Nullable org.springframework.data.domain.Sort sort, @NonNull com.broadleafcommerce.common.extension.DomainTypeAware repository)
Transforms the givensort
from properties targeting the projection layer into aSort
that targets properties at the domain layer- Parameters:
sort
- the projection-targeted sort, can be nullrepository
- ADomainTypeAware
repo from which to get information about the entity's type- Returns:
- null if
sort
is null, otherwise aSort
targeting the repository provider domain - See Also:
SortTransformer
-
transformSort
@NonNull public org.springframework.data.domain.Pageable transformSort(@NonNull org.springframework.data.domain.Pageable pageable, @NonNull com.broadleafcommerce.common.extension.DomainTypeAware repository)
Transforms the sort inside of aPageable
(if applicable). Convert thesort
from properties targeting the projection layer into aSort
that targets properties at the domain layer- Parameters:
pageable
- ThePageable
containing theSort
instance to convertrepository
- ADomainTypeAware
implementing repository from which to get information about the entity's type- Returns:
- The
Pageable
exposing the convertedSort
instance
-
getMapper
@NonNull public DomainMapperManager getMapper()
Provides access to the internalDomainMapperManager
- Returns:
- the internal
DomainMapperManager
-
getSortTransformers
@NonNull protected List<SortTransformer> getSortTransformers()
-
getValidator
@NonNull protected EntityValidatorManager getValidator()
-
-