Search in sources :

Example 1 with CartOperationRequest

use of org.broadleafcommerce.core.order.service.workflow.CartOperationRequest in project BroadleafCommerce by BroadleafCommerce.

the class OrderServiceImpl method addChildItems.

@Override
public void addChildItems(OrderItemRequestDTO orderItemRequestDTO, int numAdditionRequests, int currentAddition, ProcessContext<CartOperationRequest> context, List<ActivityMessageDTO> orderMessages) throws WorkflowException {
    if (CollectionUtils.isNotEmpty(orderItemRequestDTO.getChildOrderItems())) {
        Long parentOrderItemId = context.getSeedData().getOrderItem().getId();
        for (OrderItemRequestDTO childRequest : orderItemRequestDTO.getChildOrderItems()) {
            childRequest.setParentOrderItemId(parentOrderItemId);
            currentAddition++;
            if (childRequest.getQuantity() > 0) {
                CartOperationRequest childCartOpRequest = new CartOperationRequest(context.getSeedData().getOrder(), childRequest, currentAddition == numAdditionRequests);
                Session session = em.unwrap(Session.class);
                FlushMode current = session.getFlushMode();
                if (!autoFlushAddToCart) {
                    // Performance measure. Hibernate will sometimes perform an autoflush when performing query operations and this can
                    // be expensive. It is possible to avoid the autoflush if there's no concern about queries in the flow returning
                    // incorrect results because something has not been flushed to the database yet.
                    session.setFlushMode(FlushMode.MANUAL);
                }
                ProcessContext<CartOperationRequest> childContext;
                try {
                    childContext = (ProcessContext<CartOperationRequest>) addItemWorkflow.doActivities(childCartOpRequest);
                } finally {
                    if (!autoFlushAddToCart) {
                        session.setFlushMode(current);
                    }
                }
                orderMessages.addAll(((ActivityMessages) childContext).getActivityMessages());
                addChildItems(childRequest, numAdditionRequests, currentAddition, childContext, orderMessages);
            }
        }
    }
}
Also used : CartOperationRequest(org.broadleafcommerce.core.order.service.workflow.CartOperationRequest) OrderItemRequestDTO(org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO) FlushMode(org.hibernate.FlushMode) Session(org.hibernate.Session)

Example 2 with CartOperationRequest

use of org.broadleafcommerce.core.order.service.workflow.CartOperationRequest in project BroadleafCommerce by BroadleafCommerce.

the class OrderServiceImpl method addItemWithPriceOverrides.

@Override
@Transactional(value = "blTransactionManager", rollbackFor = { AddToCartException.class })
public Order addItemWithPriceOverrides(Long orderId, OrderItemRequestDTO orderItemRequestDTO, boolean priceOrder) throws AddToCartException {
    Order order = findOrderById(orderId);
    preValidateCartOperation(order);
    if (getAutomaticallyMergeLikeItems()) {
        OrderItem item = findMatchingItem(order, orderItemRequestDTO);
        if (item != null && item.getParentOrderItem() == null) {
            orderItemRequestDTO.setQuantity(item.getQuantity() + orderItemRequestDTO.getQuantity());
            orderItemRequestDTO.setOrderItemId(item.getId());
            try {
                return updateItemQuantity(orderId, orderItemRequestDTO, priceOrder);
            } catch (RemoveFromCartException e) {
                throw new AddToCartException("Unexpected error - system tried to remove item while adding to cart", e);
            } catch (UpdateCartException e) {
                throw new AddToCartException("Could not update quantity for matched item", e);
            }
        }
    }
    try {
        // We only want to price on the last addition for performance reasons and only if the user asked for it.
        int numAdditionRequests = priceOrder ? (getTotalChildOrderItems(orderItemRequestDTO)) : -1;
        int currentAddition = 1;
        CartOperationRequest cartOpRequest = new CartOperationRequest(findOrderById(orderId), orderItemRequestDTO, currentAddition == numAdditionRequests);
        Session session = em.unwrap(Session.class);
        FlushMode current = session.getFlushMode();
        if (!autoFlushAddToCart) {
            // Performance measure. Hibernate will sometimes perform an autoflush when performing query operations and this can
            // be expensive. It is possible to avoid the autoflush if there's no concern about queries in the flow returning
            // incorrect results because something has not been flushed to the database yet.
            session.setFlushMode(FlushMode.MANUAL);
        }
        ProcessContext<CartOperationRequest> context;
        try {
            context = (ProcessContext<CartOperationRequest>) addItemWorkflow.doActivities(cartOpRequest);
        } finally {
            if (!autoFlushAddToCart) {
                session.setFlushMode(current);
            }
        }
        List<ActivityMessageDTO> orderMessages = new ArrayList<ActivityMessageDTO>();
        orderMessages.addAll(((ActivityMessages) context).getActivityMessages());
        // Update the orderItemRequest incase it changed during the initial add to cart workflow
        orderItemRequestDTO = context.getSeedData().getItemRequest();
        numAdditionRequests = priceOrder ? (getTotalChildOrderItems(orderItemRequestDTO) - 1) : -1;
        addChildItems(orderItemRequestDTO, numAdditionRequests, currentAddition, context, orderMessages);
        context.getSeedData().getOrder().setOrderMessages(orderMessages);
        return context.getSeedData().getOrder();
    } catch (WorkflowException e) {
        throw new AddToCartException("Could not add to cart", getCartOperationExceptionRootCause(e));
    }
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) WorkflowException(org.broadleafcommerce.core.workflow.WorkflowException) ArrayList(java.util.ArrayList) ActivityMessageDTO(org.broadleafcommerce.core.order.service.call.ActivityMessageDTO) UpdateCartException(org.broadleafcommerce.core.order.service.exception.UpdateCartException) FlushMode(org.hibernate.FlushMode) CartOperationRequest(org.broadleafcommerce.core.order.service.workflow.CartOperationRequest) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) GiftWrapOrderItem(org.broadleafcommerce.core.order.domain.GiftWrapOrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) AddToCartException(org.broadleafcommerce.core.order.service.exception.AddToCartException) RemoveFromCartException(org.broadleafcommerce.core.order.service.exception.RemoveFromCartException) Session(org.hibernate.Session) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with CartOperationRequest

