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 CodeFormatAvailableCharHelperavailableCharHelperprotected com.broadleafcommerce.common.extension.TypeFactorycampaignFactoryprotected CampaignService<Campaign>campaignServiceprotected CampaignCodeGenerationPropertiescodeGenerationPropertiesprotected CampaignCodeService<CampaignCode>codeServiceprotected 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 intcalculateRequiredCodeLength(int numberOfCodes, int charSetSize)Returns the size of the code that you must create in order to support the passed in number of Codes.protected intcreateBatchOfCodes(String originalCodeFormat, CodeGeneratorBucket bucket, int numberToCreate, Set<String> currentCodeSet)protected StringcreateCode(String codeFormat, Random r)Uses the code format (e.g.protected CodeGenerationResultgenerateCodes(CodeGenerator generator, com.broadleafcommerce.data.tracking.core.context.ContextRequest contextRequest)Generates and returns a list ofCampaignCodesbased on the givenCodeGenerator.CodeGenerationResultgenerateCodesForVouchers(VoucherCodeGenerationRequest voucherCodeGenerationReq, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)Returns theCodeGenerationResultcontaining generated and/or existingCampaignCodesfor voucher offers based on the givenVoucherCodeGenerationRequest.protected CodeGenerationResultgenerateOrUseAvailableCodes(VoucherCodeGenerationRequest request, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)Returns aCodeGenerationResultcontaining a list of generated and/or existingCampaignCodesbased on the givenVoucherCodeGenerationRequest.protected CodeGenerationResultgenerateVoucherCodes(VoucherCodeGenerationRequest request, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)Delegates togenerateOrUseAvailableCodes(VoucherCodeGenerationRequest, ContextInfo)to get aCodeGenerationResult, then updatesCodeGenerationResult.getGeneratedCodes()based onVoucherCodeGenerationRequest.getCodeGenerator().protected CodeFormatAvailableCharHelpergetAvailableCharHelper()protected intgetBatchSize()Returns the batch size for generating new offer codes.protected intgetBatchSizeForVouchers()Returns the batch size for generating new offer codes for voucher offers.protected com.broadleafcommerce.common.extension.TypeFactorygetCampaignFactory()protected CampaignService<Campaign>getCampaignService()protected CampaignCodeGenerationPropertiesgetCodeGenerationProperties()protected intgetCodeMultiplier()Affects the "guessability" of codes when a code format has not been provided.protected CampaignCodeService<CampaignCode>getCodeService()protected CodeGeneratorService<CodeGenerator>getService()voidinitializeCodePrefixAndGenerateCodes(CodeGenerator info, com.broadleafcommerce.data.tracking.core.context.ContextRequest contextRequest)Deprecated.protected StringinitializeCodePrefixAndGetCodeFormat(CodeGenerator info, com.broadleafcommerce.data.tracking.core.context.ContextRequest contextRequest)If null, initializes the code prefix to the associatedcampaign'scode prefix.voidinitializeCodePrefixAndValidateGenerator(CodeGenerator info, com.broadleafcommerce.data.tracking.core.context.ContextRequest context)Deprecated.CodeGenerationResultmarkInProgressAndGenerateAndGetCodes(CodeGenerator codeGenerationInfo, com.broadleafcommerce.data.tracking.core.context.ContextRequest contextRequest)Updates theCodeGeneratorto have a status ofCodeGenerationStatusType.IN_PROCESS, then performs the code generation.protected voidprocessCodeGeneratorError(CodeGenerationResult generationResult, CodeGenerator codeGenerator, int codesCreated, String errorMessage, Exception exception, com.broadleafcommerce.data.tracking.core.context.ContextRequest contextRequest)CodeGeneratorsaveCampaignCodeGenerationInfo(CodeGenerator info)protected voidsetBatchSize(int batchSize)Deprecated.Since 1.6, useCampaignCodeGenerationProperties.getBatchSize()instead.voidsetCodeGenerationProperties(CampaignCodeGenerationProperties codeGenerationProperties)voidstartCodeGeneration(CodeGenerator codeGenerationInfo, com.broadleafcommerce.data.tracking.core.context.ContextRequest contextRequest)Deprecated.protected CodeGeneratorupdateCodesGenerated(CodeGenerator info, int codesCreated, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)protected CodeGeneratorupdateCodesGenerated(CodeGenerator info, int codesCreated, com.broadleafcommerce.data.tracking.core.context.ContextRequest contextRequest)Deprecated.Since 1.6, in favor ofupdateCodesGenerated(CodeGenerator, int, ContextInfo).-
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
-
initializeCodePrefixAndValidateGenerator
@Deprecated public void initializeCodePrefixAndValidateGenerator(CodeGenerator info, com.broadleafcommerce.data.tracking.core.context.ContextRequest context)
Deprecated.Description copied from interface:CodeGenerationServiceSets theCodeGenerator'sCodeGenerator.codePrefixto be itscampaign'scode prefix if not present. After this, validates that the codeFormat for theCodeGeneratoris large enough to hold the number of codes desired, ensuring that the codes are not easily guessable.- Specified by:
initializeCodePrefixAndValidateGeneratorin interfaceCodeGenerationService- Parameters:
info- The CodeGenerator that contains the number of codes to generate and the potential code format.context- The context of the request with which to generate codes.
-
initializeCodePrefixAndGenerateCodes
@Deprecated public void initializeCodePrefixAndGenerateCodes(CodeGenerator info, com.broadleafcommerce.data.tracking.core.context.ContextRequest contextRequest)
Deprecated.Description copied from interface:CodeGenerationServiceSets theCodeGenerator'sCodeGenerator.codePrefixto be itscampaign'scode prefix if not present. After this, generates the codes for the providedCodeGenerator.- Specified by:
initializeCodePrefixAndGenerateCodesin interfaceCodeGenerationService- Parameters:
info- The CodeGenerator that holds the information needed in order to generate the expected number of codes with the expected format, if desired.contextRequest- The context of the request with which to generate codes.
-
startCodeGeneration
@Deprecated public void startCodeGeneration(CodeGenerator codeGenerationInfo, com.broadleafcommerce.data.tracking.core.context.ContextRequest contextRequest)
Deprecated.Description copied from interface:CodeGenerationServicePrepares theCodeGeneratorfor processing, then triggers the code generation.- Specified by:
startCodeGenerationin interfaceCodeGenerationService- Parameters:
codeGenerationInfo- The CodeGenerator that holds the information needed in order to generate the expected number of codes with the expected format, if desired.contextRequest- The context of the request with which to generate codes.
-
markInProgressAndGenerateAndGetCodes
public CodeGenerationResult markInProgressAndGenerateAndGetCodes(CodeGenerator codeGenerationInfo, com.broadleafcommerce.data.tracking.core.context.ContextRequest contextRequest)
Description copied from interface:CodeGenerationServiceUpdates theCodeGeneratorto have a status ofCodeGenerationStatusType.IN_PROCESS, then performs the code generation.- Specified by:
markInProgressAndGenerateAndGetCodesin 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
CodeGenerationResultcontaining the generatedCampaignCodesand error message based on the givenCodeGenerator
-
generateCodesForVouchers
public CodeGenerationResult generateCodesForVouchers(VoucherCodeGenerationRequest voucherCodeGenerationReq, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
Description copied from interface:CodeGenerationServiceReturns theCodeGenerationResultcontaining generated and/or existingCampaignCodesfor voucher offers based on the givenVoucherCodeGenerationRequest.This will use the existing
CodeGeneratorfor the voucher campaign if exists, otherwise it will create a new one.This method will generate the
CampaignCodesand waits for its completion in the same request, therefore it's meant for small batch of code generation only.- Specified by:
generateCodesForVouchersin interfaceCodeGenerationService- Parameters:
voucherCodeGenerationReq- theVoucherCodeGenerationRequestto generate the voucherCampaignCodeswithcontextInfo- The context of the request with which to generate codes- Returns:
- the
CodeGenerationResultcontaining generated and/or existingCampaignCodesfor 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 ofCampaignCodesbased on the givenCodeGenerator.The
CampaignCodesare generated in batches based onCampaignCodeGenerationProperties.- Parameters:
generator- theCodeGeneratorto generate theCampaignCodeswithcontextRequest- the context of the request with which to generate codes- Returns:
- a list of
CampaignCodesgenerated from the givenCodeGenerator
-
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- theVoucherCodeGenerationRequestto generate theCampaignCodesfromcontextInfo- context surrounding sandboxing and multitenant state- Returns:
- a
CodeGenerationResultcontaining a list of generatedCampaignCodesbased on the givenVoucherCodeGenerationRequest
-
generateOrUseAvailableCodes
protected CodeGenerationResult generateOrUseAvailableCodes(VoucherCodeGenerationRequest request, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
Returns aCodeGenerationResultcontaining a list of generated and/or existingCampaignCodesbased on the givenVoucherCodeGenerationRequest.If there's an existing
CodeGeneratorwithVoucherCodeGenerationRequest.getVoucherCampaignId(), then it will be used to retrieve the availableCampaignCodes. Otherwise, a newCodeGeneratorwill be created based on the givenVoucherCodeGenerationRequest.getCodeGenerator().If there's sufficient amount of existing
CampaignCodesforCodeGenerator.getNumberOfCodesToGenerate(), then they will be used. Otherwise, more will be generated untilCodeGenerator.getNumberOfCodesToGenerate()is met.- Parameters:
request- theVoucherCodeGenerationRequestto get theCodeGeneratorfromcontextInfo- context surrounding sandboxing and multitenant state- Returns:
- a
CodeGenerationResultcontaining a list of generated or existingCampaignCodesbased on the givenVoucherCodeGenerationRequest
-
updateCodesGenerated
@Deprecated protected CodeGenerator updateCodesGenerated(CodeGenerator info, int codesCreated, com.broadleafcommerce.data.tracking.core.context.ContextRequest contextRequest)
Deprecated.Since 1.6, in favor ofupdateCodesGenerated(CodeGenerator, int, ContextInfo).
-
updateCodesGenerated
protected CodeGenerator updateCodesGenerated(CodeGenerator info, int codesCreated, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
-
createBatchOfCodes
protected int createBatchOfCodes(String originalCodeFormat, CodeGeneratorBucket bucket, int numberToCreate, Set<String> currentCodeSet)
- Parameters:
originalCodeFormat-bucket-numberToCreate-currentCodeSet-- Returns:
-
calculateRequiredCodeLength
protected int calculateRequiredCodeLength(int numberOfCodes, int charSetSize)Returns the size of the code that you must create in order to support the passed in number of Codes.- Parameters:
numberOfCodes-- Returns:
-
initializeCodePrefixAndGetCodeFormat
protected String initializeCodePrefixAndGetCodeFormat(CodeGenerator info, com.broadleafcommerce.data.tracking.core.context.ContextRequest contextRequest)
If null, initializes the code prefix to the associatedcampaign'scode prefix. Then, if the code format was not specified, creates one using the code prefix and alphanumeric characters for the specified length.- Parameters:
info- theCodeGeneratorto use to determine format againstcontextRequest- the context for the request- Returns:
- the specified or newly created code format for the given
CodeGenerator - Throws:
IllegalArgumentException- if theCodeGenerator.codeLengthis less than the required code length
-
createCode
protected String createCode(String codeFormat, 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)
-
-