Search in sources :

Example 1 with RemoveFromCartException

use of org.broadleafcommerce.core.order.service.exception.RemoveFromCartException 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 RemoveFromCartException

use of org.broadleafcommerce.core.order.service.exception.RemoveFromCartException 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 RemoveFromCartException

use of org.broadleafcommerce.core.order.service.exception.RemoveFromCartException in project BroadleafCommerce by BroadleafCommerce.

the class CartStateRequestProcessor method mergeCart.

/**
 * Looks up the anonymous customer and merges that cart with the cart from the given logged in <b>customer</b>. This
 * will also remove the customer from session after it has finished since it is no longer needed
 */
public Order mergeCart(Customer customer, WebRequest request) {
    Customer anonymousCustomer = customerStateRequestProcessor.getAnonymousCustomer(request);
    MergeCartResponse mergeCartResponse;
    try {
        Order cart = orderService.findCartForCustomer(anonymousCustomer);
        mergeCartResponse = mergeCartService.mergeCart(customer, cart);
    } catch (PricingException e) {
        throw new RuntimeException(e);
    } catch (RemoveFromCartException e) {
        throw new RuntimeException(e);
    }
    if (BLCRequestUtils.isOKtoUseSession(request)) {
        // The anonymous customer from session is no longer needed; it can be safely removed
        request.removeAttribute(CustomerStateRequestProcessor.getAnonymousCustomerSessionAttributeName(), WebRequest.SCOPE_GLOBAL_SESSION);
        request.removeAttribute(CustomerStateRequestProcessor.getAnonymousCustomerIdSessionAttributeName(), WebRequest.SCOPE_GLOBAL_SESSION);
        request.setAttribute(mergeCartResponseKey, mergeCartResponse, WebRequest.SCOPE_GLOBAL_SESSION);
    }
    return mergeCartResponse.getOrder();
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) PricingException(org.broadleafcommerce.core.pricing.service.exception.PricingException) Customer(org.broadleafcommerce.profile.core.domain.Customer) MergeCartResponse(org.broadleafcommerce.core.order.service.call.MergeCartResponse) RemoveFromCartException(org.broadleafcommerce.core.order.service.exception.RemoveFromCartException)

Example 4 with RemoveFromCartException

use of org.broadleafcommerce.core.order.service.exception.RemoveFromCartException in project BroadleafCommerce by BroadleafCommerce.

the class MergeCartProcessorImpl method execute.

@Override
public void execute(WebRequest request, Authentication authResult) {
    Customer loggedInCustomer = customerService.readCustomerByUsername(authResult.getName());
    Customer anonymousCustomer = customerStateRequestProcessor.getAnonymousCustomer(request);
    Order cart = null;
    if (anonymousCustomer != null) {
        cart = orderService.findCartForCustomer(anonymousCustomer);
    }
    MergeCartResponse mergeCartResponse;
    try {
        mergeCartResponse = mergeCartService.mergeCart(loggedInCustomer, cart);
    } catch (PricingException e) {
        throw new RuntimeException(e);
    } catch (RemoveFromCartException e) {
        throw new RuntimeException(e);
    }
    if (BLCRequestUtils.isOKtoUseSession(request)) {
        request.setAttribute(mergeCartResponseKey, mergeCartResponse, WebRequest.SCOPE_GLOBAL_SESSION);
    }
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) PricingException(org.broadleafcommerce.core.pricing.service.exception.PricingException) Customer(org.broadleafcommerce.profile.core.domain.Customer) MergeCartResponse(org.broadleafcommerce.core.order.service.call.MergeCartResponse) RemoveFromCartException(org.broadleafcommerce.core.order.service.exception.RemoveFromCartException)

Example 5 with RemoveFromCartException

use of org.broadleafcommerce.core.order.service.exception.RemoveFromCartException in project BroadleafCommerce by BroadleafCommerce.

the class UpdateCartServiceImpl method copyCartToCurrentContext.

