Iterator<OrderItemQualifier> unmatchedQualifiersIterator = unmatchedQualifiersMap.values().iterator();
for (PromotionQualifier qualifier : qualifiersMap.values()) {
if (unmatchedQualifiersIterator.hasNext()) {
// Reuse an existing qualifier
OrderItemQualifier existingQualifier = unmatchedQualifiersIterator.next();
existingQualifier.setOffer(qualifier.getPromotion());
existingQualifier.setQuantity(Long.valueOf(qualifier.getQuantity()));
unmatchedQualifiersIterator.remove();
} else {
// Create a new qualifier
OrderItemQualifier newQualifier = orderItemDao.createOrderItemQualifier();
newQualifier.setOrderItem(orderItem);
newQualifier.setOffer(qualifier.getPromotion());
newQualifier.setQuantity(Long.valueOf(qualifier.getQuantity()));
orderItem.getOrderItemQualifiers().add(newQualifier);
}
}
// Remove any unmatched qualifiers