@Override
  public OrderItem findLastMatchingItem(Order order, Long skuId, Long productId) {
    if (order.getOrderItems() != null) {
      for (int i = (order.getOrderItems().size() - 1); i >= 0; i--) {
        OrderItem currentItem = (order.getOrderItems().get(i));
        if (currentItem instanceof DiscreteOrderItem) {
          DiscreteOrderItem discreteItem = (DiscreteOrderItem) currentItem;
          if (skuId != null) {
            if (discreteItem.getSku() != null && skuId.equals(discreteItem.getSku().getId())) {
              return discreteItem;
            }
          } else if (productId != null
              && discreteItem.getProduct() != null
              && productId.equals(discreteItem.getProduct().getId())) {
            return discreteItem;
          }

        } else if (currentItem instanceof BundleOrderItem) {
          BundleOrderItem bundleItem = (BundleOrderItem) currentItem;
          if (skuId != null) {
            if (bundleItem.getSku() != null && skuId.equals(bundleItem.getSku().getId())) {
              return bundleItem;
            }
          } else if (productId != null
              && bundleItem.getProduct() != null
              && productId.equals(bundleItem.getProduct().getId())) {
            return bundleItem;
          }
        }
      }
    }
    return null;
  }
  public Order setUpAnonymousCartWithBundleGiftWrapReferringItemsInAnotherBundle()
      throws PricingException {
    Customer customer = customerService.saveCustomer(createNamedCustomer());

    Order order = cartService.createNewCartForCustomer(customer);

    BundleOrderItem newBundle =
        (BundleOrderItem) orderService.addBundleItemToOrder(order, createBundleOrderItemRequest());
    BundleOrderItem newBundle2 =
        (BundleOrderItem)
            orderService.addBundleItemToOrder(order, createBundleOrderItemRequestWithGiftWrap());
    GiftWrapOrderItem giftItem = null;
    for (DiscreteOrderItem addedItem : newBundle2.getDiscreteOrderItems()) {
      if (addedItem instanceof GiftWrapOrderItem) {
        giftItem = (GiftWrapOrderItem) addedItem;
      }
    }
    for (DiscreteOrderItem addedItem : newBundle.getDiscreteOrderItems()) {
      addedItem.setGiftWrapOrderItem(giftItem);
    }
    giftItem.getWrappedItems().addAll(newBundle.getDiscreteOrderItems());
    order = orderService.save(order, false);

    return order;
  }
  public Order setUpAnonymousCartWithBundleGiftWrap() throws PricingException {
    Customer customer = customerService.saveCustomer(createNamedCustomer());

    Order order = cartService.createNewCartForCustomer(customer);

    BundleOrderItemRequest itemRequest = createBundleOrderItemRequestWithGiftWrap();

    BundleOrderItem newBundle =
        (BundleOrderItem) orderService.addBundleItemToOrder(order, itemRequest);
    List<OrderItem> addedItems = new ArrayList<OrderItem>();
    GiftWrapOrderItem giftItem = null;
    for (DiscreteOrderItem addedItem : newBundle.getDiscreteOrderItems()) {
      if (addedItem instanceof GiftWrapOrderItem) {
        giftItem = (GiftWrapOrderItem) addedItem;
      } else {
        addedItems.add(addedItem);
      }
    }
    for (OrderItem addedItem : addedItems) {
      addedItem.setGiftWrapOrderItem(giftItem);
    }
    giftItem.getWrappedItems().addAll(addedItems);
    order = orderService.save(order, false);

    return order;
  }
  public Order setUpAnonymousCartWithInactiveBundleGiftWrap() throws PricingException {
    Customer customer = customerService.saveCustomer(createNamedCustomer());

    Order order = cartService.createNewCartForCustomer(customer);

    Sku newSku = addTestSku("Small Plastic Crate", "Plastic Crate", "Crates");
    Sku newInactiveSku = addTestSku("Small Red Plastic Crate", "Plastic Crate", "Crates", false);
    Sku giftWrapSku = addTestSku("Gift Box", "Gift Box", "Gift Wraps");
    Category category = newSku.getAllParentProducts().get(0).getDefaultCategory();

    List<DiscreteOrderItemRequest> discreteOrderItems = new ArrayList<DiscreteOrderItemRequest>();
    discreteOrderItems.add(createDiscreteOrderItemRequest(newSku, 1));
    discreteOrderItems.add(createDiscreteOrderItemRequest(newInactiveSku, 1));
    discreteOrderItems.add(
        createGiftWrapOrderItemRequest(giftWrapSku, 1, new ArrayList<OrderItem>()));

    BundleOrderItemRequest itemRequest = new BundleOrderItemRequest();
    itemRequest.setCategory(category);
    itemRequest.setName("test bundle " + bundleCount++);
    itemRequest.setQuantity(1);
    itemRequest.setDiscreteOrderItems(discreteOrderItems);

    BundleOrderItem newBundle =
        (BundleOrderItem) orderService.addBundleItemToOrder(order, itemRequest);
    List<OrderItem> addedItems = new ArrayList<OrderItem>();
    GiftWrapOrderItem giftItem = null;
    for (DiscreteOrderItem addedItem : newBundle.getDiscreteOrderItems()) {
      if (addedItem instanceof GiftWrapOrderItem) {
        giftItem = (GiftWrapOrderItem) addedItem;
      } else {
        addedItems.add(addedItem);
      }
    }
    for (OrderItem addedItem : addedItems) {
      addedItem.setGiftWrapOrderItem(giftItem);
    }
    giftItem.getWrappedItems().addAll(addedItems);
    order = orderService.save(order, false);

    return order;
  }
 protected OrderItem findMatchingItem(Order order, OrderItemRequestDTO itemToFind) {
   if (order == null) {
     return null;
   }
   for (OrderItem currentItem : order.getOrderItems()) {
     if (currentItem instanceof DiscreteOrderItem) {
       DiscreteOrderItem discreteItem = (DiscreteOrderItem) currentItem;
       if (itemMatches(
           discreteItem.getSku(),
           discreteItem.getProduct(),
           discreteItem.getOrderItemAttributes(),
           itemToFind)) {
         return discreteItem;
       }
     } else if (currentItem instanceof BundleOrderItem) {
       BundleOrderItem bundleItem = (BundleOrderItem) currentItem;
       if (itemMatches(bundleItem.getSku(), bundleItem.getProduct(), null, itemToFind)) {
         return bundleItem;
       }
     }
   }
   return null;
 }
  public Order setUpAnonymousCartWithBundleGiftWrapReferringToRootItems() throws PricingException {
    Customer customer = customerService.saveCustomer(createNamedCustomer());

    Order order = cartService.createNewCartForCustomer(customer);

    Sku newSku = addTestSku("Small Plastic Bowl", "Plastic Bowl", "Bowls");
    Sku newActiveSku = addTestSku("Small Red Plastic Bowl", "Plastic Bowl", "Bowls");

    Product newProduct = newSku.getAllParentProducts().get(0);
    Category newCategory = newProduct.getDefaultCategory();

    List<OrderItem> addedItems = new ArrayList<OrderItem>();
    addedItems.add(
        orderService.addSkuToOrder(
            order.getId(), newSku.getId(), newProduct.getId(), newCategory.getId(), 2));
    addedItems.add(
        orderService.addSkuToOrder(
            order.getId(), newActiveSku.getId(), newProduct.getId(), newCategory.getId(), 2));

    BundleOrderItem newBundle =
        (BundleOrderItem)
            orderService.addBundleItemToOrder(order, createBundleOrderItemRequestWithGiftWrap());
    GiftWrapOrderItem giftItem = null;
    for (DiscreteOrderItem addedItem : newBundle.getDiscreteOrderItems()) {
      if (addedItem instanceof GiftWrapOrderItem) {
        giftItem = (GiftWrapOrderItem) addedItem;
      }
    }
    for (OrderItem addedItem : addedItems) {
      addedItem.setGiftWrapOrderItem(giftItem);
    }
    giftItem.getWrappedItems().addAll(addedItems);
    order = orderService.save(order, false);

    return order;
  }