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 a 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 CrudRepository 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 recommended 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 Details

  • Method Details

    • 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 - A CrudRepository 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.
      Parameters:
      ids - Iterable of ids to read
      repository - A CrudRepository to perform the fetch operation
      Returns:
      List of matching instances
    • readAll

      public <P, D, R extends com.broadleafcommerce.common.extension.data.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 be Pageable.unpaged(), may not be null.
      repository - A PagingAndSortingRepository to perform the fetch operation
      Returns:
      the requested page of entity instances in the form of business instances
    • readAll

      public <P, D, R extends com.broadleafcommerce.common.extension.data.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 query
      repository - A PagingAndSortingRepository to perform the fetch operation involving a Sort
      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 - A CrudRepository 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 - A CrudRepository 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 validation
      EntityMissingException - 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 applied
      change - 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 be null and the failure details will be added to failures.
    • 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 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.
      Parameters:
      businessInstancesToValidate - a list of pairs containing the business instances to validate for update, and the persistence-domain representation of the same instance
      valid - the list to which successfully validated entities should be added
      failed - 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 - A CrudRepository 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 replacement
      EntityMissingException - 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 replaced
      replacement - the replacement entity
      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:
      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 be null and the failure details will be added to failures.
    • validateAllForReplace

      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. 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.
      Parameters:
      businessInstancesToValidate - a list of updates containing the business instances to validate for replace
      valid - the list to which successfully validated entities should be added
      failed - 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, 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 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 the valid list. If it fails validation, a BulkPersistenceResponse.PersistenceFailure is created for it and that failure is added to the failed list.
      Parameters:
      businessInstancesToValidate - the business instances to validate for creation
      valid - the list to which successfully validated entities should be added
      failed - 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 - A CrudRepository 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 the mapper 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 given sort from properties targeting the projection layer into a Sort that targets properties at the domain layer
      Parameters:
      sort - the projection-targeted sort, can be null
      repository - A DomainTypeAware repo from which to get information about the entity's type
      Returns:
      null if sort is null, otherwise a Sort targeting the repository provider domain
      See Also:
    • 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 a Pageable (if applicable). Convert the sort from properties targeting the projection layer into a Sort that targets properties at the domain layer
      Parameters:
      pageable - The Pageable containing the Sort instance to convert
      repository - A DomainTypeAware implementing repository from which to get information about the entity's type
      Returns:
      The Pageable exposing the converted Sort instance
    • getMapper

      @NonNull public DomainMapperManager getMapper()
      Provides access to the internal DomainMapperManager
      Returns:
      the internal DomainMapperManager
    • getSortTransformers

      @NonNull protected List<SortTransformer> getSortTransformers()
    • getValidator

      @NonNull protected EntityValidatorManager getValidator()