Class AbstractImportBatchHandler
- All Implemented Interfaces:
ImportBatchHandler
ImportBatchHandler
implementations.
Contains methods providing boilerplate logic for performing persistence and handling the outcome.
-
Constructor Summary
ConstructorsConstructorDescriptionAbstractImportBatchHandler
(com.broadleafcommerce.data.tracking.core.web.ContextRequestHydrator hydrator) -
Method Summary
Modifier and TypeMethodDescriptionprotected <T> void
addFailedConversionCompletions
(BatchRecord failedToConvert, ConversionUtils.ConversionResponse<T> failedConversion, List<BatchCompletionRecord> destination) Creates aBatchCompletionRecord
for the givenfailedToConvert
batch record, and then createsBatchCompletionRecords
for all of its nested dependents.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) protected com.broadleafcommerce.data.tracking.core.context.ContextInfo
buildContextInfo
(BatchRequest.BatchContext context, com.broadleafcommerce.data.tracking.core.type.OperationType operationType) Creates a hydratedContextInfo
with data matching values fromcontext
.protected com.broadleafcommerce.data.tracking.core.context.ContextInfo
protected <T> List<BatchCompletionRecord>
buildNonValidationErrorCompletions
(String entityResourceId, PersistenceRequest<T> persistenceRequest, com.broadleafcommerce.data.tracking.core.service.BulkPersistenceResponse.PersistenceFailure<T> persistenceFailure) protected com.broadleafcommerce.data.tracking.core.context.ContextInfo
protected <T> List<BatchCompletionRecord>
buildSuccessfulCompletionsForNoChangesRequest
(String entityResourceId, PersistenceRequest<T> persistenceRequest) protected <T> List<BatchCompletionRecord>
buildSuccessfulCompletionsForRequest
(String entityResourceId, PersistenceRequest<T> persistenceRequest) protected com.broadleafcommerce.data.tracking.core.context.ContextInfo
protected <T> List<BatchCompletionRecord>
buildValidationErrorCompletions
(String entityResourceId, PersistenceRequest<T> persistenceRequest, com.broadleafcommerce.data.tracking.core.service.BulkPersistenceResponse.PersistenceFailure<T> persistenceFailure) protected void
failRecordAndDependentsEarly
(BatchRecord recordToFail, String errorMessage, 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.void
finalizeContextForBatch
(@NonNull BatchRequest batch) Method to clear any state after processing a batch.protected void
generateAndAddCompletions
(BatchRecord batchRecord, BatchCompletionRecordStatus status, String errorMessage, List<BatchCompletionRecord> destination) protected void
generateAndAddCompletions
(PersistenceRequest.EmbeddedItemRequest embeddedItemRequest, BatchCompletionRecordStatus status, String errorMessage, List<BatchCompletionRecord> destination) protected void
generateAndAddValidationErrorCompletions
(PersistenceRequest.EmbeddedItemRequest embeddedItemRequest, String parentPath, Map<String, org.springframework.validation.FieldError> fieldErrorsByPath, String parentItemCorrelationId, List<BatchCompletionRecord> destination) protected BatchContextHelper
protected BatchCompletionRecord
getCompletionError
(BatchRecord record, String errorMessage) protected String
getFormattedEmbeddedValidationErrorString
(List<org.springframework.validation.FieldError> errorsWithEmbeddedField, String embeddedFieldNestedPath, String parentCorrelationId) protected String
getFormattedValidationErrorString
(Collection<org.springframework.validation.ObjectError> globalErrors, Collection<org.springframework.validation.FieldError> fieldErrors) protected com.broadleafcommerce.data.tracking.core.web.ContextRequestHydrator
Deprecated.No longer needed.protected com.broadleafcommerce.common.extension.importing.ImportContextConfigurationProperties
protected com.broadleafcommerce.common.extension.importing.ImportContextConfigurationProperties.ImportContextProperties
getImportContextProperties
(BatchRequest batchRequest) void
initializeContextForBatch
(@NonNull BatchRequest batch) Method to initialize fields for a batch.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 asPersistenceRequest
sprotected <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 aroundpersist(BatchRequest, List, BatchCreator, BatchUpdater, Function)
to submit therequests
in chunks/smaller-batches.protected List<org.springframework.validation.FieldError>
removeAndReturnFieldErrorsUnderPath
(Map<String, org.springframework.validation.FieldError> fieldErrorsByPath, String path) void
setBatchContextHelper
(BatchContextHelper batchContextHelper) void
setImportContextConfigurationProperties
(com.broadleafcommerce.common.extension.importing.ImportContextConfigurationProperties importContextConfigurationProperties) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.broadleafcommerce.common.dataimport.ImportBatchHandler
canHandle, getDataRouteKey, handle
-
Constructor Details
-
AbstractImportBatchHandler
public AbstractImportBatchHandler(com.broadleafcommerce.data.tracking.core.web.ContextRequestHydrator hydrator)
-
-
Method Details
-
initializeContextForBatch
Method to initialize fields for a batch. By default, initializes the default currency for the process useingSystemCurrencyContextHolder.setProcessCurrency(CurrencyUnit)
. Implementations may override for other batch initialization needs. For thread local items like process currency, thefinalizeContextForBatch(BatchRequest)
should be called to clear the process state.- Specified by:
initializeContextForBatch
in interfaceImportBatchHandler
- Parameters:
batch
- The batch to be processed.
-
finalizeContextForBatch
Method to clear any state after processing a batch. Default implementation callsSystemCurrencyContextHolder.clearProcessCurrency()
- Specified by:
finalizeContextForBatch
in interfaceImportBatchHandler
- 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 aroundpersist(BatchRequest, List, BatchCreator, BatchUpdater, Function)
to submit therequests
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 batchrequests
- the full list of entities that need to be persistedcreator
- performs the persistence for all requests wherePersistenceRequest.getOperation()
isOperationType.CREATE
updater
- performs the persistence for all requests wherePersistenceRequest.getOperation()
isOperationType.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 idbatchSize
- the maximum number of elements fromrequests
that should be submitted at once topersist(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 asPersistenceRequest
s- Type Parameters:
T
- type being persisted- Parameters:
batch
- the original record batch, used for constructing a ContextInforequests
- what to persistcreator
- performs the persistence for all requests wherePersistenceRequest.getOperation()
isOperationType.CREATE
updater
- performs the persistence for all requests wherePersistenceRequest.getOperation()
isOperationType.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) -
buildSuccessfulCompletionsForNoChangesRequest
protected <T> List<BatchCompletionRecord> buildSuccessfulCompletionsForNoChangesRequest(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
-
generateAndAddValidationErrorCompletions
protected void generateAndAddValidationErrorCompletions(PersistenceRequest.EmbeddedItemRequest embeddedItemRequest, @Nullable String parentPath, Map<String, org.springframework.validation.FieldError> fieldErrorsByPath, String parentItemCorrelationId, List<BatchCompletionRecord> destination) -
getFormattedEmbeddedValidationErrorString
-
getFormattedValidationErrorString
protected String getFormattedValidationErrorString(Collection<org.springframework.validation.ObjectError> globalErrors, Collection<org.springframework.validation.FieldError> fieldErrors) -
getCompletionError
-
addFailedConversionCompletions
protected <T> void addFailedConversionCompletions(BatchRecord failedToConvert, ConversionUtils.ConversionResponse<T> failedConversion, List<BatchCompletionRecord> destination) Creates aBatchCompletionRecord
for the givenfailedToConvert
batch record, and then createsBatchCompletionRecords
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 convertedfailedConversion
- the response describing the errors with the conversiondestination
- 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 givenrecordToFail
batch record, and then createsBatchCompletionRecords
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 recorddependentErrorMessage
- the error message to set on dependents of the recorddestination
- 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 hydratedContextInfo
with data matching values fromcontext
. This will first look atBatchRequest.BatchContext.getBaseContextInfo()
and copy that if present, supplying the specifiedoperationType
. If the batch context does not have a base context info, a new one will be created (the logic is deferred toBatchContextHelper.buildContextInfoForBatchContext(BatchRequest.BatchContext, OperationType)
).- Parameters:
context
- the batch context from which to source context-info dataoperationType
- theContextInfo.getOperationType()
to set- Returns:
- a new hydrated
ContextInfo
with data matching values fromcontext
-
getHydrator
@Deprecated protected com.broadleafcommerce.data.tracking.core.web.ContextRequestHydrator getHydrator()Deprecated.No longer needed. Deferring toBatchContextHelper
for context request hydration. -
getBatchContextHelper
-
setBatchContextHelper
-
getImportContextConfigurationProperties
protected com.broadleafcommerce.common.extension.importing.ImportContextConfigurationProperties getImportContextConfigurationProperties() -
setImportContextConfigurationProperties
@Autowired public void setImportContextConfigurationProperties(@Nullable com.broadleafcommerce.common.extension.importing.ImportContextConfigurationProperties importContextConfigurationProperties)
-