Class DefaultCodeGenerationService
- java.lang.Object
-
- com.broadleafcommerce.promotion.campaign.service.DefaultCodeGenerationService
-
- All Implemented Interfaces:
CodeGenerationService
public class DefaultCodeGenerationService extends Object implements CodeGenerationService
- Author:
- Chad Harchar (charchar)
-
-
Field Summary
Fields Modifier and Type Field Description protected CodeFormatAvailableCharHelper
availableCharHelper
protected com.broadleafcommerce.common.extension.TypeFactory
campaignFactory
protected CampaignService<Campaign>
campaignService
protected CampaignCodeGenerationProperties
codeGenerationProperties
protected CampaignCodeService<CampaignCode>
codeService
protected CodeGeneratorService<CodeGenerator>
service
-
Constructor Summary
Constructors Constructor Description DefaultCodeGenerationService(com.broadleafcommerce.common.extension.TypeFactory campaignFactory, CodeFormatAvailableCharHelper availableCharHelper, CodeGeneratorService<CodeGenerator> service, CampaignCodeService<CampaignCode> codeService, CampaignService<Campaign> campaignService)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected int
createBatchOfCodes(String originalCodeFormat, CodeGeneratorBucket bucket, int numberToCreate, Set<String> currentCodeSet)
protected String
createCode(String codeFormat, CodeGeneratorBucket bucket, Random r)
Uses the code format (e.g.protected CodeGeneratorBucket
createCodeGeneratorBucket(CodeGenerator generator, int batchSize)
Provide ability to override CodeGeneratorBucketprotected CodeGenerationResult
generateCodes(CodeGenerator generator, com.broadleafcommerce.data.tracking.core.context.ContextRequest contextRequest)
Generates and returns a list ofCampaignCodes
based on the givenCodeGenerator
.CodeGenerationResult
generateCodesForVouchers(VoucherCodeGenerationRequest voucherCodeGenerationReq, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
Returns theCodeGenerationResult
containing generated and/or existingCampaignCodes
for voucher offers based on the givenVoucherCodeGenerationRequest
.protected CodeGenerationResult
generateOrUseAvailableCodes(VoucherCodeGenerationRequest request, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
Returns aCodeGenerationResult
containing a list of generated and/or existingCampaignCodes
based on the givenVoucherCodeGenerationRequest
.protected CodeGenerationResult
generateVoucherCodes(VoucherCodeGenerationRequest request, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
Delegates togenerateOrUseAvailableCodes(VoucherCodeGenerationRequest, ContextInfo)
to get aCodeGenerationResult
, then updatesCodeGenerationResult.getGeneratedCodes()
based onVoucherCodeGenerationRequest.getCodeGenerator()
.protected CodeFormatAvailableCharHelper
getAvailableCharHelper()
protected int
getBatchSize()
Returns the batch size for generating new offer codes.protected int
getBatchSize(CodeGenerator generator)
protected int
getBatchSizeForVouchers()
Returns the batch size for generating new offer codes for voucher offers.protected com.broadleafcommerce.common.extension.TypeFactory
getCampaignFactory()
protected CampaignService<Campaign>
getCampaignService()
protected CampaignCodeGenerationProperties
getCodeGenerationProperties()
protected int
getCodeMultiplier()
Affects the "guessability" of codes when a code format has not been provided.protected CampaignCodeService<CampaignCode>
getCodeService()
protected CodeGeneratorService<CodeGenerator>
getService()
CodeGenerationResult
markInProgressAndGenerateAndGetCodes(CodeGenerator codeGenerationInfo, com.broadleafcommerce.data.tracking.core.context.ContextRequest contextRequest)
Updates theCodeGenerator
to have a status ofCodeGenerationStatusType.IN_PROCESS
, then performs the code generation.protected void
processCodeGeneratorError(CodeGenerationResult generationResult, CodeGenerator codeGenerator, int codesCreated, String errorMessage, Exception exception, com.broadleafcommerce.data.tracking.core.context.ContextRequest contextRequest)
CodeGenerator
saveCampaignCodeGenerationInfo(CodeGenerator info)
protected void
setBatchSize(int batchSize)
Deprecated.Since 1.6, useCampaignCodeGenerationProperties.getBatchSize()
instead.void
setCodeGenerationProperties(CampaignCodeGenerationProperties codeGenerationProperties)
protected CodeGenerator
updateCodesGenerated(CodeGenerator info, int codesCreated, com.broadleafcommerce.data.tracking.core.context.ContextRequest contextRequest)
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.broadleafcommerce.promotion.campaign.service.CodeGenerationService
markInProgressAndGenerateCodes
-
-
-
-
Field Detail
-
campaignFactory
protected final com.broadleafcommerce.common.extension.TypeFactory campaignFactory
-
availableCharHelper
protected final CodeFormatAvailableCharHelper availableCharHelper
-
service
protected final CodeGeneratorService<CodeGenerator> service
-
codeService
protected final CampaignCodeService<CampaignCode> codeService
-
campaignService
protected final CampaignService<Campaign> campaignService
-
codeGenerationProperties
protected CampaignCodeGenerationProperties codeGenerationProperties
-
-
Constructor Detail
-
DefaultCodeGenerationService
public DefaultCodeGenerationService(com.broadleafcommerce.common.extension.TypeFactory campaignFactory, CodeFormatAvailableCharHelper availableCharHelper, CodeGeneratorService<CodeGenerator> service, CampaignCodeService<CampaignCode> codeService, CampaignService<Campaign> campaignService)
-
-
Method Detail
-
markInProgressAndGenerateAndGetCodes
public CodeGenerationResult markInProgressAndGenerateAndGetCodes(CodeGenerator codeGenerationInfo, com.broadleafcommerce.data.tracking.core.context.ContextRequest contextRequest)
Description copied from interface:CodeGenerationService
Updates theCodeGenerator
to have a status ofCodeGenerationStatusType.IN_PROCESS
, then performs the code generation.- Specified by:
markInProgressAndGenerateAndGetCodes
in interfaceCodeGenerationService
- Parameters:
codeGenerationInfo
- The CodeGenerator that holds the information needed in order to generate the expected number of codes with the expected formatcontextRequest
- The context of the request with which to generate codes- Returns:
- the
CodeGenerationResult
containing the generatedCampaignCodes
and error message based on the givenCodeGenerator
-
generateCodesForVouchers
public CodeGenerationResult generateCodesForVouchers(VoucherCodeGenerationRequest voucherCodeGenerationReq, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
Description copied from interface:CodeGenerationService
Returns theCodeGenerationResult
containing generated and/or existingCampaignCodes
for voucher offers based on the givenVoucherCodeGenerationRequest
.This will use the existing
CodeGenerator
for the voucher campaign if exists, otherwise it will create a new one.This method will generate the
CampaignCodes
and waits for its completion in the same request, therefore it's meant for small batch of code generation only.- Specified by:
generateCodesForVouchers
in interfaceCodeGenerationService
- Parameters:
voucherCodeGenerationReq
- theVoucherCodeGenerationRequest
to generate the voucherCampaignCodes
withcontextInfo
- The context of the request with which to generate codes- Returns:
- the
CodeGenerationResult
containing generated and/or existingCampaignCodes
for voucher offers based on the givenVoucherCodeGenerationRequest
.
-
generateCodes
protected CodeGenerationResult generateCodes(CodeGenerator generator, com.broadleafcommerce.data.tracking.core.context.ContextRequest contextRequest)
Generates and returns a list ofCampaignCodes
based on the givenCodeGenerator
.The
CampaignCodes
are generated in batches based onCampaignCodeGenerationProperties
.- Parameters:
generator
- theCodeGenerator
to generate theCampaignCodes
withcontextRequest
- the context of the request with which to generate codes- Returns:
- a list of
CampaignCodes
generated from the givenCodeGenerator
-
getBatchSize
protected int getBatchSize(CodeGenerator generator)
-
createCodeGeneratorBucket
protected CodeGeneratorBucket createCodeGeneratorBucket(CodeGenerator generator, int batchSize)
Provide ability to override CodeGeneratorBucket- Parameters:
generator
-batchSize
-- Returns:
-
processCodeGeneratorError
protected void processCodeGeneratorError(CodeGenerationResult generationResult, CodeGenerator codeGenerator, int codesCreated, String errorMessage, @Nullable Exception exception, com.broadleafcommerce.data.tracking.core.context.ContextRequest contextRequest)
-
generateVoucherCodes
protected CodeGenerationResult generateVoucherCodes(VoucherCodeGenerationRequest request, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
Delegates togenerateOrUseAvailableCodes(VoucherCodeGenerationRequest, ContextInfo)
to get aCodeGenerationResult
, then updatesCodeGenerationResult.getGeneratedCodes()
based onVoucherCodeGenerationRequest.getCodeGenerator()
.- Parameters:
request
- theVoucherCodeGenerationRequest
to generate theCampaignCodes
fromcontextInfo
- context surrounding sandboxing and multitenant state- Returns:
- a
CodeGenerationResult
containing a list of generatedCampaignCodes
based on the givenVoucherCodeGenerationRequest
-
generateOrUseAvailableCodes
protected CodeGenerationResult generateOrUseAvailableCodes(VoucherCodeGenerationRequest request, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
Returns aCodeGenerationResult
containing a list of generated and/or existingCampaignCodes
based on the givenVoucherCodeGenerationRequest
.If there's an existing
CodeGenerator
withVoucherCodeGenerationRequest.getVoucherCampaignId()
, then it will be used to retrieve the availableCampaignCodes
. Otherwise, a newCodeGenerator
will be created based on the givenVoucherCodeGenerationRequest.getCodeGenerator()
.If there's sufficient amount of existing
CampaignCodes
forCodeGenerator.getNumberOfCodesToGenerate()
, then they will be used. Otherwise, more will be generated untilCodeGenerator.getNumberOfCodesToGenerate()
is met.- Parameters:
request
- theVoucherCodeGenerationRequest
to get theCodeGenerator
fromcontextInfo
- context surrounding sandboxing and multitenant state- Returns:
- a
CodeGenerationResult
containing a list of generated or existingCampaignCodes
based on the givenVoucherCodeGenerationRequest
-
updateCodesGenerated
protected CodeGenerator updateCodesGenerated(CodeGenerator info, int codesCreated, com.broadleafcommerce.data.tracking.core.context.ContextRequest contextRequest)
-
createBatchOfCodes
protected int createBatchOfCodes(String originalCodeFormat, CodeGeneratorBucket bucket, int numberToCreate, Set<String> currentCodeSet)
- Parameters:
originalCodeFormat
-bucket
-numberToCreate
-currentCodeSet
-- Returns:
-
createCode
protected String createCode(String codeFormat, CodeGeneratorBucket bucket, Random r)
Uses the code format (e.g. ###-@@@@-###) to randomly create the code. The fixedChars parameter represents the part of the code that has been locked in for example to representing the "bucket" characters currently being generated.- Parameters:
codeFormat
-r
-- Returns:
-
saveCampaignCodeGenerationInfo
public CodeGenerator saveCampaignCodeGenerationInfo(CodeGenerator info)
-
getBatchSize
protected int getBatchSize()
Returns the batch size for generating new offer codes. A separate transaction will be created for each batch of codes.- Returns:
- the batch size for generating new offer codes
-
getBatchSizeForVouchers
protected int getBatchSizeForVouchers()
Returns the batch size for generating new offer codes for voucher offers. A separate transaction will be created for each batch of codes.- Returns:
- the batch size for generating new offer codes for voucher offers
-
setBatchSize
@Deprecated protected void setBatchSize(int batchSize)
Deprecated.Since 1.6, useCampaignCodeGenerationProperties.getBatchSize()
instead.Returns the batch size for generating new offer codes. A separate transaction will be created for each batch of codes.
-
getCodeMultiplier
protected int getCodeMultiplier()
Affects the "guessability" of codes when a code format has not been provided. For example, if 100 codes are being generated, the default will insure that the codes are generated at least from a pool of 100000 possible codes.- Returns:
-
getCampaignFactory
protected com.broadleafcommerce.common.extension.TypeFactory getCampaignFactory()
-
getAvailableCharHelper
protected CodeFormatAvailableCharHelper getAvailableCharHelper()
-
getService
protected CodeGeneratorService<CodeGenerator> getService()
-
getCodeService
protected CampaignCodeService<CampaignCode> getCodeService()
-
getCampaignService
protected CampaignService<Campaign> getCampaignService()
-
getCodeGenerationProperties
protected CampaignCodeGenerationProperties getCodeGenerationProperties()
-
setCodeGenerationProperties
@Autowired public void setCodeGenerationProperties(CampaignCodeGenerationProperties codeGenerationProperties)
-
-