Class OfferCodeImportBatchHandler

java.lang.Object
com.broadleafcommerce.common.dataimport.AbstractImportBatchHandler
com.broadleafcommerce.promotion.offer.dataimport.OfferCodeImportBatchHandler
All Implemented Interfaces:
com.broadleafcommerce.common.dataimport.ImportBatchHandler

@DataRouteByKey("offer") public class OfferCodeImportBatchHandler extends com.broadleafcommerce.common.dataimport.AbstractImportBatchHandler
Author:
Vitalii Voronkov (vvoronkov)
  • Constructor Details

  • Method Details

    • canHandle

      public boolean canHandle(com.broadleafcommerce.common.dataimport.messaging.BatchRequest batch)
    • handle

      public com.broadleafcommerce.common.dataimport.messaging.BatchCompletion handle(com.broadleafcommerce.common.dataimport.messaging.BatchRequest batch)
    • convertBatchRecords

      protected Map<String,org.apache.commons.lang3.tuple.Pair<OfferCode,com.broadleafcommerce.common.dataimport.messaging.BatchRecord>> convertBatchRecords(com.broadleafcommerce.common.dataimport.messaging.BatchRequest batch, List<Offer> offers, List<Campaign> campaigns, List<com.broadleafcommerce.common.dataimport.messaging.BatchCompletionRecord> erroredCompletions, @Nullable String parentEntityId)
      Converts all of the batch records in the batch request to offer codes.

      If conversion fails for a record, a BatchCompletionRecord will be created for it and added to erroredCompletions, and it will not appear in the converted result.

      If multiple batch records are attempting to import the same code, all but the first will be considered errored. A BatchCompletionRecord will be created for the duplicates and added to erroredCompletions, and they will not appear in the converted result.

      Parameters:
      batch - the original record batch
      offers - the list of offers that are associated (typically as the parent entity) with the offer code records
      campaigns - the list of campaigns that are associated (typically as the parent entity) with the offer code records
      erroredCompletions - the list to which any errored completion records should be added
      parentEntityId - the id of the parent entity, will be set on the converted offer codes
      Returns:
      a map of the converted offer code and original BatchRecord pairs grouped by code value
    • convertBatchRecordToOfferCode

      protected Optional<OfferCode> convertBatchRecordToOfferCode(com.broadleafcommerce.common.dataimport.messaging.BatchRequest batch, com.broadleafcommerce.common.dataimport.messaging.BatchRecord record, List<Offer> offers, List<Campaign> campaigns, List<com.broadleafcommerce.common.dataimport.messaging.BatchCompletionRecord> erroredCompletions, @Nullable String parentEntityId)
      Converts the batch record into an offer code, or Optional.empty() if conversion failed.
      Parameters:
      batch - the original record batch
      record - the record which represents the OfferCode in the import file
      offers - the list of offers that are associated (typically as the parent entity) with the offer code records
      campaigns - the list of campaigns that are associated (typically as the parent entity) with the offer code records
      erroredCompletions - the list to which any errored completion records should be added
      parentEntityId - the id of the parent entity, will be set on the converted offer codes
      Returns:
      the OfferCode, or Optional.empty() if conversion failed.
    • getRelatedOffersForBatch

      protected List<Offer> getRelatedOffersForBatch(com.broadleafcommerce.common.dataimport.messaging.BatchRequest batch, @Nullable String parentEntityId)
      Queries for the related offers of the batch records by id or external id.
      Parameters:
      batch - the original record batch
      parentEntityId - the id of the parent entity
      Returns:
      a list of offers queried by id or external id
    • getRelatedCampaignsForBatch

      protected List<Campaign> getRelatedCampaignsForBatch(com.broadleafcommerce.common.dataimport.messaging.BatchRequest batch, @Nullable String parentEntityId)
      Queries for the related campaigns of the batch records by id or external id.
      Parameters:
      batch - the original record batch
      parentEntityId - the id of the parent entity
      Returns:
      a list of campaigns queried by id or external id
    • resolveParentEntityId

      protected void resolveParentEntityId(OfferCode offerCode, String batchType, @Nullable String parentEntityId)
    • resolveOfferCodeId

      protected void resolveOfferCodeId(com.broadleafcommerce.common.dataimport.messaging.BatchRecord record, OfferCode offerCode)
      Resolves and sets the id for OfferCode.
      Parameters:
      record - the record represents OfferCode from file
      offerCode - the OfferCode for which the id should be resolved
    • findRelatedOffer

      protected String findRelatedOffer(List<Offer> offers, com.broadleafcommerce.common.dataimport.messaging.BatchRecord record, String batchType, List<com.broadleafcommerce.common.dataimport.messaging.BatchCompletionRecord> erroredCompletions)
    • findRelatedCampaign

      protected String findRelatedCampaign(List<Campaign> campaigns, com.broadleafcommerce.common.dataimport.messaging.BatchRecord record, String batchType, List<com.broadleafcommerce.common.dataimport.messaging.BatchCompletionRecord> erroredCompletions)
    • persistPersistenceRequests

      protected void persistPersistenceRequests(com.broadleafcommerce.common.dataimport.messaging.BatchRequest batch, List<com.broadleafcommerce.common.dataimport.util.PersistenceRequest<OfferCode>> offerCodeRequests, List<com.broadleafcommerce.common.dataimport.messaging.BatchCompletionRecord> allCompletions)
      Perform a persist of the parsed out domain specified as PersistenceRequests and adds persistence result to allCompletions
      Parameters:
      batch - the original record batch
      offerCodeRequests - a list of the offer code persistence requests
      allCompletions - the list to which the completion records should be added
    • getDataRouteKey

      public String getDataRouteKey()
    • convertToCode

      protected Optional<OfferCode> convertToCode(com.broadleafcommerce.common.dataimport.messaging.BatchRecord codeRecord, List<com.broadleafcommerce.common.dataimport.messaging.BatchCompletionRecord> allCompletions)
    • validateAndBuildPersistenceRequests

      protected List<com.broadleafcommerce.common.dataimport.util.PersistenceRequest<OfferCode>> validateAndBuildPersistenceRequests(Map<String,org.apache.commons.lang3.tuple.Pair<OfferCode,com.broadleafcommerce.common.dataimport.messaging.BatchRecord>> codesToImport, List<Offer> offers, List<Campaign> campaigns, List<com.broadleafcommerce.common.dataimport.messaging.BatchCompletionRecord> errorsResults, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
      This method is used to validate that the offer codes can be imported. It adds the valid result to validResults and invalid to errorsResults.

      It checks the next cases:

      • Active codes must be unique within a tenant. (if multiple tenants are present, then they can use the same codes).
      • "Expired Codes" are not considered when considering uniqueness. Future dated codes ARE that are not yet active are considered in the uniqueness check.
      • If a code is imported to a campaign and that code already exists within the campaign, the existing code will be updated.
      • If a code is imported to a campaign and that code already exists within another campaign but is expired, the code will be added to this campaign.
      • If updating the expiration date of an expired code, the date will be updated if valid.
      Parameters:
      codesToImport - the map of offer code and BatchRecord pairs grouped by code value
      errorsResults - the list of BatchCompletionRecord to add the invalid codes
      context - Request context information around sandbox and multitenant state
      Returns:
      a list of PersistenceRequests for codes that passed validation
    • getExistingCodes

      protected Map<org.apache.commons.lang3.tuple.Pair<String,String>,OfferCode> getExistingCodes(Set<String> codesToImport, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
    • createPersistenceRequest

      protected com.broadleafcommerce.common.dataimport.util.PersistenceRequest<OfferCode> createPersistenceRequest(OfferCode offerCode, com.broadleafcommerce.common.dataimport.messaging.BatchRecord codeRecord, com.broadleafcommerce.data.tracking.core.type.OperationType operationType)
    • validOfferCodeUpdateRequest

      protected boolean validOfferCodeUpdateRequest(OfferCode codeToImport, OfferCode existingCode, com.broadleafcommerce.common.dataimport.messaging.BatchRecord batchRecord, List<Offer> offers, List<Campaign> campaigns, List<com.broadleafcommerce.common.dataimport.messaging.BatchCompletionRecord> errorsResults)
    • isMatchingUniqueOfferIds

      protected boolean isMatchingUniqueOfferIds(OfferCode codeToImport, OfferCode existingCode, com.broadleafcommerce.common.dataimport.messaging.BatchRecord batchRecord, List<Offer> offers)
    • isMatchingUniqueCampaignIds

      protected boolean isMatchingUniqueCampaignIds(OfferCode codeToImport, OfferCode existingCode, com.broadleafcommerce.common.dataimport.messaging.BatchRecord batchRecord, List<Campaign> campaigns)
    • isMatchingUniqueOfferCodeIds

      protected boolean isMatchingUniqueOfferCodeIds(OfferCode codeToImport, OfferCode existingCode)
    • isExistingCodeIrrelevantToCreation

      protected boolean isExistingCodeIrrelevantToCreation(OfferCode codeToImport, OfferCode existingCode)
    • isBelongsToAnotherExistingCampaign

      protected boolean isBelongsToAnotherExistingCampaign(String campaignId, OfferCode existingCode)
    • isExistingCodeExpired

      protected boolean isExistingCodeExpired(OfferCode existingCode, Instant endAfterDate)
    • isValidEndDate

      protected boolean isValidEndDate(Instant endAfterDate, OfferCode codeToImport, OfferCode existingCode)
    • getOfferCodeConverter

      protected OfferCodeConverter getOfferCodeConverter()
    • getOfferCodeDefaultsInitializer

      protected OfferCodeDefaultsInitializer getOfferCodeDefaultsInitializer()
    • getOfferCodeService

      protected OfferCodeService<OfferCode> getOfferCodeService()
    • getOfferService

      protected OfferService<Offer> getOfferService()
    • getCampaignService

      protected CampaignService<Campaign> getCampaignService()
    • getIdResolver

      protected com.broadleafcommerce.common.dataimport.util.IdResolver getIdResolver()
    • setIdResolver

      protected void setIdResolver(com.broadleafcommerce.common.dataimport.util.IdResolver idResolver)
    • getOperationResolver

      protected com.broadleafcommerce.common.dataimport.util.OperationResolver getOperationResolver()
    • setOperationResolver

      protected void setOperationResolver(com.broadleafcommerce.common.dataimport.util.OperationResolver operationResolver)