Class VariantRowConverter

java.lang.Object
com.broadleafcommerce.common.dataimport.conversion.AbstractRowConverter<com.broadleafcommerce.common.dataimport.util.ConversionUtils.ConversionResponse<Variant>>
com.broadleafcommerce.catalog.dataimport.converter.VariantRowConverter

public class VariantRowConverter extends com.broadleafcommerce.common.dataimport.conversion.AbstractRowConverter<com.broadleafcommerce.common.dataimport.util.ConversionUtils.ConversionResponse<Variant>>
Component to covert an import record into a ConversionUtils.ConversionResponse containing a Variant for persistence.
Author:
Kelly Tisdell (ktisdell)
  • Field Details

    • BATCH_CONTEXT_PREFETCHED_VARIANTS_BY_ID_MAP

      public static final String BATCH_CONTEXT_PREFETCHED_VARIANTS_BY_ID_MAP
      In BatchRequest.BatchContext.getAdditionalContextMap(), we expect a nested map under this key to contain prefetched variants by their ID.
      See Also:
    • BATCH_CONTEXT_PREFETCHED_VARIANTS_BY_EXTERNAL_ID_MAP

      public static final String BATCH_CONTEXT_PREFETCHED_VARIANTS_BY_EXTERNAL_ID_MAP
      In BatchRequest.BatchContext.getAdditionalContextMap(), we expect a nested map under this key to contain prefetched variants by their external ID.
      See Also:
    • BATCH_CONTEXT_PREFETCHED_VARIANTS_BY_SKU_BY_PRODUCT_ID_MAP

      public static final String BATCH_CONTEXT_PREFETCHED_VARIANTS_BY_SKU_BY_PRODUCT_ID_MAP
      In BatchRequest.BatchContext.getAdditionalContextMap(), we expect a doubly-nested map under this key to contain a mapping from product ID to a map of variant SKUs to variants.
      See Also:
  • Constructor Details

    • VariantRowConverter

      public VariantRowConverter(@NonNull @NonNull com.fasterxml.jackson.databind.ObjectMapper objectMapper, @NonNull @NonNull com.broadleafcommerce.common.extension.TypeFactory typeFactory, @NonNull @NonNull com.broadleafcommerce.common.dataimport.util.IdResolver idResolver)
  • Method Details

    • convert

      @NonNull public com.broadleafcommerce.common.dataimport.util.ConversionUtils.ConversionResponse<Variant> convert(@Nullable Object parent, com.broadleafcommerce.common.dataimport.messaging.BatchRecord record, com.broadleafcommerce.common.dataimport.messaging.BatchRequest.BatchContext context)
      Specified by:
      convert in class com.broadleafcommerce.common.dataimport.conversion.AbstractRowConverter<com.broadleafcommerce.common.dataimport.util.ConversionUtils.ConversionResponse<Variant>>
    • canConvert

      public boolean canConvert(com.broadleafcommerce.common.dataimport.messaging.BatchRecord record, com.broadleafcommerce.common.dataimport.messaging.BatchRequest.BatchContext context)
      Specified by:
      canConvert in class com.broadleafcommerce.common.dataimport.conversion.AbstractRowConverter<com.broadleafcommerce.common.dataimport.util.ConversionUtils.ConversionResponse<Variant>>
    • determineOperationType

      protected com.broadleafcommerce.data.tracking.core.type.OperationType determineOperationType(@NonNull @NonNull Variant variant, boolean variantAlreadyExistsInDatastore, @NonNull @NonNull com.broadleafcommerce.common.dataimport.messaging.BatchRecord record, @Nullable com.broadleafcommerce.common.dataimport.messaging.BatchRequest.BatchContext context)
      Determine the effective operation type for the given batch record and variant instance. This can help inform decisions on how to instantiate/map fields.
      Parameters:
      variant - the resolved variant instance
      variantAlreadyExistsInDatastore - whether the variant was determined to already exist in the datastore. See instantiateOrGetPrefetchedVariant(BatchRecord, Product, Map, BatchContext)
      record - the original batch record
      context - the batch context
      Returns:
      the effective operation type for the given batch record
    • resolveVariantId

      protected String resolveVariantId(@NonNull @NonNull Variant variant, @NonNull @NonNull com.broadleafcommerce.common.dataimport.messaging.BatchRecord record, @NonNull @NonNull com.broadleafcommerce.data.tracking.core.type.OperationType operation, @Nullable com.broadleafcommerce.common.dataimport.messaging.BatchRequest.BatchContext context)
    • instantiateOrGetPrefetchedVariant

      protected org.springframework.data.util.Pair<Boolean,Variant> instantiateOrGetPrefetchedVariant(@NonNull @NonNull com.broadleafcommerce.common.dataimport.messaging.BatchRecord record, @NonNull @NonNull Product parent, @NonNull @NonNull Map<String,String> mutableRow, @Nullable com.broadleafcommerce.common.dataimport.messaging.BatchRequest.BatchContext context)
      We expect the CompleteProductImportBatchHandler to have pre-fetched variants by various identifier fields if supplied in the original rows. Any variants that were found in the datastore should have been populated into BatchRequest.BatchContext.getAdditionalContextMap().

      If a variant is present in this map, we will use this existing instance.

      If a variant is not present in this map, we will simply instantiate a new instance.

      Parameters:
      record - the original variant BatchRecord
      parent - the already-converted parent Product
      mutableRow - the row from the batch record (usually with any special columns pre-removed via extractSpecialColumns(Map, BatchContext)
      context - the BatchRequest.BatchContext, which should have any pre-fetched variants in BatchRequest.BatchContext.getAdditionalContextMap()
      Returns:
      a Pair containing a boolean describing whether the variant was found in the pre-fetched map, as well as the variant instance itself
    • getPrefetchedVariant

      @Nullable protected Variant getPrefetchedVariant(com.broadleafcommerce.common.dataimport.messaging.BatchRecord batchRecord, Map<String,String> batchRecordRow, @NonNull @NonNull Product parent, @Nullable com.broadleafcommerce.common.dataimport.messaging.BatchRequest.BatchContext context)
      Checks the BatchRequest.BatchContext.getAdditionalContextMap() to see if there is already a pre-fetched variant matching the batchRecord.
      Parameters:
      batchRecord - the batch record for which to find the corresponding pre-fetched variant instance (if exists)
      batchRecordRow - the row from the batch record (pre-processed if necessary)
      parent - the already-converted parent Product
      context - the batch context which should contain pre-fetched variants set by CompleteProductImportBatchHandler
      Returns:
      the pre-fetched variant instance if found, or null
    • parseAndSetAttributes

      protected void parseAndSetAttributes(@NonNull @NonNull Variant variant, @NonNull @NonNull Map<String,String> specialColumns, @NonNull @NonNull com.broadleafcommerce.data.tracking.core.type.OperationType operationType)
    • parseAndSetFulfillmentFlatRates

      protected void parseAndSetFulfillmentFlatRates(@NonNull @NonNull Variant variant, @NonNull @NonNull Map<String,String> specialColumns, @NonNull @NonNull com.broadleafcommerce.data.tracking.core.type.OperationType operationType)
    • toKeyFulfillmentFlatRatePair

      protected org.springframework.data.util.Pair<String,FulfillmentFlatRate> toKeyFulfillmentFlatRatePair(String[] keyValueArray)
    • parseAndSetProductOptionValues

      protected void parseAndSetProductOptionValues(Product parent, Variant variant, Map<String,String> specialColumns, com.broadleafcommerce.data.tracking.core.type.OperationType operationType)
      The ProductRowConverter.parseAndSetOptions(Product, OperationType, BatchRecord, Map, BatchContext) method should have already parsed option values from this variant record (and all others) and used it to populate the AttributeChoice.getAllowedValues() on the parent.

      This method relies on that as well as the provided row value to initialize Variant.getOptionValues().

      Parameters:
      parent - the already-converted parent Product
      variant - the variant instance to set the parsed option values on
      specialColumns - special columns from the row that need special processing - should contain the options header
      operationType - the determined operation type
    • parseAndValidateIndividualVariantOptionValueToken

      public Map<String,String> parseAndValidateIndividualVariantOptionValueToken(String variantOptionValueToken)
      In VariantRowConverter.VariantProperties.OPTIONS_HEADER, there may be multiple option value tokens provided, separated by RowUtils.VALUE_SEPARATOR - for example, 'Shirt Color::Red|Shirt Size::Small'. This method is capable of parsing one such option value token ('Shirt Color::Red') and returning a property/value map of properties provided in that token.

      A few different syntaxes are accepted for this token:

      • This could be a simple key/value pair such as 'Size::Small'. In this case, the key is the attribute choice name from the product option, and the value will be used as both the choice value label and the choice value itself.
      • It may also be a more complex token such as 'attributeName::Size;value::Small;valueLabel::Small (Ages 5-6)'. With this syntax, 'valueLabel' is optional and will be defaulted to the value of 'value'.
      Parameters:
      variantOptionValueToken - an individual option value token from VariantRowConverter.VariantProperties.OPTIONS_HEADER
      Returns:
      a property/value map of select properties explicitly or implicitly provided in that token
      Throws:
      IllegalArgumentException - if the token could not be successfully parsed or failed validation
      See Also:
    • modifyVariant

      protected Variant modifyVariant(@NonNull @NonNull Variant variant, @NonNull @NonNull Product parent, @NonNull @NonNull com.broadleafcommerce.common.dataimport.messaging.BatchRecord record, @Nullable com.broadleafcommerce.common.dataimport.messaging.BatchRequest.BatchContext context)
      Hook point to allow implementers to modify the details of the variant or return a different variant instance.
      Parameters:
      variant - the variant that has been created populated with data from the row
      parent - the parent product of this variant
      record - the record that represents the row that was mapped to the variant
      context - the batch context
    • initializeData

      protected void initializeData(@NonNull @NonNull Product parent, @NonNull @NonNull Variant variant, @NonNull @NonNull com.broadleafcommerce.common.dataimport.messaging.BatchRecord record, @NonNull @NonNull Map<String,String> mutableRow, @NonNull @NonNull com.broadleafcommerce.data.tracking.core.type.OperationType operation, @Nullable com.broadleafcommerce.common.dataimport.messaging.BatchRequest.BatchContext context)
      Initialize the data from the row onto the variant. Set additional data as needed. For example, set ID and activeStartDate.
      Parameters:
      parent - the parent product of this variant
      variant - the target variant onto which we set data.
      record - the original BatchRecord
      mutableRow - the mutable row from the BatchRecord with entries removed that cannot be handled by reflection
      operation - the operation; typically CREATE or UPDATE
      context - the BatchContext
    • extractSpecialColumns

      protected Map<String,String> extractSpecialColumns(@NonNull @NonNull Map<String,String> mutableRow, com.broadleafcommerce.common.dataimport.messaging.BatchRequest.BatchContext context)
      Removes and returns columns from the row that need to have special parsing logic.
      Overrides:
      extractSpecialColumns in class com.broadleafcommerce.common.dataimport.conversion.AbstractRowConverter<com.broadleafcommerce.common.dataimport.util.ConversionUtils.ConversionResponse<Variant>>
      Parameters:
      mutableRow - the existing row from which special fields will be removed and copied to the output
      context - the batch context
      Returns:
      just the special columns of the row after being removed from mutableRow
    • getObjectMapper

      protected com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()
    • getTypeFactory

      protected com.broadleafcommerce.common.extension.TypeFactory getTypeFactory()
    • getIdResolver

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