Search in sources :

Example 6 with ItemNotFoundException

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;
    }
}
Also used : OrderItemAttribute(org.broadleafcommerce.core.order.domain.OrderItemAttribute) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) OrderItemAttributeImpl(org.broadleafcommerce.core.order.domain.OrderItemAttributeImpl) ItemNotFoundException(org.broadleafcommerce.core.order.service.exception.ItemNotFoundException)

Aggregations

ItemNotFoundException (org.broadleafcommerce.core.order.service.exception.ItemNotFoundException)6 DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)5 OrderItem (org.broadleafcommerce.core.order.domain.OrderItem)5 BundleOrderItem (org.broadleafcommerce.core.order.domain.BundleOrderItem)4 Order (org.broadleafcommerce.core.order.domain.Order)2 OrderItemAttribute (org.broadleafcommerce.core.order.domain.OrderItemAttribute)2 ArrayList (java.util.ArrayList)1 GiftWrapOrderItem (org.broadleafcommerce.core.order.domain.GiftWrapOrderItem)1 OrderItemAttributeImpl (org.broadleafcommerce.core.order.domain.OrderItemAttributeImpl)1 OrderItemRequestDTO (org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO)1 RemoveFromCartException (org.broadleafcommerce.core.order.service.exception.RemoveFromCartException)1 UpdateCartException (org.broadleafcommerce.core.order.service.exception.UpdateCartException)1 CartOperationRequest (org.broadleafcommerce.core.order.service.workflow.CartOperationRequest)1 PricingException (org.broadleafcommerce.core.pricing.service.exception.PricingException)1 WorkflowException (org.broadleafcommerce.core.workflow.WorkflowException)1 Transactional (org.springframework.transaction.annotation.Transactional)1