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;
}
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();
}
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);
}
}
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());
}
}
}
}
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());
}
}
Aggregations