Class CustomerImportBatchHandler

  • All Implemented Interfaces:
    com.broadleafcommerce.common.dataimport.ImportBatchHandler

    @DataRouteByKey("customer")
    public class CustomerImportBatchHandler
    extends com.broadleafcommerce.common.dataimport.AbstractImportBatchHandler
    Author:
    Vitalii Voronkov (vvoronkov)
    • Constructor Detail

      • CustomerImportBatchHandler

        public CustomerImportBatchHandler​(CustomerConverter customerConverter,
                                          CustomerService<Customer> customerService,
                                          com.broadleafcommerce.data.tracking.core.web.ContextRequestHydrator hydrator)
    • Method Detail

      • canHandle

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

        @SuppressNotification("SINGLE_INDEX_REQUEST")
        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<Customer,​com.broadleafcommerce.common.dataimport.messaging.BatchRecord>> convertBatchRecords​(com.broadleafcommerce.common.dataimport.messaging.BatchRequest batch,
                                                                                                                                                                          List<com.broadleafcommerce.common.dataimport.messaging.BatchCompletionRecord> erroredCompletions)
        Converts all of the batch records in the batch request to customer.

        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 customer email address, 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
        erroredCompletions - the list to which any errored completion records should be added
        Returns:
        a map of the converted Customer and original BatchRecord pairs grouped by email address value
      • buildPersistenceRequests

        protected List<com.broadleafcommerce.common.dataimport.util.PersistenceRequest<Customer>> buildPersistenceRequests​(Map<String,​org.apache.commons.lang3.tuple.Pair<Customer,​com.broadleafcommerce.common.dataimport.messaging.BatchRecord>> requestedCustomersByEmailAddress,
                                                                                                                           @Nullable
                                                                                                                           com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
        Builds the list of PersistenceRequests for the given Customers.

        This method will query the data-store for existing customers to determine whether each item should be an update or a create.

        Parameters:
        requestedCustomersByEmailAddress - the map of Customer and BatchRecord pairs grouped customer email address
        context - request context information around sandbox and multitenant state
        Returns:
        the list of PersistenceRequest for the Customer
      • buildPersistenceRequest

        protected com.broadleafcommerce.common.dataimport.util.PersistenceRequest<Customer> buildPersistenceRequest​(String requestedCustomerEmail,
                                                                                                                    org.apache.commons.lang3.tuple.Pair<Customer,​com.broadleafcommerce.common.dataimport.messaging.BatchRecord> requestedCustomerAndBatchRecord,
                                                                                                                    Map<String,​Customer> existingCustomersByEmailAddress)
        Determines id, operation type and builds the PersistenceRequest for the Customer.
        Parameters:
        requestedCustomerEmail - the email address of requested customer
        requestedCustomerAndBatchRecord - the pair of requested Customer and BatchRecord
        existingCustomersByEmailAddress - the map of Customer grouped by email address
        Returns:
        the PersistenceRequest for the Customer
      • persistPersistenceRequests

        protected void persistPersistenceRequests​(com.broadleafcommerce.common.dataimport.messaging.BatchRequest batch,
                                                  List<com.broadleafcommerce.common.dataimport.util.PersistenceRequest<Customer>> customerRequests,
                                                  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
        customerRequests - a list of the customer persistence requests
        allCompletions - the list to which the completion records should be added
      • processAddressesForSuccessfullyProcessedCustomers

        protected void processAddressesForSuccessfullyProcessedCustomers​(com.broadleafcommerce.common.dataimport.messaging.BatchRequest request,
                                                                         List<com.broadleafcommerce.common.dataimport.messaging.BatchCompletionRecord> allCompletions)
        Once customer records are successfully created/updated, we will process any requested addresses for them.

        It is important that this processing happens after customer persistence for a few reasons:

        • Managing an address for a failed customer would be invalid for obvious reasons, so confirming success before proceeding is important
        • Certain address fields are sourced directly from the Customer itself, such as Full Name, Primary Phone Number, etc. However, since this specification allows for 'updates', it's possible the row in the import file is sparse and does not directly supply those values. Thus, we must wait until _after_ persistence to see the final state of the Customer and source values from it.
        Parameters:
        request - the original batch request
        allCompletions - these are assumed to exclusively contain customer records after all customer conversion/persistence is completed. The list may be mutated.
      • preFetchCustomerAddresses

        protected void preFetchCustomerAddresses​(Set<String> customerIdsToGetAddressesFor,
                                                 @Nullable
                                                 com.broadleafcommerce.data.tracking.core.context.ContextInfo readContextInfo,
                                                 com.broadleafcommerce.common.dataimport.messaging.BatchRequest batchRequest)
        To satisfy requirements of CustomerAddressColumnConverter, this method will identify and pre-fetch existing customer addresses for the specified customers and place them into BatchRequest.BatchContext.getAdditionalContextMap() under CustomerAddressColumnConverter.BATCH_CONTEXT_PREFETCHED_CUSTOMER_ADDRESSES_BY_CUSTOMER_ID_MAP. This ahead-of-time bulk fetch is more performant and allows the converter to properly handle creates vs updates.
        Parameters:
        customerIdsToGetAddressesFor - all customer ids to fetch addresses for
        readContextInfo - context info that can be used to fetch addresses
        batchRequest - the original batch request
      • getDataRouteKey

        public String getDataRouteKey()
      • convertToCustomer

        protected Optional<Customer> convertToCustomer​(com.broadleafcommerce.common.dataimport.messaging.BatchRecord customerRecord,
                                                       List<com.broadleafcommerce.common.dataimport.messaging.BatchCompletionRecord> allCompletions)
      • getExistingCustomers

        protected Map<String,​Customer> getExistingCustomers​(Set<String> customerEmails,
                                                                  @Nullable
                                                                  com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
      • setCustomerAddressColumnConverter

        @Autowired
        public void setCustomerAddressColumnConverter​(CustomerAddressColumnConverter customerAddressColumnConverter)
      • 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)