use of org.broadleafcommerce.core.order.service.workflow.CartOperationRequest in project BroadleafCommerce by BroadleafCommerce.

the class OrderServiceImpl method updateProductOptionsForItem.

@Override
@Transactional(value = "blTransactionManager", rollbackFor = { UpdateCartException.class })
public Order updateProductOptionsForItem(Long orderId, OrderItemRequestDTO orderItemRequestDTO, boolean priceOrder) throws UpdateCartException {
    try {
        CartOperationRequest cartOpRequest = new CartOperationRequest(findOrderById(orderId), orderItemRequestDTO, priceOrder);
        ProcessContext<CartOperationRequest> context = (ProcessContext<CartOperationRequest>) updateProductOptionsForItemWorkflow.doActivities(cartOpRequest);
        context.getSeedData().getOrder().getOrderMessages().addAll(((ActivityMessages) context).getActivityMessages());
        return context.getSeedData().getOrder();
    } catch (WorkflowException e) {
        throw new UpdateCartException("Could not product options", getCartOperationExceptionRootCause(e));
    }
}
Also used : CartOperationRequest(org.broadleafcommerce.core.order.service.workflow.CartOperationRequest) WorkflowException(org.broadleafcommerce.core.workflow.WorkflowException) UpdateCartException(org.broadleafcommerce.core.order.service.exception.UpdateCartException) ProcessContext(org.broadleafcommerce.core.workflow.ProcessContext) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with CartOperationRequest

use of org.broadleafcommerce.core.order.service.workflow.CartOperationRequest in project BroadleafCommerce by BroadleafCommerce.

the class ValidateUpdateProductOptionsRequestActivity method execute.

@Override
public ProcessContext<CartOperationRequest> execute(ProcessContext<CartOperationRequest> context) throws Exception {
    CartOperationRequest request = context.getSeedData();
    OrderItemRequestDTO orderItemRequestDTO = request.getItemRequest();
    // Throw an exception if the user did not specify an orderItemId
    if (orderItemRequestDTO.getOrderItemId() == null) {
        throw new IllegalArgumentException("OrderItemId must be specified to locate the order item");
    }
    // Throw an exception if the user did not specify an order to add the item to
    if (request.getOrder() == null) {
        throw new IllegalArgumentException("Order is required when updating items in the order");
    }
    // Throw an exception if the user is trying to update an order item that is part of a bundle
    OrderItem orderItem = orderItemService.readOrderItemById(orderItemRequestDTO.getOrderItemId());
    if (orderItem != null && orderItem instanceof DiscreteOrderItem) {
        DiscreteOrderItem doi = (DiscreteOrderItem) orderItem;
        if (doi.getBundleOrderItem() != null) {
        // then its ok , since we are just updating the product options
        }
    }
    return context;
}
Also used : DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) CartOperationRequest(org.broadleafcommerce.core.order.service.workflow.CartOperationRequest) OrderItemRequestDTO(org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem)

Example 5 with CartOperationRequest

use of org.broadleafcommerce.core.order.service.workflow.CartOperationRequest in project BroadleafCommerce by BroadleafCommerce.

the class AddFulfillmentGroupItemActivity method execute.

@Override
public ProcessContext<CartOperationRequest> execute(ProcessContext<CartOperationRequest> context) throws Exception {
    CartOperationRequest request = context.getSeedData();
    request = fgItemStrategy.onItemAdded(request);
    context.setSeedData(request);
    return context;
}
Also used : CartOperationRequest(org.broadleafcommerce.core.order.service.workflow.CartOperationRequest)

Aggregations

CartOperationRequest (org.broadleafcommerce.core.order.service.workflow.CartOperationRequest)18 OrderItem (org.broadleafcommerce.core.order.domain.OrderItem)9 OrderItemRequestDTO (org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO)9 DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)5 Order (org.broadleafcommerce.core.order.domain.Order)5 FlushMode (org.hibernate.FlushMode)4 Session (org.hibernate.Session)4 UpdateCartException (org.broadleafcommerce.core.order.service.exception.UpdateCartException)3 WorkflowException (org.broadleafcommerce.core.workflow.WorkflowException)3 Transactional (org.springframework.transaction.annotation.Transactional)3 BundleOrderItem (org.broadleafcommerce.core.order.domain.BundleOrderItem)2 ArrayList (java.util.ArrayList)1 Product (org.broadleafcommerce.core.catalog.domain.Product)1 Sku (org.broadleafcommerce.core.catalog.domain.Sku)1 FulfillmentGroup (org.broadleafcommerce.core.order.domain.FulfillmentGroup)1 FulfillmentGroupItem (org.broadleafcommerce.core.order.domain.FulfillmentGroupItem)1 GiftWrapOrderItem (org.broadleafcommerce.core.order.domain.GiftWrapOrderItem)1 ActivityMessageDTO (org.broadleafcommerce.core.order.service.call.ActivityMessageDTO)1 ConfigurableOrderItemRequest (org.broadleafcommerce.core.order.service.call.ConfigurableOrderItemRequest)1 DiscreteOrderItemRequest (org.broadleafcommerce.core.order.service.call.DiscreteOrderItemRequest)1