Search in sources :

Example 1 with PricingException

use of org.broadleafcommerce.core.pricing.service.exception.PricingException in project BroadleafCommerce by BroadleafCommerce.

the class OrderServiceImpl method save.

@Override
public Order save(Order order, Boolean priceOrder) throws PricingException {
    // persist the order first
    TransactionStatus status = TransactionUtils.createTransaction("saveOrder", TransactionDefinition.PROPAGATION_REQUIRED, transactionManager);
    try {
        order = persist(order);
        TransactionUtils.finalizeTransaction(status, transactionManager, false);
    } catch (RuntimeException ex) {
        TransactionUtils.finalizeTransaction(status, transactionManager, true);
        throw ex;
    }
    // make any pricing changes - possibly retrying with the persisted state if there's a lock failure
    if (priceOrder) {
        int retryCount = 0;
        boolean isValid = false;
        while (!isValid) {
            Session session = em.unwrap(Session.class);
            FlushMode current = session.getFlushMode();
            try {
                if (!autoFlushSaveCart) {
                    // 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);
                }
                order = pricingService.executePricing(order);
                isValid = true;
            } catch (Exception ex) {
                boolean isValidCause = false;
                Throwable cause = ex;
                while (!isValidCause) {
                    if (cause.getClass().equals(LockAcquisitionException.class)) {
                        isValidCause = true;
                    }
                    cause = cause.getCause();
                    if (cause == null) {
                        break;
                    }
                }
                if (isValidCause) {
                    if (LOG.isInfoEnabled()) {
                        LOG.info("Problem acquiring lock during pricing call - attempting to price again.");
                    }
                    isValid = false;
                    if (retryCount >= pricingRetryCountForLockFailure) {
                        if (LOG.isInfoEnabled()) {
                            LOG.info("Problem acquiring lock during pricing call. Retry limit exceeded at (" + retryCount + "). Throwing exception.");
                        }
                        if (ex instanceof PricingException) {
                            throw (PricingException) ex;
                        } else {
                            throw new PricingException(ex);
                        }
                    } else {
                        order = findOrderById(order.getId());
                        retryCount++;
                    }
                    try {
                        Thread.sleep(pricingRetryWaitIntervalForLockFailure);
                    } catch (Throwable e) {
                    // do nothing
                    }
                } else {
                    if (ex instanceof PricingException) {
                        throw (PricingException) ex;
                    } else {
                        throw new PricingException(ex);
                    }
                }
            } finally {
                if (!autoFlushSaveCart) {
                    session.setFlushMode(current);
                }
            }
        }
        // make the final save of the priced order
        status = TransactionUtils.createTransaction("saveOrder", TransactionDefinition.PROPAGATION_REQUIRED, transactionManager);
        Session session = em.unwrap(Session.class);
        FlushMode current = session.getFlushMode();
        try {
            if (!autoFlushSaveCart) {
                // 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);
            }
            order = persist(order);
            if (extensionManager != null) {
                extensionManager.getProxy().attachAdditionalDataToOrder(order, priceOrder);
            }
            if (!autoFlushSaveCart) {
                session.setFlushMode(current);
            }
            TransactionUtils.finalizeTransaction(status, transactionManager, false);
        } catch (RuntimeException ex) {
            TransactionUtils.finalizeTransaction(status, transactionManager, true);
            throw ex;
        } finally {
            if (!autoFlushSaveCart && !session.getFlushMode().equals(current)) {
                session.setFlushMode(current);
            }
        }
    }
    return order;
}
Also used : PricingException(org.broadleafcommerce.core.pricing.service.exception.PricingException) TransactionStatus(org.springframework.transaction.TransactionStatus) FlushMode(org.hibernate.FlushMode) WorkflowException(org.broadleafcommerce.core.workflow.WorkflowException) IllegalCartOperationException(org.broadleafcommerce.core.order.service.exception.IllegalCartOperationException) OfferException(org.broadleafcommerce.core.offer.service.exception.OfferException) OfferAlreadyAddedException(org.broadleafcommerce.core.offer.service.exception.OfferAlreadyAddedException) ItemNotFoundException(org.broadleafcommerce.core.order.service.exception.ItemNotFoundException) LockAcquisitionException(org.hibernate.exception.LockAcquisitionException) AddToCartException(org.broadleafcommerce.core.order.service.exception.AddToCartException) OfferMaxUseExceededException(org.broadleafcommerce.core.offer.service.exception.OfferMaxUseExceededException) UpdateCartException(org.broadleafcommerce.core.order.service.exception.UpdateCartException) PricingException(org.broadleafcommerce.core.pricing.service.exception.PricingException) OfferExpiredException(org.broadleafcommerce.core.offer.service.exception.OfferExpiredException) RemoveFromCartException(org.broadleafcommerce.core.order.service.exception.RemoveFromCartException) Session(org.hibernate.Session) LockAcquisitionException(org.hibernate.exception.LockAcquisitionException)

Example 2 with PricingException

use of org.broadleafcommerce.core.pricing.service.exception.PricingException in project BroadleafCommerce by BroadleafCommerce.

the class BroadleafCheckoutController method saveGlobalOrderDetails.

/**
 * Attempts to attach the user's email to the order so that they may proceed anonymously
 * @param request
 * @param model
 * @param orderInfoForm
 * @param result
 * @return
 * @throws ServiceException
 */
