protected Order mergeGiftWrapOrderItems(MergeCartResponse mergeCartResponse, Order customerCart, Map<OrderItem, OrderItem> oldNewItemMap) throws PricingException {
//update any remaining gift wrap items with their cloned wrapped item values, instead of the originals
Iterator<OrderItem> addedItems = mergeCartResponse.getAddedItems().iterator();
while (addedItems.hasNext()) {
OrderItem addedItem = addedItems.next();
if (addedItem instanceof GiftWrapOrderItem) {
GiftWrapOrderItem giftItem = (GiftWrapOrderItem) addedItem;
List<OrderItem> itemsToAdd = new ArrayList<OrderItem>();
Iterator<OrderItem> wrappedItems = giftItem.getWrappedItems().iterator();
while (wrappedItems.hasNext()) {
OrderItem wrappedItem = wrappedItems.next();
if (oldNewItemMap.containsKey(wrappedItem)) {
OrderItem newItem = oldNewItemMap.get(wrappedItem);
newItem.setGiftWrapOrderItem(giftItem);
itemsToAdd.add(newItem);
wrappedItem.setGiftWrapOrderItem(null);
wrappedItems.remove();
}
}
giftItem.getWrappedItems().addAll(itemsToAdd);
} else if (addedItem instanceof BundleOrderItem) {
//a GiftWrapOrderItem inside a BundleOrderItem can only wrap other members of that bundle
//or root members of the order - not members of an entirely different bundle
boolean isValidBundle = true;
Map<String, DiscreteOrderItem> newItemsMap = new HashMap<String, DiscreteOrderItem>();
for (DiscreteOrderItem newItem : ((BundleOrderItem) addedItem).getDiscreteOrderItems()){
newItemsMap.put(newItem.getSku().getId() + "_" + newItem.getPrice(), newItem);
}
checkBundle: {
for (DiscreteOrderItem itemFromBundle : ((BundleOrderItem) addedItem).getDiscreteOrderItems()) {
if (itemFromBundle instanceof GiftWrapOrderItem) {
GiftWrapOrderItem giftItem = (GiftWrapOrderItem) itemFromBundle;
List<OrderItem> itemsToAdd = new ArrayList<OrderItem>();
Iterator<OrderItem> wrappedItems = giftItem.getWrappedItems().iterator();
while (wrappedItems.hasNext()) {
OrderItem wrappedItem = wrappedItems.next();
if (oldNewItemMap.containsKey(wrappedItem)) {
OrderItem newItem = oldNewItemMap.get(wrappedItem);
newItem.setGiftWrapOrderItem(giftItem);
itemsToAdd.add(newItem);
wrappedItem.setGiftWrapOrderItem(null);
wrappedItems.remove();
} else if (wrappedItem instanceof DiscreteOrderItem) {
DiscreteOrderItem discreteWrappedItem = (DiscreteOrderItem) wrappedItem;
String itemKey = discreteWrappedItem.getSku().getId() + "_" + discreteWrappedItem.getPrice();
if (newItemsMap.containsKey(itemKey)) {
OrderItem newItem = newItemsMap.get(itemKey);
newItem.setGiftWrapOrderItem(giftItem);
itemsToAdd.add(newItem);
discreteWrappedItem.setGiftWrapOrderItem(null);
wrappedItems.remove();
} else {
isValidBundle = false;