@Override
public UpdateCartResponse copyCartToCurrentContext(Order currentCart) {
    if (currentCart.getOrderItems() == null) {
        return null;
    }
    BroadleafCurrency currency = findActiveCurrency();
    if (currency == null) {
        return null;
    }
    // Reprice order logic
    List<OrderItemRequestDTO> itemsToReprice = new ArrayList<>();
    List<OrderItem> itemsToRemove = new ArrayList<>();
    List<OrderItem> itemsToReset = new ArrayList<>();
    boolean repriceOrder = true;
    for (OrderItem orderItem : currentCart.getOrderItems()) {
        // Lookup price in price list, if null, then add to itemsToRemove
        if (orderItem instanceof DiscreteOrderItem) {
            DiscreteOrderItem doi = (DiscreteOrderItem) orderItem;
            if (checkAvailabilityInLocale(doi, currency)) {
                OrderItemRequestDTO itemRequest = new OrderItemRequestDTO();
                itemRequest.setProductId(doi.getProduct().getId());
                itemRequest.setQuantity(doi.getQuantity());
                itemsToReprice.add(itemRequest);
                itemsToReset.add(orderItem);
            } else {
                itemsToRemove.add(orderItem);
            }
        } else if (orderItem instanceof BundleOrderItem) {
            BundleOrderItem boi = (BundleOrderItem) orderItem;
            for (DiscreteOrderItem doi : boi.getDiscreteOrderItems()) {
                if (checkAvailabilityInLocale(doi, currency)) {
                    OrderItemRequestDTO itemRequest = new OrderItemRequestDTO();
                    itemRequest.setProductId(doi.getProduct().getId());
                    itemRequest.setQuantity(doi.getQuantity());
                    itemsToReprice.add(itemRequest);
                    itemsToReset.add(orderItem);
                } else {
                    itemsToRemove.add(orderItem);
                }
            }
        }
    }
    for (OrderItem orderItem : itemsToReset) {
        try {
            currentCart = orderService.removeItem(currentCart.getId(), orderItem.getId(), false);
        } catch (RemoveFromCartException e) {
            LOG.error("Could not remove from cart.", e);
        }
    }
    for (OrderItemRequestDTO itemRequest : itemsToReprice) {
        try {
            currentCart = orderService.addItem(currentCart.getId(), itemRequest, false);
        } catch (AddToCartException e) {
            LOG.error("Could not add to cart.", e);
        }
    }
    // Reprice and save the cart
    try {
        currentCart = orderService.save(currentCart, repriceOrder);
    } catch (PricingException e) {
        LOG.error("Could not save cart.", e);
    }
    setSavedCurrency(currency);
    UpdateCartResponse updateCartResponse = new UpdateCartResponse();
    updateCartResponse.setRemovedItems(itemsToRemove);
    updateCartResponse.setOrder(currentCart);
    return updateCartResponse;
}
Also used : DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) ArrayList(java.util.ArrayList) OrderItemRequestDTO(org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO) BroadleafCurrency(org.broadleafcommerce.common.currency.domain.BroadleafCurrency) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) PricingException(org.broadleafcommerce.core.pricing.service.exception.PricingException) UpdateCartResponse(org.broadleafcommerce.core.order.service.call.UpdateCartResponse) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) AddToCartException(org.broadleafcommerce.core.order.service.exception.AddToCartException) RemoveFromCartException(org.broadleafcommerce.core.order.service.exception.RemoveFromCartException)

Aggregations

RemoveFromCartException (org.broadleafcommerce.core.order.service.exception.RemoveFromCartException)5 ArrayList (java.util.ArrayList)3 BundleOrderItem (org.broadleafcommerce.core.order.domain.BundleOrderItem)3 DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)3 Order (org.broadleafcommerce.core.order.domain.Order)3 OrderItem (org.broadleafcommerce.core.order.domain.OrderItem)3 PricingException (org.broadleafcommerce.core.pricing.service.exception.PricingException)3 GiftWrapOrderItem (org.broadleafcommerce.core.order.domain.GiftWrapOrderItem)2 MergeCartResponse (org.broadleafcommerce.core.order.service.call.MergeCartResponse)2 AddToCartException (org.broadleafcommerce.core.order.service.exception.AddToCartException)2 WorkflowException (org.broadleafcommerce.core.workflow.WorkflowException)2 Customer (org.broadleafcommerce.profile.core.domain.Customer)2 Transactional (org.springframework.transaction.annotation.Transactional)2 BroadleafCurrency (org.broadleafcommerce.common.currency.domain.BroadleafCurrency)1 ActivityMessageDTO (org.broadleafcommerce.core.order.service.call.ActivityMessageDTO)1 OrderItemRequestDTO (org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO)1 UpdateCartResponse (org.broadleafcommerce.core.order.service.call.UpdateCartResponse)1 ItemNotFoundException (org.broadleafcommerce.core.order.service.exception.ItemNotFoundException)1 UpdateCartException (org.broadleafcommerce.core.order.service.exception.UpdateCartException)1 CartOperationRequest (org.broadleafcommerce.core.order.service.workflow.CartOperationRequest)1