Search in sources :

Example 1 with WorkflowException

use of org.broadleafcommerce.core.workflow.WorkflowException in project BroadleafCommerce by BroadleafCommerce.

the class OrderServiceImpl method removeItem.

@Override
@Transactional(value = "blTransactionManager", rollbackFor = { RemoveFromCartException.class })
public Order removeItem(Long orderId, Long orderItemId, boolean priceOrder) throws RemoveFromCartException {
    preValidateCartOperation(findOrderById(orderId));
    try {
        OrderItem oi = orderItemService.readOrderItemById(orderItemId);
        if (oi == null) {
            throw new WorkflowException(new ItemNotFoundException());
        }
        List<Long> childrenToRemove = new ArrayList<Long>();
        if (oi instanceof BundleOrderItem) {
            List<DiscreteOrderItem> bundledItems = ((BundleOrderItem) oi).getDiscreteOrderItems();
            for (DiscreteOrderItem doi : bundledItems) {
                findAllChildrenToRemove(childrenToRemove, doi);
            }
        } else {
            findAllChildrenToRemove(childrenToRemove, oi);
        }
        for (Long childToRemove : childrenToRemove) {
            removeItemInternal(orderId, childToRemove, false);
        }
        return removeItemInternal(orderId, orderItemId, priceOrder);
    } catch (WorkflowException e) {
        throw new RemoveFromCartException("Could not remove from cart", getCartOperationExceptionRootCause(e));
    }
}
Also used : DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) 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) WorkflowException(org.broadleafcommerce.core.workflow.WorkflowException) ArrayList(java.util.ArrayList) ItemNotFoundException(org.broadleafcommerce.core.order.service.exception.ItemNotFoundException) RemoveFromCartException(org.broadleafcommerce.core.order.service.exception.RemoveFromCartException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with WorkflowException

use of org.broadleafcommerce.core.workflow.WorkflowException 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 WorkflowException

use of org.broadleafcommerce.core.workflow.WorkflowException 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 WorkflowException

use of org.broadleafcommerce.core.workflow.WorkflowException in project BroadleafCommerce by BroadleafCommerce.

the class CheckoutServiceImpl method performCheckout.

@Override
public CheckoutResponse performCheckout(Order order) throws CheckoutException {
    // Immediately fail if another thread is currently attempting to check out the order
    Object lockObject = putLock(order.getId());
    if (lockObject != null) {
        throw new CheckoutException("This order is already in the process of being submitted, unable to checkout order -- id: " + order.getId(), new CheckoutSeed(order, new HashMap<String, Object>()));
    }
    // Immediately fail if this order has already been checked out previously
    if (hasOrderBeenCompleted(order)) {
        throw new CheckoutException("This order has already been submitted or cancelled, unable to checkout order -- id: " + order.getId(), new CheckoutSeed(order, new HashMap<String, Object>()));
    }
    CheckoutSeed seed = null;
    try {
        // Do a final save of the order before going through with the checkout workflow
        order = orderService.save(order, false);
        seed = new CheckoutSeed(order, new HashMap<String, Object>());
        ProcessContext<CheckoutSeed> context = checkoutWorkflow.doActivities(seed);
        // We need to pull the order off the seed and save it here in case any activity modified the order.
        order = orderService.save(seed.getOrder(), false);
        order.getOrderMessages().addAll(((ActivityMessages) context).getActivityMessages());
        seed.setOrder(order);
        return seed;
    } catch (PricingException e) {
        throw new CheckoutException("Unable to checkout order -- id: " + order.getId(), e, seed);
    } catch (WorkflowException e) {
        throw new CheckoutException("Unable to checkout order -- id: " + order.getId(), e.getRootCause(), seed);
    } catch (RequiredAttributeNotProvidedException e) {
        throw new CheckoutException("Unable to checkout order -- id: " + order.getId(), e.getCause(), seed);
    } finally {
        // The order has completed processing, remove the order from the processing map
        removeLock(order.getId());
    }
}
Also used : PricingException(org.broadleafcommerce.core.pricing.service.exception.PricingException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) WorkflowException(org.broadleafcommerce.core.workflow.WorkflowException) RequiredAttributeNotProvidedException(org.broadleafcommerce.core.order.service.exception.RequiredAttributeNotProvidedException) CheckoutSeed(org.broadleafcommerce.core.checkout.service.workflow.CheckoutSeed) CheckoutException(org.broadleafcommerce.core.checkout.service.exception.CheckoutException)

Example 5 with WorkflowException

use of org.broadleafcommerce.core.workflow.WorkflowException in project BroadleafCommerce by BroadleafCommerce.

the class PricingServiceImpl method executePricing.

public Order executePricing(Order order) throws PricingException {
    try {
        ProcessContext<Order> context = (ProcessContext<Order>) pricingWorkflow.doActivities(order);
        Order response = context.getSeedData();
        return response;
    } catch (WorkflowException e) {
        throw new PricingException("Unable to execute pricing for order -- id: " + order.getId(), e);
    }
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) PricingException(org.broadleafcommerce.core.pricing.service.exception.PricingException) WorkflowException(org.broadleafcommerce.core.workflow.WorkflowException) ProcessContext(org.broadleafcommerce.core.workflow.ProcessContext)

Aggregations

WorkflowException (org.broadleafcommerce.core.workflow.WorkflowException)8 Transactional (org.springframework.transaction.annotation.Transactional)6 ArrayList (java.util.ArrayList)3 Order (org.broadleafcommerce.core.order.domain.Order)3 UpdateCartException (org.broadleafcommerce.core.order.service.exception.UpdateCartException)3 CartOperationRequest (org.broadleafcommerce.core.order.service.workflow.CartOperationRequest)3 BundleOrderItem (org.broadleafcommerce.core.order.domain.BundleOrderItem)2 DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)2 GiftWrapOrderItem (org.broadleafcommerce.core.order.domain.GiftWrapOrderItem)2 OrderItem (org.broadleafcommerce.core.order.domain.OrderItem)2 RemoveFromCartException (org.broadleafcommerce.core.order.service.exception.RemoveFromCartException)2 OrderPayment (org.broadleafcommerce.core.payment.domain.OrderPayment)2 PricingException (org.broadleafcommerce.core.pricing.service.exception.PricingException)2 ProcessContext (org.broadleafcommerce.core.workflow.ProcessContext)2 FlushMode (org.hibernate.FlushMode)2 Session (org.hibernate.Session)2 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CheckoutException (org.broadleafcommerce.core.checkout.service.exception.CheckoutException)1 CheckoutSeed (org.broadleafcommerce.core.checkout.service.workflow.CheckoutSeed)1