public String saveGlobalOrderDetails(HttpServletRequest request, Model model, OrderInfoForm orderInfoForm, BindingResult result) throws ServiceException {
    Order cart = CartState.getCart();
    orderInfoFormValidator.validate(orderInfoForm, result);
    if (result.hasErrors()) {
        // We need to clear the email on error in case they are trying to edit it
        try {
            cart.setEmailAddress(null);
            orderService.save(cart, false);
        } catch (PricingException pe) {
            LOG.error("Error when saving the email address for order confirmation to the cart", pe);
        }
        populateModelWithReferenceData(request, model);
        return getCheckoutView();
    }
    try {
        cart.setEmailAddress(orderInfoForm.getEmailAddress());
        orderService.save(cart, false);
    } catch (PricingException pe) {
        LOG.error("Error when saving the email address for order confirmation to the cart", pe);
    }
    return getCheckoutPageRedirect();
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) PricingException(org.broadleafcommerce.core.pricing.service.exception.PricingException)

Example 3 with PricingException

use of org.broadleafcommerce.core.pricing.service.exception.PricingException in project BroadleafCommerce by BroadleafCommerce.

the class DefaultCurrentOrderPaymentRequestService method addOrderAttributeToOrder.

@Override
public void addOrderAttributeToOrder(Long orderId, String orderAttributeKey, String orderAttributeValue) throws PaymentException {
    Order currentCart = CartState.getCart();
    Long currentCartId = currentCart.getId();
    if (orderId != null && !currentCartId.equals(orderId)) {
        logWarningIfCartMismatch(currentCartId, orderId);
        currentCart = orderService.findOrderById(orderId);
    }
    OrderAttribute orderAttribute = new OrderAttributeImpl();
    orderAttribute.setName(orderAttributeKey);
    orderAttribute.setValue(orderAttributeValue);
    orderAttribute.setOrder(currentCart);
    currentCart.getOrderAttributes().put(orderAttributeKey, orderAttribute);
    try {
        orderService.save(currentCart, false);
    } catch (PricingException e) {
        throw new PaymentException(e);
    }
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) PaymentException(org.broadleafcommerce.common.vendor.service.exception.PaymentException) PricingException(org.broadleafcommerce.core.pricing.service.exception.PricingException) OrderAttribute(org.broadleafcommerce.core.order.domain.OrderAttribute) OrderAttributeImpl(org.broadleafcommerce.core.order.domain.OrderAttributeImpl)

Example 4 with PricingException

use of org.broadleafcommerce.core.pricing.service.exception.PricingException in project BroadleafCommerce by BroadleafCommerce.

the class UpdateCartServiceImpl method updateAndValidateCart.

@Override
public void updateAndValidateCart(Order cart) {
    if (extensionManager != null) {
        ExtensionResultHolder erh = new ExtensionResultHolder();
        extensionManager.getProxy().updateAndValidateCart(cart, erh);
        Boolean clearCart = (Boolean) erh.getContextMap().get("clearCart");
        Boolean repriceCart = (Boolean) erh.getContextMap().get("repriceCart");
        Boolean saveCart = (Boolean) erh.getContextMap().get("saveCart");
        if (clearCart != null && clearCart.booleanValue()) {
            orderService.cancelOrder(cart);
            cart = orderService.createNewCartForCustomer(cart.getCustomer());
        } else {
            try {
                if (repriceCart != null && repriceCart.booleanValue()) {
                    orderService.save(cart, true, true);
                } else if (saveCart != null && saveCart.booleanValue()) {
                    orderService.save(cart, false);
                }
            } catch (PricingException pe) {
                LOG.error("Pricing Exception while validating cart.   Clearing cart.", pe);
                orderService.cancelOrder(cart);
                cart = orderService.createNewCartForCustomer(cart.getCustomer());
            }
        }
    }
}
Also used : PricingException(org.broadleafcommerce.core.pricing.service.exception.PricingException) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Example 5 with PricingException

use of org.broadleafcommerce.core.pricing.service.exception.PricingException 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)

Aggregations

PricingException (org.broadleafcommerce.core.pricing.service.exception.PricingException)11 Order (org.broadleafcommerce.core.order.domain.Order)7 RemoveFromCartException (org.broadleafcommerce.core.order.service.exception.RemoveFromCartException)4 WorkflowException (org.broadleafcommerce.core.workflow.WorkflowException)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 PaymentException (org.broadleafcommerce.common.vendor.service.exception.PaymentException)2 MergeCartResponse (org.broadleafcommerce.core.order.service.call.MergeCartResponse)2 AddToCartException (org.broadleafcommerce.core.order.service.exception.AddToCartException)2 ItemNotFoundException (org.broadleafcommerce.core.order.service.exception.ItemNotFoundException)2 UpdateCartException (org.broadleafcommerce.core.order.service.exception.UpdateCartException)2 Customer (org.broadleafcommerce.profile.core.domain.Customer)2 Collection (java.util.Collection)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 BroadleafCurrency (org.broadleafcommerce.common.currency.domain.BroadleafCurrency)1 ExtensionResultHolder (org.broadleafcommerce.common.extension.ExtensionResultHolder)1 PaymentRequestDTO (org.broadleafcommerce.common.payment.dto.PaymentRequestDTO)1 PaymentResponseDTO (org.broadleafcommerce.common.payment.dto.PaymentResponseDTO)1 PaymentGatewayConfigurationService (org.broadleafcommerce.common.payment.service.PaymentGatewayConfigurationService)1 CheckoutException (org.broadleafcommerce.core.checkout.service.exception.CheckoutException)1