Class DefaultOfferCodeService<P extends OfferCode>

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

public class DefaultOfferCodeService<P extends OfferCode> extends com.broadleafcommerce.data.tracking.core.service.BaseRsqlCrudEntityService<P> implements OfferCodeService<P>, org.springframework.context.ApplicationListener<PromotionCacheInvalidationEvent>
Author:
Karan Jariwala (karan-j-blc)
  • Field Details

  • Constructor Details

    • DefaultOfferCodeService

      public DefaultOfferCodeService(OfferCodeRepository<com.broadleafcommerce.data.tracking.core.Trackable> offerCodeRepository, CampaignRepository<com.broadleafcommerce.data.tracking.core.Trackable> campaignRepository, OfferRepository<com.broadleafcommerce.data.tracking.core.Trackable> offerRepository, com.broadleafcommerce.data.tracking.core.service.RsqlCrudEntityHelper helper, com.broadleafcommerce.common.extension.TypeFactory campaignFactory)
  • Method Details

    • init

      @PostConstruct public void init()
    • onApplicationEvent

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

      @NonNull public Map<P,List<Offer>> findOffersAndOfferCodesByCodes(@NonNull List<String> codes, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Specified by:
      findOffersAndOfferCodesByCodes in interface OfferCodeService<P extends OfferCode>
    • findByCampaignId

      @NonNull public org.springframework.data.domain.Page<P> findByCampaignId(@NonNull String campaignId, @Nullable cz.jirutka.rsql.parser.ast.Node filters, @Nullable org.springframework.data.domain.Pageable page, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Specified by:
      findByCampaignId in interface OfferCodeService<P extends OfferCode>
    • findByOfferId

      @NonNull public org.springframework.data.domain.Page<P> findByOfferId(@NonNull String offerId, @Nullable cz.jirutka.rsql.parser.ast.Node filters, @Nullable org.springframework.data.domain.Pageable page, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Specified by:
      findByOfferId in interface OfferCodeService<P extends OfferCode>
    • readAllByCode

      @NonNull public List<P> readAllByCode(@NonNull String code, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Description copied from interface: OfferCodeService
      Finds all of the OfferCodes by code.
      Specified by:
      readAllByCode in interface OfferCodeService<P extends OfferCode>
      Parameters:
      code - Code to match
      contextInfo - Additional sandbox and multitenant state data
      Returns:
      all of the OfferCodes by code
    • readAllByCodes

      @NonNull public List<P> readAllByCodes(@NonNull Set<String> codes, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Description copied from interface: OfferCodeService
      Finds all of the OfferCodes by codes.
      Specified by:
      readAllByCodes in interface OfferCodeService<P extends OfferCode>
      Parameters:
      codes - Codes to match
      contextInfo - Additional sandbox and multitenant state data
      Returns:
      all of the OfferCodes by code
    • create

      public P create(@NonNull P businessInstance, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Specified by:
      create in interface com.broadleafcommerce.data.tracking.core.service.CrudEntityService<P extends OfferCode>
      Overrides:
      create in class com.broadleafcommerce.data.tracking.core.service.BaseCrudEntityService<P extends OfferCode>
    • update

      public P update(@NonNull @NonNull String id, @NonNull P businessInstance, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
      Specified by:
      update in interface com.broadleafcommerce.data.tracking.core.service.CrudEntityService<P extends OfferCode>
      Overrides:
      update in class com.broadleafcommerce.data.tracking.core.service.BaseCrudEntityService<P extends OfferCode>
    • replace

      public P replace(@NonNull @NonNull String id, @NonNull P businessInstance, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
      Specified by:
      replace in interface com.broadleafcommerce.data.tracking.core.service.CrudEntityService<P extends OfferCode>
      Overrides:
      replace in class com.broadleafcommerce.data.tracking.core.service.BaseCrudEntityService<P extends OfferCode>
    • saveAll

      public List<com.broadleafcommerce.data.tracking.core.Trackable> saveAll(@NonNull @NonNull List<P> businessInstances, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
      Specified by:
      saveAll in interface OfferCodeService<P extends OfferCode>
    • persistCodesAndReturnNumber

      public int persistCodesAndReturnNumber(CodeGenerator info, Set<String> currentCodeSet, Class<P> payloadType, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Specified by:
      persistCodesAndReturnNumber in interface OfferCodeService<P extends OfferCode>
    • persistCodesAndReturn

      public List<P> persistCodesAndReturn(CodeGenerator info, Set<String> currentCodeSet, Class<P> payloadType, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Specified by:
      persistCodesAndReturn in interface OfferCodeService<P extends OfferCode>
    • readAllByCampaignId

      public Stream<P> readAllByCampaignId(String campaignId, @Nullable cz.jirutka.rsql.parser.ast.Node filters, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
      Description copied from interface: OfferCodeService
      Reads the offer codes for the specified campaign id matching the given filters.
      Specified by:
      readAllByCampaignId in interface OfferCodeService<P extends OfferCode>
      Parameters:
      campaignId - the campaign id
      filters - additional filters to apply in the query
      context - Request context information around sandbox and multitenant state
      Returns:
      the offer codes for the specified campaign id matching the given filters
    • readAllByOfferId

      public Stream<P> readAllByOfferId(String offerId, @Nullable cz.jirutka.rsql.parser.ast.Node filters, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
      Description copied from interface: OfferCodeService
      Reads the offer codes for the specified offer id matching the given filters.
      Specified by:
      readAllByOfferId in interface OfferCodeService<P extends OfferCode>
      Parameters:
      offerId - the offer id
      filters - additional filters to apply in the query
      context - Request context information around sandbox and multitenant state
      Returns:
      the offer codes for the specified offer id matching the given filters
    • findExistingByCodesIncludeExpiredForCampaignId

      public List<P> findExistingByCodesIncludeExpiredForCampaignId(Set<String> codes, String campaignId, Instant endAfterDate, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
      Description copied from interface: OfferCodeService
      Search all non-expired offer codes or expired but belonging to the specified campaignId. This is useful to validate that the offer codes can be added or updated. NOTE: This method is used during import of OfferCode and due to the varying limitations on how many entries can be in an "in" statement please make sure that the property "broadleaf.dataimport.processor.batch-size" does not exceed this limitation.
      Specified by:
      findExistingByCodesIncludeExpiredForCampaignId in interface OfferCodeService<P extends OfferCode>
      Parameters:
      codes - The set of codes to match
      campaignId - the campaign id
      endAfterDate - Date after which code should be active
      context - Request context information around sandbox and multitenant state
      Returns:
      all OfferCodes by OfferCode.getCode() that are also active or belonging to the specified campaignId
      See Also:
      • com.broadleafcommerce.promotion.offer.dataimport.CampaignCodeImportBatchHandler
    • findExistingByCodesIncludingInactive

      public List<P> findExistingByCodesIncludingInactive(List<String> codes, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
      Description copied from interface: OfferCodeService
      Search all non-expired offer codes or expired but belonging to the specified offerId. This is useful to validate that the offer codes can be added or updated. NOTE: This method is used during import of OfferCode and due to the varying limitations on how many entries can be in an "in" statement please make sure that the property "broadleaf.dataimport.processor.batch-size" does not exceed this limitation.
      Specified by:
      findExistingByCodesIncludingInactive in interface OfferCodeService<P extends OfferCode>
      Parameters:
      codes - The set of codes to match
      context - Request context information around sandbox and multitenant state
      Returns:
      all OfferCodes by OfferCode.getCode() that are also active or belonging to the specified offerId
      See Also:
    • findAllDuplicatesForCodesInCampaign

      public List<P> findAllDuplicatesForCodesInCampaign(@NonNull @NonNull String campaignId, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
      Description copied from interface: OfferCodeService
      Finds all duplicates OfferCodes by Campaign.getId(). Typically, this will be used to check for duplicate campaign codes of campaign.
      Specified by:
      findAllDuplicatesForCodesInCampaign in interface OfferCodeService<P extends OfferCode>
      Parameters:
      campaignId - the campaign id
      context - Request context information around sandbox and multitenant state
      Returns:
      all duplicates OfferCodes by Campaign.getId()
    • findAllByGeneratorIdAndAssigned

      public List<P> findAllByGeneratorIdAndAssigned(String codeGeneratorId, boolean isAssigned, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
      Description copied from interface: OfferCodeService
      Specified by:
      findAllByGeneratorIdAndAssigned in interface OfferCodeService<P extends OfferCode>
      Parameters:
      codeGeneratorId - the id of the code generator that these codes were generated from
      isAssigned - whether the offer codes are assigned
      context - Request context information around sandbox and multitenant state
      Returns:
      all the unassigned OfferCodes by OfferCode.getGeneratorId()
    • warmCache

      public FullCacheInitializationStatus warmCache()
      Description copied from interface: OfferCodeService
      Call to launch an async process to warm the full code 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 OfferCodeService<P extends OfferCode>
      Returns:
      The status of the cache warm process.
    • rejectDuplicateCode

      protected org.springframework.validation.Errors rejectDuplicateCode(P duplicate)
    • createOfferCodeEntities

      protected List<P> createOfferCodeEntities(CodeGenerator info, Set<String> currentCodeSet)
    • populateCodeGeneratorDetailsToCode

      protected void populateCodeGeneratorDetailsToCode(CodeGenerator codeGenerator, P offerCode)
    • saveAllNonDuplicates

      protected void saveAllNonDuplicates(String codeGeneratorId, List<P> offerCodeList, Class<P> payloadType, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
    • findDuplicateCodes

      protected List<P> findDuplicateCodes(String codeGeneratorId, List<P> offerCodeList, Class<P> payloadType, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
    • buildOfferCodeStringList

      protected List<String> buildOfferCodeStringList(List<P> offerCodeList)
    • removeDuplicatesFromList

      protected void removeDuplicatesFromList(List<P> offerCodeList, List<P> duplicateOfferCodes)
      Iterate through the codes we are about to save. If the code is in the duplicatOfferCodes list then remove it.
      Parameters:
      offerCodeList -
      duplicateOfferCodes -
    • convertOfferFromPersistentDomain

      protected Offer convertOfferFromPersistentDomain(com.broadleafcommerce.data.tracking.core.Trackable domain, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
    • filterOutOfferCodesWithInvalidCampaigns

      protected List<P> filterOutOfferCodesWithInvalidCampaigns(@NonNull @NonNull List<P> offerCodes, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Filters out OfferCodes that are tied to an invalid campaign.
      Parameters:
      offerCodes - the OfferCodes to check against
      contextInfo - context information surrounding sandboxing and multitenant state
      Returns:
      originally given list of OfferCodes if none are tied to a campaign, otherwise a filtered list of OfferCodes
    • campaignCodeIsValid

      protected boolean campaignCodeIsValid(@NonNull @NonNull Map<String,Campaign> campaignMap, @NonNull @NonNull OfferCode offerCode, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo info)
    • readAllByCodesIncludingInactive

      protected List<P> readAllByCodesIncludingInactive(@NonNull Set<String> codes, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Vehicle for populating the cache during cacheAllOfferCodes(ContextInfo). This allows for possible retrieval of inactive codes as well.
      Parameters:
      codes - The codes to cache
      contextInfo - Context information around sandbox and multitenant state
      Returns:
      The retrieved codes
    • findPossibleInfosForCacheWarmup

      protected List<com.broadleafcommerce.data.tracking.core.context.ContextInfo> findPossibleInfosForCacheWarmup()
      Discover all ContextInfo instances to use when processing via warmCache().
      Returns:
      The discovered ContextInfo cases
    • cacheAllOfferCodes

      protected void cacheAllOfferCodes(@Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      If applicable, cache all applicable offer codes in the database into cache memory store for efficient retrieval. See FullCacheType, OfferCacheProperties.getPriorCodeActiveBufferRange(), and OfferCacheProperties.getFutureCodeActiveBufferRange() for more information on configuring what codes are allowed into this cache.
      Parameters:
      contextInfo - Context information around sandbox and multitenant state
    • normalize

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

      @NonNull protected OfferCodeRepository<com.broadleafcommerce.data.tracking.core.Trackable> getOfferCodeRepository()
    • getCampaignRepository

      @NonNull protected CampaignRepository<com.broadleafcommerce.data.tracking.core.Trackable> getCampaignRepository()
    • getOfferRepository

      @NonNull protected OfferRepository<com.broadleafcommerce.data.tracking.core.Trackable> getOfferRepository()
    • getTypeFactory

      @NonNull protected com.broadleafcommerce.common.extension.TypeFactory getTypeFactory()
    • getCacheStateManager

      @Nullable protected com.broadleafcommerce.common.extension.cache.CacheStateManager getCacheStateManager()
    • setCacheStateManager

      @Autowired(required=false) public void setCacheStateManager(com.broadleafcommerce.common.extension.cache.CacheStateManager cacheStateManager)
    • getCacheProperties

      @Nullable protected OfferCacheProperties getCacheProperties()
    • setCacheProperties

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

      protected OfferService<Offer> getOfferService()
    • setOfferService

      @Autowired public void setOfferService(OfferService<Offer> offerService)
    • getCampaignService

      protected CampaignService<Campaign> getCampaignService()
    • setCampaignService

      @Autowired public void setCampaignService(CampaignService<Campaign> campaignService)
    • 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)