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 Details

    • AbstractItemMergingService

      public AbstractItemMergingService()
  • Method Details

    • 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 interface ItemMergingService<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 merging
      context - 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. See findMatchingItems(Collection, Collection) for more detail.
      Parameters:
      container - the container holding items to compare against
      itemsToAdd - the items to find matches inside the container for
      context - 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 from
      context - 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 for
      itemsToAdd - 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 compare
      item2 - 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.This implementation uses the following criteria:
      - attribute choices, which are product options of type CART_ITEM_ATTRIBUTE or VARIANT_DISTINGUISHING
      - sku, which uniquely identifies products and their variations
      - merchandising context, which identifies selector products
      - pricing key
      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:
    • 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:
    • 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:
    • 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/updated
      itemPairs - the found item pairs