Class AbstractImportBatchHandler

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

public abstract class AbstractImportBatchHandler extends Object implements ImportBatchHandler
Intended to provide a commonly useful starting point for ImportBatchHandler implementations.

Contains methods providing boilerplate logic for performing persistence and handling the outcome.

  • Constructor Details

    • AbstractImportBatchHandler

      public AbstractImportBatchHandler(com.broadleafcommerce.data.tracking.core.web.ContextRequestHydrator hydrator)
  • Method Details

    • initializeContextForBatch

      public void initializeContextForBatch(@NonNull @NonNull BatchRequest batch)
      Method to initialize fields for a batch. By default, initializes the default currency for the process useing SystemCurrencyContextHolder.setProcessCurrency(CurrencyUnit). Implementations may override for other batch initialization needs. For thread local items like process currency, the finalizeContextForBatch(BatchRequest) should be called to clear the process state.
      Specified by:
      initializeContextForBatch in interface ImportBatchHandler
      Parameters:
      batch - The batch to be processed.
    • finalizeContextForBatch

      public void finalizeContextForBatch(@NonNull @NonNull BatchRequest batch)
      Method to clear any state after processing a batch. Default implementation calls SystemCurrencyContextHolder.clearProcessCurrency()
      Specified by:
      finalizeContextForBatch in interface ImportBatchHandler
      Parameters:
      batch - The batch that was processed
    • persist

      protected <T> List<BatchCompletionRecord> persist(BatchRequest batch, List<PersistenceRequest<T>> requests, BatchCreator<T> creator, BatchUpdater<T> updater, Function<T,String> idExtractor, int batchSize)
      This is a convenience wrapper around persist(BatchRequest, List, BatchCreator, BatchUpdater, Function) to submit the requests in chunks/smaller-batches.

      This is especially important for import records that may have a very large number of dependent records. For example, the ImportService sends products in pre-defined batch sizes (default is 100). However, dependent records such as variants are sent over all together along with their associated products. The dependent records do affect the batches sent to this service, but the dependents are not split into batches of their own. For example, if the batch size is 100 coming from the ImportService and products are being imported, if the first product has 1000 variants, then this service will receive a batch with 1 product and 1000 variants. As a result, this method allows us to break those variants up further for persistence so we're not dealing with persisting 1000 variant records at once, for example.

      Type Parameters:
      T - the type being persisted
      Parameters:
      batch - the original record batch
      requests - the full list of entities that need to be persisted
      creator - performs the persistence for all requests where PersistenceRequest.getOperation() is OperationType.CREATE
      updater - performs the persistence for all requests where PersistenceRequest.getOperation() is OperationType.UPDATE
      idExtractor - can return an id given a domain instance. Used for grouping and mapping back what was persisted with the batch record's correlation id
      batchSize - the maximum number of elements from requests that should be submitted at once to persist(BatchRequest, List, BatchCreator, BatchUpdater, Function)
      Returns:
      a list of completions describing success/failure
    • persist

      protected <T> List<BatchCompletionRecord> persist(BatchRequest batch, List<PersistenceRequest<T>> requests, BatchCreator<T> creator, BatchUpdater<T> updater, Function<T,String> idExtractor)
      Perform a persist of the parsed out domain specified as PersistenceRequests
      Type Parameters:
      T - type being persisted
      Parameters:
      batch - the original record batch, used for constructing a ContextInfo
      requests - what to persist
      creator - performs the persistence for all requests where PersistenceRequest.getOperation() is OperationType.CREATE
      updater - performs the persistence for all requests where PersistenceRequest.getOperation() is OperationType.UPDATE
      idExtractor - can return an id given a domain instance. Used for grouping and mapping back what was persisted with the batch record's correlation id
      Returns:
      a list of completions describing success/failure
    • getImportContextProperties

      @Nullable protected com.broadleafcommerce.common.extension.importing.ImportContextConfigurationProperties.ImportContextProperties getImportContextProperties(BatchRequest batchRequest)
    • buildAndAddCompletionRecords

      protected <T> void buildAndAddCompletionRecords(com.broadleafcommerce.data.tracking.core.service.BulkPersistenceResponse<T> response, List<BatchCompletionRecord> completionRecords, Map<String,PersistenceRequest<T>> persistenceRequestByResourceId, Function<T,String> idExtractor)
    • buildSuccessfulCompletionsForRequest

      protected <T> List<BatchCompletionRecord> buildSuccessfulCompletionsForRequest(String entityResourceId, PersistenceRequest<T> persistenceRequest)
    • generateAndAddCompletions

      protected void generateAndAddCompletions(PersistenceRequest.EmbeddedItemRequest embeddedItemRequest, BatchCompletionRecordStatus status, @Nullable String errorMessage, List<BatchCompletionRecord> destination)
    • generateAndAddCompletions

      protected void generateAndAddCompletions(BatchRecord batchRecord, BatchCompletionRecordStatus status, @Nullable String errorMessage, List<BatchCompletionRecord> destination)
    • buildNonValidationErrorCompletions

      protected <T> List<BatchCompletionRecord> buildNonValidationErrorCompletions(String entityResourceId, PersistenceRequest<T> persistenceRequest, com.broadleafcommerce.data.tracking.core.service.BulkPersistenceResponse.PersistenceFailure<T> persistenceFailure)
    • buildValidationErrorCompletions

      protected <T> List<BatchCompletionRecord> buildValidationErrorCompletions(String entityResourceId, PersistenceRequest<T> persistenceRequest, com.broadleafcommerce.data.tracking.core.service.BulkPersistenceResponse.PersistenceFailure<T> persistenceFailure)
    • removeAndReturnFieldErrorsUnderPath

      protected List<org.springframework.validation.FieldError> removeAndReturnFieldErrorsUnderPath(Map<String,org.springframework.validation.FieldError> fieldErrorsByPath, String path)
    • generateAndAddValidationErrorCompletions

      protected void generateAndAddValidationErrorCompletions(PersistenceRequest.EmbeddedItemRequest embeddedItemRequest, @Nullable String parentPath, Map<String,org.springframework.validation.FieldError> fieldErrorsByPath, String parentItemCorrelationId, List<BatchCompletionRecord> destination)
    • getFormattedEmbeddedValidationErrorString

      protected String getFormattedEmbeddedValidationErrorString(List<org.springframework.validation.FieldError> errorsWithEmbeddedField, String embeddedFieldNestedPath, String parentCorrelationId)
    • getFormattedValidationErrorString

      protected String getFormattedValidationErrorString(Collection<org.springframework.validation.ObjectError> globalErrors, Collection<org.springframework.validation.FieldError> fieldErrors)
    • getCompletionError

      protected BatchCompletionRecord getCompletionError(BatchRecord record, String errorMessage)
    • addFailedConversionCompletions

      protected <T> void addFailedConversionCompletions(BatchRecord failedToConvert, ConversionUtils.ConversionResponse<T> failedConversion, List<BatchCompletionRecord> destination)
      Creates a BatchCompletionRecord for the given failedToConvert batch record, and then creates BatchCompletionRecords for all of its nested dependents.

      All of these completion records are added to the destination.

      Parameters:
      failedToConvert - the batch record that could not be converted
      failedConversion - the response describing the errors with the conversion
      destination - the list to which the completion records should be added
    • failRecordAndDependentsEarly

      protected void failRecordAndDependentsEarly(BatchRecord recordToFail, @Nullable String errorMessage, @Nullable String dependentErrorMessage, List<BatchCompletionRecord> destination)
      Intended for use in scenarios where a record and its dependents need to be marked as failed in an early (pre-persistence) stage (no resource tier ID is available), such as failed conversions.

      Creates a BatchCompletionRecord for the given recordToFail batch record, and then creates BatchCompletionRecords for all of its nested dependents.

      All of these completion records are added to the destination.

      Parameters:
      recordToFail - the record to mark as failed (along with any dependents)
      errorMessage - the error message to set on the record
      dependentErrorMessage - the error message to set on dependents of the record
      destination - the list to which the completion records should be added
    • buildReadContextInfo

      protected com.broadleafcommerce.data.tracking.core.context.ContextInfo buildReadContextInfo(BatchRequest.BatchContext context)
    • buildCreateContextInfo

      protected com.broadleafcommerce.data.tracking.core.context.ContextInfo buildCreateContextInfo(BatchRequest.BatchContext context)
    • buildUpdateContextInfo

      protected com.broadleafcommerce.data.tracking.core.context.ContextInfo buildUpdateContextInfo(BatchRequest.BatchContext context)
    • buildContextInfo

      protected com.broadleafcommerce.data.tracking.core.context.ContextInfo buildContextInfo(BatchRequest.BatchContext context, com.broadleafcommerce.data.tracking.core.type.OperationType operationType)
      Creates a hydrated ContextInfo with data matching values from context. This will first look at BatchRequest.BatchContext.getBaseContextInfo() and copy that if present, supplying the specified operationType. If the batch context does not have a base context info, a new one will be created (the logic is deferred to BatchContextHelper.buildContextInfoForBatchContext(BatchRequest.BatchContext, OperationType)).
      Parameters:
      context - the batch context from which to source context-info data
      operationType - the ContextInfo.getOperationType() to set
      Returns:
      a new hydrated ContextInfo with data matching values from context
    • getHydrator

      @Deprecated protected com.broadleafcommerce.data.tracking.core.web.ContextRequestHydrator getHydrator()
      Deprecated.
      No longer needed. Deferring to BatchContextHelper for context request hydration.
    • getBatchContextHelper

      protected BatchContextHelper getBatchContextHelper()
    • setBatchContextHelper

      @Autowired public void setBatchContextHelper(BatchContextHelper batchContextHelper)
    • getImportContextConfigurationProperties

      protected com.broadleafcommerce.common.extension.importing.ImportContextConfigurationProperties getImportContextConfigurationProperties()
    • setImportContextConfigurationProperties

      @Autowired public void setImportContextConfigurationProperties(@Nullable com.broadleafcommerce.common.extension.importing.ImportContextConfigurationProperties importContextConfigurationProperties)