Class AbstractItemMergingService<C,I>
- java.lang.Object
-
- com.broadleafcommerce.cartoperation.service.AbstractItemMergingService<C,I>
-
- All Implemented Interfaces:
ItemMergingService<C,I>
- Direct Known Subclasses:
DefaultCartItemMergingService
,DefaultItemListItemMergingService
public abstract class AbstractItemMergingService<C,I> extends Object implements ItemMergingService<C,I>
An abstract implementation for an item merging service. This handles any merge functionality that isn't specific to the type of item or its container. The rest is delegated to concrete implementations of this service.- Author:
- Jacob Mitash
-
-
Constructor Summary
Constructors Constructor Description AbstractItemMergingService()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract void
doMergeCombine(MergeItemPair<I> itemPair)
Perform the actual combination of two items (quantity change) for the combination merge type.protected List<MergeItemPair<I>>
findMatchingItems(@NonNull Collection<I> containerItems, @NonNull Collection<I> itemsToAdd)
Finds matching item pairs across the collections of items.protected List<MergeItemPair<I>>
findMatchingItems(C container, @NonNull Collection<I> itemsToAdd, com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
Finds matching items in the container to the collection of items.protected abstract Collection<I>
getItemsFromContainer(C container, com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
Gets the from the item container.protected void
handleRejectedMerges(@NonNull List<I> items, @NonNull List<MergeItemPair<I>> itemPairs)
Throws an exception when only one item is being added/updated and has a matching item.protected abstract String
hashMatchingCriteria(I item)
Generates a hash for any information on the item that should be used to determine if they represent the same item configuration.protected abstract boolean
isSameItem(I item1, I item2)
Tells if two item instances are actually the same instance.protected void
merge(@NonNull MergeItemPair<I> itemPair)
Merges the item pair using the merge type of the pair.protected void
mergeCombine(@NonNull MergeItemPair<I> itemPair)
Performs a "combination" merge for an item pair.protected void
mergeIgnore(@NonNull MergeItemPair<I> itemPair)
Performs a "reject or ignore" merge for an item pair.List<MergeItemPair<I>>
mergeItems(C container, @NonNull List<I> itemsToAdd, com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
Determines and prepares item merges between items being added/modified and the items of the container.protected void
mergeSeparate(@NonNull MergeItemPair<I> itemPair)
Performs a "separate" merge for an item pair.protected abstract void
resolveMergeTypes(@NonNull List<MergeItemPair<I>> itemPairs, com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
Determines what merge types should be used for the items and applies it to the merge item pairs.
-
-
-
Method Detail
-
mergeItems
public List<MergeItemPair<I>> mergeItems(@NonNull C container, @NonNull @NonNull List<I> itemsToAdd, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
Description copied from interface:ItemMergingService
Determines and prepares item merges between items being added/modified and the items of the container.- Specified by:
mergeItems
in interfaceItemMergingService<C,I>
- Parameters:
container
- the container that holds the items that will be merged.itemsToAdd
- the items that have been changed or are being added to consider for mergingcontext
- the context of the request- Returns:
- a list of matched item pairs that describe what changes should take place in order for the merge to occur
-
findMatchingItems
protected List<MergeItemPair<I>> findMatchingItems(@NonNull C container, @NonNull @NonNull Collection<I> itemsToAdd, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
Finds matching items in the container to the collection of items. SeefindMatchingItems(Collection, Collection)
for more detail.- Parameters:
container
- the container holding items to compare againstitemsToAdd
- the items to find matches inside the container forcontext
- the context of the request- Returns:
- a list of matched items
-
getItemsFromContainer
protected abstract Collection<I> getItemsFromContainer(C container, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
Gets the from the item container.- Parameters:
container
- the container to fetch the items fromcontext
- the context of the request- Returns:
- the items from the container
-
findMatchingItems
protected List<MergeItemPair<I>> findMatchingItems(@NonNull @NonNull Collection<I> containerItems, @NonNull @NonNull Collection<I> itemsToAdd)
Finds matching item pairs across the collections of items. "Matching" refers to items that essentially represent the same thing (e.g. a large, blue T-shirt SKU).- Parameters:
containerItems
- the items from the container to find matches foritemsToAdd
- the items being manipulated (added/updated) to find matches for- Returns:
- a list of item pairs for matching items.
-
isSameItem
protected abstract boolean isSameItem(I item1, I item2)
Tells if two item instances are actually the same instance. This would typically be determined by something like the item ID. Note that this does NOT indicate that "these items are the same so they should be merged". This is used to prevent creating an item match with itself during an update scenario.- Parameters:
item1
- an item to compareitem2
- another item to compare- Returns:
- true if the two items are referring to the same instance
-
hashMatchingCriteria
protected abstract String hashMatchingCriteria(I item)
Generates a hash for any information on the item that should be used to determine if they represent the same item configuration. For example, if an item contains a reference to a SKU with no options, this should hash the SKU reference since that can be used to determine if two items are the same.- Parameters:
item
- the item to generate a hash for- Returns:
- a hashed string of some subset of the items attributes
-
resolveMergeTypes
protected abstract void resolveMergeTypes(@NonNull @NonNull List<MergeItemPair<I>> itemPairs, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
Determines what merge types should be used for the items and applies it to the merge item pairs.- Parameters:
itemPairs
- the item pairs to resolve the merge types for
-
merge
protected void merge(@NonNull @NonNull MergeItemPair<I> itemPair)
Merges the item pair using the merge type of the pair.- Parameters:
itemPair
- the item pair to perform a merge on
-
mergeCombine
protected void mergeCombine(@NonNull @NonNull MergeItemPair<I> itemPair)
Performs a "combination" merge for an item pair.- Parameters:
itemPair
- the item pair to merge- See Also:
DefaultMergingType.COMBINE
-
mergeSeparate
protected void mergeSeparate(@NonNull @NonNull MergeItemPair<I> itemPair)
Performs a "separate" merge for an item pair.- Parameters:
itemPair
- the item pair to merge- See Also:
DefaultMergingType.SEPARATE
-
mergeIgnore
protected void mergeIgnore(@NonNull @NonNull MergeItemPair<I> itemPair)
Performs a "reject or ignore" merge for an item pair.- Parameters:
itemPair
- the item pair to merge- See Also:
DefaultMergingType.REJECT_OR_IGNORE
-
doMergeCombine
protected abstract void doMergeCombine(MergeItemPair<I> itemPair)
Perform the actual combination of two items (quantity change) for the combination merge type.- Parameters:
itemPair
- the item pair to combine
-
handleRejectedMerges
protected void handleRejectedMerges(@NonNull @NonNull List<I> items, @NonNull @NonNull List<MergeItemPair<I>> itemPairs)
Throws an exception when only one item is being added/updated and has a matching item. This is used to create an error when working with singular adds/updates when only one of each item is allowed in the container (DefaultMergingType.REJECT_OR_IGNORE
).- Parameters:
items
- the items being added/updateditemPairs
- the found item pairs
-
-