use of org.broadleafcommerce.core.order.service.exception.AddToCartException 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));
}
}
use of org.broadleafcommerce.core.order.service.exception.AddToCartException in project BroadleafCommerce by BroadleafCommerce.
the class OrderTest method testIllegalAddScenarios.
@Test(groups = { "testIllegalAddScenarios" }, dependsOnGroups = { "addItemToOrder" })
@Transactional
public void testIllegalAddScenarios() throws AddToCartException {
Order order = orderService.findOrderById(orderId);
assert order != null;
Product activeProduct = addTestProduct("mug", "cups", true);
Product inactiveProduct = addTestProduct("cup", "cups", false);
// Inactive skus should not be added
OrderItemRequestDTO itemRequest = new OrderItemRequestDTO().setQuantity(1).setSkuId(inactiveProduct.getDefaultSku().getId());
boolean addSuccessful = true;
try {
order = orderService.addItem(orderId, itemRequest, true);
} catch (AddToCartException e) {
addSuccessful = false;
}
assert !addSuccessful;
// Products that have SKUs marked as inactive should not be added either
itemRequest = new OrderItemRequestDTO().setQuantity(1).setProductId(inactiveProduct.getId());
addSuccessful = true;
try {
order = orderService.addItem(orderId, itemRequest, true);
} catch (AddToCartException e) {
addSuccessful = false;
}
assert !addSuccessful;
// Negative quantities are not allowed
itemRequest = new OrderItemRequestDTO().setQuantity(-1).setSkuId(activeProduct.getDefaultSku().getId());
addSuccessful = true;
try {
order = orderService.addItem(orderId, itemRequest, true);
} catch (AddToCartException e) {
addSuccessful = false;
assert e.getCause() instanceof IllegalArgumentException;
}
assert !addSuccessful;
// Order must exist
itemRequest = new OrderItemRequestDTO().setQuantity(1).setSkuId(activeProduct.getDefaultSku().getId());
addSuccessful = true;
try {
order = orderService.addItem(-1L, itemRequest, true);
} catch (AddToCartException e) {
addSuccessful = false;
assert e.getCause() instanceof IllegalArgumentException;
}
assert !addSuccessful;
// If a product is provided, it must exist
itemRequest = new OrderItemRequestDTO().setQuantity(1).setProductId(-1L);
addSuccessful = true;
try {
order = orderService.addItem(orderId, itemRequest, true);
} catch (AddToCartException e) {
addSuccessful = false;
assert e.getCause() instanceof IllegalArgumentException;
}
assert !addSuccessful;
// The SKU must exist
itemRequest = new OrderItemRequestDTO().setQuantity(1).setSkuId(-1L);
addSuccessful = true;
try {
order = orderService.addItem(orderId, itemRequest, true);
} catch (AddToCartException e) {
addSuccessful = false;
assert e.getCause() instanceof IllegalArgumentException;
}
assert !addSuccessful;
}
use of org.broadleafcommerce.core.order.service.exception.AddToCartException in project BroadleafCommerce by BroadleafCommerce.
the class OrderTest method testIllegalUpdateScenarios.
@Test(groups = { "testIllegalUpdateScenarios" }, dependsOnGroups = { "addItemToOrder" })
@Transactional
public void testIllegalUpdateScenarios() throws UpdateCartException, AddToCartException, RemoveFromCartException {
Order order = orderService.findOrderById(orderId);
assert order != null;
Product activeProduct = addTestProduct("mug", "cups", true);
Product inactiveProduct = addTestProduct("cup", "cups", false);
// Inactive skus should not be added
OrderItemRequestDTO itemRequest = new OrderItemRequestDTO().setQuantity(1).setSkuId(activeProduct.getDefaultSku().getId());
boolean addSuccessful = true;
try {
order = orderService.addItem(orderId, itemRequest, true);
} catch (AddToCartException e) {
addSuccessful = false;
}
assert addSuccessful;
// should not be able to update to negative quantity
OrderItem item = orderService.findLastMatchingItem(order, activeProduct.getDefaultSku().getId(), activeProduct.getId());
itemRequest = new OrderItemRequestDTO().setQuantity(-3).setOrderItemId(item.getId());
boolean updateSuccessful = true;
try {
orderService.updateItemQuantity(orderId, itemRequest, true);
} catch (UpdateCartException e) {
updateSuccessful = false;
}
assert !updateSuccessful;
// shouldn't be able to update the quantity of a DOI inside of a bundle
ProductBundle bundle = addProductBundle();
itemRequest = new OrderItemRequestDTO().setQuantity(1).setProductId(bundle.getId()).setSkuId(bundle.getDefaultSku().getId());
addSuccessful = true;
try {
order = orderService.addItem(orderId, itemRequest, true);
} catch (AddToCartException e) {
addSuccessful = false;
}
assert addSuccessful;
BundleOrderItem bundleItem = (BundleOrderItem) orderService.findLastMatchingItem(order, bundle.getDefaultSku().getId(), bundle.getId());
// should just be a single DOI inside the bundle
DiscreteOrderItem doi = bundleItem.getDiscreteOrderItems().get(0);
itemRequest = new OrderItemRequestDTO().setQuantity(4).setOrderItemId(doi.getId());
try {
orderService.updateItemQuantity(orderId, itemRequest, true);
} catch (UpdateCartException e) {
updateSuccessful = false;
}
assert !updateSuccessful;
}
use of org.broadleafcommerce.core.order.service.exception.AddToCartException 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;
}
Aggregations