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)
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final class
Properties that can be provided in individual option value tokens inVariantRowConverter.VariantProperties.OPTIONS_HEADER
.static final class
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
InBatchRequest.BatchContext.getAdditionalContextMap()
, we expect a nested map under this key to contain prefetched variants by their external ID.static final String
InBatchRequest.BatchContext.getAdditionalContextMap()
, we expect a nested map under this key to contain prefetched variants by their ID.static final String
InBatchRequest.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.Fields inherited from class com.broadleafcommerce.common.dataimport.conversion.AbstractRowConverter
COMPLEX_COLUMN_HEADER_PREFIX_DELIMITER
-
Constructor Summary
ConstructorDescriptionVariantRowConverter
(@NonNull com.fasterxml.jackson.databind.ObjectMapper objectMapper, @NonNull com.broadleafcommerce.common.extension.TypeFactory typeFactory, @NonNull com.broadleafcommerce.common.dataimport.util.IdResolver idResolver) -
Method Summary
Modifier and TypeMethodDescriptionboolean
canConvert
(com.broadleafcommerce.common.dataimport.messaging.BatchRecord record, com.broadleafcommerce.common.dataimport.messaging.BatchRequest.BatchContext context) com.broadleafcommerce.common.dataimport.util.ConversionUtils.ConversionResponse<Variant>
convert
(Object parent, com.broadleafcommerce.common.dataimport.messaging.BatchRecord record, com.broadleafcommerce.common.dataimport.messaging.BatchRequest.BatchContext context) protected com.broadleafcommerce.data.tracking.core.type.OperationType
determineOperationType
(@NonNull Variant variant, boolean variantAlreadyExistsInDatastore, @NonNull com.broadleafcommerce.common.dataimport.messaging.BatchRecord record, com.broadleafcommerce.common.dataimport.messaging.BatchRequest.BatchContext context) Determine the effective operation type for the given batch record and variant instance.extractSpecialColumns
(@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.protected com.broadleafcommerce.common.dataimport.util.IdResolver
protected com.fasterxml.jackson.databind.ObjectMapper
protected Variant
getPrefetchedVariant
(com.broadleafcommerce.common.dataimport.messaging.BatchRecord batchRecord, Map<String, String> batchRecordRow, @NonNull Product parent, com.broadleafcommerce.common.dataimport.messaging.BatchRequest.BatchContext context) Checks theBatchRequest.BatchContext.getAdditionalContextMap()
to see if there is already a pre-fetched variant matching thebatchRecord
.protected com.broadleafcommerce.common.extension.TypeFactory
protected void
initializeData
(@NonNull Product parent, @NonNull Variant variant, @NonNull com.broadleafcommerce.common.dataimport.messaging.BatchRecord record, @NonNull Map<String, String> mutableRow, @NonNull com.broadleafcommerce.data.tracking.core.type.OperationType operation, com.broadleafcommerce.common.dataimport.messaging.BatchRequest.BatchContext context) Initialize the data from the row onto the variant.instantiateOrGetPrefetchedVariant
(@NonNull com.broadleafcommerce.common.dataimport.messaging.BatchRecord record, @NonNull Product parent, @NonNull Map<String, String> mutableRow, com.broadleafcommerce.common.dataimport.messaging.BatchRequest.BatchContext context) We expect theCompleteProductImportBatchHandler
to have pre-fetched variants by various identifier fields if supplied in the original rows.protected Variant
modifyVariant
(@NonNull Variant variant, @NonNull Product parent, @NonNull com.broadleafcommerce.common.dataimport.messaging.BatchRecord record, 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.protected void
parseAndSetAttributes
(@NonNull Variant variant, @NonNull Map<String, String> specialColumns, @NonNull com.broadleafcommerce.data.tracking.core.type.OperationType operationType) protected void
parseAndSetFulfillmentFlatRates
(@NonNull Variant variant, @NonNull Map<String, String> specialColumns, @NonNull com.broadleafcommerce.data.tracking.core.type.OperationType operationType) protected void
parseAndSetProductOptionValues
(Product parent, Variant variant, Map<String, String> specialColumns, com.broadleafcommerce.data.tracking.core.type.OperationType operationType) TheProductRowConverter.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 theAttributeChoice.getAllowedValues()
on theparent
.parseAndValidateIndividualVariantOptionValueToken
(String variantOptionValueToken) InVariantRowConverter.VariantProperties.OPTIONS_HEADER
, there may be multiple option value tokens provided, separated byRowUtils.VALUE_SEPARATOR
- for example, 'Shirt Color::Red|Shirt Size::Small'.protected String
resolveVariantId
(@NonNull Variant variant, @NonNull com.broadleafcommerce.common.dataimport.messaging.BatchRecord record, @NonNull com.broadleafcommerce.data.tracking.core.type.OperationType operation, com.broadleafcommerce.common.dataimport.messaging.BatchRequest.BatchContext context) protected org.springframework.data.util.Pair<String,
FulfillmentFlatRate> toKeyFulfillmentFlatRatePair
(String[] keyValueArray)
-
Field Details
-
BATCH_CONTEXT_PREFETCHED_VARIANTS_BY_ID_MAP
InBatchRequest.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
InBatchRequest.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
InBatchRequest.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 classcom.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 classcom.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 instancevariantAlreadyExistsInDatastore
- whether the variant was determined to already exist in the datastore. SeeinstantiateOrGetPrefetchedVariant(BatchRecord, Product, Map, BatchContext)
record
- the original batch recordcontext
- 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 theCompleteProductImportBatchHandler
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 intoBatchRequest.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 variantBatchRecord
parent
- the already-converted parentProduct
mutableRow
- the row from the batch record (usually with any special columns pre-removed viaextractSpecialColumns(Map, BatchContext)
context
- theBatchRequest.BatchContext
, which should have any pre-fetched variants inBatchRequest.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 theBatchRequest.BatchContext.getAdditionalContextMap()
to see if there is already a pre-fetched variant matching thebatchRecord
.- 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 parentProduct
context
- the batch context which should contain pre-fetched variants set byCompleteProductImportBatchHandler
- Returns:
- the pre-fetched variant instance if found, or
null
-
parseAndSetAttributes
-
parseAndSetFulfillmentFlatRates
-
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) TheProductRowConverter.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 theAttributeChoice.getAllowedValues()
on theparent
.This method relies on that as well as the provided row value to initialize
Variant.getOptionValues()
.- Parameters:
parent
- the already-converted parentProduct
variant
- the variant instance to set the parsed option values onspecialColumns
- special columns from the row that need special processing - should contain the options headeroperationType
- the determined operation type
-
parseAndValidateIndividualVariantOptionValueToken
public Map<String,String> parseAndValidateIndividualVariantOptionValueToken(String variantOptionValueToken) InVariantRowConverter.VariantProperties.OPTIONS_HEADER
, there may be multiple option value tokens provided, separated byRowUtils.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 fromVariantRowConverter.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 rowparent
- the parent product of this variantrecord
- the record that represents the row that was mapped to the variantcontext
- 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 variantvariant
- the target variant onto which we set data.record
- the original BatchRecordmutableRow
- the mutable row from the BatchRecord with entries removed that cannot be handled by reflectionoperation
- the operation; typically CREATE or UPDATEcontext
- 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 classcom.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 outputcontext
- 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()
-