use of org.broadleafcommerce.core.order.service.exception.ItemNotFoundException in project BroadleafCommerce by BroadleafCommerce.
the class LegacyOrderServiceImpl method addOrUpdateOrderItemAttributes.
/**
* Adds the passed in name/value pair to the order-item. If the
* attribute already exists, then it is updated with the new value.
* <p/>
* If the value passed in is null and the attribute exists, it is removed
* from the order item.
*
* @param order
* @param item
* @param attributeValues
* @param priceOrder
* @return
*/
@Override
public Order addOrUpdateOrderItemAttributes(Order order, OrderItem item, Map<String, String> attributeValues, boolean priceOrder) throws ItemNotFoundException, PricingException {
if (!order.getOrderItems().contains(item)) {
throw new ItemNotFoundException("Order Item (" + item.getId() + ") not found in Order (" + order.getId() + ")");
}
OrderItem itemFromOrder = order.getOrderItems().get(order.getOrderItems().indexOf(item));
Map<String, OrderItemAttribute> orderItemAttributes = itemFromOrder.getOrderItemAttributes();
if (orderItemAttributes == null) {
orderItemAttributes = new HashMap<String, OrderItemAttribute>();
itemFromOrder.setOrderItemAttributes(orderItemAttributes);
}
boolean changeMade = false;
for (String attributeName : attributeValues.keySet()) {
String attributeValue = attributeValues.get(attributeName);
OrderItemAttribute attribute = orderItemAttributes.get(attributeName);
if (attribute != null && attribute.getValue().equals(attributeValue)) {
// no change made.
continue;
} else {
changeMade = true;
if (attribute == null) {
attribute = new OrderItemAttributeImpl();
attribute.setOrderItem(itemFromOrder);
attribute.setName(attributeName);
attribute.setValue(attributeValue);
} else if (attributeValue == null) {
orderItemAttributes.remove(attributeValue);
} else {
attribute.setValue(attributeValue);
}
}
}
if (changeMade) {
return updateOrder(order, priceOrder);
} else {
return order;
}
}
Aggregations