Class DefaultJpaCustomizableSkuInventoryRepository<D extends JpaSkuInventory>
- java.lang.Object
-
- com.broadleafcommerce.inventory.provider.jpa.repository.DefaultJpaCustomizableSkuInventoryRepository<D>
-
- Type Parameters:
D-
- All Implemented Interfaces:
CustomizableSkuInventoryRepository<D>,org.springframework.beans.factory.InitializingBean
@Transactional(transactionManager="inventoryTransactionManager") public class DefaultJpaCustomizableSkuInventoryRepository<D extends JpaSkuInventory> extends Object implements CustomizableSkuInventoryRepository<D>, org.springframework.beans.factory.InitializingBean
Spring Data Repository Fragment for providing some custom query logic, especially related to explicit locking and custom queries. Note that most default queries defined bySkuInventoryRepositorydo not assign locks. These should generally not be used for updates as there is no guarantee about concurrency. Reads that do not assign explicit locks are not guaranteed to be accurate for read or update as another thread may be updating those records concurrently. Read locks are less important because, although you can get unreliable data which may be accurate enough for many applications, the persisted data will be accurate. But, in order to have accurate persisted data in a highly concurrent system, you will need to ensure that write locks are properly applied. However, no read locks are required when having an exact inventory count is not required (where there is some room for inaccuracies).- Author:
- Kelly Tisdell (ktisdell)
- See Also:
SkuInventoryRepository,JpaSkuInventoryRepository
-
-
Constructor Summary
Constructors Constructor Description DefaultJpaCustomizableSkuInventoryRepository(com.broadleafcommerce.data.tracking.core.TrackableBehaviorUtil behaviorUtil, com.broadleafcommerce.data.tracking.jpa.filtering.fetch.rsql.JpaFiltersCriteriaBuilder criteriaBuilder, List<com.broadleafcommerce.data.tracking.core.service.RsqlQueryTransformer> rsqlQueryTransformers, com.broadleafcommerce.data.tracking.jpa.filtering.fetch.rsql.JpaCriteriaBuilderRsqlVisitor rsqlVisitor)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidafterPropertiesSet()protected Optional<javax.persistence.criteria.Predicate>buildContextFilterPredicate(javax.persistence.criteria.Path<?> path, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)Optionally returns basicPredicateforTenantJpaTracking.getTenant()based on theContextInfoargument.protected Optional<javax.persistence.criteria.Predicate>buildInventoryLocationCorrelatedSubQueryPredicate(javax.persistence.criteria.Root<?> root, javax.persistence.criteria.CriteriaQuery<?> criteriaQuery, Optional<Boolean> shippingLocation, Optional<Boolean> pickupLocation, Optional<Boolean> storeLocation, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)This creates a subquery that ensures that the associatedJpaInventoryLocationrecord is valid (i.e.protected Optional<javax.persistence.criteria.Predicate>buildInventoryLocationCorrelatedSubQueryPredicate(javax.persistence.criteria.Root<? extends JpaSkuInventory> root, javax.persistence.criteria.CriteriaQuery<? extends JpaSkuInventory> criteriaQuery, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)This creates a subquery that ensures that the associatedJpaInventoryLocationrecord is valid (i.e.Stream<D>findAll(Optional<cz.jirutka.rsql.parser.ast.Node> filters, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)Generally used for batch export, this allows a caller to query, filter, and stream paged results.Iterable<D>findAllByContextId(Iterable<String> contextIds, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)Overrides defaultTrackableRepository.findByContextId(String, ContextInfo)to add aLockModeType.PESSIMISTIC_WRITElock.List<D>findAvailableBySkuReferenceForQuantity(Map<String,Integer> skuReferenceQuantities, SkuReferenceFieldType skuReferenceFieldType, String inventoryLocationId, com.broadleafcommerce.data.tracking.core.context.ContextInfo context)FindsSkuInventoryrecords with at least the mapped available quantities for a given inventory location.List<D>findAvailableBySkuReferenceForQuantity(Map<String,Integer> skuReferenceQuantities, SkuReferenceFieldType skuReferenceFieldType, Optional<Boolean> shippingLocation, Optional<Boolean> pickupLocation, Optional<Boolean> storeLocation, com.broadleafcommerce.data.tracking.core.context.ContextInfo context)FindsSkuInventoryrecords with the mapped available quantities for given fulfillment types.List<D>findAvailableBySkuReferenceForQuantity(Set<String> skuReferences, int minQuantityAvailable, SkuReferenceFieldType skuReferenceFieldType, Optional<Boolean> shippingLocation, Optional<Boolean> pickupLocation, Optional<Boolean> storeLocation, com.broadleafcommerce.data.tracking.core.context.ContextInfo context)Finds availableSkuInventoryrecords for the provided skuReferences where each have at least the minQuantityAvailable.Optional<D>findByContextId(String contextId, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)Retrieve a domain instance based on the context id (seeTrackable.getContextId()).Optional<D>findByInventoryLocationAndSkuRefFieldValues(String inventoryLocationId, Optional<String> externalId, Optional<String> skuCode, Optional<String> upc, Optional<String> ean, Optional<String> gtin, com.broadleafcommerce.data.tracking.core.context.ContextInfo context)Tries to find a uniqueSkuInventoryrecord based on theInventoryLocation.getId()and any one ofSkuReffield values.List<D>findBySkuReferencesAndLocationId(Set<String> skuReferences, Integer minQuantity, String locationId, SkuReferenceFieldType skuReferenceFieldType, com.broadleafcommerce.data.tracking.core.context.ContextInfo context)RetrievesSkuInventoryrecords, filtering onSkuRef, based on the skuReferenceFieldType parameter, andInventoryLocation.getId().protected com.broadleafcommerce.data.tracking.core.TrackableBehaviorUtilgetBehaviorUtil()protected com.broadleafcommerce.data.tracking.jpa.filtering.fetch.rsql.JpaFiltersCriteriaBuildergetCriteriaBuilder()protected javax.persistence.EntityManagergetEntityManager()protected com.broadleafcommerce.data.tracking.jpa.filtering.narrow.JpaNarrowingHelpergetNarrowingHelper()protected List<com.broadleafcommerce.data.tracking.core.service.RsqlQueryTransformer>getRsqlQueryTransformers()protected com.broadleafcommerce.data.tracking.jpa.filtering.fetch.rsql.JpaCriteriaBuilderRsqlVisitorgetRsqlVisitor()protected SkuInventoryRepository<D>getSkuInventoryRepository()booleanisAvailableBySkuReferenceForQuantity(String skuReference, int quantity, SkuReferenceFieldType skuReferenceFieldType, Optional<Boolean> shippingLocation, Optional<Boolean> pickupLocation, Optional<Boolean> storeLocation, com.broadleafcommerce.data.tracking.core.context.ContextInfo context)Determines if theSkuInventory, identified by skuReference, is available for the specified quantity across one or moreInventoryLocations, identified by their allowed fulfillment types.Stream<D>readAllByInventoryLocationId(String inventoryLocationId, cz.jirutka.rsql.parser.ast.Node filters, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)Read all Sku Inventories for the specified inventory location id matching the given filters.Object[][]readSummaryAvailability(SkuReferenceFieldType skuReferenceFieldType, Set<String> skuReferences, int minQuantity, Optional<Boolean> shippingLocation, Optional<Boolean> pickupLocation, Optional<Boolean> storeLocation, com.broadleafcommerce.data.tracking.core.context.ContextInfo context)Queries for applications and tenant IDs associated with the requested availability.protected StringresolveSkuRefFieldName(SkuReferenceFieldType skuRefFieldType)Resolves the name of the field that should be used in a Predicate based on the providedSkuReferenceFieldType.voidsetSkuInventoryRepository(SkuInventoryRepository<D> skuInventoryRepository)
-
-
-
Constructor Detail
-
DefaultJpaCustomizableSkuInventoryRepository
public DefaultJpaCustomizableSkuInventoryRepository(@NonNull com.broadleafcommerce.data.tracking.core.TrackableBehaviorUtil behaviorUtil, @NonNull com.broadleafcommerce.data.tracking.jpa.filtering.fetch.rsql.JpaFiltersCriteriaBuilder criteriaBuilder, @NonNull List<com.broadleafcommerce.data.tracking.core.service.RsqlQueryTransformer> rsqlQueryTransformers, @NonNull com.broadleafcommerce.data.tracking.jpa.filtering.fetch.rsql.JpaCriteriaBuilderRsqlVisitor rsqlVisitor)
-
-
Method Detail
-
findBySkuReferencesAndLocationId
@NonNull @Policy(operationTypes=READ) public List<D> findBySkuReferencesAndLocationId(@NonNull Set<String> skuReferences, @Nullable Integer minQuantity, @NonNull String locationId, @NonNull SkuReferenceFieldType skuReferenceFieldType, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
Description copied from interface:CustomizableSkuInventoryRepositoryRetrievesSkuInventoryrecords, filtering onSkuRef, based on the skuReferenceFieldType parameter, andInventoryLocation.getId().- Specified by:
findBySkuReferencesAndLocationIdin interfaceCustomizableSkuInventoryRepository<D extends JpaSkuInventory>minQuantity- - (optional) the expected minimum quantity availablelocationId- - the inventory location in which to find inventoryskuReferenceFieldType- - the type of SKU reference (identifies whatSkuReffield should be filtered by the skuReferences argument- Returns:
-
findByContextId
@Policy(operationTypes=READ) public Optional<D> findByContextId(String contextId, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
Description copied from interface:CustomizableSkuInventoryRepositoryRetrieve a domain instance based on the context id (seeTrackable.getContextId()). If aNarrowExecutorimplementation was specified viaNarrow, the result should be narrowed based on sandbox and catalog state. This overrides the methodTrackableRepository.findByContextId(String, ContextInfo). The mixin implementation will provide an exclusive write lock, if applicable, to prevent concurrent modification ofSkuInventoryrecords.- Specified by:
findByContextIdin interfaceCustomizableSkuInventoryRepository<D extends JpaSkuInventory>- Parameters:
contextId- An identifying idcontextInfo- Request context information around sandbox and multitenant state- Returns:
- If nothing found, an empty Optional. Otherwise, an Optional containing the narrowed entity.
-
findAllByContextId
@Policy(operationTypes=READ) public Iterable<D> findAllByContextId(Iterable<String> contextIds, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
Overrides defaultTrackableRepository.findByContextId(String, ContextInfo)to add aLockModeType.PESSIMISTIC_WRITElock. Methods like this are used in update functionality withinCrudEntityHelperandBaseCrudEntityService.- Specified by:
findAllByContextIdin interfaceCustomizableSkuInventoryRepository<D extends JpaSkuInventory>- Parameters:
contextIds- A list of identifying idscontextInfo- Request context information around sandbox and multitenant state- Returns:
- The list of narrowed entities
-
findAvailableBySkuReferenceForQuantity
@Policy(operationTypes=READ) public List<D> findAvailableBySkuReferenceForQuantity(@NonNull Map<String,Integer> skuReferenceQuantities, @NonNull SkuReferenceFieldType skuReferenceFieldType, @NonNull Optional<Boolean> shippingLocation, @NonNull Optional<Boolean> pickupLocation, @NonNull Optional<Boolean> storeLocation, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
Description copied from interface:CustomizableSkuInventoryRepositoryFindsSkuInventoryrecords with the mapped available quantities for given fulfillment types.- Specified by:
findAvailableBySkuReferenceForQuantityin interfaceCustomizableSkuInventoryRepository<D extends JpaSkuInventory>- Returns:
-
findAvailableBySkuReferenceForQuantity
@Policy(operationTypes=READ) public List<D> findAvailableBySkuReferenceForQuantity(@NonNull Map<String,Integer> skuReferenceQuantities, @NonNull SkuReferenceFieldType skuReferenceFieldType, @NonNull String inventoryLocationId, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
Description copied from interface:CustomizableSkuInventoryRepositoryFindsSkuInventoryrecords with at least the mapped available quantities for a given inventory location.- Specified by:
findAvailableBySkuReferenceForQuantityin interfaceCustomizableSkuInventoryRepository<D extends JpaSkuInventory>- Returns:
-
readAllByInventoryLocationId
public Stream<D> readAllByInventoryLocationId(String inventoryLocationId, @Nullable cz.jirutka.rsql.parser.ast.Node filters, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
Description copied from interface:CustomizableSkuInventoryRepositoryRead all Sku Inventories for the specified inventory location id matching the given filters.- Specified by:
readAllByInventoryLocationIdin interfaceCustomizableSkuInventoryRepository<D extends JpaSkuInventory>- Parameters:
inventoryLocationId- the inventory location idfilters- additional filters to apply in the query, can be nullcontextInfo- context information surrounding sandboxing and multitenant state- Returns:
- the Sku Inventories for the specified inventory location id
-
readSummaryAvailability
public Object[][] readSummaryAvailability(SkuReferenceFieldType skuReferenceFieldType, Set<String> skuReferences, int minQuantity, Optional<Boolean> shippingLocation, Optional<Boolean> pickupLocation, Optional<Boolean> storeLocation, com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
Description copied from interface:CustomizableSkuInventoryRepositoryQueries for applications and tenant IDs associated with the requested availability. The response is a an Object array that contains: - skuReference (i.e. typically skuCode, but depends on what is requested by skuReferenceFieldType) - tenantId - applicationId (nullable) This method is pretty specific as it allows the Search Services, for example, to determine, for which SKUs have general availability across tenants and applications, not specific to inventory locations.- Specified by:
readSummaryAvailabilityin interfaceCustomizableSkuInventoryRepository<D extends JpaSkuInventory>- Parameters:
skuReferenceFieldType- The type of fields for which we are search (e.g. SKU_CODE)skuReferences- The SKU reference number or code e.g.SkuRef.skuCodeshippingLocation- Indicates if we want to include availability at shipping locationspickupLocation- - Indicates if we want to include availability at pickup locationsstoreLocation- - Indicates if we want to include availability at store locationscontext- the ContextInfo for this request- Returns:
- An object array, representing the rows and columns from the query
-
findAvailableBySkuReferenceForQuantity
@Policy(operationTypes=READ) public List<D> findAvailableBySkuReferenceForQuantity(@NonNull Set<String> skuReferences, int minQuantityAvailable, @NonNull SkuReferenceFieldType skuReferenceFieldType, @NonNull Optional<Boolean> shippingLocation, @NonNull Optional<Boolean> pickupLocation, @NonNull Optional<Boolean> storeLocation, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
Description copied from interface:CustomizableSkuInventoryRepositoryFinds availableSkuInventoryrecords for the provided skuReferences where each have at least the minQuantityAvailable.- Specified by:
findAvailableBySkuReferenceForQuantityin interfaceCustomizableSkuInventoryRepository<D extends JpaSkuInventory>- Returns:
-
findByInventoryLocationAndSkuRefFieldValues
@Policy(operationTypes=READ) public Optional<D> findByInventoryLocationAndSkuRefFieldValues(String inventoryLocationId, @NonNull Optional<String> externalId, @NonNull Optional<String> skuCode, @NonNull Optional<String> upc, @NonNull Optional<String> ean, @NonNull Optional<String> gtin, com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
Description copied from interface:CustomizableSkuInventoryRepositoryTries to find a uniqueSkuInventoryrecord based on theInventoryLocation.getId()and any one ofSkuReffield values. Note that for anySkuInventoryrecord, there can be only one unique SKU identifier, which includes any number of unique fields, perInventoryLocation.- Specified by:
findByInventoryLocationAndSkuRefFieldValuesin interfaceCustomizableSkuInventoryRepository<D extends JpaSkuInventory>- Returns:
-
isAvailableBySkuReferenceForQuantity
@Policy(operationTypes=READ) public boolean isAvailableBySkuReferenceForQuantity(@NonNull String skuReference, int quantity, @NonNull SkuReferenceFieldType skuReferenceFieldType, @NonNull Optional<Boolean> shippingLocation, @NonNull Optional<Boolean> pickupLocation, @NonNull Optional<Boolean> storeLocation, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo context)Description copied from interface:CustomizableSkuInventoryRepositoryDetermines if theSkuInventory, identified by skuReference, is available for the specified quantity across one or moreInventoryLocations, identified by their allowed fulfillment types. SkuRef is determined by the skuReference argument and the- Specified by:
isAvailableBySkuReferenceForQuantityin interfaceCustomizableSkuInventoryRepository<D extends JpaSkuInventory>quantity- - requested quantityskuReferenceFieldType- - the type of skuReference being passed inshippingLocation- -Optionalvalue represents whether we should query shipping locations. If not present, no filtering will be applied.pickupLocation- -Optionalvalue represents whether we should query pickup locations. If not present, no filtering will be applied.storeLocation- -Optionalvalue represents whether we should query store locations. If not present, no filtering will be applied.- Returns:
-
findAll
@Policy(operationTypes=READ) public Stream<D> findAll(@NonNull Optional<cz.jirutka.rsql.parser.ast.Node> filters, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
Description copied from interface:CustomizableSkuInventoryRepositoryGenerally used for batch export, this allows a caller to query, filter, and stream paged results.- Specified by:
findAllin interfaceCustomizableSkuInventoryRepository<D extends JpaSkuInventory>- Returns:
-
buildInventoryLocationCorrelatedSubQueryPredicate
protected Optional<javax.persistence.criteria.Predicate> buildInventoryLocationCorrelatedSubQueryPredicate(javax.persistence.criteria.Root<? extends JpaSkuInventory> root, javax.persistence.criteria.CriteriaQuery<? extends JpaSkuInventory> criteriaQuery, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
This creates a subquery that ensures that the associatedJpaInventoryLocationrecord is valid (i.e. that it exists, it is not archived, and it belongs to the same context / tenant as theJpaSkuInventoryrecord). We do this instead of a join to avoid complications around pessimistic locking across tables, as well as to avoid pulling back additional (unnecessary) data in aManyToOnejoin that would otherwise require a Hibernate proxy to lazy load the data.- Parameters:
root-criteriaQuery-contextInfo-- Returns:
-
buildInventoryLocationCorrelatedSubQueryPredicate
protected Optional<javax.persistence.criteria.Predicate> buildInventoryLocationCorrelatedSubQueryPredicate(javax.persistence.criteria.Root<?> root, javax.persistence.criteria.CriteriaQuery<?> criteriaQuery, Optional<Boolean> shippingLocation, Optional<Boolean> pickupLocation, Optional<Boolean> storeLocation, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
This creates a subquery that ensures that the associatedJpaInventoryLocationrecord is valid (i.e. that it exists, it is not archived, and it belongs to the same context / tenant as theJpaSkuInventoryrecord). We do this instead of a join to avoid complications around pessimistic locking across tables, as well as to avoid pulling back additional (unnecessary) data in aManyToOnejoin that would otherwise require a Hibernate proxy to lazy load the data. This also filters based on whether theInventoryLocationis a shippingLocation, pickupLocation, or storeLocation.- Parameters:
root-criteriaQuery-shippingLocation-pickupLocation-storeLocation-contextInfo-- Returns:
-
buildContextFilterPredicate
protected Optional<javax.persistence.criteria.Predicate> buildContextFilterPredicate(javax.persistence.criteria.Path<?> path, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
Optionally returns basicPredicateforTenantJpaTracking.getTenant()based on theContextInfoargument. This predicate provides filtering for tenantId and archived flags. Only use this to query or filterPathelements that represent entities that implementTrackable. This is applied to queries forJpaSkuInventoryas well as to correlated subqueries forJpaInventoryLocation.- Parameters:
path-contextInfo-- Returns:
-
resolveSkuRefFieldName
protected String resolveSkuRefFieldName(SkuReferenceFieldType skuRefFieldType)
Resolves the name of the field that should be used in a Predicate based on the providedSkuReferenceFieldType.- Parameters:
skuRefFieldType-- Returns:
-
setSkuInventoryRepository
@Autowired public void setSkuInventoryRepository(SkuInventoryRepository<D> skuInventoryRepository)
-
afterPropertiesSet
public void afterPropertiesSet() throws Exception- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean- Throws:
Exception
-
getEntityManager
protected javax.persistence.EntityManager getEntityManager()
-
getSkuInventoryRepository
protected SkuInventoryRepository<D> getSkuInventoryRepository()
-
getNarrowingHelper
protected com.broadleafcommerce.data.tracking.jpa.filtering.narrow.JpaNarrowingHelper getNarrowingHelper()
-
getBehaviorUtil
protected com.broadleafcommerce.data.tracking.core.TrackableBehaviorUtil getBehaviorUtil()
-
getCriteriaBuilder
protected com.broadleafcommerce.data.tracking.jpa.filtering.fetch.rsql.JpaFiltersCriteriaBuilder getCriteriaBuilder()
-
getRsqlQueryTransformers
protected List<com.broadleafcommerce.data.tracking.core.service.RsqlQueryTransformer> getRsqlQueryTransformers()
-
getRsqlVisitor
protected com.broadleafcommerce.data.tracking.jpa.filtering.fetch.rsql.JpaCriteriaBuilderRsqlVisitor getRsqlVisitor()
-
-