Class DefaultOfferService<P extends Offer>

java.lang.Object
com.broadleafcommerce.data.tracking.core.service.BaseCrudEntityService<P>
com.broadleafcommerce.data.tracking.core.service.BaseRsqlCrudEntityService<P>
com.broadleafcommerce.promotion.offer.service.DefaultOfferService<P>
All Implemented Interfaces:
com.broadleafcommerce.data.tracking.core.service.CrudEntityService<P>, com.broadleafcommerce.data.tracking.core.service.RsqlCrudEntityService<P>, OfferService<P>, EventListener, org.springframework.context.ApplicationListener<PromotionCacheInvalidationEvent>

public class DefaultOfferService<P extends Offer> extends com.broadleafcommerce.data.tracking.core.service.BaseRsqlCrudEntityService<P> implements OfferService<P>, org.springframework.context.ApplicationListener<PromotionCacheInvalidationEvent>
Author:
Nathan Moore (nathanmoore).
  • Field Details

  • Constructor Details

    • DefaultOfferService

      public DefaultOfferService(OfferRepository<com.broadleafcommerce.data.tracking.core.Trackable> repository, com.broadleafcommerce.data.tracking.core.service.RsqlCrudEntityHelper helper, com.broadleafcommerce.common.extension.cache.CacheStateManager cacheStateManager, com.broadleafcommerce.data.tracking.core.filtering.fetch.FilterParser<cz.jirutka.rsql.parser.ast.Node> parser)
  • Method Details

    • init

      @PostConstruct public void init()
    • onApplicationEvent

      public void onApplicationEvent(PromotionCacheInvalidationEvent event)
      Specified by:
      onApplicationEvent in interface org.springframework.context.ApplicationListener<P extends Offer>
    • readAutomaticAndCodeAndCustomerOffersIn

      public Stream<P> readAutomaticAndCodeAndCustomerOffersIn(Iterable<String> offerIdsToFilterOn, Iterable<String> trackingIds, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Specified by:
      readAutomaticAndCodeAndCustomerOffersIn in interface OfferService<P extends Offer>
    • readAutomaticAndCodeAndCustomerOffersIn

      public Stream<P> readAutomaticAndCodeAndCustomerOffersIn(Iterable<String> offerIdsToFilterOn, Iterable<String> trackingIds, @Nullable EnhancedOrder order, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Description copied from interface: OfferService
      Reads all active offers where Offer.isAutomaticallyConsidered() is true or the offers match the passed in offerIds or contain the passed in campaign trackingIds.
      Specified by:
      readAutomaticAndCodeAndCustomerOffersIn in interface OfferService<P extends Offer>
      Parameters:
      offerIdsToFilterOn - offerIds the returned Offer(s) are associated with.
      trackingIds - tracking ids the returned Offer(s) are associated with.
      order - the order for this request
      contextInfo - Context information around sandbox and multitenant state.
      Returns:
      the offers matching the given criteria
    • buildCodeResponsesForFilters

      protected void buildCodeResponsesForFilters(Set<P> all, EnhancedOrder order, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
    • pruneInactiveTrackingIds

      protected Iterable<String> pruneInactiveTrackingIds(Iterable<String> trackingIds, com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
    • readActiveByIds

      public List<P> readActiveByIds(Iterable<String> offerIds, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Description copied from interface: OfferService
      Reads all active offers filtering where contextId in offerIds.
      Specified by:
      readActiveByIds in interface OfferService<P extends Offer>
      Parameters:
      offerIds - The context ids to filter on
      contextInfo - Context information around sandbox and multitenant state.
      Returns:
      All active offers filtering where contextId in offerIds.
    • readByIds

      public List<P> readByIds(Iterable<String> offerIds, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Description copied from interface: OfferService
      Reads all offers filtering where contextId in offerIds.
      Specified by:
      readByIds in interface OfferService<P extends Offer>
      Parameters:
      offerIds - The context ids to filter on
      contextInfo - Context information around sandbox and multitenant state.
      Returns:
      All active offers filtering where contextId in offerIds.
    • readVoucherOffersIn

      public Stream<P> readVoucherOffersIn(Iterable<UserTarget> userTargets, Iterable<String> offerIds, Iterable<String> trackingIds, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Description copied from interface: OfferService
      Reads all voucher offers based on the provided user targets, codes, or tracking ids. Skips the active date narrowing to retrieve offers that may have been validly applied in the past.
      Specified by:
      readVoucherOffersIn in interface OfferService<P extends Offer>
      Parameters:
      userTargets - users the returned Offer(s) should target
      offerIds - offer ids the returned Offer(s) are associated with.
      trackingIds - tracking ids the returned Offer(s) are associated with.
      contextInfo - Context information around sandbox and multitenant state.
      Returns:
      all voucher offers based on the provided user targets, codes, or tracking ids
    • readAllCustomerOffersIn

      public Stream<P> readAllCustomerOffersIn(Iterable<UserTarget> userTargets, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Description copied from interface: OfferService
      Reads all active offers where Offer.userTargets intersects with the provided UserTargets, or the offer has no UserTargets
      Specified by:
      readAllCustomerOffersIn in interface OfferService<P extends Offer>
      Parameters:
      userTargets - users the returned Offer(s) should target
      contextInfo - Context information around sandbox and multitenant state.
      Returns:
      the offers matching the given criteria
    • readAllByName

      public org.springframework.data.domain.Page<P> readAllByName(@Nullable String name, cz.jirutka.rsql.parser.ast.Node filters, @Nullable org.springframework.data.domain.Pageable page, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
      Specified by:
      readAllByName in interface OfferService<P extends Offer>
    • readAllActiveByCodesIn

      public List<P> readAllActiveByCodesIn(Iterable<String> codes, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Description copied from interface: OfferService
      Reads all active offers where Offer.getOfferCodes() intersects with the provided codes.
      Specified by:
      readAllActiveByCodesIn in interface OfferService<P extends Offer>
      Parameters:
      codes - codes the returned Offer(s) are associated with.
      contextInfo - Context information around sandbox and multitenant state.
      Returns:
      all active Offers associated to the provided codes.
    • readAllByContextIdInOrExternalIdIn

      public List<P> readAllByContextIdInOrExternalIdIn(Iterable<String> contextIds, Iterable<String> externalIds, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Specified by:
      readAllByContextIdInOrExternalIdIn in interface OfferService<P extends Offer>
    • existsById

      public boolean existsById(String id, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Description copied from interface: OfferService
      Returns whether the offer with the given id exists in the data store
      Specified by:
      existsById in interface OfferService<P extends Offer>
      Parameters:
      id - the id of the offer to find
      contextInfo - context information surrounding sandboxing and multi tenant state
      Returns:
      whether the offer with the given id exists in the data store
    • existsByIdAndActive

      public boolean existsByIdAndActive(String id, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Description copied from interface: OfferService
      Returns whether the offer with the given id exists and is active in the data store.
      Specified by:
      existsByIdAndActive in interface OfferService<P extends Offer>
      Parameters:
      id - the id of the offer to find
      contextInfo - context information surrounding sandboxing and multi tenant state
      Returns:
      whether the offer with the given id exists and is active in the data store
    • getRepositoryDomain

      public String getRepositoryDomain()
      Specified by:
      getRepositoryDomain in interface OfferService<P extends Offer>
    • convertFromDomain

      protected List<P> convertFromDomain(List<com.broadleafcommerce.data.tracking.core.Trackable> offers, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
    • readByTrackingIds

      public List<P> readByTrackingIds(Iterable<String> trackingIds, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Reads all active offers having one or more matches to the requested tracking ids. This is a specialized call in the flow of #readAutomaticAndCodeAndCustomerOffersIn(Iterable, Iterable, Iterable, EnhancedOrder, ContextInfo) that fulfills utilizing cache. From that same method, if cache is disabled, this method will not be called and a more optimized, single query is emitted to cover all the offer relationship filters.
      Specified by:
      readByTrackingIds in interface OfferService<P extends Offer>
      Parameters:
      trackingIds - Tracking ids the returned Offer(s) are associated with
      contextInfo - Context information around sandbox and multitenant state
      Returns:
      Active offers having one or more matches to the requested tracking ids
    • warmCache

      public FullCacheInitializationStatus warmCache()
      Description copied from interface: OfferService
      Call to launch an async process to warm the full offer cache, if enabled. This method may be safely called repeatedly. If it is determined the cache has not been warmed yet, a new thread will spawn to fully populate the cache. Otherwise, the status of the cache warm process will simply be returned. For callers interested in the status, this method may be called repeatedly until the status FullCacheInitializationStatus.READY is returned, indicating the cache is fully warm.

      This method should only be called prior to the service accepting traffic. It is most useful as part of a readiness probe, for example, as part of a kubernetes pod readiness check.
      Specified by:
      warmCache in interface OfferService<P extends Offer>
      Returns:
      The status of the cache warm process.
    • processOfferByIdForCache

      protected List<com.broadleafcommerce.data.tracking.core.Trackable> processOfferByIdForCache(List<String> notCached, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Retrieve offers by ids for the purpose of caching. Also, perform any additional processing on the found records, including adding associations to other caches.
      Parameters:
      notCached - The offer ids that are considered cache misses and must be looked up
      contextInfo - Context information around sandbox and multitenant state
      Returns:
      The repository instances of offers found matching notCached.
    • findPossibleInfosForCacheWarmup

      protected List<com.broadleafcommerce.data.tracking.core.context.ContextInfo> findPossibleInfosForCacheWarmup()
    • buildInfos

      protected List<com.broadleafcommerce.data.tracking.core.context.ContextInfo> buildInfos(String tenant, @Nullable String application)
    • normalize

      @Nullable protected com.broadleafcommerce.data.tracking.core.context.ContextInfo normalize(@Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
    • cacheAllOffers

      protected void cacheAllOffers(@Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      If applicable, cache all active offer in the database into cache memory store for efficient retrieval.
      Parameters:
      contextInfo - Context information around sandbox and multitenant state
    • readAutomaticOffers

      protected List<P> readAutomaticOffers(@Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
    • createCodeResponse

      protected com.broadleafcommerce.promotion.offer.client.web.context.discounts.CodeResponse createCodeResponse(OfferNotAppliedReasonEnum notUsedReason)
    • getRepository

      @NonNull protected OfferRepository<com.broadleafcommerce.data.tracking.core.Trackable> getRepository()
      Overrides:
      getRepository in class com.broadleafcommerce.data.tracking.core.service.BaseCrudEntityService<P extends Offer>
    • getCacheProperties

      @Nullable protected OfferCacheProperties getCacheProperties()
    • setCacheProperties

      @Autowired(required=false) public void setCacheProperties(OfferCacheProperties cacheProperties)
    • getCampaignService

      protected CampaignService<Campaign> getCampaignService()
    • setCampaignService

      @Autowired public void setCampaignService(CampaignService<Campaign> campaignService)
    • getOfferFactory

      protected com.broadleafcommerce.common.extension.TypeFactory getOfferFactory()
    • setOfferFactory

      @Autowired public void setOfferFactory(com.broadleafcommerce.common.extension.TypeFactory offerFactory)
    • getMessageSource

      protected org.springframework.context.MessageSource getMessageSource()
    • setMessageSource

      @Autowired public void setMessageSource(org.springframework.context.MessageSource messageSource)
    • getApplicationRepository

      protected com.broadleafcommerce.data.tracking.jpa.tenant.repository.JpaCommonApplicationRepository<com.broadleafcommerce.data.tracking.jpa.tenant.domain.application.JpaApplication> getApplicationRepository()
    • setApplicationRepository

      @Autowired public void setApplicationRepository(com.broadleafcommerce.data.tracking.jpa.tenant.repository.JpaCommonApplicationRepository<com.broadleafcommerce.data.tracking.jpa.tenant.domain.application.JpaApplication> applicationRepository)