Class DefaultCouponCodeGenerationService

  • All Implemented Interfaces:
    CouponCodeGenerationService, org.springframework.beans.factory.InitializingBean

    public class DefaultCouponCodeGenerationService
    extends java.lang.Object
    implements CouponCodeGenerationService, org.springframework.beans.factory.InitializingBean
    The default coupon code generation service.
    • Field Detail

      • UNEXPECTED_ERROR_MSG

        protected static final java.lang.String UNEXPECTED_ERROR_MSG
        See Also:
        Constant Field Values
    • Constructor Detail

      • DefaultCouponCodeGenerationService

        public DefaultCouponCodeGenerationService()
    • Method Detail

      • afterPropertiesSet

        public void afterPropertiesSet()
        Specified by:
        afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
      • generateCouponSignature

        public java.lang.String generateCouponSignature()
        Description copied from interface: CouponCodeGenerationService
        generates a base64 encoded String representing the signature of the multi code coupon. The signature is used to generate the cipher-text part of the coupon code which incorporates the signed first half of the coupon code.
        Specified by:
        generateCouponSignature in interface CouponCodeGenerationService
        Returns:
        base64 encoded String representing the signature of the multi code coupon
      • generateCouponCodes

        public java.util.Optional<MediaModel> generateCouponCodes​(MultiCodeCouponModel multiCodeCoupon,
                                                                  int quantity)
        Description copied from interface: CouponCodeGenerationService
        Generates codes for the given multi-code coupon. Returns a MediaModel containing a csv file with the generated codes. Note that the returned file contains quantity coupon codes or less. It contains less codes if the requested quantity cannot be generated (e.g. if the limit of codes has been reached). If no coupon codes could be generated an Optional.empty() is returned.
        Specified by:
        generateCouponCodes in interface CouponCodeGenerationService
        Parameters:
        multiCodeCoupon - Multi code coupon instance
        quantity - number of coupon codes to be generated
        Returns:
        MediaModel containing a csv file with the generated codes. If no coupon codes could be generated an Optional.empty() is returned.
      • generateMediaForMultiCodeCoupon

        protected java.util.Optional<MediaModel> generateMediaForMultiCodeCoupon​(MultiCodeCouponModel multiCodeCoupon,
                                                                                 int quantity)
      • getMediaFolderForCouponCodes

        protected MediaFolderModel getMediaFolderForCouponCodes()
      • extractCouponPrefix

        public java.lang.String extractCouponPrefix​(java.lang.String couponCode)
        Description copied from interface: CouponCodeGenerationService
        tries to find the prefix substring of the given coupon code.
        Specified by:
        extractCouponPrefix in interface CouponCodeGenerationService
        Parameters:
        couponCode - coupon code to extract prefix substring
        Returns:
        the (assumed) prefix of the given coupon code or null if none could be determined
      • verifyCouponCode

        public boolean verifyCouponCode​(MultiCodeCouponModel coupon,
                                        java.lang.String couponCode)
        Description copied from interface: CouponCodeGenerationService
        verifies if the given couponCode is valid for the given coupon.
        Specified by:
        verifyCouponCode in interface CouponCodeGenerationService
        Parameters:
        coupon - Coupon against which check will be fired
        couponCode - Coupon code to verify
        Returns:
        true if the couponCode is valid, otherwise false
      • validateCouponFormat

        protected boolean validateCouponFormat​(MultiCodeCouponModel coupon,
                                               java.lang.String couponCode)
        validates the given coupon code with regards to its format (i.e. correct prefix, separators, coupon parts).
      • verifyCipherText

        protected boolean verifyCipherText​(MultiCodeCouponModel coupon,
                                           java.lang.String couponCode)
        Returns:
        true if generated ciphertext matches given ciphertext , false otherwise
      • verifyUsedCouponCodeNumber

        protected boolean verifyUsedCouponCodeNumber​(MultiCodeCouponModel coupon,
                                                     org.apache.commons.lang3.tuple.Pair<java.lang.String,​java.lang.String> pair)
        Returns:
        true if used coupon code number is greater than 0 and less than CouponCodeNumber on multi code coupon,false otherwise
      • isValidCodeSeparator

        public boolean isValidCodeSeparator​(java.lang.String codeSeparator)
        Description copied from interface: CouponCodeGenerationService
        verifies if the given string is a valid code separator
        Specified by:
        isValidCodeSeparator in interface CouponCodeGenerationService
        Parameters:
        codeSeparator - Code separator
        Returns:
        true if the codeSeperator is valid, otherwise false
      • extractClearTextAndCipherText

        protected org.apache.commons.lang3.tuple.Pair<java.lang.String,​java.lang.String> extractClearTextAndCipherText​(MultiCodeCouponModel coupon,
                                                                                                                             java.lang.String couponCode)
        returns a string pair containing the clear text and the cipher text part of the given coupon code.
        Returns:
        a string pair or null if the parts cannot be determined
      • removeCodeSeparators

        protected java.lang.String removeCodeSeparators​(java.lang.String codeWithOutPrefix,
                                                        MultiCodeCouponModel coupon)
        removes the code separator from the given codeWithoutPrefix. Note: Expects the given code to be without its prefix.
        Throws:
        java.lang.IllegalArgumentException - if the given code doesn't match with the required length of the given coupon's configuration
      • getCipherTextLength

        protected int getCipherTextLength​(MultiCodeCouponModel coupon)
        returns the amount of cipher-text characters to be created for the given coupon.
      • getClearTextLength

        protected int getClearTextLength​(MultiCodeCouponModel coupon)
        returns the amount of clear-text characters to be created for the given coupon.
      • getAndValidateAlphabetLength

        protected int getAndValidateAlphabetLength​(java.lang.String globalCharacterSet)
        validates the coupon alphabet length and returns it
      • getAndValidateGlobalCharacterSet

        protected java.lang.String getAndValidateGlobalCharacterSet()
        validates that the global character set doesn't contain the defined code separators and that all characters are single-byte characters.
        Returns:
        the validated global character set
        Throws:
        CouponCodeGenerationException - if the validation fails
      • getAndValidateAlgorithm

        protected java.lang.String getAndValidateAlgorithm()
        validates the configured algorithm used for the cipher part of the coupon code
      • setConfigurationService

        public void setConfigurationService​(ConfigurationService configurationService)
      • getModelService

        protected ModelService getModelService()
      • setModelService

        public void setModelService​(ModelService modelService)
      • getMediaService

        protected MediaService getMediaService()
      • setMediaService

        public void setMediaService​(MediaService mediaService)
      • getCodeSeparatorPattern

        protected java.lang.String getCodeSeparatorPattern()
      • setCodeSeparatorPattern

        public void setCodeSeparatorPattern​(java.lang.String codeSeparatorPattern)
      • setCouponCodesGenerator

        public void setCouponCodesGenerator​(CouponCodesGenerator couponCodesGenerator)
      • getBatchSize

        protected java.lang.Integer getBatchSize()
      • getKeyGenerator

        protected KeyGenerator getKeyGenerator()
      • setKeyGenerator

        public void setKeyGenerator​(KeyGenerator keyGenerator)